|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.IO;
- using System.Linq;
- using RobocraftX.Common;
-
- namespace CodeGenerator
- {
- public class BlockClassGenerator
- {
- public void Generate(string name, string group)
- {
- if (group is null)
- {
- group = GetGroup(name) + "_BLOCK_GROUP";
- if (typeof(CommonExclusiveGroups).GetFields().All(field => field.Name != group))
- group = GetGroup(name) + "_BLOCK_BUILD_GROUP";
- }
-
- var codeUnit = new CodeCompileUnit();
- var ns = new CodeNamespace("TechbloxModdingAPI.Blocks");
- ns.Imports.Add(new CodeNamespaceImport("RobocraftX.Common"));
- ns.Imports.Add(new CodeNamespaceImport("Svelto.ECS"));
- var cl = new CodeTypeDeclaration(name);
- cl.Members.Add(new CodeConstructor
- {
- Parameters = {new CodeParameterDeclarationExpression("EGID", "egid")},
- Comments = { new CodeCommentStatement($"{name} constructor", true)}
- });
- cl.Members.Add(new CodeConstructor
- {
- Parameters =
- {
- new CodeParameterDeclarationExpression(typeof(uint), "id")
- },
- Comments = {new CodeCommentStatement($"{name} constructor", true)},
- BaseConstructorArgs =
- {
- new CodeObjectCreateExpression("EGID", new CodeVariableReferenceExpression("id"),
- new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("CommonExclusiveGroups"),
- group))
- }
- });
- ns.Types.Add(cl);
- codeUnit.Namespaces.Add(ns);
-
- var provider = CodeDomProvider.CreateProvider("CSharp");
- using (var sw = new StreamWriter($"{name}.cs"))
- {
- provider.GenerateCodeFromCompileUnit(codeUnit, sw, new CodeGeneratorOptions {BracingStyle = "C"});
- }
- }
-
- private static string GetGroup(string name)
- {
- var ret = "";
- foreach (var ch in name)
- {
- if (char.IsUpper(ch) && ret.Length > 0)
- ret += "_" + ch;
- else
- ret += char.ToUpper(ch);
- }
-
- return ret;
- }
- }
- }
|