Skip to content

Commit

Permalink
Warning cleanup + add two method property getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Sep 7, 2020
1 parent c769e23 commit 5346ab6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 49 deletions.
76 changes: 36 additions & 40 deletions Cpp2IL/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
//Disabled because it's slow as hell
// #define DUMP_PACKAGE_SUCCESS_DATA

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -11,7 +14,6 @@
using CommandLine;
using Cpp2IL.Analysis;
using LibCpp2IL;
using LibCpp2IL.Metadata;
using LibCpp2IL.PE;
using Mono.Cecil;
using SharpDisasm;
Expand Down Expand Up @@ -39,9 +41,7 @@ internal class Options
public bool SkipMetadataTextFiles { get; set; }
}

public static float MetadataVersion = 24f;

private static readonly string[] blacklistedExecutableFilenames =
private static readonly string[] BlacklistedExecutableFilenames =
{
"UnityCrashHandler.exe",
"UnityCrashHandler64.exe",
Expand Down Expand Up @@ -80,7 +80,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))));
.First(f => f.EndsWith(".exe") && !BlacklistedExecutableFilenames.Any(bl => f.EndsWith(bl))));

if (CommandLineOptions.ExeName != null)
{
Expand Down Expand Up @@ -162,7 +162,7 @@ public static void Main(string[] args)
#region Assembly Generation

LibCpp2IlMain.GetMethodDefinitionByGlobalAddress(0x180623548);

var resolver = new RegistryAssemblyResolver();
var moduleParams = new ModuleParameters
{
Expand All @@ -175,12 +175,12 @@ public static void Main(string[] args)
Console.WriteLine("Building assemblies...");
Console.WriteLine("\tPass 1: Creating types...");

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

Console.WriteLine("\tPass 2: Setting parents and handling inheritance...");

//Stateful method, no return value
AssemblyBuilder.ConfigureHierarchy(LibCpp2IlMain.TheMetadata, LibCpp2IlMain.ThePe);
AssemblyBuilder.ConfigureHierarchy(LibCpp2IlMain.TheMetadata, LibCpp2IlMain.ThePe!);

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

Expand Down Expand Up @@ -417,36 +417,35 @@ public static void Main(string[] args)
.Append("\n");
}

if (false)
#if DUMP_PACKAGE_SUCCESS_DATA
Console.WriteLine("By Package:");
var keys = methodTaintDict
.Select(kvp => kvp.Key)
.GroupBy(
GetPackageName,
className => className,
(packageName, classEnumerable) => new
{
package = packageName,
classes = classEnumerable.ToList()
})
.ToList();

foreach (var key in keys)
{
Console.WriteLine("By Package:");
var keys = methodTaintDict
.Select(kvp => kvp.Key)
.GroupBy(
GetPackageName,
className => className,
(packageName, classEnumerable) => new
{
package = packageName,
classes = classEnumerable.ToList()
})
.ToList();
var resultLine = new StringBuilder();
var totalClassCount = key.classes.Count;
resultLine.Append($"\tIn package {key.package} ({totalClassCount} classes): ");

foreach (var key in keys)
foreach (var reason in Enum.GetValues(typeof(AsmDumper.TaintReason)))
{
var resultLine = new StringBuilder();
var totalClassCount = key.classes.Count;
resultLine.Append($"\tIn package {key.package} ({totalClassCount} classes): ");

foreach (var reason in Enum.GetValues(typeof(AsmDumper.TaintReason)))
{
var count = (decimal) methodTaintDict.Where(kvp => key.classes.Contains(kvp.Key)).Count(v => v.Value == (AsmDumper.TaintReason) reason);
resultLine.Append(reason).Append(":").Append(count).Append($" ({Math.Round(count * 100 / totalClassCount, 1)}%) ");
}

Console.WriteLine(resultLine.ToString());
var count = (decimal) methodTaintDict.Where(kvp => key.classes.Contains(kvp.Key)).Count(v => v.Value == (AsmDumper.TaintReason) reason);
resultLine.Append(reason).Append(":").Append(count).Append($" ({Math.Round(count * 100 / totalClassCount, 1)}%) ");
}

Console.WriteLine(resultLine.ToString());
}
#endif


File.WriteAllText(Path.Combine(outputPath, "method_statuses.txt"), summary.ToString());
Expand All @@ -458,12 +457,8 @@ public static void Main(string[] args)
// Console.WriteLine("[Finished. Press enter to exit]");
// Console.ReadLine();
}


#region Assembly Generation Helper Functions

#endregion


#if DUMP_PACKAGE_SUCCESS_DATA
private static string GetPackageName(string fullName)
{
if (fullName.Contains("::"))
Expand All @@ -475,5 +470,6 @@ private static string GetPackageName(string fullName)

return string.Join(".", split);
}
#endif
}
}
1 change: 1 addition & 0 deletions LibCpp2IL/Il2CppGlobalGenericMethodRef.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LibCpp2IL.Metadata;
using LibCpp2IL.Reflection;
#pragma warning disable 8618

