ConfigComponentSystem.cs 2.2 KB

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