Skip to content

Commit 4b39634

Browse files
author
Sam Byass
committed
Fix dependency on dotnet core dlls
1 parent 0857c37 commit 4b39634

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

Cpp2IL/AssemblyBuilder.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ internal static List<AssemblyDefinition> CreateAssemblies(Il2CppMetadata metadat
7070
{
7171
//This is a new type so ensure it's registered
7272
definition = new TypeDefinition(ns, name, (TypeAttributes) type.flags);
73+
if (ns == "System" && name == "String")
74+
{
75+
typeof(TypeReference).GetField("etype", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(definition, (byte) 0x0e); //mark as string
76+
}
7377
mainModule.Types.Add(definition);
78+
SharedState.AllTypeDefinitions.Add(definition);
7479
SharedState.TypeDefsByIndex.Add(defNumber, definition);
7580
}
7681

@@ -130,12 +135,12 @@ private static void CreateDefaultConstructor(TypeDefinition typeDefinition)
130135
var defaultConstructor = new MethodDefinition(
131136
".ctor",
132137
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
133-
module.ImportReference(typeof(void))
138+
module.ImportReference(Utils.TryLookupTypeDefByName("System.Void").Item1)
134139
);
135140

136141
var processor = defaultConstructor.Body.GetILProcessor();
137-
processor.Emit(OpCodes.Ldarg_0);
138-
processor.Emit(OpCodes.Call, module.ImportReference(typeof(Attribute).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]));
142+
// processor.Emit(OpCodes.Ldarg_0);
143+
// processor.Emit(OpCodes.Call, module.ImportReference(Utils.TryLookupTypeDefByName("System.Attribute").Item1.GetConstructors().First()));
139144
processor.Emit(OpCodes.Ret);
140145

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

149-
var stringTypeReference = imageDef.MainModule.ImportReference(typeof(string));
150-
var attributeTypeReference = imageDef.MainModule.ImportReference(typeof(Attribute));
154+
var stringTypeReference = imageDef.MainModule.ImportReference(Utils.TryLookupTypeDefByName("System.String").Item1);
155+
var attributeTypeReference = imageDef.MainModule.ImportReference(Utils.TryLookupTypeDefByName("System.Attribute").Item1);
151156

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

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

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

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

Cpp2IL/Program.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ internal class Options
2424
{
2525
[Option("game-path", Required = true, HelpText = "Specify path to the game folder (containing the exe)")]
2626
public string GamePath { get; set; }
27-
27+
2828
[Option("exe-name", Required = false, HelpText = "Specify an override for the unity executable name in case the auto-detection doesn't work.")]
2929
public string ExeName { get; set; }
30-
30+
3131
[Option("skip-analysis", Required = false, HelpText = "Skip the analysis section and stop once DummyDLLs have been generated.")]
3232
public bool SkipAnalysis { get; set; }
33-
33+
3434
[Option("skip-metadata-txts", Required = false, HelpText = "Skip the generation of [classname]_metadata.txt files.")]
3535
public bool SkipMetadataTextFiles { get; set; }
3636
}
37-
37+
3838
public static float MetadataVersion = 24f;
3939

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

6263
CommandLineOptions = null;
63-
Parser.Default.ParseArguments<Options>(args).WithParsed(options =>
64-
{
65-
CommandLineOptions = options;
66-
});
64+
Parser.Default.ParseArguments<Options>(args).WithParsed(options => { CommandLineOptions = options; });
6765

6866
if (CommandLineOptions == null)
6967
{
@@ -72,7 +70,7 @@ public static void Main(string[] args)
7270
}
7371

7472
string loc;
75-
73+
7674
//TODO: No longer needed
7775
// if (Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.Win32NT)
7876
// {
@@ -112,7 +110,7 @@ public static void Main(string[] args)
112110
var assemblyPath = Path.Combine(baseGamePath, "GameAssembly.dll");
113111
var exeName = Path.GetFileNameWithoutExtension(Directory.GetFiles(baseGamePath)
114112
.First(f => f.EndsWith(".exe") && !blacklistedExecutableFilenames.Any(bl => f.EndsWith(bl))));
115-
113+
116114
if (CommandLineOptions.ExeName != null)
117115
{
118116
exeName = CommandLineOptions.ExeName;
@@ -122,7 +120,7 @@ public static void Main(string[] args)
122120
{
123121
Console.WriteLine($"Auto-detected game name: {exeName}");
124122
}
125-
123+
126124
var unityPlayerPath = Path.Combine(baseGamePath, $"{exeName}.exe");
127125
var metadataPath = Path.Combine(baseGamePath, $"{exeName}_Data", "il2cpp_data", "Metadata",
128126
"global-metadata.dat");
@@ -136,10 +134,10 @@ public static void Main(string[] args)
136134
PrintUsage();
137135
return;
138136
}
139-
137+
140138
Console.WriteLine($"Located game EXE: {unityPlayerPath}");
141139
Console.WriteLine($"Located global-metadata: {metadataPath}");
142-
140+
143141
Console.WriteLine("\nAttempting to determine Unity version...");
144142

145143
int[] unityVerUseful;
@@ -161,7 +159,7 @@ public static void Main(string[] args)
161159
verString.Append(Convert.ToChar(ggmBytes[idx]));
162160
idx++;
163161
}
164-
162+
165163
var unityVer = verString.ToString();
166164
unityVer = unityVer.Substring(0, unityVer.IndexOf("f", StringComparison.Ordinal));
167165
Console.WriteLine("Read version string from globalgamemanagers: " + unityVer);
@@ -217,9 +215,16 @@ public static void Main(string[] args)
217215
Console.WriteLine("\tPass 3: Handling Fields, methods, and properties (THIS MAY TAKE A WHILE)...");
218216

219217
var methods = new List<(TypeDefinition type, List<CppMethodData> methods)>();
218+
220219
for (var imageIndex = 0; imageIndex < Metadata.assemblyDefinitions.Length; imageIndex++)
221220
{
222-
Console.WriteLine($"\t\tProcessing DLL {imageIndex + 1} of {Metadata.assemblyDefinitions.Length}...");
221+
var imageDef = Metadata.assemblyDefinitions[imageIndex];
222+
var firstTypeDefinition = SharedState.TypeDefsByIndex[imageDef.firstTypeIndex];
223+
var currentAssembly = firstTypeDefinition.Module.Assembly;
224+
225+
Console.WriteLine($"\t\tProcessing DLL {imageIndex + 1} of {Metadata.assemblyDefinitions.Length}: {currentAssembly.Name}...");
226+
227+
223228
methods.AddRange(AssemblyBuilder.ProcessAssemblyTypes(Metadata, ThePE, Metadata.assemblyDefinitions[imageIndex]));
224229
}
225230

0 commit comments

Comments
 (0)