ConfigComponentSystem.cs 2.2 KB

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