LubanConfigLoader.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using Luban;
  5. #if DOTNET || UNITY_STANDALONE
  6. using System.Threading.Tasks;
  7. #endif
  8. namespace ET
  9. {
  10. public partial class ConfigLoader : Singleton<ConfigLoader>, ISingletonAwake
  11. {
  12. public struct ConfigDeserialize
  13. {
  14. public Type Type;
  15. public byte[] ConfigBytes;
  16. }
  17. public struct LubanGetConfigBytes
  18. {
  19. public Type Type;
  20. public string CodeMode;
  21. }
  22. public struct GetAllConfigBytes
  23. {
  24. }
  25. public struct GetOneConfigBytes
  26. {
  27. public string ConfigName;
  28. }
  29. public struct LubanGetAllConfigBytes
  30. {
  31. }
  32. public struct LubanGetOneConfigBytes
  33. {
  34. public string ConfigName;
  35. }
  36. private readonly ConcurrentDictionary<Type, ILubanConfig> m_AllConfig = new();
  37. public void Awake()
  38. {
  39. }
  40. public async ETTask Reload(Type configType)
  41. {
  42. var configAttribute = configType.GetCustomAttributes(typeof(ConfigAttribute), false)[0] as ConfigAttribute;
  43. var oneConfigBytes = await EventSystem.Instance.Invoke<LubanGetOneConfigBytes, ETTask<byte[]>>(configAttribute.ConfigType, new LubanGetOneConfigBytes()
  44. {
  45. ConfigName = configType.Name
  46. });
  47. LoadOneConfig(configType, oneConfigBytes);
  48. ResolveRef();
  49. ConfigProcess();
  50. }
  51. public async ETTask LoadAsync()
  52. {
  53. m_AllConfig.Clear();
  54. var configBytes = await EventSystem.Instance.Invoke<LubanGetAllConfigBytes, ETTask<Dictionary<Type, byte[]>>>(new LubanGetAllConfigBytes());
  55. #if DOTNET || UNITY_STANDALONE
  56. using ListComponent<Task> listTasks = ListComponent<Task>.Create();
  57. foreach (Type type in configBytes.Keys)
  58. {
  59. var oneConfigBytes = configBytes[type];
  60. Task task = Task.Run(() => LoadOneConfig(type, oneConfigBytes));
  61. listTasks.Add(task);
  62. }
  63. await Task.WhenAll(listTasks.ToArray());
  64. #else
  65. foreach (Type type in configBytes.Keys)
  66. {
  67. LoadOneConfig(type, configBytes[type]);
  68. }
  69. #endif
  70. ResolveRef();
  71. ConfigProcess();
  72. }
  73. private void LoadOneConfig(Type configType, byte[] oneConfigBytes)
  74. {
  75. var configAttribute = configType.GetCustomAttributes(typeof(ConfigAttribute), false)[0] as ConfigAttribute;
  76. var category = EventSystem.Instance.Invoke<ConfigDeserialize, object>(configAttribute.ConfigType, new ConfigDeserialize() { Type = configType, ConfigBytes = oneConfigBytes });
  77. if (category is not ILubanConfig iConfig)
  78. {
  79. Log.Error($"{configType}, 数据类型错误 实际 {category.GetType()}");
  80. return;
  81. }
  82. m_AllConfig[configType] = iConfig;
  83. World.Instance.AddSingleton(category as ASingleton);
  84. }
  85. private void ResolveRef()
  86. {
  87. foreach (var targetConfig in m_AllConfig.Values)
  88. {
  89. targetConfig.ResolveRef();
  90. }
  91. foreach (var targetConfig in m_AllConfig.Values)
  92. {
  93. Initialized(targetConfig);
  94. }
  95. }
  96. private void Initialized(ILubanConfig configCategory)
  97. {
  98. var iConfigSystems = EntitySystemSingleton.Instance.TypeSystems.GetSystems(configCategory.GetType(), typeof(ILubanConfigSystem));
  99. if (iConfigSystems == null)
  100. {
  101. return;
  102. }
  103. foreach (ILubanConfigSystem aConfigSystem in iConfigSystems)
  104. {
  105. if (aConfigSystem == null)
  106. {
  107. continue;
  108. }
  109. try
  110. {
  111. aConfigSystem.LubanConfig(configCategory);
  112. }
  113. catch (Exception e)
  114. {
  115. Log.Error(e);
  116. }
  117. }
  118. }
  119. private void ConfigProcess()
  120. {
  121. var hashSet = CodeTypes.Instance.GetTypes(typeof(ConfigProcessAttribute));
  122. foreach (Type type in hashSet)
  123. {
  124. object obj = Activator.CreateInstance(type);
  125. if (obj is ISingletonAwake awakeSingleton)
  126. {
  127. awakeSingleton.Awake();
  128. }
  129. World.Instance.AddSingleton((ASingleton)obj);
  130. }
  131. }
  132. }
  133. }