ProjectGenerator.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text;
  5. using Microsoft.Build.Construction;
  6. using Microsoft.Build.Evaluation;
  7. using GodotTools.Shared;
  8. namespace GodotTools.ProjectEditor
  9. {
  10. public static class ProjectGenerator
  11. {
  12. public static string GodotSdkAttrValue => $"Godot.NET.Sdk/{GeneratedGodotNupkgsVersions.GodotNETSdk}";
  13. public static ProjectRootElement GenGameProject(string name)
  14. {
  15. if (name.Length == 0)
  16. throw new ArgumentException("Project name is empty.", nameof(name));
  17. var root = ProjectRootElement.Create(NewProjectFileOptions.None);
  18. root.Sdk = GodotSdkAttrValue;
  19. var mainGroup = root.AddPropertyGroup();
  20. mainGroup.AddProperty("TargetFramework", "net8.0");
  21. mainGroup.AddProperty("EnableDynamicLoading", "true");
  22. string sanitizedName = IdentifierUtils.SanitizeQualifiedIdentifier(name, allowEmptyIdentifiers: true);
  23. // If the name is not a valid namespace, manually set RootNamespace to a sanitized one.
  24. if (sanitizedName != name)
  25. mainGroup.AddProperty("RootNamespace", sanitizedName);
  26. return root;
  27. }
  28. public static string GenAndSaveGameProject(string dir, string name)
  29. {
  30. if (name.Length == 0)
  31. throw new ArgumentException("Project name is empty.", nameof(name));
  32. string path = Path.Combine(dir, name + ".csproj");
  33. var root = GenGameProject(name);
  34. // Save (without BOM)
  35. root.Save(path, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
  36. return Guid.NewGuid().ToString().ToUpperInvariant();
  37. }
  38. }
  39. }