ConfigComponentSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. List<Task> listTasks = new List<Task>();
  46. foreach (Type type in types)
  47. {
  48. Task task = Task.Run(() => self.LoadOneInThread(type, configBytes));
  49. listTasks.Add(task);
  50. }
  51. await Task.WhenAll(listTasks.ToArray());
  52. }
  53. private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary<string, byte[]> configBytes)
  54. {
  55. byte[] oneConfigBytes = configBytes[configType.Name];
  56. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  57. lock (self)
  58. {
  59. self.AllConfig[configType] = category;
  60. }
  61. }
  62. }
  63. }