Skip to content

Commit

Permalink
Fix dependency on dotnet core dlls
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Sep 6, 2020
1 parent 0857c37 commit 4b39634
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
20 changes: 12 additions & 8 deletions Cpp2IL/AssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ internal static List<AssemblyDefinition> CreateAssemblies(Il2CppMetadata metadat
{
//This is a new type so ensure it's registered
definition = new TypeDefinition(ns, name, (TypeAttributes) type.flags);
if (ns == "System" && name == "String")
{
typeof(TypeReference).GetField("etype", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(definition, (byte) 0x0e); //mark as string
}
mainModule.Types.Add(definition);
SharedState.AllTypeDefinitions.Add(definition);
SharedState.TypeDefsByIndex.Add(defNumber, definition);
}

Expand Down Expand Up @@ -130,12 +135,12 @@ private static void CreateDefaultConstructor(TypeDefinition typeDefinition)
var defaultConstructor = new MethodDefinition(
".ctor",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
module.ImportReference(typeof(void))
module.ImportReference(Utils.TryLookupTypeDefByName("System.Void").Item1)
);

var processor = defaultConstructor.Body.GetILProcessor();
processor.Emit(OpCodes.Ldarg_0);
processor.Emit(OpCodes.Call, module.ImportReference(typeof(Attribute).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]));
// processor.Emit(OpCodes.Ldarg_0);
// processor.Emit(OpCodes.Call, module.ImportReference(Utils.TryLookupTypeDefByName("System.Attribute").Item1.GetConstructors().First()));
processor.Emit(OpCodes.Ret);

typeDefinition.Methods.Add(defaultConstructor);
Expand All @@ -146,8 +151,8 @@ private static void InjectCustomAttributes(AssemblyDefinition imageDef)
//From il2cppdumper. Credit perfare
var namespaceName = "Cpp2IlInjected";

var stringTypeReference = imageDef.MainModule.ImportReference(typeof(string));
var attributeTypeReference = imageDef.MainModule.ImportReference(typeof(Attribute));
var stringTypeReference = imageDef.MainModule.ImportReference(Utils.TryLookupTypeDefByName("System.String").Item1);
var attributeTypeReference = imageDef.MainModule.ImportReference(Utils.TryLookupTypeDefByName("System.Attribute").Item1);

var addressAttribute = new TypeDefinition(namespaceName, "AddressAttribute", (TypeAttributes) 0x100001, attributeTypeReference);
addressAttribute.Fields.Add(new FieldDefinition("RVA", FieldAttributes.Public, stringTypeReference));
Expand Down Expand Up @@ -197,7 +202,6 @@ private static void InjectCustomAttributes(AssemblyDefinition imageDef)
{
var typeDef = metadata.typeDefs[index];
var typeDefinition = SharedState.TypeDefsByIndex[index];
SharedState.AllTypeDefinitions.Add(typeDefinition);
SharedState.MonoToCppTypeDefs[typeDefinition] = typeDef;

methods.Add((type: typeDefinition, methods: ProcessTypeContents(metadata, theDll, typeDef, typeDefinition, imageDef)));
Expand All @@ -224,7 +228,7 @@ private static List<CppMethodData> ProcessTypeContents(Il2CppMetadata metadata,
var attributeAttribute = ilTypeDefinition.Module.Types.First(x => x.Name == "AttributeAttribute").Methods[0];
var metadataOffsetAttribute = ilTypeDefinition.Module.Types.First(x => x.Name == "MetadataOffsetAttribute").Methods[0];
var tokenAttribute = ilTypeDefinition.Module.Types.First(x => x.Name == "TokenAttribute").Methods[0];
var stringType = ilTypeDefinition.Module.ImportReference(typeof(string));
var stringType = ilTypeDefinition.Module.ImportReference(Utils.TryLookupTypeDefByName("System.String").Item1);

//Token attribute
var customTokenAttribute = new CustomAttribute(ilTypeDefinition.Module.ImportReference(tokenAttribute));
Expand Down Expand Up @@ -305,7 +309,7 @@ private static List<CppMethodData> ProcessTypeContents(Il2CppMetadata metadata,
var methodReturnType = cppAssembly.types[methodDef.returnType];
var methodName = metadata.GetStringFromIndex(methodDef.nameIndex);
var methodDefinition = new MethodDefinition(methodName, (MethodAttributes) methodDef.flags,
ilTypeDefinition.Module.ImportReference(typeof(void)));
ilTypeDefinition.Module.ImportReference(Utils.TryLookupTypeDefByName("System.Void").Item1));

var offsetInRam = cppAssembly.GetMethodPointer(methodDef.methodIndex, methodId, imageDef.assemblyIndex, methodDef.token);

Expand Down
37 changes: 21 additions & 16 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ internal class Options
{
[Option("game-path", Required = true, HelpText = "Specify path to the game folder (containing the exe)")]
public string GamePath { get; set; }

[Option("exe-name", Required = false, HelpText = "Specify an override for the unity executable name in case the auto-detection doesn't work.")]
public string ExeName { get; set; }

[Option("skip-analysis", Required = false, HelpText = "Skip the analysis section and stop once DummyDLLs have been generated.")]
public bool SkipAnalysis { get; set; }

[Option("skip-metadata-txts", Required = false, HelpText = "Skip the generation of [classname]_metadata.txt files.")]
public bool SkipMetadataTextFiles { get; set; }
}

public static float MetadataVersion = 24f;

private static readonly string[] blacklistedExecutableFilenames = {
private static readonly string[] blacklistedExecutableFilenames =
{
"UnityCrashHandler.exe",
"UnityCrashHandler64.exe",
"install.exe"
Expand All @@ -60,10 +61,7 @@ public static void Main(string[] args)
Console.WriteLine("Running on " + Environment.OSVersion.Platform);

CommandLineOptions = null;
Parser.Default.ParseArguments<Options>(args).WithParsed(options =>
{
CommandLineOptions = options;
});
Parser.Default.ParseArguments<Options>(args).WithParsed(options => { CommandLineOptions = options; });

if (CommandLineOptions == null)
{
Expand All @@ -72,7 +70,7 @@ public static void Main(string[] args)
}

string loc;

//TODO: No longer needed
// if (Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.Win32NT)
// {
Expand Down Expand Up @@ -112,7 +110,7 @@ public static void Main(string[] args)
var assemblyPath = Path.Combine(baseGamePath, "GameAssembly.dll");
var exeName = Path.GetFileNameWithoutExtension(Directory.GetFiles(baseGamePath)
.First(f => f.EndsWith(".exe") && !blacklistedExecutableFilenames.Any(bl => f.EndsWith(bl))));

if (CommandLineOptions.ExeName != null)
{
exeName = CommandLineOptions.ExeName;
Expand All @@ -122,7 +120,7 @@ public static void Main(string[] args)
{
Console.WriteLine($"Auto-detected game name: {exeName}");
}

var unityPlayerPath = Path.Combine(baseGamePath, $"{exeName}.exe");
var metadataPath = Path.Combine(baseGamePath, $"{exeName}_Data", "il2cpp_data", "Metadata",
"global-metadata.dat");
Expand All @@ -136,10 +134,10 @@ public static void Main(string[] args)
PrintUsage();
return;
}

Console.WriteLine($"Located game EXE: {unityPlayerPath}");
Console.WriteLine($"Located global-metadata: {metadataPath}");

Console.WriteLine("\nAttempting to determine Unity version...");

int[] unityVerUseful;
Expand All @@ -161,7 +159,7 @@ public static void Main(string[] args)
verString.Append(Convert.ToChar(ggmBytes[idx]));
idx++;
}

var unityVer = verString.ToString();
unityVer = unityVer.Substring(0, unityVer.IndexOf("f", StringComparison.Ordinal));
Console.WriteLine("Read version string from globalgamemanagers: " + unityVer);
Expand Down Expand Up @@ -217,9 +215,16 @@ public static void Main(string[] args)
Console.WriteLine("\tPass 3: Handling Fields, methods, and properties (THIS MAY TAKE A WHILE)...");

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

for (var imageIndex = 0; imageIndex < Metadata.assemblyDefinitions.Length; imageIndex++)
{
Console.WriteLine($"\t\tProcessing DLL {imageIndex + 1} of {Metadata.assemblyDefinitions.Length}...");
var imageDef = Metadata.assemblyDefinitions[imageIndex];
var firstTypeDefinition = SharedState.TypeDefsByIndex[imageDef.firstTypeIndex];
var currentAssembly = firstTypeDefinition.Module.Assembly;

Console.WriteLine($"\t\tProcessing DLL {imageIndex + 1} of {Metadata.assemblyDefinitions.Length}: {currentAssembly.Name}...");


methods.AddRange(AssemblyBuilder.ProcessAssemblyTypes(Metadata, ThePE, Metadata.assemblyDefinitions[imageIndex]));
}

Expand Down

0 comments on commit 4b39634

Please sign in to comment.