ConfigComponentSystem.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 ()
  48. //{
  49. ListComponent<Task> listTasks = ListComponent<Task>.Create();
  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. listTasks.Dispose();
  57. //}
  58. }
  59. private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary<string, byte[]> configBytes)
  60. {
  61. byte[] oneConfigBytes = configBytes[configType.Name];
  62. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  63. lock (self)
  64. {
  65. self.AllConfig[configType] = category;
  66. }
  67. }
  68. }
  69. }