-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathModuleWrapper.cs
More file actions
536 lines (443 loc) · 22.3 KB
/
ModuleWrapper.cs
File metadata and controls
536 lines (443 loc) · 22.3 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.CApi.Exceptions;
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_GetEventTypeSize() != (byte) EventType.SIZE)
{
throw new OutdatedSdkException("EventType", "Event type enum size doesn't match. Please, update the nuget");
}
if (library.Shared.Core_GetBaseObjectTypeSize() != (byte) BaseObjectType.Size)
{
throw new OutdatedSdkException("BaseObjectType", "BaseObject type enum size doesn't match. Please, update the nuget");
}
}
var playerFactory = _resource.GetPlayerFactory() ?? new PlayerFactory();
var vehicleFactory = _resource.GetVehicleFactory() ?? new VehicleFactory();
var pedFactory = _resource.GetPedFactory() ?? new PedFactory();
var objectFactory = _resource.GetObjectFactory() ?? new ObjectFactory();
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 virtualEntityFactory = _resource.GetVirtualEntityFactory() ?? new VirtualEntityFactory();
var virtualEntityGroupPoolFactory = _resource.GetVirtualEntityGroupFactory() ?? new VirtualEntityGroupFactory();
var markerPoolFactory = _resource.GetMarkerFactory() ?? new MarkerFactory();
var connectionInfoFactory = _resource.GetConnectionInfoFactory() ?? new ConnectionInfoFactory();
var playerPool = _resource.GetPlayerPool(playerFactory);
var vehiclePool = _resource.GetVehiclePool(vehicleFactory);
var pedPool = _resource.GetPedPool(pedFactory);
var objectPool = _resource.GetObjectPool(objectFactory);
var blipPool = _resource.GetBlipPool(blipFactory);
var checkpointPool = _resource.GetCheckpointPool(checkpointFactory);
var voiceChannelPool = _resource.GetVoiceChannelPool(voiceChannelFactory);
var colShapePool = _resource.GetColShapePool(colShapeFactory);
var virtualEntityPool = _resource.GetVirtualEntityPool(virtualEntityFactory);
var virtualEntityGroupPool = _resource.GetVirtualEntityGroupPool(virtualEntityGroupPoolFactory);
var markerPool = _resource.GetMarkerPool(markerPoolFactory);
var connectionInfoPool = _resource.GetConnectionInfoPool(connectionInfoFactory);
var nativeResourcePool = _resource.GetNativeResourcePool(nativeResourceFactory);
var baseObjectPool =
_resource.GetBaseBaseObjectPool(playerPool, vehiclePool, pedPool, objectPool, blipPool, checkpointPool, voiceChannelPool,
colShapePool, virtualEntityPool, virtualEntityGroupPool, markerPool, connectionInfoPool);
var core = _resource.GetCore(serverPointer, resourcePointer, assemblyLoadContext, library, baseObjectPool, nativeResourcePool);
_core = core;
Alt.CoreImpl = core;
AltShared.Core = core;
if (library.Outdated)
{
Alt.LogWarning("Found mismatching SDK methods. Please update AltV.Net NuGet");
}
core.GetAllPlayers();
core.GetAllVehicles();
core.GetAllPeds();
core.GetAllNetworkObjects();
core.GetAllColShapes();
core.GetAllMarkers();
core.GetAllBlips();
core.GetAllCheckpoints();
core.GetAllVirtualEntities();
core.GetAllVirtualEntityGroups();
core.GetAllConnectionInfos();
core.GetAllMetrics();
core.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} Sender: {Alt.Resource.Path} {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.PoolManager.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,
byte state)
{
_core.OnCheckpoint(checkpointPointer, entityPointer, baseObjectType, state == 1);
}
public static void OnPlayerConnect(IntPtr playerPointer, string reason)
{
_core.OnPlayerConnect(playerPointer, 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, uint weapon, ushort healthDamage, ushort armourDamage)
{
_core.OnPlayerDamage(playerPointer, attackerEntityPointer, attackerBaseObjectType,
weapon, healthDamage, armourDamage);
}
public static void OnPlayerDeath(IntPtr playerPointer, IntPtr killerEntityPointer,
BaseObjectType killerBaseObjectType, uint weapon)
{
_core.OnPlayerDeath(playerPointer, killerEntityPointer, killerBaseObjectType, weapon);
}
public static void OnPlayerHeal(IntPtr playerPointer, ushort oldHealth, ushort newHealth, ushort oldArmour, ushort newArmour)
{
_core.OnPlayerHeal(playerPointer, oldHealth, newHealth, oldArmour, newArmour);
}
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 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,
byte state)
{
_core.OnColShape(colShapePointer, targetEntityPointer, entityType, state == 1);
}
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 OnVehicleHorn(IntPtr eventPointer, IntPtr targetPointer, IntPtr reporterPointer, byte state)
{
_core.OnVehicleHorn(eventPointer, targetPointer, reporterPointer, state == 1);
}
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 eventPtr, IntPtr target, BaseObjectType targetType, IntPtr player)
{
_core.OnPlayerRequestControl(eventPtr, 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 OnPlayerDimensionChange(IntPtr player, int oldDimension, int newDimension)
{
_core.OnPlayerDimensionChange(player, oldDimension, newDimension);
}
public static void OnPlayerConnectDenied(PlayerConnectDeniedReason reason, string name, string ip, ulong passwordHash, byte isDebug, string branch, ushort versionMajor, ushort versionMinor, string cdnUrl, long discordId)
{
_core.OnPlayerConnectDenied(reason, name, ip, passwordHash, isDebug == 1, branch, versionMajor, versionMinor, cdnUrl,
discordId);
}
public static void OnVehicleSiren(IntPtr targetVehiclePointer, byte state)
{
_core.OnVehicleSiren(targetVehiclePointer, state == 1);
}
public static void OnPlayerSpawn(IntPtr playerPointer)
{
_core.OnPlayerSpawn(playerPointer);
}
public static void OnCreateBaseObject(IntPtr baseObject, BaseObjectType type, uint id)
{
_core.OnCreateBaseObject(baseObject, type, id);
}
public static void OnRemoveBaseObject(IntPtr baseObject, BaseObjectType type)
{
if (type == BaseObjectType.Player)
{
_core.OnPlayerRemove(baseObject);
}
else if (type == BaseObjectType.Vehicle)
{
_core.OnVehicleRemove(baseObject);
}
else if (type == BaseObjectType.Ped)
{
_core.OnPedRemove(baseObject);
}
_core.OnRemoveBaseObject(baseObject, type);
}
public static void OnRequestSyncedScene(IntPtr eventPointer, IntPtr source, int sceneid)
{
_core.OnRequestSyncedScene(eventPointer, source, sceneid);
}
public static void OnStartSyncedScene(IntPtr source, int sceneid, Position position, Rotation rotation, uint animDictHash, IntPtr[] entites, BaseObjectType[] types, uint[] animHashes, ulong size)
{
_core.OnStartSyncedScene(source, sceneid, position, rotation, animDictHash, entites, types, animHashes, size);
}
public static void OnStopSyncedScene(IntPtr source, int sceneid)
{
_core.OnStopSyncedScene(source, sceneid);
}
public static void OnUpdateSyncedScene(IntPtr source, float startRate, int sceneid)
{
_core.OnUpdateSyncedScene(source, startRate, sceneid);
}
public static void OnClientRequestObject(IntPtr eventPointer, IntPtr source, uint model, Position position)
{
_core.OnClientRequestObject(eventPointer, source, model, position);
}
public static void OnClientDeleteObject(IntPtr eventPointer, IntPtr source)
{
_core.OnClientDeleteObject(eventPointer, source);
}
public static void OnGivePedScriptedTask(IntPtr eventPointer, IntPtr source, IntPtr target, uint taskType)
{
_core.OnGivePedScriptedTask(eventPointer, source, target, taskType);
}
public static void OnPedDamage(IntPtr pedpointer, IntPtr attackerentitypointer, BaseObjectType attackerbaseobjecttype, uint weapon, ushort healthdamage, ushort armourdamage)
{
_core.OnPedDamage(pedpointer, attackerentitypointer, attackerbaseobjecttype, weapon, healthdamage, armourdamage);
}
public static void OnPedDeath(IntPtr pedpointer, IntPtr killerentitypointer, BaseObjectType killerbaseobjecttype, uint weapon)
{
_core.OnPedDeath(pedpointer, killerentitypointer, killerbaseobjecttype, weapon);
}
public static void OnPedHeal(IntPtr pedpointer, ushort oldhealth, ushort newhealth, ushort oldarmour, ushort newarmour)
{
_core.OnPedHeal(pedpointer, oldhealth, newhealth, oldarmour, newarmour);
}
public static void OnPlayerStartTalking(IntPtr playerpointer)
{
_core.OnPlayerStartTalking(playerpointer);
}
public static void OnPlayerStopTalking(IntPtr playerpointer)
{
_core.OnPlayerStopTalking(playerpointer);
}
public static void OnScriptRPC(IntPtr eventpointer, IntPtr targetpointer, string name, IntPtr pointer, ulong size, ushort answerId)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnScriptRPC(eventpointer, targetpointer, name, args, answerId);
}
public static void OnScriptRPCAnswer(IntPtr targetpointer, ushort answerid, IntPtr answer, string answererror)
{
_core.OnScriptAnswerRPC(targetpointer, answerid, answer, answererror);
}
}
}