-
Notifications
You must be signed in to change notification settings - Fork 67
/
ModuleWrapper.cs
481 lines (400 loc) · 17.9 KB
/
ModuleWrapper.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
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
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using AltV.Net.CApi;
using AltV.Net.CApi.Exceptions;
using AltV.Net.Client.Elements.Data;
using AltV.Net.Client.Elements.Factories;
using AltV.Net.Client.Elements.Pools;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Shared;
using AltV.Net.Shared.Events;
namespace AltV.Net.Client
{
public class ModuleWrapper
{
private static Core _core;
private static IResource _resource;
private static IntPtr _resourcePointer;
private static IntPtr _corePointer;
private static string DllName;
public static void MainWithAssembly(Assembly resourceAssembly, IntPtr resourcePointer, IntPtr corePointer, string dllName, Dictionary<ulong, IntPtr> cApiFuncTable)
{
DllName = dllName;
var library = new Library(cApiFuncTable, true);
var logger = new Logger(library, corePointer);
Alt.Logger = logger;
if (library.Outdated)
{
Alt.LogWarning("Found mismatching SDK methods. Please update AltV.Net.Client NuGet.");
}
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");
}
}
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
_resourcePointer = resourcePointer;
_corePointer = corePointer;
var type = typeof(IResource);
var resource = resourceAssembly.GetTypes().FirstOrDefault(t => t.IsClass && !t.IsAbstract && type.IsAssignableFrom(t));
if (resource is null)
{
throw new Exception("Cannot find resource");
return;
}
_resource = (IResource) Activator.CreateInstance(resource)!;
Alt.Logger = _resource.GetLogger(library, corePointer);
var playerPool = new PlayerPool(_resource.GetPlayerFactory());
var vehiclePool = new VehiclePool(_resource.GetVehicleFactory());
var pedPool = new PedPool(_resource.GetPedFactory());
var blipPool = new BlipPool(_resource.GetBlipFactory());
var checkpointPool = new CheckpointPool(_resource.GetCheckpointFactory());
var colShapePool = new ColShapePool(_resource.GetColShapeFactory());
var audioPool = new AudioPool(_resource.GetAudioFactory());
var httpClientPool = new HttpClientPool(_resource.GetHttpClientFactory());
var webSocketClientPool = new WebSocketClientPool(_resource.GetWebSocketClientFactory());
var webViewPool = new WebViewPool(_resource.GetWebViewFactory());
var rmlDocumentPool = new RmlDocumentPool(new RmlDocumentFactory());
var rmlElementPool = new RmlElementPool(new RmlElementFactory());
var localObjectPool = new LocalObjectPool(_resource.GetLocalObjectFactory());
var objectPool = new ObjectPool(_resource.GetObjectFactory());
var virtualEntityPool = new VirtualEntityPool(_resource.GetVirtualEntityFactory());
var virtualEntityGroupPool = new VirtualEntityGroupPool(_resource.GetVirtualEntityGroupFactory());
var textLabelPool = new TextLabelPool(_resource.GetTextLabelFactory());
var nativeResourcePool = new NativeResourcePool(_resource.GetResourceFactory());
var localVehiclePool = new LocalVehiclePool(_resource.GetLocalVehicleFactory());
var localPedPool = new LocalPedPool(_resource.GetLocalPedFactory());
var audioFilterPool = new AudioFilterPool(_resource.GetAudioFilterFactory());
var audioOutputPool = new AudioOutputPool(_resource.GetAudioOutputFactory());
var AudioOutputAttachedPool = new AudioOutputAttachedPool(_resource.GetAudioOutputAttachedFactory());
var AudioOutputFrontendPool = new AudioOutputFrontendPool(_resource.GetAudioOutputFrontendFactory());
var AudioOutputWorldPool = new AudioOutputWorldPool(_resource.GetAudioOutputWorldFactory());
var fontPool = new FontPool(_resource.GetFontFactory());
var markerPool = new MarkerPool(_resource.GetMarkerFactory());
var baseBaseObjectPool = new PoolManager(playerPool, vehiclePool, pedPool,
blipPool, checkpointPool, audioPool,
httpClientPool, webSocketClientPool, webViewPool,
rmlElementPool, rmlDocumentPool, localObjectPool, objectPool,
virtualEntityPool, virtualEntityGroupPool,
textLabelPool, colShapePool, localVehiclePool,
localPedPool, audioFilterPool, audioOutputPool, AudioOutputFrontendPool, AudioOutputAttachedPool, AudioOutputWorldPool, fontPool, markerPool);
var timerPool = new TimerPool();
var natives = _resource.GetNatives(library);
var client = new Core(
library,
corePointer,
resourcePointer,
baseBaseObjectPool,
nativeResourcePool,
timerPool,
logger,
natives
);
_core = client;
Alt.CoreImpl = client;
AltShared.Core = client;
Alt.Log("Core initialized");
_core.GetAllPlayers();
_core.GetAllVehicles();
_core.GetAllBlips();
_core.GetAllCheckpoints();
_core.GetAllColShapes();
_core.GetAllMarkers();
_core.GetAllPeds();
_core.GetAllLocalObjects();
_core.GetAllLocalPeds();
_core.GetAllLocalVehicles();
_core.GetAllNetworkObjects();
_core.GetAllTextLabels();
_core.GetAllVirtualEntities();
_core.GetAllVirtualEntityGroups();
_core.GetAllWorldObjects();
playerPool.InitLocalPlayer(_core);
_core.Resource.CSharpResourceImpl.SetDelegates();
Alt.Log("Delegates set");
_resource.OnStart();
Alt.Log("Startup finished");
}
private static void OnUnhandledException(object _, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
Alt.LogError(e.IsTerminating ? "FATAL EXCEPTION:" : "UNHANDLED EXCEPTION:");
Alt.LogError(exception?.ToString());
_resource.OnUnhandledException(e);
if (!e.IsTerminating) return;
}
public static void OnStop()
{
AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
_resource.OnStop();
Alt.Log("Stopping timers");
foreach (var safeTimer in _core.RunningTimers.ToArray())
{
safeTimer.Stop();
safeTimer.Dispose();
}
_core.PoolManager.Dispose();
_core.Resource.CSharpResourceImpl.Dispose();
}
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.OnRemoveEntity(baseObject, BaseObjectType.Player);
}
else if (type == BaseObjectType.Vehicle)
{
_core.OnRemoveEntity(baseObject, BaseObjectType.Vehicle);
}
else if (type == BaseObjectType.Ped)
{
_core.OnRemoveEntity(baseObject, BaseObjectType.Ped);
}
_core.OnRemoveBaseObject(baseObject, type);
}
public static void OnTick()
{
_core.TimerPool.Tick(_core.Resource.Name);
_resource.OnTick();
_core.OnTick();
}
public static void OnPlayerSpawn()
{
_core.OnPlayerSpawn();
}
public static void OnPlayerDisconnect()
{
_core.OnPlayerDisconnect();
}
public static void OnPlayerEnterVehicle(IntPtr pointer, byte seat)
{
_core.OnPlayerEnterVehicle(pointer, seat);
}
public static void OnGameEntityCreate(IntPtr pointer, byte type)
{
_core.OnGameEntityCreate(pointer, type);
}
public static void OnGameEntityDestroy(IntPtr pointer, byte type)
{
_core.OnGameEntityDestroy(pointer, type);
}
public static void OnAnyResourceError(string name)
{
_core.OnAnyResourceError(name);
}
public static void OnAnyResourceStart(string name)
{
_core.OnAnyResourceStart(name);
}
public static void OnAnyResourceStop(string name)
{
_core.OnAnyResourceStop(name);
}
public static void OnKeyDown(uint key)
{
var consoleKey = (Key) key;
_core.OnKeyDown(consoleKey);
}
public static void OnKeyUp(uint key)
{
var consoleKey = (Key) key;
_core.OnKeyUp(consoleKey);
}
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 OnClientEvent(string name, IntPtr pointer, ulong size)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnClientEvent(name, args);
}
public static void OnWebViewEvent(IntPtr webView, string name, IntPtr pointer, ulong size)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnWebViewEvent(webView, name, args);
}
public static void OnWebSocketEvent(IntPtr webSocket, string name, IntPtr pointer, ulong size)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int) size);
}
_core.OnWebSocketEvent(webSocket, name, args);
}
public static void OnRmlElementEvent(IntPtr rmlElement, string name, IntPtr pointer)
{
_core.OnRmlElementEvent(rmlElement, name, pointer);
}
public static void OnConsoleCommand(string name,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)]
string[] args, int _)
{
args ??= Array.Empty<string>();
_core.OnConsoleCommand(name, args);
}
public static void OnGlobalMetaChange(string key, IntPtr value, IntPtr oldValue)
{
_core.OnGlobalMetaChange(key, value, oldValue);
}
public static void OnGlobalSyncedMetaChange(string key, IntPtr value, IntPtr oldValue)
{
_core.OnGlobalSyncedMetaChange(key, value, oldValue);
}
public static void OnConnectionComplete()
{
_core.OnConnectionComplete();
}
public static void OnPlayerChangeVehicleSeat(IntPtr vehicle, byte oldSeat, byte newSeat)
{
_core.OnPlayerChangeVehicleSeat(vehicle, oldSeat, newSeat);
}
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 OnPlayerWeaponShoot(uint weapon, ushort totalAmmo, ushort ammoInClip)
{
_core.OnPlayerWeaponShoot(weapon, totalAmmo, ammoInClip);
}
public static void OnPlayerWeaponChange(uint oldWeapon, uint newWeapon)
{
_core.OnPlayerWeaponChange(oldWeapon, newWeapon);
}
public static void OnWeaponDamage(IntPtr eventPointer, IntPtr entityPointer,
BaseObjectType entityType, uint weapon, ushort damage, Position shotOffset, BodyPart bodyPart,
IntPtr sourceEntityPointer, BaseObjectType sourceEntityType)
{
_core.OnWeaponDamage(eventPointer, entityPointer, entityType, weapon, damage, shotOffset, bodyPart, sourceEntityPointer, sourceEntityType);
}
public static void OnLocalMetaChange(string key, IntPtr value, IntPtr oldValue)
{
_core.OnLocalMetaChange(key, value, oldValue);
}
public static void OnStreamSyncedMetaChange(IntPtr target, BaseObjectType type, string key, IntPtr value, IntPtr oldValue)
{
_core.OnStreamSyncedMetaChange(target, type, key, value, oldValue);
}
public static void OnSyncedMetaChange(IntPtr target, BaseObjectType type, string key, IntPtr value, IntPtr oldValue)
{
_core.OnSyncedMetaChange(target, type, key, value, oldValue);
}
public static void OnTaskChange(int oldTask, int newTask)
{
_core.OnTaskChange(oldTask, newTask);
}
public static void OnWindowFocusChange(byte state)
{
_core.OnWindowFocusChange(state);
}
public static void OnWindowResolutionChange(Vector2 oldRes, Vector2 newRes)
{
_core.OnWindowResolutionChange(oldRes, newRes);
}
public static void OnNetOwnerChange(IntPtr target, BaseObjectType type, IntPtr newOwner, IntPtr oldOwner)
{
var playerPool = _core.PoolManager.Player.GetAllEntities().Select(x => x.PlayerNativePointer);
_core.OnNetOwnerChange(target, type, newOwner, oldOwner);
}
public static void OnWorldObjectPositionChange(IntPtr target, BaseObjectType type, Position position)
{
_core.OnWorldObjectPositionChange(target, type, position);
}
public static void OnPlayerLeaveVehicle(IntPtr vehicle, byte seat)
{
_core.OnPlayerLeaveVehicle(vehicle, seat);
}
public static void OnWorldObjectStreamIn(IntPtr target, BaseObjectType type)
{
_core.OnWorldObjectStreamIn(target, type);
}
public static void OnWorldObjectStreamOut(IntPtr target, BaseObjectType type)
{
_core.OnWorldObjectStreamOut(target, type);
}
public static void OnColShape(IntPtr colshapepointer, IntPtr targetentitypointer, BaseObjectType entitytype, byte state)
{
_core.OnColShape(colshapepointer, targetentitypointer, entitytype, state == 1);
}
public static void OnCheckpoint(IntPtr colshapepointer, IntPtr targetentitypointer, BaseObjectType entitytype, byte state)
{
_core.OnCheckpoint(colshapepointer, targetentitypointer, entitytype, state == 1);
}
public static void OnMetaChange(IntPtr target, BaseObjectType type, string key, IntPtr value, IntPtr oldvalue)
{
_core.OnMetaChange(target, type, key, value, oldvalue);
}
public static void OnPlayerStartEnterVehicle(IntPtr targetpointer, IntPtr player, byte seat)
{
_core.OnPlayerStartEnterVehicle(targetpointer, player, seat);
}
public static void OnPlayerStartLeaveVehicle(IntPtr targetpointer, IntPtr player, byte seat)
{
_core.OnPlayerStartLeaveVehicle(targetpointer, player, seat);
}
public static void OnEntityHitEntity(IntPtr targetpointer, BaseObjectType targettype, IntPtr damagerpointer, BaseObjectType damagertype, uint weaponhash)
{
_core.OnEntityHitEntity(targetpointer, targettype, damagerpointer, damagertype, weaponhash);
}
public static void OnPlayerBulletHit(uint weapon, IntPtr victimpointer, BaseObjectType victimtype, Position pos)
{
_core.OnPlayerBulletHit(weapon, victimpointer, victimtype, pos);
}
public static void OnVoiceConnection(VoiceConnectionState state)
{
_core.OnVoiceConnection(state);
}
public static void OnAudioEvent(IntPtr audio, string name, IntPtr pointer, ulong size)
{
var args = new IntPtr[size];
if (pointer != IntPtr.Zero)
{
Marshal.Copy(pointer, args, 0, (int)size);
}
_core.OnAudioEvent(audio, name, args);
}
public static void OnScriptRPCAnswer(ushort answerid, IntPtr answer, string answerError)
{
_core.OnScriptRPCAnswer(answerid, answer, answerError);
}
public static void OnScriptRPC(IntPtr eventpointer, 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, name, args, answerid);
}
}
}