ConfigComponentSystem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. namespace ET
  5. {
  6. [ObjectSystem]
  7. public class ConfigAwakeSystem : AwakeSystem<ConfigComponent>
  8. {
  9. public override void Awake(ConfigComponent self)
  10. {
  11. ConfigComponent.Instance = self;
  12. }
  13. }
  14. [ObjectSystem]
  15. public class ConfigDestroySystem : DestroySystem<ConfigComponent>
  16. {
  17. public override void Destroy(ConfigComponent self)
  18. {
  19. ConfigComponent.Instance = null;
  20. }
  21. }
  22. public static class ConfigComponentSystem
  23. {
  24. public static void LoadOneConfig(this ConfigComponent self, Type configType)
  25. {
  26. byte[] oneConfigBytes = self.ConfigLoader.GetOneConfigBytes(configType.FullName);
  27. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  28. self.AllConfig[configType] = category;
  29. }
  30. public static void Load(this ConfigComponent self)
  31. {
  32. self.AllConfig.Clear();
  33. HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
  34. Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
  35. self.ConfigLoader.GetAllConfigBytes(configBytes);
  36. foreach (Type type in types)
  37. {
  38. self.LoadOneInThread(type, configBytes);
  39. }
  40. }
  41. public static async ETTask LoadAsync(this ConfigComponent self)
  42. {
  43. self.AllConfig.Clear();
  44. HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
  45. Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
  46. self.ConfigLoader.GetAllConfigBytes(configBytes);
  47. using (ListComponent<Task> listTasks = ListComponent<Task>.Create())
  48. {
  49. foreach (Type type in types)
  50. {
  51. Task task = Task.Run(() => self.LoadOneInThread(type, configBytes));
  52. listTasks.Add(task);
  53. }
  54. await Task.WhenAll(listTasks.ToArray());
  55. }
  56. }
  57. private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary<string, byte[]> configBytes)
  58. {
  59. byte[] oneConfigBytes = configBytes[configType.Name];
  60. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  61. lock (self)
  62. {
  63. self.AllConfig[configType] = category;
  64. }
  65. }
  66. }
  67. }