Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / generator / ResourcePathGenerator.cs
@小李xl 小李xl on 24 Feb 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. "excelTool"
  50. };
  51.  
  52. private static string resultStr = "";
  53.  
  54. //保存路径
  55. private static string savePath = "src/game/manager/ResourcePath.cs";
  56.  
  57. /// <summary>
  58. /// 执行生成操作, 返回是否执行成功
  59. /// </summary>
  60. public static bool Generate()
  61. {
  62. try
  63. {
  64. resultStr = "/// <summary>\n" +
  65. "/// 编辑器下所有资源路径, 该类为 Tools 面板下自动生成的, 请不要手动编辑!\n" +
  66. "/// </summary>\n" +
  67. "public class ResourcePath\n" +
  68. "{\n";
  69.  
  70. GD.Print("更新 ResourcePath...");
  71.  
  72. var directoryInfo = new DirectoryInfo(System.Environment.CurrentDirectory);
  73. EachDir(directoryInfo);
  74.  
  75. resultStr += "}";
  76. File.WriteAllText(savePath, resultStr);
  77. GD.Print("ResourcePath.cs 写出完成!");
  78. }
  79. catch (Exception e)
  80. {
  81. GD.PrintErr(e.ToString());
  82. return false;
  83. }
  84.  
  85. return true;
  86. }
  87. private static void EachDir(DirectoryInfo directoryInfos)
  88. {
  89. if (directoryInfos.FullName.Length > System.Environment.CurrentDirectory.Length)
  90. {
  91. var path = directoryInfos.FullName.Substring(System.Environment.CurrentDirectory.Length + 1);
  92. path = path.Replace('\\', '/');
  93. if (exclude.Contains(path))
  94. {
  95. GD.Print("扫描排除路径: " + path);
  96. return;
  97. }
  98. }
  99.  
  100. var fileInfos = directoryInfos.GetFiles();
  101. for (var i = 0; i < fileInfos.Length; i++)
  102. {
  103. HandleFile(fileInfos[i]);
  104. }
  105.  
  106. var directories = directoryInfos.GetDirectories();
  107. for (var i = 0; i < directories.Length; i++)
  108. {
  109. EachDir(directories[i]);
  110. }
  111. }
  112.  
  113. private static void HandleFile(FileInfo fileInfo)
  114. {
  115. if (suffix.Contains(fileInfo.Extension))
  116. {
  117. var field = fileInfo.FullName.Substring(System.Environment.CurrentDirectory.Length + 1);
  118. field = field.Replace("\\", "/");
  119. var resPath = "res://" + field;
  120. field = field.Replace(".", "_");
  121. field = field.Replace("/", "_");
  122. field = Regex.Replace(field, "[^\\w]", "");
  123. resultStr += $" public const string {field} = \"{resPath}\";\n";
  124. }
  125. }
  126. }
  127.  
  128. #endif