-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialisedField.cs
More file actions
167 lines (133 loc) · 6.09 KB
/
Copy pathSerialisedField.cs
File metadata and controls
167 lines (133 loc) · 6.09 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using FieldInjector.FieldSerialisers;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using Il2CppInterop.Runtime.Runtime;
using Il2CppInterop.Runtime.Runtime.VersionSpecific.FieldInfo;
using static FieldInjector.Util;
using static Il2CppInterop.Runtime.IL2CPP;
namespace FieldInjector
{
internal abstract unsafe class SerialisedField
{
protected FieldInfo field;
public abstract IntPtr FieldType { get; }
public IntPtr NativeField { get; set; }
public FieldInfo ManagedField => this.field;
protected virtual Type TargetType => this.field.FieldType;
protected SerialisedField(FieldInfo field)
{
this.field = field;
}
public abstract Expression GetNativeToManagedExpression(Expression nativePtr);
public abstract Expression GetManagedToNativeExpression(Expression monoObj);
public virtual IEnumerable<Expression> GetDeserialiseExpression(Expression monoObj, Expression nativePtr, Expression fieldPtr)
{
// OLD:
// monoObj.field = NativeToMono(il2cpp_field_get_value_object(nativeFieldInfo, nativePtr));
/* New
* fieldPtr = il2cpp_field_get_value_object(nativeFieldInfo, nativePtr);
* monoObj.field = fieldPtr != IntPtr.Zero ? NativeToMono(fieldPtr) : default;
*/
if (this.NativeField == IntPtr.Zero)
{
throw new InvalidOperationException("Something went very wrong");
}
MethodInfo get_value = ((Func<IntPtr, IntPtr, IntPtr>)il2cpp_field_get_value_object).Method;
Expression fieldValuePtr = Expression.Call(
get_value,
Expression.Constant(this.NativeField),
nativePtr);
yield return Expression.Assign(fieldPtr, fieldValuePtr);
Expression hasValue = Expression.NotEqual(fieldPtr, Expression.Constant(IntPtr.Zero));
yield return Expression.Assign(Expression.Field(monoObj, this.field),
Expression.Condition(hasValue,
this.GetNativeToManagedExpression(fieldPtr),
Expression.Default(this.field.FieldType)
)
);
}
public virtual IEnumerable<Expression> GetSerialiseExpression(Expression monoObj, Expression nativePtr)
{
// valueType:
// field_set_value_object(nativePtr, this.NativeField, MonoToNative(monoObj.field))
// refType:
// field_set_value_object(nativePtr, this.NativeField, monoObj.field != null ? MonoToNative(monoObj.field) : IntPtr.Zero)
if (this.NativeField == IntPtr.Zero)
{
throw new InvalidOperationException("Something went very wrong");
}
MethodInfo set_value = ((Action<IntPtr, IntPtr, IntPtr>)field_set_value_object).Method;
Expression monoValue = Expression.Field(monoObj, this.field);
Expression fieldValuePtr = this.GetManagedToNativeExpression(monoValue);
if (!this.field.FieldType.IsValueType)
{
fieldValuePtr = Expression.Condition(
Expression.NotEqual(monoValue, Expression.Constant(null)),
fieldValuePtr, Expression.Constant(IntPtr.Zero));
}
yield return Expression.Call(set_value,
nativePtr,
Expression.Constant(this.NativeField),
fieldValuePtr);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "il2cpp method wrapper")]
protected static void field_set_value_object(IntPtr instance, IntPtr field, IntPtr obj)
{
il2cpp_field_set_value(instance, field, (void*)obj);
}
public static SerialisedField InferFromField(FieldInfo field)
{
Type fieldType = field.FieldType;
if (typeof(Il2CppSystem.Object).IsAssignableFrom(fieldType))
{
return new ObjectField(field);
}
else if (fieldType == typeof(string))
{
return new StringField(field);
}
else if (fieldType.IsValueType)
{
if (SerialisationHandler._injectedStructs.ContainsKey(fieldType))
{
return (SerialisedField)Activator.CreateInstance(typeof(CustomStructField<>).MakeGenericType(fieldType), field);
}
else if (fieldType.IsEnum || GetClassPointerForType(fieldType) != null)
{
return new StructField(field);
}
throw new NotSupportedException();
}
else if (fieldType.IsArray && fieldType.GetArrayRank() == 1)
{
return new ArrayField(field);
}
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
return new ListField(field);
}
throw new NotSupportedException($"Field type not supported: {field}");
}
public abstract int GetFieldSize(out int align);
public void FillFieldInfoStruct(INativeFieldInfoStruct infoOut, Il2CppClass* klassPtr, ref int offset)
{
infoOut.Name = Marshal.StringToHGlobalAnsi(this.field.Name);
infoOut.Parent = klassPtr;
var typePtr = (MyIl2CppType*)Marshal.AllocHGlobal(Marshal.SizeOf<MyIl2CppType>());
*typePtr = *(MyIl2CppType*)this.FieldType;
typePtr->attrs = (ushort)Il2CppSystem.Reflection.FieldAttributes.Public;
infoOut.Type = (Il2CppTypeStruct*)typePtr;
var size = this.GetFieldSize(out int align);
offset = AlignTo(offset, align);
infoOut.Offset = offset;
offset += size;
}
public override string ToString()
{
return this.GetType().Name + ":" + this.TargetType;
}
}
}