Simplify string obfuscation processing

This commit is contained in:
Sinai 2021-05-07 01:53:02 +10:00
parent 8534c08f49
commit 56875e0641
3 changed files with 15 additions and 28 deletions

View File

@ -64,7 +64,7 @@ namespace UnityExplorer
#region Deobfuscation cache
private static readonly Dictionary<string, Type> DeobfuscatedTypes = new Dictionary<string, Type>();
//internal static Dictionary<string, string> s_deobfuscatedTypeNames = new Dictionary<string, string>();
private static readonly Dictionary<string, string> reverseDeobCache = new Dictionary<string, string>();
private static void BuildDeobfuscationCache()
{
@ -83,7 +83,6 @@ namespace UnityExplorer
try
{
// Thanks to Slaynash for this
if (type.CustomAttributes.Any(it => it.AttributeType.Name == "ObfuscatedNameAttribute"))
{
var cppType = Il2CppType.From(type);
@ -91,13 +90,21 @@ namespace UnityExplorer
if (!DeobfuscatedTypes.ContainsKey(cppType.FullName))
{
DeobfuscatedTypes.Add(cppType.FullName, type);
//s_deobfuscatedTypeNames.Add(cppType.FullName, type.FullName);
reverseDeobCache.Add(type.FullName, cppType.FullName);
}
}
}
catch { }
}
internal override string Internal_ProcessTypeInString(string theString, Type type)
{
if (reverseDeobCache.TryGetValue(type.FullName, out string obName))
return theString.Replace(obName, type.FullName);
return theString;
}
#endregion
// Get type by name
@ -287,10 +294,7 @@ namespace UnityExplorer
return null;
if (type.IsEnum)
{
// TODO not tested
return Il2CppSystem.Enum.ToObject(Il2CppType.From(type), (ulong)value);
}
if (type.IsPrimitive && AllTypes.TryGetValue($"Il2Cpp{type.FullName}", out Type cppType))
{
@ -358,22 +362,6 @@ namespace UnityExplorer
return s;
}
internal override string Internal_ProcessTypeInString(string theString, Type type, ref string typeName)
{
if (!Il2CppTypeNotNull(type))
return theString;
var cppType = Il2CppType.From(type);
if (cppType != null && DeobfuscatedTypes.ContainsKey(cppType.FullName))
{
typeName = DeobfuscatedTypes[cppType.FullName].FullName;
theString = theString.Replace(cppType.FullName, typeName);
}
return theString;
}
#endregion

View File

@ -105,10 +105,10 @@ namespace UnityExplorer
=> obj;
// Processing deobfuscated type names in strings
public static string ProcessTypeInString(Type type, string theString, ref string typeName)
=> Instance.Internal_ProcessTypeInString(theString, type, ref typeName);
public static string ProcessTypeInString(Type type, string theString)
=> Instance.Internal_ProcessTypeInString(theString, type);
internal virtual string Internal_ProcessTypeInString(string theString, Type type, ref string typeName)
internal virtual string Internal_ProcessTypeInString(string theString, Type type)
=> theString;
// Force loading modules

View File

@ -166,15 +166,14 @@ namespace UnityExplorer.UI.Utility
toString = ex.ReflectionExToString();
}
string _ = null;
toString = ReflectionUtility.ProcessTypeInString(type, toString, ref _);
toString = ReflectionUtility.ProcessTypeInString(type, toString);
#if CPP
if (value is Il2CppSystem.Type cppType)
{
var monoType = Il2CppReflection.GetUnhollowedType(cppType);
if (monoType != null)
toString = ReflectionUtility.ProcessTypeInString(monoType, toString, ref _);
toString = ReflectionUtility.ProcessTypeInString(monoType, toString);
}
#endif