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