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
511 lines (414 loc) · 15.9 KB
/
ModuleWrapper.cs
File metadata and controls
511 lines (414 loc) · 15.9 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
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using AltV.Net.CApi;
using AltV.Net.Client.Elements.Data;
using AltV.Net.Client.Elements.Factories;
using AltV.Net.Client.Elements.Pools;
using AltV.Net.Client.Extensions;
using AltV.Net.Client.WinApi;
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;
Alt.Log("Library initialized");
unsafe
{
if (library.Shared.Core_GetEventEnumSize() != (byte) EventType.SIZE)
{
throw new Exception("Event 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.Log("Resource created");
Alt.Logger = _resource.GetLogger(library, corePointer);
var playerPool = new PlayerPool(_resource.GetPlayerFactory());
Alt.Log("Player pool created");
var vehiclePool = new VehiclePool(_resource.GetVehicleFactory());
Alt.Log("Vehicle pool created");
var blipPool = new BlipPool(_resource.GetBlipFactory());
Alt.Log("Blip pool created");
var checkpointPool = new CheckpointPool(_resource.GetCheckpointFactory());
Alt.Log("Checkpoint pool created");
var audioPool = new AudioPool(_resource.GetAudioFactory());
Alt.Log("Audio pool created");
var httpClientPool = new HttpClientPool(_resource.GetHttpClientFactory());
Alt.Log("Http client pool created");
var webSocketClientPool = new WebSocketClientPool(_resource.GetWebSocketClientFactory());
Alt.Log("WebSocket client pool created");
var webViewPool = new WebViewPool(_resource.GetWebViewFactory());
Alt.Log("Webview pool created");
var rmlDocumentPool = new RmlDocumentPool(new RmlDocumentFactory());
var rmlElementPool = new RmlElementPool(new RmlElementFactory());
Alt.Log("Rml pools created");
var objectPool = new ObjectPool(_resource.GetObjectFactory());
Alt.Log("Object pool created");
var nativeResourcePool = new NativeResourcePool(_resource.GetResourceFactory());
Alt.Log("Native resource pool created");
var baseBaseObjectPool = new BaseBaseObjectPool(playerPool, vehiclePool, blipPool, checkpointPool, audioPool, httpClientPool, webSocketClientPool, webViewPool, rmlElementPool, rmlDocumentPool, objectPool);
var baseEntityPool = new BaseEntityPool(playerPool, vehiclePool);
var timerPool = new TimerPool();
var natives = _resource.GetNatives(library);
var client = new Core(
library,
corePointer,
resourcePointer,
playerPool,
vehiclePool,
blipPool,
checkpointPool,
audioPool,
httpClientPool,
webSocketClientPool,
webViewPool,
rmlDocumentPool,
rmlElementPool,
objectPool,
baseBaseObjectPool,
baseEntityPool,
nativeResourcePool,
timerPool,
logger,
natives
);
_core = client;
Alt.CoreImpl = client;
AltShared.Core = client;
Alt.Log("Core initialized");
_core.GetPlayers();
_core.GetVehicles();
_core.GetBlips();
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());
if (!e.IsTerminating) return;
if (_core is null)
{
Alt.LogError("Cannot show error dialog because core is not initialized yet");
return;
}
if (_resource is null)
{
Alt.LogError("Cannot show error dialog because resources is not initialized yet");
return;
}
var options = _resource.OnUnhandledException(e);
if (options == null)
{
Alt.LogInfo("Unhandled exception handler was null, skipping error dialog");
return;
}
try
{
var dialog = new ErrorDialog(_core, options, exception);
dialog.Show();
}
catch (Exception executionException)
{
Alt.LogError("Failed to show error dialog: " + executionException);
}
}
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.PlayerPool.Dispose();
_core.VehiclePool.Dispose();
_core.BlipPool.Dispose();
_core.AudioPool.Dispose();
_core.CheckpointPool.Dispose();
_core.HttpClientPool.Dispose();
_core.WebSocketClientPool.Dispose();
_core.WebViewPool.Dispose();
_core.RmlElementPool.Dispose();
_core.RmlDocumentPool.Dispose();
_core.Resource.CSharpResourceImpl.Dispose();
}
public static void OnCreatePlayer(IntPtr pointer, ushort id)
{
_core.OnCreatePlayer(pointer, id);
}
public static void OnRemovePlayer(IntPtr pointer)
{
_core.OnRemovePlayer(pointer);
}
public static void OnCreateObject(IntPtr pointer, ushort id)
{
_core.OnCreateObject(pointer, id);
}
public static void OnRemoveObject(IntPtr pointer)
{
_core.OnRemoveObject(pointer);
}
public static void OnCreateVehicle(IntPtr pointer, ushort id)
{
_core.OnCreateVehicle(pointer, id);
}
public static void OnRemoveVehicle(IntPtr pointer)
{
_core.OnRemoveVehicle(pointer);
}
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 OnRmlElementEvent(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.OnRmlElementEvent(webView, name, args);
}
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 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.PlayerPool.GetAllEntities().Select(x => x.PlayerNativePointer);
_core.OnNetOwnerChange(target, type, newOwner, oldOwner);
}
public static void OnRemoveEntity(IntPtr target, BaseObjectType type)
{
_core.OnRemoveEntity(target, type);
}
public static void OnPlayerLeaveVehicle(IntPtr vehicle, byte seat)
{
_core.OnPlayerLeaveVehicle(vehicle, seat);
}
public static void OnBlipCreate(IntPtr blipPointer)
{
_core.OnBlipCreate(blipPointer);
}
public static void OnWebViewCreate(IntPtr webView)
{
_core.OnWebViewCreate(webView);
}
public static void OnCheckpointCreate(IntPtr checkpoint)
{
_core.OnCheckpointCreate(checkpoint);
}
public static void OnWebSocketClientCreate(IntPtr webSocket)
{
_core.OnWebSocketClientCreate(webSocket);
}
public static void OnHttpClientCreate(IntPtr httpClient)
{
_core.OnHttpClientCreate(httpClient);
}
public static void OnAudioCreate(IntPtr audio)
{
_core.OnAudioCreate(audio);
}
public static void OnRmlElementCreate(IntPtr element)
{
_core.OnRmlElementCreate(element);
}
public static void OnRmlDocumentCreate(IntPtr document)
{
_core.OnRmlDocumentCreate(document);
}
public static void OnBlipRemove(IntPtr blipPointer)
{
_core.OnBlipRemove(blipPointer);
}
public static void OnWebViewRemove(IntPtr webView)
{
_core.OnWebViewRemove(webView);
}
public static void OnCheckpointRemove(IntPtr checkpoint)
{
_core.OnCheckpointRemove(checkpoint);
}
public static void OnWebSocketClientRemove(IntPtr webSocket)
{
_core.OnWebSocketClientRemove(webSocket);
}
public static void OnHttpClientRemove(IntPtr httpClient)
{
_core.OnHttpClientRemove(httpClient);
}
public static void OnAudioRemove(IntPtr audio)
{
_core.OnAudioRemove(audio);
}
public static void OnRmlElementRemove(IntPtr element)
{
_core.OnRmlElementRemove(element);
}
public static void OnRmlDocumentRemove(IntPtr document)
{
_core.OnRmlDocumentRemove(document);
}
}
}