Skip to content

Commit

Permalink
Fix folder creation conditions + make libil2cpp provide methods by ad…
Browse files Browse the repository at this point in the history
…dress
  • Loading branch information
Sam committed Sep 10, 2020
1 parent f183286 commit 3558745
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
32 changes: 16 additions & 16 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ public static void Main(string[] args)
//Dump DLLs

#region Assembly Generation

LibCpp2IlMain.GetMethodDefinitionByGlobalAddress(0x180623548);

var resolver = new RegistryAssemblyResolver();
var moduleParams = new ModuleParameters
Expand All @@ -173,7 +171,7 @@ public static void Main(string[] args)


Console.WriteLine("Building assemblies...");
Console.WriteLine("\tPass 1: Creating types...");
Console.WriteLine("\tPass 1: Creating empty types...");

Assemblies = AssemblyBuilder.CreateAssemblies(LibCpp2IlMain.TheMetadata!, resolver, moduleParams);

Expand All @@ -182,20 +180,30 @@ public static void Main(string[] args)
//Stateful method, no return value
AssemblyBuilder.ConfigureHierarchy(LibCpp2IlMain.TheMetadata, LibCpp2IlMain.ThePe!);

Console.WriteLine("\tPass 3: Handling Fields, methods, and properties (THIS MAY TAKE A WHILE)...");
Console.WriteLine("\tPass 3: Populating types...");

var methods = new List<(TypeDefinition type, List<CppMethodData> methods)>();

//Create out dirs if needed
var outputPath = Path.GetFullPath("cpp2il_out");
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);

var methodOutputDir = Path.Combine(outputPath, "types");
if (!(CommandLineOptions.SkipAnalysis && CommandLineOptions.SkipMetadataTextFiles) && !Directory.Exists(methodOutputDir))
Directory.CreateDirectory(methodOutputDir);

for (var imageIndex = 0; imageIndex < LibCpp2IlMain.TheMetadata.assemblyDefinitions.Length; imageIndex++)
{
var imageDef = LibCpp2IlMain.TheMetadata.assemblyDefinitions[imageIndex];
var firstTypeDefinition = SharedState.TypeDefsByIndex[imageDef.firstTypeIndex];
var currentAssembly = firstTypeDefinition.Module.Assembly;

Console.WriteLine($"\t\tProcessing DLL {imageIndex + 1} of {LibCpp2IlMain.TheMetadata.assemblyDefinitions.Length}: {currentAssembly.Name}...");
Console.WriteLine($"\t\tPopulating {imageDef.typeCount} types in assembly {imageIndex + 1} of {LibCpp2IlMain.TheMetadata.assemblyDefinitions.Length}: {imageDef.Name}...");

var assemblySpecificPath = Path.Combine(methodOutputDir, imageDef.Name.Replace(".dll", ""));
if (!(CommandLineOptions.SkipMetadataTextFiles && CommandLineOptions.SkipAnalysis) && !Directory.Exists(assemblySpecificPath))
Directory.CreateDirectory(assemblySpecificPath);

methods.AddRange(AssemblyBuilder.ProcessAssemblyTypes(LibCpp2IlMain.TheMetadata, LibCpp2IlMain.ThePe, LibCpp2IlMain.TheMetadata.assemblyDefinitions[imageIndex]));
methods.AddRange(AssemblyBuilder.ProcessAssemblyTypes(LibCpp2IlMain.TheMetadata, LibCpp2IlMain.ThePe, imageDef));
}

//Invert dict for CppToMono
Expand Down Expand Up @@ -281,14 +289,6 @@ public static void Main(string[] args)

Utils.BuildPrimitiveMappings();

var outputPath = Path.GetFullPath("cpp2il_out");
if (!Directory.Exists(outputPath))
Directory.CreateDirectory(outputPath);

var methodOutputDir = Path.Combine(outputPath, "types");
if (!CommandLineOptions.SkipAnalysis && !Directory.Exists(methodOutputDir))
Directory.CreateDirectory(methodOutputDir);

Console.WriteLine("Saving Header DLLs to " + outputPath + "...");

GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
Expand Down
33 changes: 31 additions & 2 deletions LibCpp2IL/LibCpp2IlMain.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibCpp2IL.Metadata;
Expand All @@ -13,11 +15,20 @@ public class LibCpp2IlSettings
}

public static readonly LibCpp2IlSettings Settings = new LibCpp2IlSettings();

public static float MetadataVersion = 24f;
public static PE.PE? ThePe;
public static Il2CppMetadata? TheMetadata;

private static readonly Dictionary<ulong, List<Il2CppMethodDefinition>> MethodsByPtr = new Dictionary<ulong, List<Il2CppMethodDefinition>>();

public static List<Il2CppMethodDefinition>? GetListOfMethodImplementationsAtAddress(ulong addr)
{
MethodsByPtr.TryGetValue(addr, out var ret);

return ret;
}

public static string? GetLiteralByAddress(ulong address)
{
var literal = LibCpp2IlGlobalMapper.Literals.FirstOrDefault(lit => lit.Offset == address);
Expand Down Expand Up @@ -78,13 +89,31 @@ public static bool Initialize(byte[] peBytes, byte[] metadataBytes, int[] unityV

if (TheMetadata == null)
return false;

Console.WriteLine("Read Metadata ok.");

ThePe = new PE.PE(new MemoryStream(peBytes, 0, peBytes.Length, false, true), TheMetadata.maxMetadataUsages);
if (!ThePe.PlusSearch(TheMetadata.methodDefs.Count(x => x.methodIndex >= 0), TheMetadata.typeDefs.Length))
return false;

LibCpp2IlGlobalMapper.MapGlobalIdentifiers(TheMetadata, ThePe);
Console.WriteLine("Read PE Data ok.");

var start = DateTime.Now;
Console.Write("Mapping Globals...");
LibCpp2IlGlobalMapper.MapGlobalIdentifiers(TheMetadata, ThePe);
Console.WriteLine($"OK ({(DateTime.Now - start).TotalMilliseconds}ms)");

start = DateTime.Now;
Console.Write("Mapping pointers to Il2CppMethodDefinitions...");
foreach (var (method, ptr) in TheMetadata.methodDefs.AsParallel().Select(method => (method, ptr: method.MethodPointer)))
{
if(!MethodsByPtr.ContainsKey(ptr))
MethodsByPtr[ptr] = new List<Il2CppMethodDefinition>();

MethodsByPtr[ptr].Add(method);
}
Console.WriteLine($"OK ({(DateTime.Now - start).TotalMilliseconds}ms)");

return true;
}

Expand Down

0 comments on commit 3558745

Please sign in to comment.