-
Notifications
You must be signed in to change notification settings - Fork 67
/
Core.cs
1244 lines (1101 loc) · 46.3 KB
/
Core.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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using AltV.Net.CApi;
using AltV.Net.Client.Elements;
using AltV.Net.Client.Elements.Data;
using AltV.Net.Client.Elements.Entities;
using AltV.Net.Client.Elements.Interfaces;
using AltV.Net.Client.Elements.Pools;
using AltV.Net.Data;
using AltV.Net.Elements.Args;
using AltV.Net.Elements.Entities;
using AltV.Net.Native;
using AltV.Net.Shared;
using AltV.Net.Shared.Enums;
using AltV.Net.Shared.Utils;
using WeaponData = AltV.Net.Client.Elements.Data.WeaponData;
namespace AltV.Net.Client
{
public partial class Core : SharedCore, ICore
{
public INativeResourcePool NativeResourcePool { get; }
public ITimerPool TimerPool { get; }
public override IPoolManager PoolManager { get; }
public override INativeResource Resource { get; }
public ILogger Logger { get; }
public INatives Natives { get; }
public LocalStorage LocalStorage { get; }
public Voice Voice { get; }
public Discord Discord { get; }
public FocusData FocusData { get; }
public List<SafeTimer> RunningTimers { get; } = new();
public Core(
ILibrary library,
IntPtr nativePointer,
IntPtr resourcePointer,
IPoolManager poolManager,
INativeResourcePool nativeResourcePool,
ITimerPool timerPool,
ILogger logger,
INatives natives
) : base(nativePointer, library)
{
Logger = logger;
PoolManager = poolManager;
NativeResourcePool = nativeResourcePool;
TimerPool = timerPool;
nativeResourcePool.GetOrCreate(this, resourcePointer, out var resource);
Resource = resource;
LocalStorage = new LocalStorage(this, GetLocalStoragePtr());
Voice = new Voice(this);
Discord = new Discord(this);
FocusData = new FocusData(this);
Natives = natives;
}
#region Log
private IntPtr GetLocalStoragePtr()
{
unsafe
{
return this.Library.Client.Resource_GetLocalStorage(this.Resource.NativePointer);
}
}
public new void LogInfo(string message) => Logger.LogInfo(message);
public new void LogWarning(string message) => Logger.LogWarning(message);
public new void LogError(string message) => Logger.LogError(message);
public new void LogDebug(string message) => Logger.LogDebug(message);
#endregion
public IReadOnlyCollection<IPlayer> GetAllPlayers()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetPlayers(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (IPlayer)PoolManager.GetOrCreate(this, e, BaseObjectType.Player)).ToArray();
Library.Shared.FreePlayerArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IVehicle> GetAllVehicles()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetVehicles(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (IVehicle)PoolManager.GetOrCreate(this, e, BaseObjectType.Vehicle)).ToArray();
Library.Shared.FreeVehicleArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IBlip> GetAllBlips()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetBlips(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (IBlip)PoolManager.GetOrCreate(this, e, BaseObjectType.Blip)).ToArray();
Library.Shared.FreeBlipArray(ptr);
return arr;
}
}
public IReadOnlyCollection<ICheckpoint> GetAllCheckpoints()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetCheckpoints(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (ICheckpoint)PoolManager.GetOrCreate(this, e, BaseObjectType.Checkpoint)).ToArray();
Library.Shared.FreeCheckpointArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IVirtualEntity> GetAllVirtualEntities()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetVirtualEntities(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (IVirtualEntity)PoolManager.GetOrCreate(this, e, BaseObjectType.VirtualEntity)).ToArray();
Library.Shared.FreeVirtualEntityArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IVirtualEntityGroup> GetAllVirtualEntityGroups()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetVirtualEntityGroups(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (IVirtualEntityGroup)PoolManager.GetOrCreate(this, e, BaseObjectType.VirtualEntityGroup)).ToArray();
Library.Shared.FreeVirtualEntityGroupArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IPed> GetAllPeds()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetPeds(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => (IPed)PoolManager.GetOrCreate(this, e, BaseObjectType.Ped)).ToArray();
Library.Shared.FreePedArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IObject> GetAllNetworkObjects()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetNetworkObjects(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int)size);
var arr = data.Select(e => PoolManager.Object.GetOrCreate(this, e)).ToArray();
Library.Shared.FreeNetworkObjectArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IColShape> GetAllColShapes()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetColShapes(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int)size);
var arr = data.Select(e => PoolManager.ColShape.GetOrCreate(this, e)).ToArray();
Library.Shared.FreeColShapeArray(ptr);
return arr;
}
}
public IReadOnlyCollection<IMarker> GetAllMarkers()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetMarkers(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int)size);
var arr = data.Select(e => PoolManager.Marker.GetOrCreate(this, e)).ToArray();
Library.Shared.FreeMarkerArray(ptr);
return arr;
}
}
public IReadOnlyCollection<ITextLabel> GetAllTextLabels()
{
unsafe
{
CheckIfCallIsValid();
ulong size = 0;
var ptr = Library.Shared.Core_GetMarkers(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int)size);
var arr = data.Select(e => PoolManager.TextLabel.GetOrCreate(this, e)).ToArray();
Library.Shared.FreeTextLabelArray(ptr);
return arr;
}
}
public IntPtr CreateMarkerPtr(out uint id, MarkerType type, Position pos, Rgba color, bool useStreaming, uint streamingDistance)
{
unsafe
{
uint pId = default;
var markerPoint = Library.Client.Core_CreateMarker_Client(NativePointer, (byte)type, pos, color, useStreaming ? (byte)1:(byte)0, streamingDistance, Resource.NativePointer, &pId);
id = pId;
return markerPoint;
}
}
public new IBaseObject GetBaseObjectById(BaseObjectType type, uint id)
{
return (IBaseObject) base.GetBaseObjectById(type, id);
}
public HandlingData? GetHandlingByModelHash(uint modelHash)
{
return new HandlingData(this, modelHash);
}
public AudioCategory GetAudioCategoryByName(string audioCategory)
{
return new AudioCategory(this, audioCategory);
}
public WeaponData? GetWeaponDataByWeaponHash(uint weaponHash)
{
return new WeaponData(this, weaponHash);
}
public DiscordUser? GetDiscordUser()
{
unsafe
{
var ptr = Library.Client.Core_GetDiscordUser(NativePointer);
if (ptr == IntPtr.Zero) return null;
var structure = Marshal.PtrToStructure<DiscordUser>(ptr);
Library.Client.Core_DeallocDiscordUser(ptr);
return structure;
}
}
#region Create
public IntPtr CreatePointBlipPtr(out uint id, Position position)
{
unsafe
{
uint pId = default;
var pointBlip = Library.Client.Core_Client_CreatePointBlip(NativePointer, position, Resource.NativePointer, &pId);
id = pId;
return pointBlip;
}
}
public IBlip CreatePointBlip(Position position)
{
var ptr = CreatePointBlipPtr(out var id, position);
if (ptr == IntPtr.Zero) return null;
return PoolManager.Blip.Create(this, ptr, id);
}
public IntPtr CreateRadiusBlipPtr(out uint id, Position position, float radius)
{
unsafe
{
uint pId = default;
var radiusBlip = Library.Client.Core_Client_CreateRadiusBlip(NativePointer, position, radius, Resource.NativePointer, &pId);
id = pId;
return radiusBlip;
}
}
public IBlip CreateRadiusBlip(Position position, float radius)
{
var ptr = CreateRadiusBlipPtr(out var id, position, radius);
if (ptr == IntPtr.Zero) return null;
return PoolManager.Blip.Create(this, ptr, id);
}
public IntPtr CreateAreaBlipPtr(out uint id, Position position, int width, int height)
{
unsafe
{
uint pId = default;
var ariaBlip = Library.Client.Core_Client_CreateAreaBlip(NativePointer, position, width, height, Resource.NativePointer, &pId);
id = pId;
return ariaBlip;
}
}
public IBlip CreateAreaBlip(Position position, int width, int height)
{
var ptr = CreateAreaBlipPtr(out var id, position, width, height);
if (ptr == IntPtr.Zero) return null;
return PoolManager.Blip.Create(this, ptr, id);
}
public IntPtr CreateWebViewPtr(out uint id, string url, bool isOverlay = false, Vector2? pos = null, Vector2? size = null)
{
pos ??= Vector2.Zero;
size ??= Vector2.Zero;
unsafe
{
uint pId = default;
var urlPtr = MemoryUtils.StringToHGlobalUtf8(url);
var ptr = Library.Client.Core_CreateWebView(NativePointer, Resource.NativePointer, urlPtr, pos.Value, size.Value, (byte) (isOverlay ? 1 : 0), &pId);
id = pId;
Marshal.FreeHGlobal(urlPtr);
return ptr;
}
}
public IWebView CreateWebView(string url, bool isOverlay = false, Vector2? pos = null, Vector2? size = null)
{
var ptr = CreateWebViewPtr(out var id, url, isOverlay, pos, size);
if (ptr == IntPtr.Zero) return null;
return PoolManager.WebView.Create(this, ptr, id);
}
public IntPtr CreateWebViewPtr(out uint id, string url, uint propHash, string targetTexture)
{
unsafe
{
uint pId = default;
var urlPtr = MemoryUtils.StringToHGlobalUtf8(url);
var targetTexturePtr = MemoryUtils.StringToHGlobalUtf8(targetTexture);
var ptr = Library.Client.Core_CreateWebView3D(NativePointer, Resource.NativePointer, urlPtr, propHash, targetTexturePtr, &pId);
id = pId;
Marshal.FreeHGlobal(urlPtr);
Marshal.FreeHGlobal(targetTexturePtr);
return ptr;
}
}
public IWebView CreateWebView(string url, uint propHash, string targetTexture)
{
var ptr = CreateWebViewPtr(out var id, url, propHash, targetTexture);
if (ptr == IntPtr.Zero) return null;
return PoolManager.WebView.Create(this, ptr, id);
}
public IntPtr CreateRmlDocumentPtr(out uint id, string url)
{
unsafe
{
uint pId = default;
var urlPtr = MemoryUtils.StringToHGlobalUtf8(url);
var ptr = Library.Client.Core_CreateRmlDocument(NativePointer, Resource.NativePointer, urlPtr, &pId);
id = pId;
Marshal.FreeHGlobal(urlPtr);
return ptr;
}
}
public IRmlDocument CreateRmlDocument(string url)
{
var ptr = CreateRmlDocumentPtr(out var id, url);
if (ptr == IntPtr.Zero) return null;
return PoolManager.RmlDocument.Create(this, ptr, id);
}
public IntPtr CreateCheckpointPtr(out uint id, CheckpointType type, Vector3 pos, Vector3 nextPos, float radius, float height, Rgba color, Rgba iconColor, uint streamingDistance)
{
unsafe
{
uint pId = default;
var checkPoint = Library.Client.Core_CreateCheckpoint(NativePointer, (byte) type, pos, nextPos, radius, height, color, iconColor, streamingDistance, Resource.NativePointer, &pId);
id = pId;
return checkPoint;
}
}
public ICheckpoint CreateCheckpoint(CheckpointType type, Vector3 pos, Vector3 nextPos, float radius, float height, Rgba color, Rgba iconColor, uint streamingDistance)
{
var ptr = CreateCheckpointPtr(out var id, type, pos, nextPos, radius, height, color, iconColor, streamingDistance);
if (ptr == IntPtr.Zero) return null;
return PoolManager.Checkpoint.Create(this, ptr, id);
}
public IntPtr CreateAudioPtr(out uint id, string source, float volume, bool isRadio = false, bool clearCache = true, string basePath = "")
{
unsafe
{
uint pId = default;
var sourcePtr = MemoryUtils.StringToHGlobalUtf8(source);
var basePathPtr = MemoryUtils.StringToHGlobalUtf8(basePath);
var ptr = Library.Client.Core_CreateAudio(NativePointer, sourcePtr, volume, isRadio ? (byte)1 : (byte)0, clearCache ? (byte)1 : (byte)0, basePathPtr, Resource.NativePointer, &pId);
id = pId;
Marshal.FreeHGlobal(basePathPtr);
Marshal.FreeHGlobal(sourcePtr);
return ptr;
}
}
public IAudio CreateAudio(string source, float volume, bool isRadio = false, bool clearCache = true, string basePath = "")
{
var ptr = CreateAudioPtr(out var id, source, volume, isRadio, clearCache, basePath);
if (ptr == IntPtr.Zero) return null;
return PoolManager.Audio.Create(this, ptr, id);
}
public IntPtr CreateAudioFilterPtr(out uint id, uint hash)
{
unsafe
{
uint pId = default;
var ptr = Library.Client.Core_CreateAudioFilter(NativePointer, hash, Resource.NativePointer, &pId);
id = pId;
return ptr;
}
}
public IntPtr CreateFrontendOutputPtr(out uint id, uint categoryHash)
{
unsafe
{
uint pId = default;
var ptr = Library.Client.Core_CreateFrontendOutput(NativePointer, categoryHash, Resource.NativePointer, &pId);
id = pId;
return ptr;
}
}
public IntPtr CreateWorldOutputPtr(out uint id, uint categoryHash, Position pos)
{
unsafe
{
uint pId = default;
var ptr = Library.Client.Core_CreateWorldOutput(NativePointer, categoryHash, pos, Resource.NativePointer, &pId);
id = pId;
return ptr;
}
}
public IntPtr CreateAttachedOutputPtr(out uint id, uint categoryHash, IWorldObject worldObject)
{
unsafe
{
uint pId = default;
var ptr = Library.Client.Core_CreateAttachedOutput(NativePointer, categoryHash, worldObject.WorldObjectNativePointer, Resource.NativePointer, &pId);
id = pId;
return ptr;
}
}
public IAudioFilter CreateAudioFilter(uint hash)
{
var ptr = CreateAudioFilterPtr(out var id, hash);
if (ptr == IntPtr.Zero) return null;
return PoolManager.AudioFilter.Create(this, ptr, id);
}
public IAudioOutputFrontend CreateAudioOutputFrontend(uint categoryHash)
{
var ptr = CreateFrontendOutputPtr(out var id, categoryHash);
if (ptr == IntPtr.Zero) return null;
return PoolManager.AudioOutputFrontend.Create(this, ptr, id);
}
public IAudioOutputWorld CreateAudioOutputWorld(uint categoryHash, Position pos)
{
var ptr = CreateWorldOutputPtr(out var id, categoryHash, pos);
if (ptr == IntPtr.Zero) return null;
return PoolManager.AudioOutputWorld.Create(this, ptr, id);
}
public IAudioOutputAttached CreateAudioOutputAttached(uint categoryHash, IWorldObject worldObject)
{
var ptr = CreateAttachedOutputPtr(out var id, categoryHash, worldObject);
if (ptr == IntPtr.Zero) return null;
return PoolManager.AudioOutputAttached.Create(this, ptr, id);
}
public WeaponData[] GetAllWeaponData()
{
unsafe
{
CheckIfCallIsValid();
var weaponDataCount = Library.Client.Core_GetAllWeaponDataCount(NativePointer);
var weaponHashes = new uint[weaponDataCount];
Library.Client.Core_GetAllWeaponData(NativePointer, weaponHashes, weaponDataCount);
var weaponDatas = new WeaponData[weaponDataCount];
for (ulong i = 0; i < weaponDataCount; i++)
{
var weaponHash = weaponHashes[i];
weaponDatas[i] = new WeaponData(this, weaponHash);
}
return weaponDatas;
}
}
public IntPtr CreateLocalObjectPtr(out uint id, uint modelHash, Position position, Rotation rotation, bool noOffset = false,
bool dynamic = false, bool useStreaming = false, uint streamingDistance = 0)
{
unsafe
{
uint pId = default;
var ptr = Library.Client.Core_CreateLocalObject(NativePointer, modelHash, position, rotation, (byte) (noOffset ? 1 : 0), (byte) (dynamic ? 1 : 0), (byte) (useStreaming ? 1 : 0), streamingDistance, Resource.NativePointer, &pId);
id = pId;
return ptr;
}
}
public ILocalObject CreateLocalObject(uint modelHash, Position position, Rotation rotation, bool noOffset = false, bool dynamic = false, bool useStreaming = false, uint streamingDistance = 0)
{
var ptr = CreateLocalObjectPtr(out var id, modelHash, position, rotation, noOffset, dynamic, useStreaming, streamingDistance);
if (ptr == IntPtr.Zero) return null;
return PoolManager.LocalObject.Create(this, ptr, id);
}
public IntPtr CreateHttpClientPtr(out uint id)
{
unsafe
{
uint pId = default;
var httpClient = Library.Client.Core_CreateHttpClient(NativePointer, Resource.NativePointer, &pId);
id = pId;
return httpClient;
}
}
public IHttpClient CreateHttpClient()
{
var ptr = CreateHttpClientPtr(out var id);
if (ptr == IntPtr.Zero) return null;
return PoolManager.HttpClient.Create(this, ptr, id);
}
public IntPtr CreateWebSocketClientPtr(out uint id, string url)
{
unsafe
{
uint pId = default;
var urlPtr = MemoryUtils.StringToHGlobalUtf8(url);
var ptr = Library.Client.Core_CreateWebsocketClient(NativePointer, Resource.NativePointer, urlPtr,
&pId);
id = pId;
Marshal.FreeHGlobal(urlPtr);
return ptr;
}
}
public IWebSocketClient CreateWebSocketClient(string url)
{
var ptr = CreateWebSocketClientPtr(out var id, url);
if (ptr == IntPtr.Zero) return null;
return PoolManager.WebSocketClient.Create(this, ptr, id);
}
public IntPtr CreateColShapeCirclePtr(out uint id, Position position, float radius)
{
unsafe
{
uint pId = default;
var colShapeCircle = Library.Shared.Core_CreateColShapeCircle(NativePointer, position, radius, &pId);
id = pId;
return colShapeCircle;
}
}
public IColShape CreateColShapeCircle(Position position, float radius)
{
var ptr = CreateColShapeCirclePtr(out var id, position, radius);
if (ptr == IntPtr.Zero) return null;
return PoolManager.ColShape.Create(this, ptr, id);
}
public IntPtr CreateColShapeCubePtr(out uint id, Position pos1, Position pos2)
{
unsafe
{
uint pId = default;
var colShapeCube = Library.Shared.Core_CreateColShapeCube(NativePointer, pos1, pos2, &pId);
id = pId;
return colShapeCube;
}
}
public IColShape CreateColShapeCube(Position pos1, Position pos2)
{
var ptr = CreateColShapeCubePtr(out var id, pos1, pos2);
if (ptr == IntPtr.Zero) return null;
return PoolManager.ColShape.Create(this, ptr, id);
}
public IntPtr CreateColShapeCylinderPtr(out uint id, Position position, float radius, float height)
{
unsafe
{
uint pId = default;
var colShapeCylinder = Library.Shared.Core_CreateColShapeCylinder(NativePointer, position, radius, height, &pId);
id = pId;
return colShapeCylinder;
}
}
public IColShape CreateColShapeCylinder(Position position, float radius, float height)
{
var ptr = CreateColShapeCylinderPtr(out var id, position, radius, height);
if (ptr == IntPtr.Zero) return null;
return PoolManager.ColShape.Create(this, ptr, id);
}
public IntPtr CreateColShapePolygonPtr(out uint id, float minZ, float maxZ, Vector2[] points)
{
unsafe
{
uint pId = default;
var colShapePolygon = Library.Shared.Core_CreateColShapePolygon(NativePointer, minZ, maxZ, points, points.Length, &pId);
id = pId;
return colShapePolygon;
}
}
public IColShape CreateColShapePolygon(float minZ, float maxZ, Vector2[] points)
{
var ptr = CreateColShapePolygonPtr(out var id, minZ, maxZ, points);
if (ptr == IntPtr.Zero) return null;
return PoolManager.ColShape.Create(this, ptr, id);
}
public IntPtr CreateColShapeRectanglePtr(out uint id, float x1, float y1, float x2, float y2, float z)
{
unsafe
{
uint pId = default;
var colShapeRectangle = Library.Shared.Core_CreateColShapeRectangle(NativePointer, x1, y1, x2, y2, z, &pId);
id = pId;
return colShapeRectangle;
}
}
public IColShape CreateColShapeRectangle(float x1, float y1, float x2, float y2, float z)
{
var ptr = CreateColShapeRectanglePtr(out var id, x1, y1, x2, y2, z);
if (ptr == IntPtr.Zero) return null;
return PoolManager.ColShape.Create(this, ptr, id);
}
public IntPtr CreateColShapeSpherePtr(out uint id, Vector3 position, float radius)
{
unsafe
{
uint pId = default;
var colShapeSphere = Library.Shared.Core_CreateColShapeSphere(NativePointer, position, radius, &pId);
id = pId;
return colShapeSphere;
}
}
public IColShape CreateColShapeSphere(Vector3 position, float radius)
{
var ptr = CreateColShapeSpherePtr(out var id, position, radius);
if (ptr == IntPtr.Zero) return null;
return PoolManager.ColShape.Create(this, ptr, id);
}
public void UpdateClipContext(Dictionary<string, string> context)
{
unsafe
{
var data = new Dictionary<IntPtr, IntPtr>();
var keys = new IntPtr[context.Count];
var values = new IntPtr[context.Count];
for (var i = 0; i < context.Count; i++)
{
var keyptr = MemoryUtils.StringToHGlobalUtf8(context.ElementAt(i).Key);
var valueptr = MemoryUtils.StringToHGlobalUtf8(context.ElementAt(i).Value);
keys[i] = keyptr;
values[i] = valueptr;
data.Add(keyptr, valueptr);
}
Library.Client.Core_UpdateClipContext(NativePointer, keys, values,(uint)data.Count);
foreach (var dataValue in data)
{
Marshal.FreeHGlobal(dataValue.Key);
Marshal.FreeHGlobal(dataValue.Value);
}
}
}
public ulong ServerTime
{
get
{
unsafe
{
return this.Library.Client.Core_GetServerTime(NativePointer);
}
}
}
#endregion
#region TriggerServerEvent
public void TriggerServerEvent(string eventName, MValueConst[] args)
{
var eventNamePtr = MemoryUtils.StringToHGlobalUtf8(eventName);
TriggerServerEvent(eventNamePtr, args);
Marshal.FreeHGlobal(eventNamePtr);
}
public void TriggerServerEvent(IntPtr eventNamePtr, MValueConst[] args)
{
var size = args.Length;
var mValuePointers = new IntPtr[size];
for (var i = 0; i < size; i++)
{
mValuePointers[i] = args[i].nativePointer;
}
TriggerServerEvent(eventNamePtr, mValuePointers);
}
public void TriggerServerEvent(string eventName, IntPtr[] args)
{
var eventNamePtr = MemoryUtils.StringToHGlobalUtf8(eventName);
TriggerServerEvent(eventNamePtr, args);
Marshal.FreeHGlobal(eventNamePtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TriggerServerEvent(IntPtr eventNamePtr, IntPtr[] args)
{
unsafe
{
Library.Client.Core_TriggerServerEvent(NativePointer, eventNamePtr, args, args.Length);
}
}
public void TriggerServerEvent(IntPtr eventNamePtr, params object[] args)
{
if (args == null) throw new ArgumentException("Arguments array should not be null.");
var size = args.Length;
var mValues = new MValueConst[size];
CreateMValues(mValues, args);
TriggerServerEvent(eventNamePtr, mValues);
for (var i = 0; i < size; i++)
{
mValues[i].Dispose();
}
}
public void TriggerServerEvent(string eventName, params object[] args)
{
if (args == null) throw new ArgumentException("Arguments array should not be null.");
var size = args.Length;
var mValues = new MValueConst[size];
CreateMValues(mValues, args);
TriggerServerEvent(eventName, mValues);
for (var i = 0; i < size; i++)
{
mValues[i].Dispose();
}
}
public ushort TriggerServerRPCEvent(string name, params object[] args)
{
if (args == null) throw new ArgumentException("Arguments array should not be null.");
var size = args.Length;
var mValues = new MValueConst[size];
CreateMValues(mValues, args);
var result = TriggerServerRPCEvent(name, mValues);
for (var i = 0; i < size; i++)
{
mValues[i].Dispose();
}
return result;
}
public ushort TriggerServerRPCEvent(string eventName, MValueConst[] args)
{
var eventNamePtr = MemoryUtils.StringToHGlobalUtf8(eventName);
var result = TriggerServerRPCEvent(eventNamePtr, args);
Marshal.FreeHGlobal(eventNamePtr);
return result;
}
public ushort TriggerServerRPCEvent(IntPtr eventNamePtr, MValueConst[] args)
{
var size = args.Length;
var mValuePointers = new IntPtr[size];
for (var i = 0; i < size; i++)
{
mValuePointers[i] = args[i].nativePointer;
}
return TriggerServerRPCEvent(eventNamePtr, mValuePointers);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort TriggerServerRPCEvent(IntPtr eventNamePtr, IntPtr[] args)
{
unsafe
{
return Library.Client.Core_TriggerServerRPCEvent(NativePointer, eventNamePtr, args, args.Length);
}
}
public void TriggerServerRPCAnswer(ushort answerId, object answer, string error)
{
CreateMValue(out var mValue, answer);
TriggerServerRPCAnswer(answerId, mValue, error);
mValue.Dispose();
}
public void TriggerServerRPCAnswer(ushort answerId, MValueConst answer, string error)
{
unsafe
{
var errorPtr = MemoryUtils.StringToHGlobalUtf8(error);
Library.Client.Core_TriggerServerRPCAnswer(NativePointer, answerId, answer.nativePointer, errorPtr);
Marshal.FreeHGlobal(errorPtr);
}
if (UnansweredClientRpcRequest.Contains(answerId))
{
UnansweredClientRpcRequest.Remove(answerId);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort TriggerServerRPCAnswerEvent(IntPtr eventNamePtr, IntPtr[] args)
{
unsafe
{
return Library.Client.Core_TriggerServerRPCEvent(NativePointer, eventNamePtr, args, args.Length);
}
}
public void TriggerServerEventUnreliable(string eventName, MValueConst[] args)
{
var eventNamePtr = MemoryUtils.StringToHGlobalUtf8(eventName);
TriggerServerEventUnreliable(eventNamePtr, args);
Marshal.FreeHGlobal(eventNamePtr);
}
public void TriggerServerEventUnreliable(IntPtr eventNamePtr, MValueConst[] args)
{
var size = args.Length;
var mValuePointers = new IntPtr[size];
for (var i = 0; i < size; i++)
{
mValuePointers[i] = args[i].nativePointer;
}
TriggerServerEventUnreliable(eventNamePtr, mValuePointers);
}
public void TriggerServerEventUnreliable(string eventName, IntPtr[] args)
{
var eventNamePtr = MemoryUtils.StringToHGlobalUtf8(eventName);
TriggerServerEventUnreliable(eventNamePtr, args);
Marshal.FreeHGlobal(eventNamePtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TriggerServerEventUnreliable(IntPtr eventNamePtr, IntPtr[] args)
{
unsafe
{
Library.Client.Core_TriggerServerEventUnreliable(NativePointer, eventNamePtr, args, args.Length);
}
}
public void TriggerServerEventUnreliable(IntPtr eventNamePtr, params object[] args)
{
if (args == null) throw new ArgumentException("Arguments array should not be null.");
var size = args.Length;
var mValues = new MValueConst[size];
CreateMValues(mValues, args);
TriggerServerEventUnreliable(eventNamePtr, mValues);
for (var i = 0; i < size; i++)
{
mValues[i].Dispose();
}
}
public void TriggerServerEventUnreliable(string eventName, params object[] args)
{
if (args == null) throw new ArgumentException("Arguments array should not be null.");
var size = args.Length;
var mValues = new MValueConst[size];
CreateMValues(mValues, args);
TriggerServerEventUnreliable(eventName, mValues);
for (var i = 0; i < size; i++)
{
mValues[i].Dispose();
}
}
#endregion
public INativeResource GetResource(string name)
{
unsafe
{
var namePtr = MemoryUtils.StringToHGlobalUtf8(name);
var ptr = Library.Shared.Core_GetResource(NativePointer, namePtr);
Marshal.FreeHGlobal(namePtr);
NativeResourcePool.GetOrCreate(this, ptr, out var resource);
return resource;
}
}
public bool HasResource(string name)
{
unsafe
{
var namePtr = MemoryUtils.StringToHGlobalUtf8(name);
var ptr = Library.Shared.Core_GetResource(NativePointer, namePtr);
Marshal.FreeHGlobal(namePtr);
return ptr != IntPtr.Zero;
}
}
public INativeResource[] GetAllResources()
{
unsafe
{
uint size = 0;
var ptr = Library.Shared.Core_GetAllResources(NativePointer, &size);
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => NativeResourcePool.GetOrCreate(this, e, out var v) ? v : null).ToArray();
Library.Shared.FreeResourceArray(ptr);
return arr;
}
}
public string[] MarshalStringArrayPtrAndFree(IntPtr ptr, uint size)
{
unsafe
{
var data = new IntPtr[size];
Marshal.Copy(ptr, data, 0, (int) size);
var arr = data.Select(e => Marshal.PtrToStringUTF8(e)).ToArray();
this.Library.Shared.FreeStringArray(ptr, size);
return arr;
}
}
public uint SetTimeout(Action action, uint duration, [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
{
return TimerPool.Add(new ModuleTimer(action, duration, true, filePath, lineNumber));
}