Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / generator / BuffGenerator.cs
@小李xl 小李xl on 15 Mar 2024 5 KB buff系统重构中
  1. #if TOOLS
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using Godot;
  8.  
  9. namespace Generator;
  10.  
  11. /// <summary>
  12. /// 生成 Buff 属性表
  13. /// </summary>
  14. public static class BuffGenerator
  15. {
  16. private const string SavePath = "src/game/manager/BuffRegister.cs";
  17. public static bool Generate()
  18. {
  19. try
  20. {
  21. var str = "";
  22. var buffInfos = new Dictionary<string, BuffInfo>();
  23. var types = typeof(BuffGenerator).Assembly.GetTypes();
  24. //包含[BuffAttribute]特性
  25. var enumerable = types.Where(type =>
  26. type.IsClass && !type.IsAbstract && type.IsAssignableTo(typeof(BuffFragment)));
  27. foreach (var type in enumerable)
  28. {
  29. var attribute = (BuffAttribute)type.GetCustomAttribute(typeof(BuffAttribute), false);
  30. if (attribute != null)
  31. {
  32. if (buffInfos.ContainsKey(attribute.BuffName))
  33. {
  34. GD.PrintErr($"Buff '{attribute.BuffName}' 重名!");
  35. return false;
  36. }
  37. var buffInfo = new BuffInfo(attribute.BuffName, attribute.Description, type);
  38. str += $"{buffInfo.Name}: {buffInfo.Description}\n";
  39. buffInfos.Add(attribute.BuffName, buffInfo);
  40. //判断重写参数情况
  41. //1参数
  42. var methodInfo1 = type.GetMethod(nameof(BuffFragment.InitParam),
  43. BindingFlags.Instance | BindingFlags.Public, new Type[] { typeof(float) });
  44. if (methodInfo1 != null &&
  45. methodInfo1.GetBaseDefinition().DeclaringType != methodInfo1.DeclaringType)
  46. {
  47. buffInfo.Params.Add(1);
  48. }
  49.  
  50. //2参数
  51. var methodInfo2 = type.GetMethod(nameof(BuffFragment.InitParam),
  52. BindingFlags.Instance | BindingFlags.Public, new Type[] { typeof(float), typeof(float) });
  53. if (methodInfo2 != null &&
  54. methodInfo2.GetBaseDefinition().DeclaringType != methodInfo2.DeclaringType)
  55. {
  56. buffInfo.Params.Add(2);
  57. }
  58.  
  59. //3参数
  60. var methodInfo3 = type.GetMethod(nameof(BuffFragment.InitParam),
  61. BindingFlags.Instance | BindingFlags.Public,
  62. new Type[] { typeof(float), typeof(float), typeof(float) });
  63. if (methodInfo3 != null &&
  64. methodInfo3.GetBaseDefinition().DeclaringType != methodInfo3.DeclaringType)
  65. {
  66. buffInfo.Params.Add(3);
  67. }
  68.  
  69. //4参数
  70. var methodInfo4 = type.GetMethod(nameof(BuffFragment.InitParam),
  71. BindingFlags.Instance | BindingFlags.Public,
  72. new Type[] { typeof(float), typeof(float), typeof(float), typeof(float) });
  73. if (methodInfo4 != null &&
  74. methodInfo4.GetBaseDefinition().DeclaringType != methodInfo4.DeclaringType)
  75. {
  76. buffInfo.Params.Add(4);
  77. }
  78. }
  79. }
  80. GenerateCode(buffInfos);
  81. GD.Print("-----------------------------");
  82. GD.Print(str);
  83. }
  84. catch (Exception e)
  85. {
  86. GD.PrintErr(e.Message + "\n" + e.StackTrace);
  87. return false;
  88. }
  89.  
  90. return true;
  91. }
  92. private static void GenerateCode(Dictionary<string, BuffInfo> buffInfo)
  93. {
  94. var str = "";
  95. foreach (var kv in buffInfo)
  96. {
  97. var info = kv.Value;
  98. var s = "";
  99. for (var i = 0; i < info.Params.Count; i++)
  100. {
  101. if (i > 0) s += ", ";
  102. s += info.Params[i];
  103. }
  104.  
  105. str += $" BuffInfos.Add(\"{info.Name}\", new BuffInfo(\"{info.Name}\", null, new List<int>() {{ {s} }}, typeof({info.Type.FullName})));\n";
  106. }
  107. var code = $"using System.Collections.Generic;\n" +
  108. $"/// <summary>\n" +
  109. $"/// buff 注册类, 调用 Init() 函数初始化数据\n" +
  110. $"/// 注意: 该类为 Tools 面板下自动生成的, 请不要手动编辑!\n" +
  111. $"/// </summary>\n" +
  112. $"public class BuffRegister\n" +
  113. $"{{\n" +
  114. $" /// <summary>\n" +
  115. $" /// 所有 buff 信息\n" +
  116. $" /// </summary>\n" +
  117. $" public static Dictionary<string, BuffInfo> BuffInfos {{ get; private set; }}\n" +
  118. $" /// <summary>\n" +
  119. $" /// 初始化 buff\n" +
  120. $" /// </summary>\n" +
  121. $" public static void Init()\n" +
  122. $" {{\n" +
  123. $" BuffInfos = new Dictionary<string, BuffInfo>();\n" +
  124. str +
  125. $" }}\n" +
  126. $"}}";
  127. File.WriteAllText(SavePath, code);
  128. }
  129. }
  130. #endif