Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / generator / ResourcePathGenerator.cs
@小李xl 小李xl on 29 Jan 2024 3 KB 金币效果
  1. #if TOOLS
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using Godot;
  8.  
  9. namespace Generator;
  10.  
  11. /// <summary>
  12. /// ResourcePath 类文件生成器
  13. /// </summary>
  14. public static class ResourcePathGenerator
  15. {
  16. //支持后缀
  17. private static string[] suffix =
  18. {
  19. ".png",
  20. ".jpg",
  21. ".txt",
  22. ".json",
  23. ".ini",
  24. ".tscn",
  25. ".tres",
  26. ".otf",
  27. ".gdshader",
  28. ".ogg",
  29. ".mp3",
  30. ".wav",
  31. ".svg",
  32. ".ttf",
  33. ".otf"
  34. };
  35. //排除的文件夹, 斜杠用: /
  36. private static string[] exclude =
  37. {
  38. ".vscode",
  39. ".idea",
  40. ".git",
  41. ".import",
  42. ".mono",
  43. ".VSCodeCounter",
  44. "android",
  45. "addons",
  46. ".godot",
  47. ".vs",
  48. "resource/map"
  49. };
  50.  
  51. private static string resultStr = "";
  52.  
  53. //保存路径
  54. private static string savePath = "src/game/manager/ResourcePath.cs";
  55.  
  56. /// <summary>
  57. /// 执行生成操作, 返回是否执行成功
  58. /// </summary>
  59. public static bool Generate()
  60. {
  61. try
  62. {
  63. resultStr = "/// <summary>\n" +
  64. "/// 编辑器下所有资源路径, 该类为 Tools 面板下自动生成的, 请不要手动编辑!\n" +
  65. "/// </summary>\n" +
  66. "public class ResourcePath\n" +
  67. "{\n";
  68.  
  69. GD.Print("更新 ResourcePath...");
  70.  
  71. var directoryInfo = new DirectoryInfo(System.Environment.CurrentDirectory);
  72. EachDir(directoryInfo);
  73.  
  74. resultStr += "}";
  75. File.WriteAllText(savePath, resultStr);
  76. GD.Print("ResourcePath.cs 写出完成!");
  77. }
  78. catch (Exception e)
  79. {
  80. GD.PrintErr(e.ToString());
  81. return false;
  82. }
  83.  
  84. return true;
  85. }
  86. private static void EachDir(DirectoryInfo directoryInfos)
  87. {
  88. if (directoryInfos.FullName.Length > System.Environment.CurrentDirectory.Length)
  89. {
  90. var path = directoryInfos.FullName.Substring(System.Environment.CurrentDirectory.Length + 1);
  91. path = path.Replace('\\', '/');
  92. if (exclude.Contains(path))
  93. {
  94. GD.Print("扫描排除路径: " + path);
  95. return;
  96. }
  97. }
  98.  
  99. var fileInfos = directoryInfos.GetFiles();
  100. for (var i = 0; i < fileInfos.Length; i++)
  101. {
  102. HandleFile(fileInfos[i]);
  103. }
  104.  
  105. var directories = directoryInfos.GetDirectories();
  106. for (var i = 0; i < directories.Length; i++)
  107. {
  108. EachDir(directories[i]);
  109. }
  110. }
  111.  
  112. private static void HandleFile(FileInfo fileInfo)
  113. {
  114. if (suffix.Contains(fileInfo.Extension))
  115. {
  116. var field = fileInfo.FullName.Substring(System.Environment.CurrentDirectory.Length + 1);
  117. field = field.Replace("\\", "/");
  118. var resPath = "res://" + field;
  119. field = field.Replace(".", "_");
  120. field = field.Replace("/", "_");
  121. field = Regex.Replace(field, "[^\\w]", "");
  122. resultStr += $" public const string {field} = \"{resPath}\";\n";
  123. }
  124. }
  125. }
  126.  
  127. #endif