Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / generator / UiManagerMethodsGenerator.cs
  1. using System;
  2. using System.IO;
  3. using System.Text.RegularExpressions;
  4. using Godot;
  5.  
  6. namespace Generator;
  7.  
  8. /// <summary>
  9. /// 生成 UiManager 中打开Ui相关的函数代码
  10. /// </summary>
  11. public static class UiManagerMethodsGenerator
  12. {
  13. private static string savePath = "src/game/manager/UiManager_Methods.cs";
  14.  
  15. /// <summary>
  16. /// 执行生成操作, 并返回执行结果
  17. /// </summary>
  18. public static bool Generate()
  19. {
  20. //扫描所有ui
  21. if (!Directory.Exists(GameConfig.UiPrefabDir))
  22. {
  23. return true;
  24. }
  25.  
  26. try
  27. {
  28. var directoryInfo = new DirectoryInfo(GameConfig.UiPrefabDir);
  29. var fileInfos = directoryInfo.GetFiles();
  30.  
  31. var code = $"/**\n" +
  32. $" * 该类为自动生成的, 请不要手动编辑, 以免造成代码丢失\n" +
  33. $" */\n" +
  34. $"public static partial class UiManager\n" +
  35. $"{{\n" +
  36. $"\n";
  37.  
  38. var uiNameClass = $" public static class UiName\n" +
  39. $" {{\n";
  40. var methodClass = "";
  41.  
  42. foreach (var fileInfo in fileInfos)
  43. {
  44. if (fileInfo.Extension == ".tscn")
  45. {
  46. var uiName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 5);
  47. uiNameClass += $" public const string {uiName} = \"{uiName}\";\n";
  48. methodClass += $" /// <summary>\n" +
  49. $" /// 打开 {uiName}, 并返回UI实例\n" +
  50. $" /// </summary>\n" +
  51. $" public static UI.{uiName}.{uiName}Panel Open_{uiName}()\n" +
  52. $" {{\n" +
  53. $" return OpenUi<UI.{uiName}.{uiName}Panel>(UiName.{uiName});\n" +
  54. $" }}\n" +
  55. $"\n" +
  56. $" /// <summary>\n" +
  57. $" /// 获取所有 {uiName} 的实例, 如果没有实例, 则返回一个空数组\n" +
  58. $" /// </summary>\n" +
  59. $" public static UI.{uiName}.{uiName}Panel[] Get_{uiName}_Instance()\n" +
  60. $" {{\n" +
  61. $" return GetUiInstance<UI.{uiName}.{uiName}Panel>(nameof(UI.{uiName}.{uiName}));\n" +
  62. $" }}\n" +
  63. $"\n";
  64. }
  65. }
  66. uiNameClass += $" }}\n\n";
  67.  
  68. code += uiNameClass;
  69. code += methodClass;
  70. code += $"}}\n";
  71.  
  72. File.WriteAllText(savePath, code);
  73. }
  74. catch (Exception e)
  75. {
  76. GD.PrintErr(e.ToString());
  77. return false;
  78. }
  79.  
  80. return true;
  81. }
  82. }