namespace LibCpp2IL
{
Expand Down
2 changes: 1 addition & 1 deletion LibCpp2IL/LibCpp2IlMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class LibCpp2IlSettings
}

//Nasty fallback but we shouldn't ever get here.
return TheMetadata.methodDefs.FirstOrDefault(type => type.GlobalKey == global.Value.Name);
return TheMetadata!.methodDefs.FirstOrDefault(type => type.GlobalKey == global.Value.Name);
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion LibCpp2IL/Metadata/Il2CppMethodDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public class Il2CppMethodDefinition

public Il2CppTypeDefinition? DeclaringType => LibCpp2IlMain.TheMetadata == null ? null : LibCpp2IlMain.TheMetadata.typeDefs[declaringTypeIdx];

public ulong MethodPointer => LibCpp2IlMain.ThePe == null || LibCpp2IlMain.TheMetadata == null ? 0 : LibCpp2IlMain.ThePe.GetMethodPointer(methodIndex, MethodIndex, DeclaringType.DeclaringAssembly.assemblyIndex, token);
public ulong MethodPointer => LibCpp2IlMain.ThePe == null || LibCpp2IlMain.TheMetadata == null || DeclaringType == null ? 0 : LibCpp2IlMain.ThePe.GetMethodPointer(methodIndex, MethodIndex, DeclaringType!.DeclaringAssembly!.assemblyIndex, token);

public long MethodOffsetInFile => MethodPointer == 0 || LibCpp2IlMain.ThePe == null ? 0 : LibCpp2IlMain.ThePe.MapVirtualAddressToRaw(MethodPointer);

public ulong Rva => MethodPointer == 0 || LibCpp2IlMain.ThePe == null ? 0 : LibCpp2IlMain.ThePe.GetRVA(MethodPointer);

public Il2CppParameterReflectionData[]? Parameters => LibCpp2IlMain.TheMetadata == null || LibCpp2IlMain.ThePe == null
? null
Expand Down
2 changes: 1 addition & 1 deletion LibCpp2IL/Metadata/Il2CppTypeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Il2CppAssemblyDefinition? DeclaringAssembly
.ToArray();

public object?[]? FieldDefaults => Fields?
.Select((f, idx) => (f.FieldIndex, FieldAttributes[idx]))
.Select((f, idx) => (f.FieldIndex, FieldAttributes![idx]))
.Select(tuple => (tuple.Item2 & System.Reflection.FieldAttributes.HasDefault) != 0 ? LibCpp2IlMain.TheMetadata!.GetFieldDefaultValueFromIndex(tuple.FieldIndex) : null)
.Select(def => def == null ? null : LibCpp2ILUtils.GetDefaultValue(def.dataIndex, def.typeIndex, LibCpp2IlMain.TheMetadata!, LibCpp2IlMain.ThePe!))
.ToArray();
Expand Down
1 change: 1 addition & 0 deletions LibCpp2IL/Reflection/Il2CppParameterReflectionData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;

#pragma warning disable 8618
namespace LibCpp2IL.Reflection
{
public class Il2CppParameterReflectionData
Expand Down
14 changes: 8 additions & 6 deletions LibCpp2IL/Reflection/Il2CppTypeReflectionData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using LibCpp2IL.Metadata;

#pragma warning disable 8618
namespace LibCpp2IL.Reflection
{
/// <summary>
Expand All @@ -24,32 +25,33 @@ public class Il2CppTypeReflectionData
public byte arrayRank;
public string variableGenericParamName;
public bool isPointer;
#pragma warning restore 8618

private string getPtrSuffix()
private string GetPtrSuffix()
{
return isPointer ? "*" : "";
}

public override string ToString()
{
if (isArray)
return arrayType + "[]".Repeat(arrayRank) + getPtrSuffix();
return arrayType + "[]".Repeat(arrayRank) + GetPtrSuffix();

if (!isType)
return variableGenericParamName + getPtrSuffix();
return variableGenericParamName + GetPtrSuffix();

if (!isGenericType)
return baseType.FullName! + getPtrSuffix();
return baseType!.FullName! + GetPtrSuffix();

var builder = new StringBuilder(baseType.FullName + "<");
var builder = new StringBuilder(baseType!.FullName + "<");
foreach (var genericParam in genericParams)
{
builder.Append(genericParam).Append(", ");
}

builder.Remove(builder.Length - 2, 2);
builder.Append(">");
return builder + getPtrSuffix();
return builder + GetPtrSuffix();
}
}
}

0 comments on commit 5346ab6

Please sign in to comment.