-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTypeTranslator.cs
112 lines (102 loc) · 3.78 KB
/
TypeTranslator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using Il2CppInspector.Reflection;
using Il2CppTranslator.Util;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Il2CppTranslator
{
public static class TypeTranslator
{
public static TypeInfo GetMatchingType(Type type, Translator translator)
{
if (translator.Translated.Contains(type))
{
return null;
}
List<string> sequence = type.GetFieldSequence();
List<TypeInfo> types = Helpers.FindTypesWithFieldSequence(type.GetFieldSequence(), translator)
.Where(t => t.DeclaredFields.Count(f => !f.IsStatic && !f.IsLiteral) == type.GetDeclaredFields().Count(f => !f.IsStatic && !f.IsLiteral))
.ToList();
if (types.Count == 0)
{
return null;
}
if (types.Count == 1)
{
return types[0];
}
List<TypeInfo> fieldNameMatches = types.Where(t => type.GetDeclaredFields().Any(f => t.DeclaredFields.Any(f2 => f2.Name.Equals(f.Name)))).ToList();
if (fieldNameMatches.Count == 1)
{
return fieldNameMatches[0];
}
List<TypeInfo> staticFieldCountMatches = types.Where(t => type.GetDeclaredFields().Count(f => f.IsStatic) == t.DeclaredFields.Count(f => f.IsStatic)).ToList();
if (staticFieldCountMatches.Count == 1)
{
return staticFieldCountMatches[0];
}
return null;
}
public static void TranslateRecursively(Type clean, TypeInfo obf, Translator translator)
{
if (translator.Translated.Contains(clean))
{
return;
}
if (clean.Name != obf.Name)
{
translator.Translations.Add((obf.Name, clean.Name));
obf.Name = clean.Name;
}
List<System.Reflection.FieldInfo> fields = clean.GetDeclaredFields();
foreach (var field in fields)
{
FieldInfo match = FindMatchingField(field, obf.DeclaredFields);
if (match == null)
{
continue;
}
if (match.Name != field.Name)
{
translator.Translations.Add((match.Name, field.Name));
match.Name = field.Name;
}
if (Attribute.IsDefined(field.FieldType, typeof(TranslatorAttribute)) && field.FieldType != clean)
{
TranslateRecursively(field.FieldType, match.FieldType, translator);
}
}
translator.Translated.Add(clean);
}
internal static FieldInfo FindMatchingField(System.Reflection.FieldInfo field, IEnumerable<FieldInfo> fields)
{
foreach (FieldInfo f in fields)
{
if (f.IsStatic != field.IsStatic || f.IsPrivate != field.IsPrivate || f.IsPublic != field.IsPublic)
{
continue;
}
if (f.Name == field.Name)
{
return f;
}
if (f.Offset == GetFieldOffset(field))
{
return f;
}
}
return null;
}
internal static int GetFieldOffset(System.Reflection.FieldInfo field)
{
if (!Attribute.IsDefined(field, typeof(TranslatorFieldOffsetAttribute)))
{
return -1;
}
else
{
return ((TranslatorFieldOffsetAttribute)Attribute.GetCustomAttribute(field, typeof(TranslatorFieldOffsetAttribute))).Offset;
}
}
}
}