forked from FabianTerhorst/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleWrapper.cs
More file actions
476 lines (391 loc) · 18.5 KB
/
ModuleWrapper.cs
File metadata and controls
476 lines (391 loc) · 18.5 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using AltV.Net.CApi;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Elements.Factories;
using AltV.Net.ResourceLoaders;
using AltV.Net.Shared;
using AltV.Net.Shared.Events;
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: InternalsVisibleTo("AltV.Net.Mock")]
namespace AltV.Net
{
internal static class ModuleWrapper
{
private static Core _core;
private static IResource _resource;
private static IScript[] _scripts;
private static IModule[] _modules;
private static void OnStartResource(IntPtr serverPointer, IntPtr resourcePointer, string resourceName,
string entryPoint)
{
_resource.OnStart();
}
//TODO: optimize assembly looping with single assembly loop otherwise module startup time is to slow for large projects,
//TODO: also can we reduce iterations by some assembies, e.g. only assemblies interested for us
//TODO: make optional resource startup time improvements for specifying IScript and IModules manually in IResource so module doesnt has to search them
public static void MainWithAssembly(IntPtr serverPointer, IntPtr resourcePointer,
AssemblyLoadContext assemblyLoadContext, Dictionary<ulong, IntPtr> cApiFuncTable)
{
var defaultResource = !AssemblyLoader.FindType(assemblyLoadContext.Assemblies, out _resource);
if (defaultResource)
{
var altVNetAsyncAssembly = assemblyLoadContext.LoadFromAssemblyName(new AssemblyName("AltV.Net.Async"));
try
{
foreach (var type in altVNetAsyncAssembly.GetTypes())
{
if (type.Name != "DefaultAsyncResource") continue;
var constructor =
type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
null,
Type.EmptyTypes, null);
_resource = (IResource) constructor?.Invoke(null);
break;
}
}
catch
{
// ignored
}
_resource ??= new DefaultResource();
}
//var entityFactories = AssemblyLoader.FindAllTypesWithAttribute<IEntityFactory<IEntity>, EntityFactoryAttribute>(assemblyLoadContext.Assemblies);
//var baseObjectFactories = AssemblyLoader.FindAllTypesWithAttribute<IBaseObjectFactory<IBaseObject>, EntityFactoryAttribute>(assemblyLoadContext.Assemblies);
//TODO: when resource has entity factories overwritten dont loop assemblies because of performance
//TODO: when not default resource and entity factory was found and none default resource has entity factory overwritten by not returning null throw exception
//TODO: check
//TODO: do the same with the pools
var library = _resource.GetLibrary() ?? new Library(cApiFuncTable, false);
unsafe
{
if (library.Shared.Core_GetEventEnumSize() != (byte) EventType.SIZE)
{
throw new Exception("Event type enum size doesn't match. Please, update the nuget");
}
}
var playerFactory = _resource.GetPlayerFactory() ?? new PlayerFactory();
var vehicleFactory = _resource.GetVehicleFactory() ?? new VehicleFactory();
var blipFactory = _resource.GetBlipFactory() ?? new BlipFactory();
var checkpointFactory = _resource.GetCheckpointFactory() ?? new CheckpointFactory();
var voiceChannelFactory = _resource.GetVoiceChannelFactory() ?? new VoiceChannelFactory();
var colShapeFactory = _resource.GetColShapeFactory() ?? new ColShapeFactory();
var nativeResourceFactory = _resource.GetNativeResourceFactory() ?? new NativeResourceFactory();
var playerPool = _resource.GetPlayerPool(playerFactory);
var vehiclePool = _resource.GetVehiclePool(vehicleFactory);
var blipPool = _resource.GetBlipPool(blipFactory);
var checkpointPool = _resource.GetCheckpointPool(checkpointFactory);
var voiceChannelPool = _resource.GetVoiceChannelPool(voiceChannelFactory);
var colShapePool = _resource.GetColShapePool(colShapeFactory);
var nativeResourcePool = _resource.GetNativeResourcePool(nativeResourceFactory);
var entityPool = _resource.GetBaseEntityPool(playerPool, vehiclePool);
var baseObjectPool =
_resource.GetBaseBaseObjectPool(playerPool, vehiclePool, blipPool, checkpointPool, voiceChannelPool,
colShapePool);
var server = _resource.GetCore(serverPointer, resourcePointer, assemblyLoadContext, library, baseObjectPool, entityPool,
playerPool, vehiclePool, blipPool, checkpointPool, voiceChannelPool, colShapePool, nativeResourcePool);
_core = server;
Alt.CoreImpl = server;
AltShared.Core = server;
foreach (var unused in server.GetPlayers())
{
}
foreach (var unused in server.GetVehicles())
{
}
server.Resource.CSharpResourceImpl.SetDelegates(OnStartResource);
_scripts = AssemblyLoader.FindAllTypes<IScript>(assemblyLoadContext.Assemblies);
foreach (var script in _scripts)
{
if (script.GetType().GetInterfaces().Contains(typeof(IResource)))
{
throw new InvalidOperationException(
"IScript must not be a IResource for type:" + script.GetType());
}
}
_core.OnScriptsLoaded(_scripts);
_modules = AssemblyLoader.FindAllTypes<IModule>(assemblyLoadContext.Assemblies);
_core.OnModulesLoaded(_modules);
foreach (var module in _modules)
{
module.OnScriptsStarted(_scripts);
}
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Alt.Log(
$"< ==== UNHANDLED EXCEPTION ==== > {Environment.NewLine} Received an unhandled exception from {sender?.GetType()}: " +
(Exception) e.ExceptionObject);
}
public static void OnStop()
{
_resource.OnStop();
foreach (var module in _modules)
{
module.OnStop();
}
_core.BlipPool.Dispose();
_core.CheckpointPool.Dispose();
_core.PlayerPool.Dispose();
_core.VehiclePool.Dispose();
_core.ColShapePool.Dispose();
_core.VoiceChannelPool.Dispose();
Alt.Core.Resource.CSharpResourceImpl.Dispose();
AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
_core.Dispose();
_modules = new IModule[0];
_scripts = new IScript[0];
_resource = null;
}
public static void OnTick()
{
_resource.OnTick();
}
public static void OnCheckpoint(IntPtr checkpointPointer, IntPtr entityPointer, BaseObjectType baseObjectType,
bool state)
{
_core.OnCheckpoint(checkpointPointer, entityPointer, baseObjectType, state);
}
public static void OnPlayerConnect(IntPtr playerPointer, ushort playerId, string reason)
{
_core.OnPlayerConnect(playerPointer, playerId, reason);
}
public static void OnPlayerBeforeConnect(IntPtr eventPointer, IntPtr connectionInfoPointer, string reason)
{
var connectionInfo = Marshal.PtrToStructure<PlayerConnectionInfo>(connectionInfoPointer);
_core.OnPlayerBeforeConnect(eventPointer, connectionInfo, reason);
}
public static void OnResourceStart(IntPtr resourcePointer)
{
_core.OnResourceStart(resourcePointer);
}
public static void OnResourceStop(IntPtr resourcePointer)
{
_core.OnResourceStop(resourcePointer);
}
public static void OnResourceError(IntPtr resourcePointer)
{
_core.OnResourceError(resourcePointer);
}
public static void OnPlayerDamage(IntPtr playerPointer, IntPtr attackerEntityPointer,
BaseObjectType attackerBaseObjectType,
ushort attackerEntityId, uint weapon, ushort healthDamage, ushort armourDamage)
{
_core.OnPlayerDamage(playerPointer, attackerEntityPointer, attackerBaseObjectType, attackerEntityId,
weapon, healthDamage, armourDamage);
}
public static void OnPlayerDeath(IntPtr playerPointer, IntPtr killerEntityPointer,
BaseObjectType killerBaseObjectType, uint weapon)
{
_core.OnPlayerDeath(playerPointer, killerEntityPointer, killerBaseObjectType, weapon);
}
public static void OnExplosion(IntPtr eventPointer, IntPtr playerPointer, ExplosionType explosionType,
Position position, uint explosionFx, IntPtr targetEntityPointer, BaseObjectType targetEntityType)
{
_core.OnExplosion(eventPointer, playerPointer, explosionType, position, explosionFx, targetEntityPointer, targetEntityType);
}
public static void OnWeaponDamage(IntPtr eventPointer, IntPtr playerPointer, IntPtr entityPointer,
BaseObjectType entityType, uint weapon, ushort damage, Position shotOffset, BodyPart bodyPart)
{
_core.OnWeaponDamage(eventPointer, playerPointer, entityPointer, entityType, weapon, damage, shotOffset, bodyPart);
}
public static void OnPlayerChangeVehicleSeat(IntPtr vehiclePointer, IntPtr playerPointer, byte oldSeat,
byte newSeat)
{
_core.OnPlayerChangeVehicleSeat(vehiclePointer, playerPointer, oldSeat, newSeat);
}
public static void OnPlayerEnterVehicle(IntPtr vehiclePointer, IntPtr playerPointer, byte seat)
{
_core.OnPlayerEnterVehicle(vehiclePointer, playerPointer, seat);
}
public static void OnPlayerEnteringVehicle(IntPtr vehiclePointer, IntPtr playerPointer, byte seat)
{
_core.OnPlayerEnteringVehicle(vehiclePointer, playerPointer, seat);
}
public static void OnPlayerLeaveVehicle(IntPtr vehiclePointer, IntPtr playerPointer, byte seat)
{
_core.OnPlayerLeaveVehicle(vehiclePointer, playerPointer, seat);
}
public static void OnPlayerDisconnect(IntPtr playerPointer, string reason)
{
_core.OnPlayerDisconnect(playerPointer, reason);
}
public static void OnClientEvent(IntPtr playerPointer, string name, IntPtr pointer, ulong size)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnClientEvent(playerPointer, name, args);
}
public static void OnServerEvent(string name, IntPtr pointer, ulong size)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnServerEvent(name, args);
}
public static void OnCreatePlayer(IntPtr playerPointer, ushort playerId)
{
_core.OnCreatePlayer(playerPointer, playerId);
}
public static void OnRemovePlayer(IntPtr playerPointer)
{
_core.OnRemovePlayer(playerPointer);
}
public static void OnCreateObject(IntPtr playerPointer, ushort playerId)
{
_core.OnCreateObject(playerPointer, playerId);
}
public static void OnRemoveObject(IntPtr playerPointer)
{
_core.OnRemoveObject(playerPointer);
}
public static void OnCreateVehicle(IntPtr vehiclePointer, ushort vehicleId)
{
_core.OnCreateVehicle(vehiclePointer, vehicleId);
}
public static void OnRemoveVehicle(IntPtr vehiclePointer)
{
_core.OnRemoveVehicle(vehiclePointer);
}
public static void OnCreateBlip(IntPtr blipPointer)
{
_core.OnCreateBlip(blipPointer);
}
public static void OnCreateVoiceChannel(IntPtr channelPointer)
{
_core.OnCreateVoiceChannel(channelPointer);
}
public static void OnCreateColShape(IntPtr colShapePointer)
{
_core.OnCreateColShape(colShapePointer);
}
public static void OnRemoveBlip(IntPtr blipPointer)
{
_core.OnRemoveBlip(blipPointer);
}
public static void OnCreateCheckpoint(IntPtr checkpointPointer)
{
_core.OnCreateCheckpoint(checkpointPointer);
}
public static void OnRemoveCheckpoint(IntPtr checkpointPointer)
{
_core.OnRemoveCheckpoint(checkpointPointer);
}
public static void OnRemoveVoiceChannel(IntPtr channelPointer)
{
_core.OnRemoveVoiceChannel(channelPointer);
}
public static void OnRemoveColShape(IntPtr colShapePointer)
{
_core.OnRemoveColShape(colShapePointer);
}
public static void OnPlayerRemove(IntPtr playerPointer)
{
_core.OnPlayerRemove(playerPointer);
}
public static void OnVehicleRemove(IntPtr vehiclePointer)
{
_core.OnVehicleRemove(vehiclePointer);
}
public static void OnConsoleCommand(string name,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
string[] args, int argsSize)
{
args ??= new string[0];
_core.OnConsoleCommand(name, args);
}
public static void OnMetaDataChange(IntPtr entityPointer, BaseObjectType entityType, string key,
IntPtr value)
{
_core.OnMetaDataChange(entityPointer, entityType, key, value);
}
public static void OnSyncedMetaDataChange(IntPtr entityPointer, BaseObjectType entityType, string key,
IntPtr value)
{
_core.OnSyncedMetaDataChange(entityPointer, entityType, key, value);
}
public static void OnColShape(IntPtr colShapePointer, IntPtr targetEntityPointer, BaseObjectType entityType,
bool state)
{
_core.OnColShape(colShapePointer, targetEntityPointer, entityType, state);
}
public static void OnVehicleDestroy(IntPtr vehiclePointer)
{
_core.OnVehicleDestroy(vehiclePointer);
}
public static void OnFire(IntPtr eventPointer, IntPtr playerPointer,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]
FireInfo[] fires, int length)
{
fires ??= new FireInfo[0];
_core.OnFire(eventPointer, playerPointer, fires);
}
public static void OnStartProjectile(IntPtr eventPointer, IntPtr sourcePlayerPointer, Position startPosition, Position direction, uint ammoHash, uint weaponHash)
{
_core.OnStartProjectile(eventPointer, sourcePlayerPointer, startPosition, direction, ammoHash, weaponHash);
}
public static void OnPlayerWeaponChange(IntPtr eventPointer, IntPtr targetPlayerPointer, uint oldWeapon, uint newWeapon)
{
_core.OnPlayerWeaponChange(eventPointer, targetPlayerPointer, oldWeapon, newWeapon);
}
public static void OnNetOwnerChange(IntPtr eventPointer, IntPtr targetEntityPointer, BaseObjectType targetEntityType, IntPtr oldNetOwnerPointer, IntPtr newNetOwnerPointer)
{
_core.OnNetOwnerChange(eventPointer, targetEntityPointer, targetEntityType, oldNetOwnerPointer, newNetOwnerPointer);
}
public static void OnVehicleAttach(IntPtr eventPointer, IntPtr targetPointer, IntPtr attachedPointer)
{
_core.OnVehicleAttach(eventPointer, targetPointer, attachedPointer);
}
public static void OnVehicleDetach(IntPtr eventPointer, IntPtr targetPointer, IntPtr detachedPointer)
{
_core.OnVehicleDetach(eventPointer, targetPointer, detachedPointer);
}
public static void OnVehicleDamage(IntPtr eventPointer, IntPtr vehiclePointer, IntPtr entityPointer, BaseObjectType entityType, uint bodyHealthDamage,
uint additionalBodyHealthDamage, uint engineHealthDamage, uint petrolTankDamage, uint weaponHash)
{
_core.OnVehicleDamage(eventPointer, vehiclePointer, entityPointer, entityType, bodyHealthDamage, additionalBodyHealthDamage,
engineHealthDamage, petrolTankDamage, weaponHash);
}
public static void OnConnectionQueueAdd(IntPtr connectionInfo)
{
_core.OnConnectionQueueAdd(connectionInfo);
}
public static void OnConnectionQueueRemove(IntPtr connectionInfo)
{
_core.OnConnectionQueueRemove(connectionInfo);
}
public static void OnServerStarted()
{
_core.OnServerStarted();
}
public static void OnPlayerRequestControl(IntPtr target, BaseObjectType targetType, IntPtr player)
{
_core.OnPlayerRequestControl(target, targetType, player);
}
public static void OnPlayerChangeAnimation(IntPtr player, uint oldDict, uint newDict, uint oldName, uint newName)
{
_core.OnPlayerChangeAnimation(player, oldDict, newDict, oldName, newName);
}
public static void OnPlayerChangeInterior(IntPtr player, uint oldIntLoc, uint newIntLoc)
{
_core.OnPlayerChangeInterior(player, oldIntLoc, newIntLoc);
}
public static void OnPlayerConnectDenied(PlayerConnectDeniedReason reason, string name, string ip, ulong passwordHash, bool isDebug, string branch, uint majorVersion, string cdnUrl, long discordId)
{
_core.onPlayerConnectDenied(reason, name, ip, passwordHash, isDebug, branch, majorVersion, cdnUrl,
discordId);
}
}
}