ConfigComponent.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading;
  6. using Excel2Json;
  7. using Fort23.Core;
  8. using Fort23.GameData;
  9. using Utility;
  10. #if !COMBAT_SERVER
  11. using UnityEditor;
  12. using UnityEngine;
  13. #endif
  14. namespace Fort23.UTool
  15. {
  16. /// <summary>
  17. /// 配置表组件
  18. /// </summary>
  19. public class ConfigComponent : Singleton<ConfigComponent>
  20. {
  21. private Dictionary<Type, ConfigHolder> _allConfigHolders = new Dictionary<Type, ConfigHolder>();
  22. #if COMBAT_SERVER
  23. private bool init;
  24. public void SetConfig(Dictionary<Type, ConfigHolder> allConfigHolders)
  25. {
  26. if (init)
  27. {
  28. return;
  29. }
  30. init = true;
  31. _allConfigHolders.Clear();
  32. _allConfigHolders = allConfigHolders;
  33. }
  34. public override void Dispose()
  35. {
  36. _allConfigHolders.Clear();
  37. base.Dispose();
  38. }
  39. #endif
  40. #if !COMBAT_SERVER
  41. #if UNITY_EDITOR
  42. /// <summary>
  43. /// 预先加载所有配置文件
  44. /// </summary>
  45. /// <param name="isLogin">登录的时候只加载指定列表里面的数据,这样可以减少加载时间</param>
  46. public void EditorPreload(bool isLogin = false)
  47. {
  48. _allConfigHolders.Clear();
  49. ConfigHolder configHolder = null;
  50. Assembly gameDataAssembly = EventSystem.Instance.GetAssembly("Fort23.GameData.dll");
  51. if (gameDataAssembly == null)
  52. {
  53. LogTool.Error("Fort23.GameData资源加载失败");
  54. return;
  55. }
  56. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  57. foreach (Type type in gameDataAssembly.GetTypes())
  58. {
  59. object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
  60. if (attrs.Length == 0)
  61. {
  62. continue;
  63. }
  64. try
  65. {
  66. ConfigAttribute configAttribute =
  67. type.GetCustomAttribute(typeof(ConfigAttribute)) as ConfigAttribute;
  68. if (configAttribute != null)
  69. {
  70. TextAsset ta =
  71. AssetDatabase.LoadAssetAtPath<TextAsset>(@"Assets\Res\Config\" + configAttribute.prefab);
  72. configHolder = JsonHelper.FromJson(ta.text, type) as ConfigHolder;
  73. if (configHolder != null)
  74. {
  75. configHolder.Init();
  76. _allConfigHolders[configHolder.ConfigType] = configHolder;
  77. }
  78. else
  79. {
  80. LogTool.Error($"JSON转{type.Name}失败!");
  81. }
  82. }
  83. else
  84. {
  85. LogTool.Error($"配置文件类{type.Name}没有添加ConfigAttribute!");
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. LogTool.Error("导表错误" + type);
  91. LogTool.Exception(e);
  92. }
  93. }
  94. LogTool.Log("所有配置文件初始化完成");
  95. }
  96. #endif
  97. /// <summary>
  98. /// 预先加载所有配置文件
  99. /// </summary>
  100. /// <param name="isLogin">登录的时候只加载指定列表里面的数据,这样可以减少加载时间</param>
  101. public async CTask Preload()
  102. {
  103. _allConfigHolders.Clear();
  104. ConfigHolder configHolder = null;
  105. Assembly gameDataAssembly = EventSystem.Instance.GetAssembly("Fort23.GameData.dll");
  106. if (gameDataAssembly == null)
  107. {
  108. LogTool.Error("Fort23.GameData资源加载失败");
  109. return;
  110. }
  111. CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
  112. foreach (Type type in gameDataAssembly.GetTypes())
  113. {
  114. object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
  115. if (attrs.Length == 0)
  116. {
  117. continue;
  118. }
  119. try
  120. {
  121. ConfigAttribute configAttribute = type.GetCustomAttribute(typeof(ConfigAttribute)) as ConfigAttribute;
  122. if (configAttribute != null)
  123. {
  124. CTask<AssetHandle> cTask = AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(configAttribute.prefab, callBack: delegate(AssetHandle json)
  125. {
  126. if (json == null)
  127. {
  128. LogTool.Error("加载配置表错误"+configAttribute.prefab);
  129. return;
  130. }
  131. configHolder = JsonHelper.FromJson(json.AssetObject<TextAsset>().text, type) as ConfigHolder;
  132. json.Release();
  133. // TODO 验证服预留
  134. try
  135. {
  136. if (configHolder != null)
  137. {
  138. configHolder.Init();
  139. _allConfigHolders[configHolder.ConfigType] = configHolder;
  140. }
  141. else
  142. {
  143. LogTool.Error($"JSON转{type.Name}失败!");
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. LogTool.Error(e);
  149. LogTool.Error($"JSON转{type.Name}失败!");
  150. }
  151. #if !COMBAT_SERVER
  152. });
  153. cTaskAwaitBuffer.AddTask(cTask);
  154. #endif
  155. }
  156. else
  157. {
  158. LogTool.Error($"配置文件类{type.Name}没有添加ConfigAttribute!");
  159. }
  160. }
  161. catch (Exception e)
  162. {
  163. LogTool.Error("导表错误"+type);
  164. LogTool.Exception(e);
  165. }
  166. }
  167. await cTaskAwaitBuffer.WaitAll();
  168. // tasks.Dispose();
  169. LogTool.Log("所有配置文件初始化完成");
  170. }
  171. #endif
  172. public T Get<T>(int ID) where T : struct, IConfig
  173. {
  174. ConfigHolder configHolder;
  175. if (!_allConfigHolders.TryGetValue(typeof(T), out configHolder))
  176. {
  177. LogTool.Exception(new Exception($"ConfigComponent not found key: {typeof(T).FullName}"));
  178. return default;
  179. }
  180. IConfig iconfig = configHolder.TryGet(ID);
  181. return iconfig is T ? (T)iconfig : default;
  182. }
  183. public T[] GetAll<T>() where T : struct, IConfig
  184. {
  185. ConfigHolder configHolder;
  186. if (!_allConfigHolders.TryGetValue(typeof(T), out configHolder))
  187. {
  188. throw new Exception($"ConfigComponent not found key: {typeof(T).FullName}");
  189. }
  190. List<IConfig> lists = configHolder.GetAll();
  191. T[] configs = new T[configHolder.GetAll().Count];
  192. for (int i = 0; i < configs.Length; i++)
  193. {
  194. IConfig iconfig = lists[i];
  195. configs[i] = iconfig is T ? (T)iconfig : default;
  196. }
  197. return configs;
  198. }
  199. }
  200. }