ConfigComponentSystem.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = ConfigComponent.GetOneConfigBytes(configType.FullName);
  25. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  26. self.AllConfig[configType] = category;
  27. }
  28. public static async ETTask LoadAsync(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. ConfigComponent.GetAllConfigBytes(configBytes);
  34. List<Task> listTasks = new List<Task>();
  35. foreach (Type type in types)
  36. {
  37. Task task = Task.Run(() => self.LoadOneInThread(type, configBytes));
  38. listTasks.Add(task);
  39. }
  40. await Task.WhenAll(listTasks.ToArray());
  41. }
  42. private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary<string, byte[]> configBytes)
  43. {
  44. byte[] oneConfigBytes = configBytes[configType.Name];
  45. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  46. lock (self)
  47. {
  48. self.AllConfig[configType] = category;
  49. }
  50. }
  51. }
  52. }