From 9e455fdabea0da1ba8617c02ab64e6e7f47d4863 Mon Sep 17 00:00:00 2001 From: Robert Xiao Date: Mon, 29 Jun 2020 12:35:26 -0600 Subject: [PATCH] Renaming for clarity. Renamed the class CppDeclarations => CppDeclarationGenerator to better reflect its function and emphasize its statefulness. Renamed Visit{Type,Method} => Include{Type,Method} to clarify that these methods include the type or method into the generator state. Renamed GenerateVisitedTypes => GenerateRemainingTypeDeclarations to clarify that it outputs *remaining* declarations, i.e. declarations that haven't been generated yet. --- ...arations.cs => CppDeclarationGenerator.cs} | 40 ++++++++++--------- .../Outputs/IDAPythonScript.cs | 30 +++++++------- 2 files changed, 36 insertions(+), 34 deletions(-) rename Il2CppInspector.Common/CppUtils/{CppDeclarations.cs => CppDeclarationGenerator.cs} (94%) mode change 100644 => 100755 mode change 100644 => 100755 Il2CppInspector.Common/Outputs/IDAPythonScript.cs diff --git a/Il2CppInspector.Common/CppUtils/CppDeclarations.cs b/Il2CppInspector.Common/CppUtils/CppDeclarationGenerator.cs old mode 100644 new mode 100755 similarity index 94% rename from Il2CppInspector.Common/CppUtils/CppDeclarations.cs rename to Il2CppInspector.Common/CppUtils/CppDeclarationGenerator.cs index f88c44e..98a5d24 --- a/Il2CppInspector.Common/CppUtils/CppDeclarations.cs +++ b/Il2CppInspector.Common/CppUtils/CppDeclarationGenerator.cs @@ -16,7 +16,7 @@ using System.Text.RegularExpressions; namespace Il2CppInspector.CppUtils { // Class for generating C header declarations from Reflection objects (TypeInfo, etc.) - public class CppDeclarations + public class CppDeclarationGenerator { private readonly Il2CppModel model; @@ -36,7 +36,7 @@ namespace Il2CppInspector.CppUtils } public InheritanceStyleEnum InheritanceStyle; - public CppDeclarations(Il2CppModel model, UnityVersion version) { + public CppDeclarationGenerator(Il2CppModel model, UnityVersion version) { this.model = model; if (version == null) { UnityHeader = UnityHeader.GuessHeadersForModel(model)[0]; @@ -328,10 +328,11 @@ namespace Il2CppInspector.CppUtils private readonly List TodoTypeStructs = new List(); /// - /// Visit a type and all types it depends on. Must call this before generating type structs. + /// Include the given type into this generator. This will add the given type and all types it depends on. + /// Call GenerateRemainingTypeDeclarations to produce the actual type declarations afterwards. /// /// - public void VisitType(TypeInfo ti) { + public void IncludeType(TypeInfo ti) { if (VisitedTypes.Contains(ti)) return; if (ti.ContainsGenericParameters) @@ -340,15 +341,15 @@ namespace Il2CppInspector.CppUtils if (ti.IsArray) { VisitFieldStructs(ti); - VisitType(ti.ElementType); - VisitType(ti.BaseType); + IncludeType(ti.ElementType); + IncludeType(ti.BaseType); return; } else if (ti.HasElementType) { - VisitType(ti.ElementType); + IncludeType(ti.ElementType); return; } else if (ti.IsEnum) { VisitFieldStructs(ti); - VisitType(ti.GetEnumUnderlyingType()); + IncludeType(ti.GetEnumUnderlyingType()); return; } @@ -357,16 +358,16 @@ namespace Il2CppInspector.CppUtils VisitFieldStructs(ti); if (ti.BaseType != null) - VisitType(ti.BaseType); + IncludeType(ti.BaseType); TypeNamer.GetName(ti); foreach (var fi in ti.DeclaredFields) - VisitType(fi.FieldType); + IncludeType(fi.FieldType); foreach (var mi in GetFilledVTable(ti)) if (mi != null && !mi.ContainsGenericParameters) - VisitMethod(mi); + IncludeMethod(mi); TodoTypeStructs.Add(ti); } @@ -444,11 +445,11 @@ namespace Il2CppInspector.CppUtils } /// - /// Generate every type that has been visited so far. Types that have previously been generated - /// by this instance will not be generated again. + /// Output type declarations for every type that was included since the last call to GenerateRemainingTypeDeclarations + /// Type declarations that have previously been generated by this instance of CppDeclarationGenerator will not be generated again. /// /// A string containing C type declarations - public string GenerateVisitedTypes() { + public string GenerateRemainingTypeDeclarations() { var csrc = new StringBuilder(); GenerateVisitedFieldStructs(csrc); @@ -463,18 +464,19 @@ namespace Il2CppInspector.CppUtils #region Method Generation /// - /// Visit a method and all types it takes/returns. Must call this before generating method declarations. + /// Analyze a method and include all types that it takes and returns. + /// Must call this before generating the method's declaration with GenerateMethodDeclaration or GenerateFunctionPointer. /// /// - public void VisitMethod(MethodBase method, TypeInfo declaringType = null) { + public void IncludeMethod(MethodBase method, TypeInfo declaringType = null) { if (!method.IsStatic) - VisitType(declaringType ?? method.DeclaringType); + IncludeType(declaringType ?? method.DeclaringType); if (method is MethodInfo mi) - VisitType(mi.ReturnType); + IncludeType(mi.ReturnType); foreach (var pi in method.DeclaredParameters) { - VisitType(pi.ParameterType); + IncludeType(pi.ParameterType); } } diff --git a/Il2CppInspector.Common/Outputs/IDAPythonScript.cs b/Il2CppInspector.Common/Outputs/IDAPythonScript.cs old mode 100644 new mode 100755 index e6d3024..074f0a9 --- a/Il2CppInspector.Common/Outputs/IDAPythonScript.cs +++ b/Il2CppInspector.Common/Outputs/IDAPythonScript.cs @@ -19,19 +19,19 @@ namespace Il2CppInspector.Outputs private readonly Il2CppModel model; private StreamWriter writer; public UnityVersion UnityVersion; - private CppDeclarations typeGenerator; + private CppDeclarationGenerator declGenerator; public IDAPythonScript(Il2CppModel model) => this.model = model; public void WriteScriptToFile(string outputFile) { - typeGenerator = new CppDeclarations(model, UnityVersion); - UnityVersion = typeGenerator.UnityVersion; + declGenerator = new CppDeclarationGenerator(model, UnityVersion); + UnityVersion = declGenerator.UnityVersion; using var fs = new FileStream(outputFile, FileMode.Create); writer = new StreamWriter(fs, Encoding.UTF8); writeLine("# Generated script file by Il2CppInspector - http://www.djkaty.com - https://github.com/djkaty"); - writeLine("# Target Unity version: " + typeGenerator.UnityHeader.ToString()); + writeLine("# Target Unity version: " + declGenerator.UnityHeader.ToString()); writeLine("print('Generated script file by Il2CppInspector - http://www.djkaty.com - https://github.com/djkaty')"); writeSectionHeader("Preamble"); writePreamble(); @@ -83,7 +83,7 @@ typedef __int64 int64_t; "); var prefix = (model.Package.BinaryImage.Bits == 32) ? "#define IS_32BIT\n" : ""; - writeDecls(prefix + typeGenerator.UnityHeader.GetHeaderText()); + writeDecls(prefix + declGenerator.UnityHeader.GetHeaderText()); } private void writeMethods() { @@ -110,10 +110,10 @@ typedef __int64 int64_t; private void writeMethods(IEnumerable methods) { foreach (var method in methods.Where(m => m.VirtualAddress.HasValue)) { - typeGenerator.VisitMethod(method); - writeDecls(typeGenerator.GenerateVisitedTypes()); + declGenerator.IncludeMethod(method); + writeDecls(declGenerator.GenerateRemainingTypeDeclarations()); var address = method.VirtualAddress.Value.Start; - writeTypedName(address, typeGenerator.GenerateMethodDeclaration(method), typeGenerator.MethodNamer.GetName(method)); + writeTypedName(address, declGenerator.GenerateMethodDeclaration(method), declGenerator.MethodNamer.GetName(method)); writeComment(address, method); } } @@ -141,7 +141,7 @@ typedef __int64 int64_t; return; } - var stringType = typeGenerator.AsCType(model.TypesByFullName["System.String"]); + var stringType = declGenerator.AsCType(model.TypesByFullName["System.String"]); foreach (var usage in model.Package.MetadataUsages) { var address = usage.VirtualAddress; string name; @@ -155,10 +155,10 @@ typedef __int64 int64_t; case MetadataUsageType.Type: case MetadataUsageType.TypeInfo: var type = model.GetMetadataUsageType(usage); - typeGenerator.VisitType(type); - writeDecls(typeGenerator.GenerateVisitedTypes()); + declGenerator.IncludeType(type); + writeDecls(declGenerator.GenerateRemainingTypeDeclarations()); - name = typeGenerator.TypeNamer.GetName(type); + name = declGenerator.TypeNamer.GetName(type); if (usage.Type == MetadataUsageType.TypeInfo) writeTypedName(address, $"struct {name}__Class *", $"{name}__TypeInfo"); else @@ -168,10 +168,10 @@ typedef __int64 int64_t; case MetadataUsageType.MethodDef: case MetadataUsageType.MethodRef: var method = model.GetMetadataUsageMethod(usage); - typeGenerator.VisitMethod(method); - writeDecls(typeGenerator.GenerateVisitedTypes()); + declGenerator.IncludeMethod(method); + writeDecls(declGenerator.GenerateRemainingTypeDeclarations()); - name = typeGenerator.MethodNamer.GetName(method); + name = declGenerator.MethodNamer.GetName(method); writeTypedName(address, "struct MethodInfo *", $"{name}__MethodInfo"); writeComment(address, method); break;