ConfigComponentSystem.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 async ETTask LoadAsync(this ConfigComponent self)
  23. {
  24. self.AllConfig.Clear();
  25. HashSet<Type> types = Game.EventSystem.GetTypes(typeof (ConfigAttribute));
  26. Dictionary<string, byte[]> configBytes = new Dictionary<string, byte[]>();
  27. ConfigComponent.GetAllConfigBytes(configBytes);
  28. List<Task> listTasks = new List<Task>();
  29. foreach (Type type in types)
  30. {
  31. Task task = Task.Run(() => self.LoadOneInThread(type, configBytes));
  32. listTasks.Add(task);
  33. }
  34. await Task.WhenAll(listTasks.ToArray());
  35. }
  36. private static void LoadOneInThread(this ConfigComponent self, Type configType, Dictionary<string, byte[]> configBytes)
  37. {
  38. byte[] oneConfigBytes = configBytes[configType.Name];
  39. object category = ProtobufHelper.FromBytes(configType, oneConfigBytes, 0, oneConfigBytes.Length);
  40. lock (self)
  41. {
  42. self.AllConfig[configType] = category;
  43. }
  44. }
  45. }
  46. }