forked from FabianTerhorst/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function.cs
365 lines (314 loc) · 14.4 KB
/
Function.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using AltV.Net.Elements.Entities;
using AltV.Net.Elements.Args;
using AltV.Net.FunctionParser;
using AltV.Net.ObjectMethodExecutors;
using AltV.Net.ObjectMethodExecutors.ParameterValues;
using AltV.Net.Shared;
using AltV.Net.Shared.Elements.Entities;
namespace AltV.Net
{
//TODO: when received MValue is from type null set the value to null
public partial class Function
{
public delegate object Func(params object[] args);
/*private delegate bool CallArgsLengthCheck(Type[] args, int requiredArgsCount, FunctionTypeInfo[] typeInfos,
MValueConst[] values);
private delegate bool CallArgsLengthCheckPlayer(Type[] args, int requiredArgsCount,
FunctionTypeInfo[] typeInfos, IPlayer player,
MValueConst[] values);*/
[Obsolete("Use Alt.CreateFunction or overload with ISharedCore argument instead")]
public static Function Create<T>(T func) where T : Delegate
{
AltShared.Core.LogWarning("Function.Create<T>(T func) is deprecated, use Alt.CreateFunction or overload with ISharedCore argument instead");
return Create(AltShared.Core, func);
}
//TODO: for high optimization add ParseBoolUnsafe ect. that doesn't contains the mValue type check for scenarios where we already had to check the mValue type
// Returns null when function signature isn't supported
public static Function Create<T>(ISharedCore core, T func) where T : Delegate
{
var genericArguments = func.GetType().GetGenericArguments();
var parameters = func.Method.GetParameters();
var returnType = func.Method.ReturnType;
if (returnType != FunctionTypes.Void)
{
genericArguments = genericArguments.SkipLast(1).ToArray();
}
//TODO: check for unsupported types
var constParsers = new FunctionMValueConstParser[parameters.Length];
var objectParsers = new FunctionObjectParser[parameters.Length];
var stringParsers = new FunctionStringParser[parameters.Length];
var typeInfos = new FunctionTypeInfo[parameters.Length];
var requiredArgsCount = 0;
for (int i = 0, length = parameters.Length; i < length; i++)
{
var parameterInfo = parameters[i];
var arg = parameterInfo.ParameterType;
var typeInfo = typeInfos[i] = new FunctionTypeInfo(arg, parameterInfo);
if (!typeInfo.IsNullable && !typeInfo.IsParamArray && !parameterInfo.HasDefaultValue)
{
requiredArgsCount++;
}
constParsers[i] = typeInfo.ConstParser;
objectParsers[i] = typeInfo.ObjectParser;
stringParsers[i] = typeInfo.StringParser;
if (constParsers[i] == null || objectParsers[i] == null || stringParsers[i] == null)
{
AltShared.Core.LogWarning("Failed to construct a function because of unsupported argument type " + arg + " at index " + i);
return null;
}
}
for (int i = 0, length = typeInfos.Length; i < length; i++)
{
if (typeInfos[i].IsParamArray && i + 1 < length)
{
throw new ArgumentException(
"params array needs to be at the end of the method. E.g. (int p1, int p2, params int[] args)");
}
if (!typeInfos[i].IsNullable || i + 1 >= length) continue;
if (!typeInfos[i + 1].IsNullable && !typeInfos[i + 1].IsParamArray)
{
throw new ArgumentException(
"Method nullable needs to be at the end of the method. E.g. (int p1, int? p2, int p3?).");
}
}
return new Function(core, func, returnType, genericArguments, constParsers, objectParsers, stringParsers,
typeInfos, requiredArgsCount);
}
/*private static bool CheckArgsLength(Type[] args, int requiredArgsCount, FunctionTypeInfo[] typeInfos,
MValueConst[] values)
{
return values.Length >= requiredArgsCount;
}
private static bool CheckArgsLengthWithPlayer(Type[] args, int requiredArgsCount, FunctionTypeInfo[] typeInfos,
IPlayer player,
MValueConst[] values)
{
return values.Length + 1 >= requiredArgsCount && typeInfos[0].IsPlayer;
}*/
internal readonly ISharedCore core;
internal readonly Delegate @delegate;
internal readonly Type returnType;
internal readonly Type[] args;
internal readonly FunctionMValueConstParser[] constParsers;
internal readonly FunctionObjectParser[] objectParsers;
internal readonly FunctionStringParser[] stringParsers;
internal readonly FunctionTypeInfo[] typeInfos;
internal readonly int requiredArgsCount;
private readonly ObjectMethodExecutor objectMethodExecutor;
private readonly object target;
private Function(ISharedCore core, Delegate @delegate, Type returnType, Type[] args,
FunctionMValueConstParser[] constParsers,
FunctionObjectParser[] objectParsers, FunctionStringParser[] stringParsers, FunctionTypeInfo[] typeInfos,
int requiredArgsCount)
{
this.core = core;
this.@delegate = @delegate;
this.returnType = returnType;
this.args = args;
this.constParsers = constParsers;
this.objectParsers = objectParsers;
this.stringParsers = stringParsers;
this.typeInfos = typeInfos;
this.requiredArgsCount = requiredArgsCount;
var parameterDefaultValues = ParameterDefaultValues
.GetParameterDefaultValues(@delegate.Method);
objectMethodExecutor = ObjectMethodExecutor.Create(
@delegate.Method,
@delegate.Target?.GetType().GetTypeInfo(),
parameterDefaultValues);
target = @delegate.Target;
}
//TODO: make call async because it doesnt matter there is no concurrent problems inside
//TODO: write function parser in cpp so it can do that natively without any native calls for mvalues (experimental)
//TODO: register event callbacks to cpp as dynamic function pointers with void** so cpp knows the types it needs to check
//TODO: add support for nullable args, these are reducing the required length, add support for default values as well
//TODO: maybe cache var invokeValues = new object[length];
internal object Call(MValueConst[] values)
{
var invokeValues = CalculateInvokeValues(values);
if (invokeValues == null) return null;
var resultObj = @delegate.DynamicInvoke(invokeValues);
return returnType == FunctionTypes.Void ? null : resultObj;
}
internal object CallCatching(MValueConst[] values, string exceptionLocation)
{
try
{
return this.Call(values);
}
catch (TargetInvocationException exception)
{
core.LogError($"Exception at {exceptionLocation}: {exception.InnerException}");
return null;
}
catch (Exception exception)
{
core.LogError($"Exception at {exceptionLocation}: {exception}");
return null;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal object[] CalculateInvokeValues(MValueConst[] values)
{
var length = values.Length;
if (length < requiredArgsCount)
{
return null;
}
var argsLength = args.Length;
var invokeValues = new object[argsLength];
var parserValuesLength = Math.Min(argsLength, length);
for (var i = 0; i < parserValuesLength; i++)
{
invokeValues[i] = constParsers[i](core, in values[i], args[i], typeInfos[i]);
}
for (var i = parserValuesLength; i < argsLength; i++)
{
invokeValues[i] = typeInfos[i].DefaultValue;
}
if (argsLength > 0)
{
var lastTypeInfo = typeInfos[^1];
if (lastTypeInfo.IsParamArray)
{
if (length > requiredArgsCount)
{
var remainingLength = length - requiredArgsCount;
var remainingValues = lastTypeInfo.CreateArrayOfTypeExp(remainingLength);
var elementTypeInfo = lastTypeInfo.Element;
var elementConstParser = elementTypeInfo.ConstParser;
var elementType = lastTypeInfo.ElementType;
if (elementConstParser != null)
{
for (var i = 0; i < remainingLength; i++)
{
var elementObject = elementConstParser(core, in values[i + requiredArgsCount],
elementType, lastTypeInfo.Element);
var convertedElementObject = Convert.ChangeType(elementObject, elementType);
remainingValues.SetValue(convertedElementObject, i);
}
}
invokeValues[^1] = remainingValues;
}
else
{
var remainingValues = lastTypeInfo.EmptyArrayOfType;
invokeValues[^1] = remainingValues;
}
}
}
return invokeValues;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal object[] CalculateInvokeValues(object[] values)
{
var length = values.Length;
if (length < requiredArgsCount)
{
return null;
}
var argsLength = args.Length;
var invokeValues = new object[argsLength];
var parserValuesLength = Math.Min(argsLength, length);
for (var i = 0; i < parserValuesLength; i++)
{
invokeValues[i] = objectParsers[i](core, values[i], args[i], typeInfos[i]);
}
for (var i = parserValuesLength; i < argsLength; i++)
{
invokeValues[i] = typeInfos[i].DefaultValue;
}
if (argsLength > 0)
{
var lastTypeInfo = typeInfos[^1];
if (lastTypeInfo.IsParamArray)
{
if (length > requiredArgsCount)
{
var remainingLength = length - requiredArgsCount;
var remainingValues = lastTypeInfo.CreateArrayOfTypeExp(remainingLength);
var elementTypeInfo = lastTypeInfo.Element;
var elementConstParser = elementTypeInfo.ObjectParser;
var elementType = lastTypeInfo.ElementType;
if (elementConstParser != null)
{
for (var i = 0; i < remainingLength; i++)
{
var elementObject = elementConstParser(core, values[i + requiredArgsCount],
elementType, lastTypeInfo.Element);
var convertedElementObject = Convert.ChangeType(elementObject, elementType);
remainingValues.SetValue(convertedElementObject, i);
}
}
invokeValues[^1] = remainingValues;
}
else
{
var remainingValues = lastTypeInfo.EmptyArrayOfType;
invokeValues[^1] = remainingValues;
}
}
}
return invokeValues;
}
internal void Invoke(object[] invokeValues, out MValueConst resultMValue)
{
var result = @delegate.DynamicInvoke(invokeValues);
if (returnType == FunctionTypes.Void)
{
resultMValue = MValueConst.Nil;
return;
}
core.CreateMValue(out resultMValue, result);
}
internal async Task<MValueConst> InvokeAsync(object[] invokeValues)
{
if (returnType == FunctionTypes.Void)
{
@delegate.DynamicInvoke(invokeValues);
return MValueConst.Nil;
}
//TODO: fix async with result
var result = await (Task<object>) @delegate.DynamicInvoke(invokeValues);
if (returnType == FunctionTypes.Void) return MValueConst.Nil;
core.CreateMValue(out var mValueConst, result);
return mValueConst;
}
public void InvokeNoResult(object[] invokeValues)
{
@delegate.DynamicInvoke(invokeValues);
}
internal Task InvokeTaskOrNull(object[] invokeValues)
{
var result = @delegate.DynamicInvoke(invokeValues);
if (result is Task task)
{
return task;
}
return null;
}
internal IntPtr Call(IntPtr pointer, long size)
{
var currArgs = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, currArgs, 0, (int) size);
}
var mValues = new MValueConst[size];
for (var i = 0; i < size; i++)
{
mValues[i] = new MValueConst(core, currArgs[i]);
}
core.CreateMValue(out var resultMValue, Call(mValues));
if (resultMValue.nativePointer != IntPtr.Zero) return resultMValue.nativePointer;
core.CreateMValueNil(out resultMValue);
return resultMValue.nativePointer;
}
}
}