ConfigComponent.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Fort23.GameData;
  6. using UnityEngine;
  7. using Utility;
  8. public class ConfigComponent : Singleton<ConfigComponent>
  9. {
  10. private Dictionary<Type, ConfigHolder> _allConfigHolders = new Dictionary<Type, ConfigHolder>();
  11. private readonly Dictionary<string, Assembly> _assemblies = new Dictionary<string, Assembly>();
  12. public void Preload(bool isLogin = false)
  13. {
  14. string[] assemblyNames =
  15. {
  16. "GameData.dll"
  17. };
  18. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  19. {
  20. string assemblyName = $"{assembly.GetName().Name}.dll";
  21. if (!((IList)assemblyNames).Contains(assemblyName))
  22. {
  23. continue;
  24. }
  25. _assemblies[assemblyName] = assembly;
  26. }
  27. _allConfigHolders.Clear();
  28. ConfigHolder configHolder = null;
  29. Assembly gameDataAssembly = _assemblies["GameData.dll"];
  30. foreach (Type type in gameDataAssembly.GetTypes())
  31. {
  32. object[] attrs = type.GetCustomAttributes(typeof(ConfigAttribute), false);
  33. if (attrs.Length == 0)
  34. {
  35. continue;
  36. }
  37. try
  38. {
  39. ConfigAttribute configAttribute =
  40. type.GetCustomAttribute(typeof(ConfigAttribute)) as ConfigAttribute;
  41. if (configAttribute != null)
  42. {
  43. TextAsset ta = Resources.Load<TextAsset>("Config/" + configAttribute.prefab);
  44. configHolder = JsonHelper.FromJson(ta.text, type) as ConfigHolder;
  45. if (configHolder != null)
  46. {
  47. configHolder.Init();
  48. _allConfigHolders[configHolder.ConfigType] = configHolder;
  49. }
  50. else
  51. {
  52. Debug.LogError($"JSON转{type.Name}失败!");
  53. }
  54. }
  55. else
  56. {
  57. Debug.LogError($"配置文件类{type.Name}没有添加ConfigAttribute!");
  58. }
  59. }
  60. catch (Exception e)
  61. {
  62. Debug.LogError("导表错误" + type);
  63. }
  64. }
  65. }
  66. public T Get<T>(int ID) where T : struct, IConfig
  67. {
  68. ConfigHolder configHolder;
  69. if (!_allConfigHolders.TryGetValue(typeof(T), out configHolder))
  70. {
  71. Debug.LogException(new Exception($"ConfigComponent not found key: {typeof(T).FullName}"));
  72. return default;
  73. }
  74. IConfig iconfig = configHolder.TryGet(ID);
  75. return iconfig is T ? (T)iconfig : default;
  76. }
  77. public T[] GetAll<T>() where T : struct, IConfig
  78. {
  79. ConfigHolder configHolder;
  80. if (!_allConfigHolders.TryGetValue(typeof(T), out configHolder))
  81. {
  82. throw new Exception($"ConfigComponent not found key: {typeof(T).FullName}");
  83. }
  84. List<IConfig> lists = configHolder.GetAll();
  85. T[] configs = new T[configHolder.GetAll().Count];
  86. for (int i = 0; i < configs.Length; i++)
  87. {
  88. IConfig iconfig = lists[i];
  89. configs[i] = iconfig is T ? (T)iconfig : default;
  90. }
  91. return configs;
  92. }
  93. }