GMKeyHelper.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace ET.Client
  5. {
  6. public static class GMKeyHelper
  7. {
  8. [StaticField]
  9. private static List<int> m_AllGMKey;
  10. [StaticField]
  11. private static Dictionary<int, string> m_GMKeyDesc;
  12. /// <summary>
  13. /// 反射获取枚举列表
  14. /// 注意: 运行时使用的是ET编译过的dll
  15. /// 如果新增了没有编译 则无法获取到最新数据 请注意一定要编译后运行
  16. /// </summary>
  17. public static List<int> GetKeys(bool force = false)
  18. {
  19. if (m_AllGMKey != null && !force)
  20. {
  21. return m_AllGMKey;
  22. }
  23. HashSet<int> hashKeys = new();
  24. m_AllGMKey = new();
  25. m_GMKeyDesc = new();
  26. var assembly = YIUIFramework.AssemblyHelper.GetAssembly("ET.ModelView");
  27. if (assembly == null)
  28. {
  29. Log.Error($"没有找到ET.ModelView程序集");
  30. return m_AllGMKey;
  31. }
  32. Type gmKeyType = assembly.GetType("ET.Client.EGMType");
  33. if (gmKeyType == null)
  34. {
  35. Log.Error($"没有找到ET.Client.EGMType类型");
  36. return m_AllGMKey;
  37. }
  38. FieldInfo[] fields = gmKeyType.GetFields(BindingFlags.Public | BindingFlags.Static);
  39. foreach (FieldInfo field in fields)
  40. {
  41. if (field.IsLiteral && field.FieldType == typeof(int))
  42. {
  43. int key = (int)field.GetValue(null);
  44. if (!hashKeys.Add(key))
  45. {
  46. Log.Error($"GMKey重复: {key}");
  47. continue;
  48. }
  49. m_AllGMKey.Add(key);
  50. var keyDesc = "";
  51. var labelTextAttribute = field.GetCustomAttribute<GMGroupAttribute>();
  52. if (labelTextAttribute != null)
  53. {
  54. keyDesc = labelTextAttribute.Name;
  55. }
  56. m_GMKeyDesc[key] = keyDesc;
  57. }
  58. }
  59. m_AllGMKey.Sort();
  60. return m_AllGMKey;
  61. }
  62. // 获取GM描述
  63. public static string GetDesc(int key)
  64. {
  65. return m_GMKeyDesc?.GetValueOrDefault(key, "");
  66. }
  67. // 统一显示GM描述
  68. public static string GetDisplayDesc(int key)
  69. {
  70. return $"{key}_{m_GMKeyDesc?.GetValueOrDefault(key, "")}";
  71. }
  72. }
  73. }