ConfigComponentSystem.cs 2.2 KB

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