2
0

CSharpProject.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Godot;
  2. using System;
  3. using System.Collections.Generic;
  4. using Godot.Collections;
  5. using GodotTools.Internals;
  6. using GodotTools.ProjectEditor;
  7. using File = GodotTools.Utils.File;
  8. using Directory = GodotTools.Utils.Directory;
  9. namespace GodotTools
  10. {
  11. public static class CSharpProject
  12. {
  13. public static string GenerateGameProject(string dir, string name)
  14. {
  15. try
  16. {
  17. return ProjectGenerator.GenGameProject(dir, name, compileItems: new string[] { });
  18. }
  19. catch (Exception e)
  20. {
  21. GD.PushError(e.ToString());
  22. return string.Empty;
  23. }
  24. }
  25. public static void AddItem(string projectPath, string itemType, string include)
  26. {
  27. if (!(bool) Internal.GlobalDef("mono/project/auto_update_project", true))
  28. return;
  29. ProjectUtils.AddItemToProjectChecked(projectPath, itemType, include);
  30. }
  31. public static void FixApiHintPath(string projectPath)
  32. {
  33. try
  34. {
  35. ProjectUtils.FixApiHintPath(projectPath);
  36. }
  37. catch (Exception e)
  38. {
  39. GD.PushError(e.ToString());
  40. }
  41. }
  42. private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  43. private static ulong ConvertToTimestamp(this DateTime value)
  44. {
  45. TimeSpan elapsedTime = value - Epoch;
  46. return (ulong) elapsedTime.TotalSeconds;
  47. }
  48. public static void GenerateScriptsMetadata(string projectPath, string outputPath)
  49. {
  50. if (File.Exists(outputPath))
  51. File.Delete(outputPath);
  52. var oldDict = Internal.GetScriptsMetadataOrNothing();
  53. var newDict = new Godot.Collections.Dictionary<string, object>();
  54. foreach (var includeFile in ProjectUtils.GetIncludeFiles(projectPath, "Compile"))
  55. {
  56. string projectIncludeFile = ("res://" + includeFile).SimplifyGodotPath();
  57. ulong modifiedTime = File.GetLastWriteTime(projectIncludeFile).ConvertToTimestamp();
  58. if (oldDict.TryGetValue(projectIncludeFile, out var oldFileVar))
  59. {
  60. var oldFileDict = (Dictionary) oldFileVar;
  61. if (ulong.TryParse(oldFileDict["modified_time"] as string, out ulong storedModifiedTime))
  62. {
  63. if (storedModifiedTime == modifiedTime)
  64. {
  65. // No changes so no need to parse again
  66. newDict[projectIncludeFile] = oldFileDict;
  67. continue;
  68. }
  69. }
  70. }
  71. ScriptClassParser.ParseFileOrThrow(projectIncludeFile, out var classes);
  72. string searchName = System.IO.Path.GetFileNameWithoutExtension(projectIncludeFile);
  73. var classDict = new Dictionary();
  74. foreach (var classDecl in classes)
  75. {
  76. if (classDecl.BaseCount == 0)
  77. continue; // Does not inherit nor implement anything, so it can't be a script class
  78. string classCmp = classDecl.Nested ?
  79. classDecl.Name.Substring(classDecl.Name.LastIndexOf(".", StringComparison.Ordinal) + 1) :
  80. classDecl.Name;
  81. if (classCmp != searchName)
  82. continue;
  83. classDict["namespace"] = classDecl.Namespace;
  84. classDict["class_name"] = classDecl.Name;
  85. classDict["nested"] = classDecl.Nested;
  86. break;
  87. }
  88. if (classDict.Count == 0)
  89. continue; // Not found
  90. newDict[projectIncludeFile] = new Dictionary {["modified_time"] = $"{modifiedTime}", ["class"] = classDict};
  91. }
  92. if (newDict.Count > 0)
  93. {
  94. string json = JSON.Print(newDict);
  95. string baseDir = outputPath.GetBaseDir();
  96. if (!Directory.Exists(baseDir))
  97. Directory.CreateDirectory(baseDir);
  98. File.WriteAllText(outputPath, json);
  99. }
  100. }
  101. }
  102. }