private void IndexMetadataUsage() { if (il2Cpp.Version <= 16 || il2Cpp.Version >= 27) { return; } foreach (var(metadataUsageIndex, typeIndex) in metadata.metadataUsageDic[1]) //kIl2CppMetadataUsageTypeInfo { var type = il2Cpp.types[typeIndex]; var typeDef = executor.GetTypeDefinitionFromIl2CppType(type); if (typeDef != null) { typeDefToAddress[typeDef] = il2Cpp.GetRVA(il2Cpp.metadataUsages[metadataUsageIndex]); } } foreach (var(metadataUsageIndex, methodDefIndex) in metadata.metadataUsageDic[3]) //kIl2CppMetadataUsageMethodDef { var methodDef = metadata.methodDefs[methodDefIndex]; var typeDef = metadata.typeDefs[methodDef.declaringType]; var staticMethod = new StructStaticMethodInfo(); var typeName = executor.GetTypeDefName(typeDef, true, true); staticMethod.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[metadataUsageIndex]); staticMethod.Name = metadata.GetStringFromIndex(methodDef.nameIndex); if (!TypeNameToStaticMethods.ContainsKey(typeName)) { TypeNameToStaticMethods.Add(typeName, new List <StructStaticMethodInfo>()); } TypeNameToStaticMethods[typeName].Add(staticMethod); } foreach (var(metadataUsageIndex, methodSpecIndex) in metadata.metadataUsageDic[6]) //kIl2CppMetadataUsageMethodRef { var methodSpec = il2Cpp.methodSpecs[methodSpecIndex]; var methodDef = metadata.methodDefs[methodSpec.methodDefinitionIndex]; var typeDef = metadata.typeDefs[methodDef.declaringType]; var staticMethod = new StructStaticMethodInfo(); staticMethod.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[metadataUsageIndex]); var(typeName, methodSpecMethodName, typeArgs) = executor.GetMethodSpecName(methodSpec, true); staticMethod.Name = methodSpecMethodName; staticMethod.TypeArgs = typeArgs; if (!TypeNameToStaticMethods.ContainsKey(typeName)) { TypeNameToStaticMethods.Add(typeName, new List <StructStaticMethodInfo>()); } TypeNameToStaticMethods[typeName].Add(staticMethod); } }
public void Decompile(Config config, string outputDir) { var writer = new StreamWriter(new FileStream(outputDir + "dump.cs", FileMode.Create), new UTF8Encoding(false)); //dump image for (var imageIndex = 0; imageIndex < metadata.imageDefs.Length; imageIndex++) { var imageDef = metadata.imageDefs[imageIndex]; writer.Write($"// Image {imageIndex}: {metadata.GetStringFromIndex(imageDef.nameIndex)} - {imageDef.typeStart}\n"); } //dump type for (var imageIndex = 0; imageIndex < metadata.imageDefs.Length; imageIndex++) { try { var imageDef = metadata.imageDefs[imageIndex]; var typeEnd = imageDef.typeStart + imageDef.typeCount; for (int typeDefIndex = imageDef.typeStart; typeDefIndex < typeEnd; typeDefIndex++) { var typeDef = metadata.typeDefs[typeDefIndex]; var extends = new List <string>(); if (typeDef.parentIndex >= 0) { var parent = il2Cpp.types[typeDef.parentIndex]; var parentName = executor.GetTypeName(parent, false, false); if (!typeDef.IsValueType && !typeDef.IsEnum && parentName != "object") { extends.Add(parentName); } } if (typeDef.interfaces_count > 0) { for (int i = 0; i < typeDef.interfaces_count; i++) { var @interface = il2Cpp.types[metadata.interfaceIndices[typeDef.interfacesStart + i]]; extends.Add(executor.GetTypeName(@interface, false, false)); } } writer.Write($"\n// Namespace: {metadata.GetStringFromIndex(typeDef.namespaceIndex)}\n"); if (config.DumpAttribute) { writer.Write(GetCustomAttribute(imageDef, typeDef.customAttributeIndex, typeDef.token)); } if (config.DumpAttribute && (typeDef.flags & TYPE_ATTRIBUTE_SERIALIZABLE) != 0) { writer.Write("[Serializable]\n"); } var visibility = typeDef.flags & TYPE_ATTRIBUTE_VISIBILITY_MASK; switch (visibility) { case TYPE_ATTRIBUTE_PUBLIC: case TYPE_ATTRIBUTE_NESTED_PUBLIC: writer.Write("public "); break; case TYPE_ATTRIBUTE_NOT_PUBLIC: case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM: case TYPE_ATTRIBUTE_NESTED_ASSEMBLY: writer.Write("internal "); break; case TYPE_ATTRIBUTE_NESTED_PRIVATE: writer.Write("private "); break; case TYPE_ATTRIBUTE_NESTED_FAMILY: writer.Write("protected "); break; case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM: writer.Write("protected internal "); break; } if ((typeDef.flags & TYPE_ATTRIBUTE_ABSTRACT) != 0 && (typeDef.flags & TYPE_ATTRIBUTE_SEALED) != 0) { writer.Write("static "); } else if ((typeDef.flags & TYPE_ATTRIBUTE_INTERFACE) == 0 && (typeDef.flags & TYPE_ATTRIBUTE_ABSTRACT) != 0) { writer.Write("abstract "); } else if (!typeDef.IsValueType && !typeDef.IsEnum && (typeDef.flags & TYPE_ATTRIBUTE_SEALED) != 0) { writer.Write("sealed "); } if ((typeDef.flags & TYPE_ATTRIBUTE_INTERFACE) != 0) { writer.Write("interface "); } else if (typeDef.IsEnum) { writer.Write("enum "); } else if (typeDef.IsValueType) { writer.Write("struct "); } else { writer.Write("class "); } var typeName = executor.GetTypeDefName(typeDef, false, true); writer.Write($"{typeName}"); if (extends.Count > 0) { writer.Write($" : {string.Join(", ", extends)}"); } if (config.DumpTypeDefIndex) { writer.Write($" // TypeDefIndex: {typeDefIndex}\n{{"); } else { writer.Write("\n{"); } //dump field if (config.DumpField && typeDef.field_count > 0) { writer.Write("\n\t// Fields\n"); var fieldEnd = typeDef.fieldStart + typeDef.field_count; for (var i = typeDef.fieldStart; i < fieldEnd; ++i) { var fieldDef = metadata.fieldDefs[i]; var fieldType = il2Cpp.types[fieldDef.typeIndex]; var isStatic = false; if (config.DumpAttribute) { writer.Write(GetCustomAttribute(imageDef, fieldDef.customAttributeIndex, fieldDef.token, "\t")); } writer.Write("\t"); var access = fieldType.attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK; switch (access) { case FIELD_ATTRIBUTE_PRIVATE: writer.Write("private "); break; case FIELD_ATTRIBUTE_PUBLIC: writer.Write("public "); break; case FIELD_ATTRIBUTE_FAMILY: writer.Write("protected "); break; case FIELD_ATTRIBUTE_ASSEMBLY: case FIELD_ATTRIBUTE_FAM_AND_ASSEM: writer.Write("internal "); break; case FIELD_ATTRIBUTE_FAM_OR_ASSEM: writer.Write("protected internal "); break; } if ((fieldType.attrs & FIELD_ATTRIBUTE_LITERAL) != 0) { writer.Write("const "); } else { if ((fieldType.attrs & FIELD_ATTRIBUTE_STATIC) != 0) { isStatic = true; writer.Write("static "); } if ((fieldType.attrs & FIELD_ATTRIBUTE_INIT_ONLY) != 0) { writer.Write("readonly "); } } writer.Write($"{executor.GetTypeName(fieldType, false, false)} {metadata.GetStringFromIndex(fieldDef.nameIndex)}"); if (metadata.GetFieldDefaultValueFromIndex(i, out var fieldDefaultValue) && fieldDefaultValue.dataIndex != -1) { if (TryGetDefaultValue(fieldDefaultValue.typeIndex, fieldDefaultValue.dataIndex, out var value)) { writer.Write($" = "); if (value is string str) { writer.Write($"\"{str.ToEscapedString()}\""); } else if (value is char c) { var v = (int)c; writer.Write($"'\\x{v:x}'"); } else if (value != null) { writer.Write($"{value}"); } } else { writer.Write($" /*Metadata offset 0x{value:X}*/"); } } if (config.DumpFieldOffset) { writer.Write("; // 0x{0:X}\n", il2Cpp.GetFieldOffsetFromIndex(typeDefIndex, i - typeDef.fieldStart, i, typeDef.IsValueType, isStatic)); } else { writer.Write(";\n"); } } } //dump property if (config.DumpProperty && typeDef.property_count > 0) { writer.Write("\n\t// Properties\n"); var propertyEnd = typeDef.propertyStart + typeDef.property_count; for (var i = typeDef.propertyStart; i < propertyEnd; ++i) { var propertyDef = metadata.propertyDefs[i]; if (config.DumpAttribute) { writer.Write(GetCustomAttribute(imageDef, propertyDef.customAttributeIndex, propertyDef.token, "\t")); } writer.Write("\t"); if (propertyDef.get >= 0) { var methodDef = metadata.methodDefs[typeDef.methodStart + propertyDef.get]; writer.Write(GetModifiers(methodDef)); var propertyType = il2Cpp.types[methodDef.returnType]; writer.Write($"{executor.GetTypeName(propertyType, false, false)} {metadata.GetStringFromIndex(propertyDef.nameIndex)} {{ "); } else if (propertyDef.set >= 0) { var methodDef = metadata.methodDefs[typeDef.methodStart + propertyDef.set]; writer.Write(GetModifiers(methodDef)); var parameterDef = metadata.parameterDefs[methodDef.parameterStart]; var propertyType = il2Cpp.types[parameterDef.typeIndex]; writer.Write($"{executor.GetTypeName(propertyType, false, false)} {metadata.GetStringFromIndex(propertyDef.nameIndex)} {{ "); } if (propertyDef.get >= 0) { writer.Write("get; "); } if (propertyDef.set >= 0) { writer.Write("set; "); } writer.Write("}"); writer.Write("\n"); } } //dump method if (config.DumpMethod && typeDef.method_count > 0) { writer.Write("\n\t// Methods\n"); var methodEnd = typeDef.methodStart + typeDef.method_count; for (var i = typeDef.methodStart; i < methodEnd; ++i) { writer.Write("\n"); var methodDef = metadata.methodDefs[i]; if (config.DumpAttribute) { writer.Write(GetCustomAttribute(imageDef, methodDef.customAttributeIndex, methodDef.token, "\t")); } if (config.DumpMethodOffset) { var methodPointer = il2Cpp.GetMethodPointer(methodDef, imageIndex); if (methodPointer > 0) { var fixedMethodPointer = il2Cpp.GetRVA(methodPointer); writer.Write("\t// RVA: 0x{0:X} Offset: 0x{1:X} VA: 0x{2:X}", fixedMethodPointer, il2Cpp.MapVATR(methodPointer), methodPointer); } else { writer.Write("\t// RVA: -1 Offset: -1"); } if (methodDef.slot != ushort.MaxValue) { writer.Write(" Slot: {0}", methodDef.slot); } writer.Write("\n"); } writer.Write("\t"); writer.Write(GetModifiers(methodDef)); var methodReturnType = il2Cpp.types[methodDef.returnType]; var methodName = metadata.GetStringFromIndex(methodDef.nameIndex); if (methodDef.genericContainerIndex >= 0) { var genericContainer = metadata.genericContainers[methodDef.genericContainerIndex]; methodName += executor.GetGenericContainerParams(genericContainer); } if (methodReturnType.byref == 1) { writer.Write("ref "); } writer.Write($"{executor.GetTypeName(methodReturnType, false, false)} {methodName}("); var parameterStrs = new List <string>(); for (var j = 0; j < methodDef.parameterCount; ++j) { var parameterStr = ""; var parameterDef = metadata.parameterDefs[methodDef.parameterStart + j]; var parameterName = metadata.GetStringFromIndex(parameterDef.nameIndex); var parameterType = il2Cpp.types[parameterDef.typeIndex]; var parameterTypeName = executor.GetTypeName(parameterType, false, false); if (parameterType.byref == 1) { if ((parameterType.attrs & PARAM_ATTRIBUTE_OUT) != 0 && (parameterType.attrs & PARAM_ATTRIBUTE_IN) == 0) { parameterStr += "out "; } else if ((parameterType.attrs & PARAM_ATTRIBUTE_OUT) == 0 && (parameterType.attrs & PARAM_ATTRIBUTE_IN) != 0) { parameterStr += "in "; } else { parameterStr += "ref "; } } else { if ((parameterType.attrs & PARAM_ATTRIBUTE_IN) != 0) { parameterStr += "[In] "; } if ((parameterType.attrs & PARAM_ATTRIBUTE_OUT) != 0) { parameterStr += "[Out] "; } } parameterStr += $"{parameterTypeName} {parameterName}"; if (metadata.GetParameterDefaultValueFromIndex(methodDef.parameterStart + j, out var parameterDefault) && parameterDefault.dataIndex != -1) { if (TryGetDefaultValue(parameterDefault.typeIndex, parameterDefault.dataIndex, out var value)) { parameterStr += " = "; if (value is string str) { parameterStr += $"\"{str.ToEscapedString()}\""; } else if (value is char c) { var v = (int)c; parameterStr += $"'\\x{v:x}'"; } else if (value != null) { parameterStr += $"{value}"; } } else { parameterStr += $" /*Metadata offset 0x{value:X}*/"; } } parameterStrs.Add(parameterStr); } writer.Write(string.Join(", ", parameterStrs)); writer.Write(") { }\n"); if (il2Cpp.methodDefinitionMethodSpecs.TryGetValue(i, out var methodSpecs)) { writer.Write("\t/* GenericInstMethod :\n"); var groups = methodSpecs.GroupBy(x => il2Cpp.methodSpecGenericMethodPointers[x]); foreach (var group in groups) { writer.Write("\t|\n"); var genericMethodPointer = group.Key; if (genericMethodPointer > 0) { var fixedPointer = il2Cpp.GetRVA(genericMethodPointer); writer.Write($"\t|-RVA: 0x{fixedPointer:X} Offset: 0x{il2Cpp.MapVATR(genericMethodPointer):X} VA: 0x{genericMethodPointer:X}\n"); } else { writer.Write("\t|-RVA: -1 Offset: -1\n"); } foreach (var methodSpec in group) { (var methodSpecTypeName, var methodSpecMethodName) = executor.GetMethodSpecName(methodSpec); writer.Write($"\t|-{methodSpecTypeName}.{methodSpecMethodName}\n"); } } writer.Write("\t*/\n"); } } } writer.Write("}\n"); } } catch (Exception e) { Console.WriteLine("ERROR: Some errors in dumping"); writer.Write("/*"); writer.Write(e); writer.Write("*/\n}\n"); } } writer.Close(); }
public void WriteScript(Config config) { var json = new ScriptJson(); // 生成唯一名称 for (var imageIndex = 0; imageIndex < metadata.imageDefs.Length; imageIndex++) { var imageDef = metadata.imageDefs[imageIndex]; var typeEnd = imageDef.typeStart + imageDef.typeCount; for (int typeIndex = imageDef.typeStart; typeIndex < typeEnd; typeIndex++) { var typeDef = metadata.typeDefs[typeIndex]; typeDefImageIndices.Add(typeDef, imageIndex); CreateStructNameDic(typeDef); } } // 生成后面处理泛型实例要用到的字典 foreach (var il2CppType in il2Cpp.types.Where(x => x.type == Il2CppTypeEnum.IL2CPP_TYPE_GENERICINST)) { var genericClass = il2Cpp.MapVATR <Il2CppGenericClass>(il2CppType.data.generic_class); var typeDef = metadata.typeDefs[genericClass.typeDefinitionIndex]; var typeBaseName = structNameDic[typeDef]; var typeToReplaceName = FixName(executor.GetTypeDefName(typeDef, true, true)); var typeReplaceName = FixName(executor.GetTypeName(il2CppType, true, false)); var typeStructName = typeBaseName.Replace(typeToReplaceName, typeReplaceName); nameGenericClassDic[typeStructName] = il2CppType; genericClassStructNameDic[il2CppType.data.generic_class] = typeStructName; } // 处理函数 for (var imageIndex = 0; imageIndex < metadata.imageDefs.Length; imageIndex++) { var imageDef = metadata.imageDefs[imageIndex]; var typeEnd = imageDef.typeStart + imageDef.typeCount; for (int typeIndex = imageDef.typeStart; typeIndex < typeEnd; typeIndex++) { var typeDef = metadata.typeDefs[typeIndex]; AddStruct(typeDef); var typeName = executor.GetTypeDefName(typeDef, true, true); var methodEnd = typeDef.methodStart + typeDef.method_count; for (var i = typeDef.methodStart; i < methodEnd; ++i) { var methodDef = metadata.methodDefs[i]; var methodName = metadata.GetStringFromIndex(methodDef.nameIndex); var methodPointer = il2Cpp.GetMethodPointer(methodDef, imageIndex); if (methodPointer > 0) { var scriptMethod = new ScriptMethod(); json.ScriptMethod.Add(scriptMethod); scriptMethod.Address = il2Cpp.GetRVA(methodPointer); var methodFullName = typeName + "$$" + methodName; scriptMethod.Name = methodFullName; var methodReturnType = il2Cpp.types[methodDef.returnType]; var returnType = ParseType(methodReturnType); if (methodReturnType.byref == 1) { returnType += "*"; } var signature = $"{returnType} {FixName(methodFullName)} ("; var parameterStrs = new List <string>(); if (il2Cpp.Version <= 22f || (methodDef.flags & METHOD_ATTRIBUTE_STATIC) == 0) { var thisType = ParseType(il2Cpp.types[typeDef.byrefTypeIndex]); parameterStrs.Add($"{thisType} __this"); } for (var j = 0; j < methodDef.parameterCount; j++) { var parameterDef = metadata.parameterDefs[methodDef.parameterStart + j]; var parameterName = metadata.GetStringFromIndex(parameterDef.nameIndex); var parameterType = il2Cpp.types[parameterDef.typeIndex]; var parameterCType = ParseType(parameterType); if (parameterType.byref == 1) { parameterCType += "*"; } parameterStrs.Add($"{parameterCType} {FixName(parameterName)}"); } signature += string.Join(", ", parameterStrs); signature += ");"; scriptMethod.Signature = signature; } //泛型实例函数 if (il2Cpp.methodDefinitionMethodSpecs.TryGetValue(i, out var methodSpecs)) { foreach (var methodSpec in methodSpecs) { var genericMethodPointer = il2Cpp.methodSpecGenericMethodPointers[methodSpec]; if (genericMethodPointer > 0) { var scriptMethod = new ScriptMethod(); json.ScriptMethod.Add(scriptMethod); scriptMethod.Address = il2Cpp.GetRVA(genericMethodPointer); (var methodSpecTypeName, var methodSpecMethodName) = executor.GetMethodSpecName(methodSpec, true); var methodFullName = methodSpecTypeName + "$$" + methodSpecMethodName; scriptMethod.Name = methodFullName; var genericContext = executor.GetMethodSpecGenericContext(methodSpec); var methodReturnType = il2Cpp.types[methodDef.returnType]; var returnType = ParseType(methodReturnType, genericContext); if (methodReturnType.byref == 1) { returnType += "*"; } var signature = $"{returnType} {FixName(methodFullName)} ("; var parameterStrs = new List <string>(); if (il2Cpp.Version <= 22f || (methodDef.flags & METHOD_ATTRIBUTE_STATIC) == 0) { string thisType; if (methodSpec.classIndexIndex != -1) { var typeBaseName = structNameDic[typeDef]; var typeToReplaceName = FixName(typeName); var typeReplaceName = FixName(methodSpecTypeName); var typeStructName = typeBaseName.Replace(typeToReplaceName, typeReplaceName); var il2CppType = nameGenericClassDic[typeStructName]; thisType = ParseType(il2CppType); } else { thisType = ParseType(il2Cpp.types[typeDef.byrefTypeIndex]); } parameterStrs.Add($"{thisType} __this"); } for (var j = 0; j < methodDef.parameterCount; j++) { var parameterDef = metadata.parameterDefs[methodDef.parameterStart + j]; var parameterName = metadata.GetStringFromIndex(parameterDef.nameIndex); var parameterType = il2Cpp.types[parameterDef.typeIndex]; var parameterCType = ParseType(parameterType, genericContext); if (parameterType.byref == 1) { parameterCType += "*"; } parameterStrs.Add($"{parameterCType} {FixName(parameterName)}"); } signature += string.Join(", ", parameterStrs); signature += ");"; scriptMethod.Signature = signature; } } } } } } // 处理MetadataUsage if (il2Cpp.Version > 16) { foreach (var i in metadata.metadataUsageDic[1]) //kIl2CppMetadataUsageTypeInfo { var type = il2Cpp.types[i.Value]; var typeName = executor.GetTypeName(type, true, false); var scriptMetadata = new ScriptMetadata(); json.ScriptMetadata.Add(scriptMetadata); scriptMetadata.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[i.Key]); scriptMetadata.Name = "Class$" + typeName; var signature = GetIl2CppStructName(type); if (signature.EndsWith("_array")) { scriptMetadata.Signature = signature + "*"; } else { scriptMetadata.Signature = FixName(signature) + "_c*"; } } foreach (var i in metadata.metadataUsageDic[2]) //kIl2CppMetadataUsageIl2CppType { var type = il2Cpp.types[i.Value]; var typeName = executor.GetTypeName(type, true, false); var scriptMetadata = new ScriptMetadata(); json.ScriptMetadata.Add(scriptMetadata); scriptMetadata.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[i.Key]); scriptMetadata.Name = "Object$" + typeName; var signature = GetIl2CppStructName(type); if (signature.EndsWith("_array")) { scriptMetadata.Signature = signature + "*"; } else { scriptMetadata.Signature = FixName(signature) + "_o*"; } } foreach (var i in metadata.metadataUsageDic[3]) //kIl2CppMetadataUsageMethodDef { var methodDef = metadata.methodDefs[i.Value]; var typeDef = metadata.typeDefs[methodDef.declaringType]; var typeName = executor.GetTypeDefName(typeDef, true, true); var methodName = typeName + "." + metadata.GetStringFromIndex(methodDef.nameIndex) + "()"; var scriptMetadataMethod = new ScriptMetadataMethod(); json.ScriptMetadataMethod.Add(scriptMetadataMethod); scriptMetadataMethod.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[i.Key]); scriptMetadataMethod.Name = "Method$" + methodName; var imageIndex = typeDefImageIndices[typeDef]; var methodPointer = il2Cpp.GetMethodPointer(methodDef, imageIndex); if (methodPointer > 0) { scriptMetadataMethod.MethodAddress = il2Cpp.GetRVA(methodPointer); } } foreach (var i in metadata.metadataUsageDic[4]) //kIl2CppMetadataUsageFieldInfo { var fieldRef = metadata.fieldRefs[i.Value]; var type = il2Cpp.types[fieldRef.typeIndex]; var typeDef = metadata.typeDefs[type.data.klassIndex]; var fieldDef = metadata.fieldDefs[typeDef.fieldStart + fieldRef.fieldIndex]; var fieldName = executor.GetTypeName(type, true, false) + "." + metadata.GetStringFromIndex(fieldDef.nameIndex); var scriptMetadata = new ScriptMetadata(); json.ScriptMetadata.Add(scriptMetadata); scriptMetadata.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[i.Key]); scriptMetadata.Name = "Field$" + fieldName; } foreach (var i in metadata.metadataUsageDic[5]) //kIl2CppMetadataUsageStringLiteral { var scriptString = new ScriptString(); json.ScriptString.Add(scriptString); scriptString.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[i.Key]); scriptString.Value = metadata.GetStringLiteralFromIndex(i.Value); } var stringLiterals = json.ScriptString.Select(x => new { value = x.Value, address = $"0x{x.Address:X}" }).ToArray(); File.WriteAllText("stringliteral.json", JsonConvert.SerializeObject(stringLiterals, Formatting.Indented), new UTF8Encoding(false)); foreach (var i in metadata.metadataUsageDic[6]) //kIl2CppMetadataUsageMethodRef { var methodSpec = il2Cpp.methodSpecs[i.Value]; var scriptMetadataMethod = new ScriptMetadataMethod(); json.ScriptMetadataMethod.Add(scriptMetadataMethod); scriptMetadataMethod.Address = il2Cpp.GetRVA(il2Cpp.metadataUsages[i.Key]); (var methodSpecTypeName, var methodSpecMethodName) = executor.GetMethodSpecName(methodSpec, true); scriptMetadataMethod.Name = "Method$" + methodSpecTypeName + "." + methodSpecMethodName + "()"; var genericMethodPointer = il2Cpp.methodSpecGenericMethodPointers[methodSpec]; if (genericMethodPointer > 0) { scriptMetadataMethod.MethodAddress = il2Cpp.GetRVA(genericMethodPointer); } } } if (config.MakeFunction) { List <ulong> orderedPointers; if (il2Cpp.Version >= 24.2f) { orderedPointers = new List <ulong>(); foreach (var methodPointers in il2Cpp.codeGenModuleMethodPointers) { orderedPointers.AddRange(methodPointers); } } else { orderedPointers = il2Cpp.methodPointers.ToList(); } orderedPointers.AddRange(il2Cpp.genericMethodPointers); orderedPointers.AddRange(il2Cpp.invokerPointers); orderedPointers.AddRange(il2Cpp.customAttributeGenerators); if (il2Cpp.Version >= 22) { if (il2Cpp.reversePInvokeWrappers != null) { orderedPointers.AddRange(il2Cpp.reversePInvokeWrappers); } if (il2Cpp.unresolvedVirtualCallPointers != null) { orderedPointers.AddRange(il2Cpp.unresolvedVirtualCallPointers); } } //TODO interopData内也包含函数 orderedPointers = orderedPointers.Distinct().OrderBy(x => x).ToList(); orderedPointers.Remove(0); for (int i = 0; i < orderedPointers.Count; i++) { orderedPointers[i] = il2Cpp.GetRVA(orderedPointers[i]); } json.Addresses = orderedPointers; } File.WriteAllText("script.json", JsonConvert.SerializeObject(json, Formatting.Indented)); //il2cpp.h for (int i = 0; i < genericClassList.Count; i++) { var pointer = genericClassList[i]; AddGenericClassStruct(pointer); } var preHeader = new StringBuilder(); var headerStruct = new StringBuilder(); var headerClass = new StringBuilder(); foreach (var info in structInfoList) { structInfoWithStructName.Add(info.TypeName + "_o", info); } foreach (var info in structInfoList) { preHeader.Append($"struct {info.TypeName}_o;\n"); if (info.IsValueType) { headerStruct.Append(RecursionStructInfo(info)); } else { headerClass.Append($"struct {info.TypeName}_StaticFields {{\n"); foreach (var field in info.StaticFields) { headerClass.Append($"\t{field.FieldTypeName} {field.FieldName};\n"); } headerClass.Append("};\n"); headerClass.Append($"struct {info.TypeName}_VTable {{\n"); foreach (var method in info.VTableMethod) { headerClass.Append($"\tVirtualInvokeData {method.MethodName};\n"); } headerClass.Append("};\n"); headerClass.Append($"struct {info.TypeName}_c {{\n" + $"\tIl2CppClass_1 _1;\n" + $"\t{info.TypeName}_StaticFields* static_fields;\n" + $"\tIl2CppClass_2 _2;\n" + $"\t{info.TypeName}_VTable vtable;\n" + $"}};\n"); headerClass.Append($"struct {info.TypeName}_o {{\n" + $"\t{info.TypeName}_c *klass;\n" + $"\tvoid *monitor;\n"); foreach (var field in info.Fields) { headerClass.Append($"\t{field.FieldTypeName} {field.FieldName};\n"); } headerClass.Append("};\n"); } } var sb = new StringBuilder(); if (il2Cpp is PE) { sb.Append(HeaderConstants.TypedefHeader); } sb.Append(HeaderConstants.GenericHeader); switch (il2Cpp.Version) { case 22f: sb.Append(HeaderConstants.HeaderV22); break; case 23f: case 24f: sb.Append(HeaderConstants.HeaderV240); break; case 24.1f: sb.Append(HeaderConstants.HeaderV241); break; case 24.2f: case 24.3f: sb.Append(HeaderConstants.HeaderV242); break; //TODO default: Console.WriteLine($"WARNING: This il2cpp version [{il2Cpp.Version}] does not support generating .h files"); return; } sb.Append(preHeader); sb.Append(arrayClassPreHeader); sb.Append(headerStruct); sb.Append(headerClass); sb.Append(arrayClassHeader); File.WriteAllText("il2cpp.h", sb.ToString()); }