diff --git a/CodeGenerator/App.config b/CodeGenerator/App.config new file mode 100644 index 0000000..1fe7c1f --- /dev/null +++ b/CodeGenerator/App.config @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/CodeGenerator/BlockClassGenerator.cs b/CodeGenerator/BlockClassGenerator.cs index c2307a0..a56b222 100644 --- a/CodeGenerator/BlockClassGenerator.cs +++ b/CodeGenerator/BlockClassGenerator.cs @@ -8,7 +8,6 @@ using System.Reflection; using Gamecraft.Tweaks; using RobocraftX.Common; using Svelto.ECS; -using Techblox.EngineBlock; namespace CodeGenerator { @@ -97,6 +96,8 @@ namespace CodeGenerator { if (!typeof(IEntityComponent).IsAssignableFrom(type)) throw new ArgumentException("Type must be an entity component"); + bool reflection = type.IsNotPublic; + var reflectedType = new CodeSnippetExpression($"HarmonyLib.AccessTools.TypeByName(\"{type.FullName}\")"); foreach (var field in type.GetFields()) { var attr = field.GetCustomAttribute(); @@ -108,10 +109,20 @@ namespace CodeGenerator } propName = char.ToUpper(propName[0]) + propName.Substring(1); - var structFieldReference = new CodeFieldReferenceExpression(new CodeMethodInvokeExpression( + var getStruct = new CodeMethodInvokeExpression( + new CodeMethodReferenceExpression(new CodeSnippetExpression("BlockEngine"), + "GetBlockInfo", new CodeTypeReference(type)), + new CodeThisReferenceExpression()); + CodeExpression structFieldReference = new CodeFieldReferenceExpression(getStruct, field.Name); + CodeExpression reflectedGet = new CodeCastExpression(field.FieldType, new CodeMethodInvokeExpression( new CodeMethodReferenceExpression(new CodeSnippetExpression("BlockEngine"), - "GetBlockInfo", new CodeTypeReference(type)), - new CodeThisReferenceExpression()), field.Name); + "GetBlockInfo"), + new CodeThisReferenceExpression(), reflectedType, new CodePrimitiveExpression(field.Name))); + CodeExpression reflectedSet = new CodeMethodInvokeExpression( + new CodeMethodReferenceExpression(new CodeSnippetExpression("BlockEngine"), + "SetBlockInfo"), + new CodeThisReferenceExpression(), reflectedType, new CodePrimitiveExpression(field.Name), + new CodePropertySetValueReferenceExpression()); cl.Members.Add(new CodeMemberProperty { Name = propName, @@ -119,18 +130,23 @@ namespace CodeGenerator HasSet = true, GetStatements = { - new CodeMethodReturnStatement(structFieldReference) + new CodeMethodReturnStatement(reflection ? reflectedGet : structFieldReference) }, SetStatements = { - new CodeAssignStatement(structFieldReference, new CodePropertySetValueReferenceExpression()) + reflection + ? (CodeStatement)new CodeExpressionStatement(reflectedSet) + : new CodeAssignStatement(structFieldReference, new CodePropertySetValueReferenceExpression()) }, Type = new CodeTypeReference(field.FieldType), Attributes = MemberAttributes.Public | MemberAttributes.Final, Comments = { - _start, new CodeCommentStatement($"Gets or sets the {baseClass}'s {propName} property." + - $" {(attr != null ? "Tweakable stat." : "May not be saved.")}", true), _end + _start, + new CodeCommentStatement($"Gets or sets the {baseClass}'s {propName} property." + + $" {(attr != null ? "Tweakable stat." : "May not be saved.")}", + true), + _end } }); } diff --git a/CodeGenerator/CodeGenerator.csproj b/CodeGenerator/CodeGenerator.csproj index f657a48..9de07d8 100644 --- a/CodeGenerator/CodeGenerator.csproj +++ b/CodeGenerator/CodeGenerator.csproj @@ -32,6 +32,10 @@ 4 + + ..\packages\Lib.Harmony.2.2.0\lib\net472\0Harmony.dll + True + @@ -1164,6 +1168,16 @@ ..\..\ref\Techblox_Data\Managed\VisualProfiler.dll + + + + + + + {7fd5a7d8-4f3e-426a-b07d-7dc70442a4df} + TechbloxModdingAPI + +