GMCommandComponentSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Sirenix.OdinInspector;
  6. using UnityEngine;
  7. using YIUIFramework;
  8. namespace ET.Client
  9. {
  10. /// <summary>
  11. /// 文档: https://lib9kmxvq7k.feishu.cn/wiki/NYADwMydliVmQ7kWXOuc0yxGn7p
  12. /// </summary>
  13. [FriendOf(typeof(GMCommandComponent))]
  14. public static class GMCommandComponentSystem
  15. {
  16. [EntitySystem]
  17. public class GMCommandComponentAwakeSystem : AwakeSystem<GMCommandComponent>
  18. {
  19. protected override void Awake(GMCommandComponent self)
  20. {
  21. self.AllCommandInfo = new Dictionary<int, List<GMCommandInfo>>();
  22. GMKeyHelper.GetKeys();
  23. self.Init();
  24. self.YIUIRoot().OpenPanelAsync<GMPanelComponent>().NoContext();
  25. }
  26. }
  27. [EntitySystem]
  28. public class GMCommandComponentDestroySystem : DestroySystem<GMCommandComponent>
  29. {
  30. protected override void Destroy(GMCommandComponent self)
  31. {
  32. }
  33. }
  34. private static void Init(this GMCommandComponent self)
  35. {
  36. var types = CodeTypes.Instance.GetTypes(typeof(GMAttribute));
  37. foreach (var type in types)
  38. {
  39. var customAttribute = type.GetCustomAttribute<GMAttribute>(true);
  40. var gmCommandInfo = new GMCommandInfo();
  41. var obj = (IGMCommand)Activator.CreateInstance(type);
  42. gmCommandInfo.GMType = customAttribute.GMType;
  43. gmCommandInfo.GMTypeName = GMKeyHelper.GetDesc(customAttribute.GMType);
  44. gmCommandInfo.GMLevel = customAttribute.GMLevel;
  45. gmCommandInfo.GMName = customAttribute.GMName;
  46. gmCommandInfo.GMDesc = customAttribute.GMDesc;
  47. gmCommandInfo.Command = obj;
  48. gmCommandInfo.ParamInfoList = obj.GetParams() ?? new();
  49. self.AddInfo(gmCommandInfo);
  50. }
  51. }
  52. private static void AddInfo(this GMCommandComponent self, GMCommandInfo info)
  53. {
  54. if (!self.AllCommandInfo.ContainsKey(info.GMType))
  55. {
  56. self.AllCommandInfo.Add(info.GMType, new List<GMCommandInfo>());
  57. }
  58. var listInfo = self.AllCommandInfo[info.GMType];
  59. listInfo.Add(info);
  60. }
  61. public static async ETTask Run(this GMCommandComponent self, GMCommandInfo info)
  62. {
  63. /*
  64. * TODO 判断info中的 GM命令需求等级
  65. * 取自身数据中的等级 判断是否符合要求
  66. */
  67. var paramList = info.ParamInfoList;
  68. var objData = new List<object>();
  69. for (int i = 0; i < paramList.Count; i++)
  70. {
  71. var paramInfo = paramList[i];
  72. var objValue = paramInfo.ParamType.TryToValue(paramInfo);
  73. objData.Add(objValue);
  74. }
  75. EntityRef<GMCommandComponent> selfRef = self;
  76. var banClickCode = self.YIUIMgr().BanLayerOptionForever();
  77. try
  78. {
  79. var paramVo = ParamVo.Get(objData);
  80. var closeGM = await info.Command.Run(self.Root(), paramVo);
  81. ParamVo.Put(paramVo);
  82. if (closeGM)
  83. {
  84. self = selfRef;
  85. await self.DynamicEvent(new OnGMEventClose());
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. Log.Error($"GM 执行错误 {e}");
  91. }
  92. finally
  93. {
  94. self = selfRef;
  95. self.YIUIMgr().RecoverLayerOptionForever(banClickCode);
  96. }
  97. }
  98. }
  99. }