Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed deobfuscation map generation #88

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
39 changes: 35 additions & 4 deletions Il2CppInterop.Generator/Contexts/AssemblyRewriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ public AssemblyRewriteContext(RewriteGlobalContext globalContext, AssemblyDefini

public TypeRewriteContext GetContextForOriginalType(TypeDefinition type)
{
return myOldTypeMap[type];
try
{
return myOldTypeMap[type];
}
catch
{
foreach (var oldtype in myOldTypeMap.Keys)
{
if (type.Name == oldtype.Name) return myOldTypeMap[oldtype];
}
return myNewTypeMap[type];
}
}

public TypeRewriteContext? TryGetContextForOriginalType(TypeDefinition type)
Expand Down Expand Up @@ -119,17 +130,31 @@ public TypeReference RewriteTypeRef(TypeReference? typeRef)
return Imports.Module.Void();

if (typeRef.FullName == "System.String")
return Imports.Module.String();
return sourceModule.ImportReference(GlobalContext.GetAssemblyByName("mscorlib")
.GetTypeByName("System.String").NewType);

if (typeRef.FullName == "System.Object")
return sourceModule.ImportReference(GlobalContext.GetAssemblyByName("mscorlib")
.GetTypeByName("System.Object").NewType);

if (typeRef.FullName == "Il2CppSystem.Object")
return Imports.Module.Object();

if (typeRef.FullName == "System.Attribute")
return sourceModule.ImportReference(GlobalContext.GetAssemblyByName("mscorlib")
.GetTypeByName("System.Attribute").NewType);

var originalTypeDef = typeRef.Resolve();
TypeDefinition? originalTypeDef;
try
{
originalTypeDef = typeRef.Resolve();
}
catch
{
return Imports.Il2CppObjectBase;
}
if (originalTypeDef == null)
return Imports.Il2CppObjectBase;
var targetAssembly = GlobalContext.GetNewAssemblyForOriginal(originalTypeDef.Module.Assembly);
var target = targetAssembly.GetContextForOriginalType(originalTypeDef).NewType;

Expand All @@ -138,7 +163,13 @@ public TypeReference RewriteTypeRef(TypeReference? typeRef)

public TypeRewriteContext GetTypeByName(string name)
{
return myNameTypeMap[name];
return myNameTypeMap.TryGetValue(name, out var result1) ?
result1 :
myNameTypeMap.TryGetValue(name.Replace("System", "Il2CppSystem"), out var result2) ?
result2 :
myNameTypeMap.TryGetValue(name.Replace("Il2CppSystem", "System"), out var result3) ?
result3 :
null;
}

public TypeRewriteContext? TryGetTypeByName(string name)
Expand Down
34 changes: 29 additions & 5 deletions Il2CppInterop.Generator/Contexts/RewriteGlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,31 @@ internal void AddAssemblyContext(string assemblyName, AssemblyRewriteContext con

public AssemblyRewriteContext GetNewAssemblyForOriginal(AssemblyDefinition oldAssembly)
{
return myAssembliesByOld[oldAssembly];
try
{
return myAssembliesByOld[oldAssembly];
}
catch
{
foreach (var assembly in myAssembliesByOld.Keys)
{
if (assembly.Name == oldAssembly.Name)
return myAssembliesByOld[assembly];
}
return myAssemblies.TryGetValue("Il2Cppmscorlib", out var result2) ? result2 : null;
}
}

public TypeRewriteContext GetNewTypeForOriginal(TypeDefinition originalType)
{
return GetNewAssemblyForOriginal(originalType.Module.Assembly)
.GetContextForOriginalType(originalType);
return (GetNewAssemblyForOriginal(originalType.Module.Assembly) ??
(myAssemblies.TryGetValue("Il2Cppmscorlib", out var result1) ?
result1 :
null))?
.GetContextForOriginalType(originalType) ??
(myAssemblies.TryGetValue("Il2Cppmscorlib", out var result2) ?
result2.GetContextForOriginalType(originalType) :
null);
}

public TypeRewriteContext? TryGetNewTypeForOriginal(TypeDefinition originalType)
Expand All @@ -109,12 +127,18 @@ public TypeRewriteContext.TypeSpecifics JudgeSpecificsByOriginalType(TypeReferen
return TypeRewriteContext.TypeSpecifics.ReferenceType;

var fieldTypeContext = GetNewTypeForOriginal(typeRef.Resolve());
return fieldTypeContext.ComputedTypeSpecifics;
return fieldTypeContext != null ? fieldTypeContext.ComputedTypeSpecifics : TypeRewriteContext.TypeSpecifics.NotComputed;
}

public AssemblyRewriteContext GetAssemblyByName(string name)
{
return myAssemblies[name];
return myAssemblies.TryGetValue(name, out var result1) ?
result1 :
myAssemblies.TryGetValue("mscorlib", out var result2) ?
result2 :
myAssemblies.TryGetValue("Il2Cppmscorlib", out var result3) ?
result3 :
null;
}

public AssemblyRewriteContext? TryGetAssemblyByName(string name)
Expand Down
4 changes: 2 additions & 2 deletions Il2CppInterop.Generator/Il2CppInterop.Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Mono.Cecil" Version="0.11.3"/>
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Il2CppInterop.Common\Il2CppInterop.Common.csproj"/>
<ProjectReference Include="..\Il2CppInterop.Common\Il2CppInterop.Common.csproj" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions Il2CppInterop.Generator/Passes/Pass11ComputeTypeSpecifics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static void DoPass(RewriteGlobalContext context)

private static void ComputeSpecifics(TypeRewriteContext typeContext)
{
if (typeContext == null) return;
if (typeContext.ComputedTypeSpecifics != TypeRewriteContext.TypeSpecifics.NotComputed) return;
typeContext.ComputedTypeSpecifics = TypeRewriteContext.TypeSpecifics.Computing;

Expand All @@ -38,6 +39,7 @@ private static void ComputeSpecifics(TypeRewriteContext typeContext)
}

var fieldTypeContext = typeContext.AssemblyContext.GlobalContext.GetNewTypeForOriginal(fieldType.Resolve());
if (fieldTypeContext == null) return;
ComputeSpecifics(fieldTypeContext);
if (fieldTypeContext.ComputedTypeSpecifics != TypeRewriteContext.TypeSpecifics.BlittableStruct)
{
Expand Down
12 changes: 4 additions & 8 deletions Il2CppInterop.Generator/Runners/DeobfuscationMapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ void DoEnum(TypeRewriteContext obfuscatedType, TypeRewriteContext cleanType)
var matchedField =
cleanType.OriginalType.Fields[obfuscatedType.OriginalType.Fields.IndexOf(originalTypeField)];

writer.WriteLine(obfuscatedType.NewType.GetNamespacePrefix() + "." + obfuscatedType.NewType.Name +
"::" + Pass22GenerateEnums.GetUnmangledName(originalTypeField) + ";" +
matchedField.Name + ";0");
writer.WriteLine(obfuscatedType.NewType.GetNamespacePrefix() + obfuscatedType.NewType.Name + "::" + Pass22GenerateEnums.GetUnmangledName(originalTypeField) + ";" + matchedField.Name + ";0");
}
}

Expand All @@ -147,16 +145,14 @@ void DoEnum(TypeRewriteContext obfuscatedType, TypeRewriteContext cleanType)

void DoType(TypeRewriteContext typeContext, TypeRewriteContext? enclosingType)
{
if (!typeContext.OriginalNameWasObfuscated) return;
if (cleanAssembly.TryGetTypeByName(typeContext.NewType.Name) != null) return;

var cleanType = FindBestMatchType(typeContext, cleanAssembly, enclosingType);
if (cleanType.Item1 == null) return;

if (!usedNames.TryGetValue(cleanType.Item1.NewType, out var existing) ||
existing.Item2 < cleanType.Item2)
usedNames[cleanType.Item1.NewType] = (
typeContext.NewType.GetNamespacePrefix() + "." + typeContext.NewType.Name, cleanType.Item2,
typeContext.OriginalType.Namespace != cleanType.Item1.OriginalType.Namespace);
existing.Penalty < cleanType.Item2)
usedNames[cleanType.Item1.NewType] = (typeContext.NewType.GetNamespacePrefix() + "." + typeContext.NewType.Name, cleanType.Item2, typeContext.OriginalType.Namespace != cleanType.Item1.OriginalType.Namespace);
else
return;

Expand Down
Loading