A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.4KB

  1. using System.CodeDom;
  2. using System.CodeDom.Compiler;
  3. using System.IO;
  4. using System.Linq;
  5. using RobocraftX.Common;
  6. namespace CodeGenerator
  7. {
  8. public class BlockClassGenerator
  9. {
  10. public void Generate(string name, string group)
  11. {
  12. if (group is null)
  13. {
  14. group = GetGroup(name) + "_BLOCK_GROUP";
  15. if (typeof(CommonExclusiveGroups).GetFields().All(field => field.Name != group))
  16. group = GetGroup(name) + "_BLOCK_BUILD_GROUP";
  17. }
  18. var codeUnit = new CodeCompileUnit();
  19. var ns = new CodeNamespace("TechbloxModdingAPI.Blocks");
  20. ns.Imports.Add(new CodeNamespaceImport("RobocraftX.Common"));
  21. ns.Imports.Add(new CodeNamespaceImport("Svelto.ECS"));
  22. var cl = new CodeTypeDeclaration(name);
  23. cl.Members.Add(new CodeConstructor
  24. {
  25. Parameters = {new CodeParameterDeclarationExpression("EGID", "egid")},
  26. Comments = { new CodeCommentStatement($"{name} constructor", true)}
  27. });
  28. cl.Members.Add(new CodeConstructor
  29. {
  30. Parameters =
  31. {
  32. new CodeParameterDeclarationExpression(typeof(uint), "id")
  33. },
  34. Comments = {new CodeCommentStatement($"{name} constructor", true)},
  35. BaseConstructorArgs =
  36. {
  37. new CodeObjectCreateExpression("EGID", new CodeVariableReferenceExpression("id"),
  38. new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("CommonExclusiveGroups"),
  39. group))
  40. }
  41. });
  42. ns.Types.Add(cl);
  43. codeUnit.Namespaces.Add(ns);
  44. var provider = CodeDomProvider.CreateProvider("CSharp");
  45. using (var sw = new StreamWriter($"{name}.cs"))
  46. {
  47. provider.GenerateCodeFromCompileUnit(codeUnit, sw, new CodeGeneratorOptions {BracingStyle = "C"});
  48. }
  49. }
  50. private static string GetGroup(string name)
  51. {
  52. var ret = "";
  53. foreach (var ch in name)
  54. {
  55. if (char.IsUpper(ch) && ret.Length > 0)
  56. ret += "_" + ch;
  57. else
  58. ret += char.ToUpper(ch);
  59. }
  60. return ret;
  61. }
  62. }
  63. }