Newer
Older
DungeonShooting / DungeonShooting_Godot / src / config / ExcelConfig.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using Godot;
  5.  
  6. namespace Config;
  7.  
  8. public static partial class ExcelConfig
  9. {
  10. /// <summary>
  11. /// ActivityBase.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  12. /// </summary>
  13. public static List<ActivityBase> ActivityBase_List { get; private set; }
  14. /// <summary>
  15. /// ActivityBase.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  16. /// </summary>
  17. public static Dictionary<string, ActivityBase> ActivityBase_Map { get; private set; }
  18.  
  19. /// <summary>
  20. /// ActivityMaterial.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  21. /// </summary>
  22. public static List<ActivityMaterial> ActivityMaterial_List { get; private set; }
  23. /// <summary>
  24. /// ActivityMaterial.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  25. /// </summary>
  26. public static Dictionary<string, ActivityMaterial> ActivityMaterial_Map { get; private set; }
  27.  
  28. /// <summary>
  29. /// AiAttackAttr.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  30. /// </summary>
  31. public static List<AiAttackAttr> AiAttackAttr_List { get; private set; }
  32. /// <summary>
  33. /// AiAttackAttr.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  34. /// </summary>
  35. public static Dictionary<string, AiAttackAttr> AiAttackAttr_Map { get; private set; }
  36.  
  37. /// <summary>
  38. /// BulletBase.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  39. /// </summary>
  40. public static List<BulletBase> BulletBase_List { get; private set; }
  41. /// <summary>
  42. /// BulletBase.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  43. /// </summary>
  44. public static Dictionary<string, BulletBase> BulletBase_Map { get; private set; }
  45.  
  46. /// <summary>
  47. /// Sound.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  48. /// </summary>
  49. public static List<Sound> Sound_List { get; private set; }
  50. /// <summary>
  51. /// Sound.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  52. /// </summary>
  53. public static Dictionary<string, Sound> Sound_Map { get; private set; }
  54.  
  55. /// <summary>
  56. /// WeaponBase.xlsx表数据集合, 以 List 形式存储, 数据顺序与 Excel 表相同
  57. /// </summary>
  58. public static List<WeaponBase> WeaponBase_List { get; private set; }
  59. /// <summary>
  60. /// WeaponBase.xlsx表数据集合, 里 Map 形式存储, key 为 Id
  61. /// </summary>
  62. public static Dictionary<string, WeaponBase> WeaponBase_Map { get; private set; }
  63.  
  64.  
  65. private static bool _init = false;
  66. /// <summary>
  67. /// 初始化所有配置表数据
  68. /// </summary>
  69. public static void Init()
  70. {
  71. if (_init) return;
  72. _init = true;
  73.  
  74. _InitActivityBaseConfig();
  75. _InitActivityMaterialConfig();
  76. _InitAiAttackAttrConfig();
  77. _InitBulletBaseConfig();
  78. _InitSoundConfig();
  79. _InitWeaponBaseConfig();
  80.  
  81. _InitActivityBaseRef();
  82. _InitWeaponBaseRef();
  83. }
  84. private static void _InitActivityBaseConfig()
  85. {
  86. try
  87. {
  88. var text = _ReadConfigAsText("res://resource/config/ActivityBase.json");
  89. ActivityBase_List = new List<ActivityBase>(JsonSerializer.Deserialize<List<Ref_ActivityBase>>(text));
  90. ActivityBase_Map = new Dictionary<string, ActivityBase>();
  91. foreach (var item in ActivityBase_List)
  92. {
  93. ActivityBase_Map.Add(item.Id, item);
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. GD.PrintErr(e.ToString());
  99. throw new Exception("初始化表'ActivityBase'失败!");
  100. }
  101. }
  102. private static void _InitActivityMaterialConfig()
  103. {
  104. try
  105. {
  106. var text = _ReadConfigAsText("res://resource/config/ActivityMaterial.json");
  107. ActivityMaterial_List = JsonSerializer.Deserialize<List<ActivityMaterial>>(text);
  108. ActivityMaterial_Map = new Dictionary<string, ActivityMaterial>();
  109. foreach (var item in ActivityMaterial_List)
  110. {
  111. ActivityMaterial_Map.Add(item.Id, item);
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. GD.PrintErr(e.ToString());
  117. throw new Exception("初始化表'ActivityMaterial'失败!");
  118. }
  119. }
  120. private static void _InitAiAttackAttrConfig()
  121. {
  122. try
  123. {
  124. var text = _ReadConfigAsText("res://resource/config/AiAttackAttr.json");
  125. AiAttackAttr_List = JsonSerializer.Deserialize<List<AiAttackAttr>>(text);
  126. AiAttackAttr_Map = new Dictionary<string, AiAttackAttr>();
  127. foreach (var item in AiAttackAttr_List)
  128. {
  129. AiAttackAttr_Map.Add(item.Id, item);
  130. }
  131. }
  132. catch (Exception e)
  133. {
  134. GD.PrintErr(e.ToString());
  135. throw new Exception("初始化表'AiAttackAttr'失败!");
  136. }
  137. }
  138. private static void _InitBulletBaseConfig()
  139. {
  140. try
  141. {
  142. var text = _ReadConfigAsText("res://resource/config/BulletBase.json");
  143. BulletBase_List = JsonSerializer.Deserialize<List<BulletBase>>(text);
  144. BulletBase_Map = new Dictionary<string, BulletBase>();
  145. foreach (var item in BulletBase_List)
  146. {
  147. BulletBase_Map.Add(item.Id, item);
  148. }
  149. }
  150. catch (Exception e)
  151. {
  152. GD.PrintErr(e.ToString());
  153. throw new Exception("初始化表'BulletBase'失败!");
  154. }
  155. }
  156. private static void _InitSoundConfig()
  157. {
  158. try
  159. {
  160. var text = _ReadConfigAsText("res://resource/config/Sound.json");
  161. Sound_List = JsonSerializer.Deserialize<List<Sound>>(text);
  162. Sound_Map = new Dictionary<string, Sound>();
  163. foreach (var item in Sound_List)
  164. {
  165. Sound_Map.Add(item.Id, item);
  166. }
  167. }
  168. catch (Exception e)
  169. {
  170. GD.PrintErr(e.ToString());
  171. throw new Exception("初始化表'Sound'失败!");
  172. }
  173. }
  174. private static void _InitWeaponBaseConfig()
  175. {
  176. try
  177. {
  178. var text = _ReadConfigAsText("res://resource/config/WeaponBase.json");
  179. WeaponBase_List = new List<WeaponBase>(JsonSerializer.Deserialize<List<Ref_WeaponBase>>(text));
  180. WeaponBase_Map = new Dictionary<string, WeaponBase>();
  181. foreach (var item in WeaponBase_List)
  182. {
  183. WeaponBase_Map.Add(item.Id, item);
  184. }
  185. }
  186. catch (Exception e)
  187. {
  188. GD.PrintErr(e.ToString());
  189. throw new Exception("初始化表'WeaponBase'失败!");
  190. }
  191. }
  192.  
  193. private static void _InitActivityBaseRef()
  194. {
  195. foreach (Ref_ActivityBase item in ActivityBase_List)
  196. {
  197. try
  198. {
  199. if (!string.IsNullOrEmpty(item.__Material))
  200. {
  201. item.Material = ActivityMaterial_Map[item.__Material];
  202. }
  203.  
  204. }
  205. catch (Exception e)
  206. {
  207. GD.PrintErr(e.ToString());
  208. throw new Exception("初始化'ActivityBase'引用其他表数据失败, 当前行id: " + item.Id);
  209. }
  210. }
  211. }
  212. private static void _InitWeaponBaseRef()
  213. {
  214. foreach (Ref_WeaponBase item in WeaponBase_List)
  215. {
  216. try
  217. {
  218. if (!string.IsNullOrEmpty(item.__Activity))
  219. {
  220. item.Activity = ActivityBase_Map[item.__Activity];
  221. }
  222.  
  223. if (!string.IsNullOrEmpty(item.__Bullet))
  224. {
  225. item.Bullet = BulletBase_Map[item.__Bullet];
  226. }
  227.  
  228. if (!string.IsNullOrEmpty(item.__Shell))
  229. {
  230. item.Shell = ActivityBase_Map[item.__Shell];
  231. }
  232.  
  233. if (!string.IsNullOrEmpty(item.__ShootSound))
  234. {
  235. item.ShootSound = Sound_Map[item.__ShootSound];
  236. }
  237.  
  238. if (!string.IsNullOrEmpty(item.__BeginReloadSound))
  239. {
  240. item.BeginReloadSound = Sound_Map[item.__BeginReloadSound];
  241. }
  242.  
  243. if (!string.IsNullOrEmpty(item.__ReloadSound))
  244. {
  245. item.ReloadSound = Sound_Map[item.__ReloadSound];
  246. }
  247.  
  248. if (!string.IsNullOrEmpty(item.__ReloadFinishSound))
  249. {
  250. item.ReloadFinishSound = Sound_Map[item.__ReloadFinishSound];
  251. }
  252.  
  253. if (!string.IsNullOrEmpty(item.__BeLoadedSound))
  254. {
  255. item.BeLoadedSound = Sound_Map[item.__BeLoadedSound];
  256. }
  257.  
  258. if (item.__OtherSoundMap != null)
  259. {
  260. item.OtherSoundMap = new Dictionary<string, Sound>();
  261. foreach (var pair in item.__OtherSoundMap)
  262. {
  263. item.OtherSoundMap.Add(pair.Key, Sound_Map[pair.Value]);
  264. }
  265. }
  266.  
  267. if (!string.IsNullOrEmpty(item.__AiUseAttribute))
  268. {
  269. item.AiUseAttribute = WeaponBase_Map[item.__AiUseAttribute];
  270. }
  271.  
  272. if (!string.IsNullOrEmpty(item.__AiAttackAttr))
  273. {
  274. item.AiAttackAttr = AiAttackAttr_Map[item.__AiAttackAttr];
  275. }
  276.  
  277. }
  278. catch (Exception e)
  279. {
  280. GD.PrintErr(e.ToString());
  281. throw new Exception("初始化'WeaponBase'引用其他表数据失败, 当前行id: " + item.Id);
  282. }
  283. }
  284. }
  285. private static string _ReadConfigAsText(string path)
  286. {
  287. var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
  288. var asText = file.GetAsText();
  289. file.Dispose();
  290. return asText;
  291. }
  292. }