Skip to content

Commit 4101d31

Browse files
committed
update(natives): Make methods sync
1 parent c3811d2 commit 4101d31

20 files changed

+124
-31
lines changed

managed/src/SwiftlyS2.Generated/Natives/Commands.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ internal static class NativeCommands {
1717
/// 1 -> not silent, 2 -> silent, -1 -> invalid player, 0 -> no command
1818
/// </summary>
1919
public unsafe static int HandleCommandForPlayer(int playerid, string command) {
20+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
21+
throw new InvalidOperationException("This method can only be called from the main thread.");
22+
}
2023
var pool = ArrayPool<byte>.Shared;
2124
var commandLength = Encoding.UTF8.GetByteCount(command);
2225
var commandBuffer = pool.Rent(commandLength + 1);

managed/src/SwiftlyS2.Generated/Natives/Convars.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal static class NativeConvars {
1414
private unsafe static delegate* unmanaged<int, byte*, void> _QueryClientConvar;
1515

1616
public unsafe static void QueryClientConvar(int playerid, string cvarName) {
17+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
18+
throw new InvalidOperationException("This method can only be called from the main thread.");
19+
}
1720
var pool = ArrayPool<byte>.Shared;
1821
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
1922
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);
@@ -458,6 +461,9 @@ public unsafe static int GetConvarType(string cvarName) {
458461
private unsafe static delegate* unmanaged<int, byte*, byte*, void> _SetClientConvarValueString;
459462

460463
public unsafe static void SetClientConvarValueString(int playerid, string cvarName, string defaultValue) {
464+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
465+
throw new InvalidOperationException("This method can only be called from the main thread.");
466+
}
461467
var pool = ArrayPool<byte>.Shared;
462468
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
463469
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);
@@ -494,6 +500,9 @@ public unsafe static ulong GetFlags(string cvarName) {
494500
private unsafe static delegate* unmanaged<byte*, ulong, void> _SetFlags;
495501

496502
public unsafe static void SetFlags(string cvarName, ulong flags) {
503+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
504+
throw new InvalidOperationException("This method can only be called from the main thread.");
505+
}
497506
var pool = ArrayPool<byte>.Shared;
498507
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
499508
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);
@@ -568,6 +577,9 @@ public unsafe static nint GetDefaultValuePtr(string cvarName) {
568577
private unsafe static delegate* unmanaged<byte*, nint, void> _SetDefaultValue;
569578

570579
public unsafe static void SetDefaultValue(string cvarName, nint defaultValue) {
580+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
581+
throw new InvalidOperationException("This method can only be called from the main thread.");
582+
}
571583
var pool = ArrayPool<byte>.Shared;
572584
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
573585
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);
@@ -582,6 +594,9 @@ public unsafe static void SetDefaultValue(string cvarName, nint defaultValue) {
582594
private unsafe static delegate* unmanaged<byte*, byte*, void> _SetDefaultValueString;
583595

584596
public unsafe static void SetDefaultValueString(string cvarName, string defaultValue) {
597+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
598+
throw new InvalidOperationException("This method can only be called from the main thread.");
599+
}
585600
var pool = ArrayPool<byte>.Shared;
586601
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
587602
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);
@@ -618,6 +633,9 @@ public unsafe static nint GetValuePtr(string cvarName) {
618633
private unsafe static delegate* unmanaged<byte*, nint, void> _SetValuePtr;
619634

620635
public unsafe static void SetValuePtr(string cvarName, nint value) {
636+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
637+
throw new InvalidOperationException("This method can only be called from the main thread.");
638+
}
621639
var pool = ArrayPool<byte>.Shared;
622640
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
623641
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);
@@ -632,6 +650,9 @@ public unsafe static void SetValuePtr(string cvarName, nint value) {
632650
private unsafe static delegate* unmanaged<byte*, nint, void> _SetValueInternalPtr;
633651

634652
public unsafe static void SetValueInternalPtr(string cvarName, nint value) {
653+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
654+
throw new InvalidOperationException("This method can only be called from the main thread.");
655+
}
635656
var pool = ArrayPool<byte>.Shared;
636657
var cvarNameLength = Encoding.UTF8.GetByteCount(cvarName);
637658
var cvarNameBuffer = pool.Rent(cvarNameLength + 1);

managed/src/SwiftlyS2.Generated/Natives/EngineHelpers.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public unsafe static bool IsMapValid(string map_name) {
4646
private unsafe static delegate* unmanaged<byte*, void> _ExecuteCommand;
4747

4848
public unsafe static void ExecuteCommand(string command) {
49+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
50+
throw new InvalidOperationException("This method can only be called from the main thread.");
51+
}
4952
var pool = ArrayPool<byte>.Shared;
5053
var commandLength = Encoding.UTF8.GetByteCount(command);
5154
var commandBuffer = pool.Rent(commandLength + 1);

managed/src/SwiftlyS2.Generated/Natives/EntitySystem.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ internal static class NativeEntitySystem {
1414
private unsafe static delegate* unmanaged<nint, nint, void> _Spawn;
1515

1616
public unsafe static void Spawn(nint entity, nint keyvalues) {
17+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
18+
throw new InvalidOperationException("This method can only be called from the main thread.");
19+
}
1720
_Spawn(entity, keyvalues);
1821
}
1922

2023
private unsafe static delegate* unmanaged<nint, void> _Despawn;
2124

2225
public unsafe static void Despawn(nint entity) {
26+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
27+
throw new InvalidOperationException("This method can only be called from the main thread.");
28+
}
2329
_Despawn(entity);
2430
}
2531

@@ -41,6 +47,9 @@ public unsafe static nint CreateEntityByName(string name) {
4147
private unsafe static delegate* unmanaged<nint, byte*, nint, nint, nint, int, void> _AcceptInput;
4248

4349
public unsafe static void AcceptInput(nint entity, string input, nint activator, nint caller, nint value, int outputID) {
50+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
51+
throw new InvalidOperationException("This method can only be called from the main thread.");
52+
}
4453
var pool = ArrayPool<byte>.Shared;
4554
var inputLength = Encoding.UTF8.GetByteCount(input);
4655
var inputBuffer = pool.Rent(inputLength + 1);
@@ -55,6 +64,9 @@ public unsafe static void AcceptInput(nint entity, string input, nint activator,
5564
private unsafe static delegate* unmanaged<nint, byte*, nint, nint, nint, float, void> _AddEntityIOEvent;
5665

5766
public unsafe static void AddEntityIOEvent(nint entity, string input, nint activator, nint caller, nint value, float delay) {
67+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
68+
throw new InvalidOperationException("This method can only be called from the main thread.");
69+
}
5870
var pool = ArrayPool<byte>.Shared;
5971
var inputLength = Encoding.UTF8.GetByteCount(input);
6072
var inputBuffer = pool.Rent(inputLength + 1);

managed/src/SwiftlyS2.Generated/Natives/FileSystem.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public unsafe static string GetSearchPath(string pathId, int searchPathType, int
3535
private unsafe static delegate* unmanaged<byte*, byte*, int, int, void> _AddSearchPath;
3636

3737
public unsafe static void AddSearchPath(string path, string pathId, int searchPathAdd, int searchPathPriority) {
38+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
39+
throw new InvalidOperationException("This method can only be called from the main thread.");
40+
}
3841
var pool = ArrayPool<byte>.Shared;
3942
var pathLength = Encoding.UTF8.GetByteCount(path);
4043
var pathBuffer = pool.Rent(pathLength + 1);
@@ -156,6 +159,9 @@ public unsafe static string ReadFile(string fileName, string pathId) {
156159
private unsafe static delegate* unmanaged<byte*, byte*, byte*, byte> _WriteFile;
157160

158161
public unsafe static bool WriteFile(string fileName, string pathId, string content) {
162+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
163+
throw new InvalidOperationException("This method can only be called from the main thread.");
164+
}
159165
var pool = ArrayPool<byte>.Shared;
160166
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
161167
var fileNameBuffer = pool.Rent(fileNameLength + 1);
@@ -251,6 +257,9 @@ public unsafe static bool IsFileWritable(string fileName, string pathId) {
251257
private unsafe static delegate* unmanaged<byte*, byte*, byte, byte> _SetFileWritable;
252258

253259
public unsafe static bool SetFileWritable(string fileName, string pathId, bool writable) {
260+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
261+
throw new InvalidOperationException("This method can only be called from the main thread.");
262+
}
254263
var pool = ArrayPool<byte>.Shared;
255264
var fileNameLength = Encoding.UTF8.GetByteCount(fileName);
256265
var fileNameBuffer = pool.Rent(fileNameLength + 1);

managed/src/SwiftlyS2.Generated/Natives/GameEvents.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,18 @@ public unsafe static void FreeEvent(nint _event) {
465465
private unsafe static delegate* unmanaged<nint, byte, void> _FireEvent;
466466

467467
public unsafe static void FireEvent(nint _event, bool dontBroadcast) {
468+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
469+
throw new InvalidOperationException("This method can only be called from the main thread.");
470+
}
468471
_FireEvent(_event, dontBroadcast ? (byte)1 : (byte)0);
469472
}
470473

471474
private unsafe static delegate* unmanaged<nint, int, void> _FireEventToClient;
472475

473476
public unsafe static void FireEventToClient(nint _event, int playerid) {
477+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
478+
throw new InvalidOperationException("This method can only be called from the main thread.");
479+
}
474480
_FireEventToClient(_event, playerid);
475481
}
476482

managed/src/SwiftlyS2.Generated/Natives/NetMessages.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,9 @@ public unsafe static void ClearRepeatedField(nint netmsg, string fieldName) {
11231123
private unsafe static delegate* unmanaged<nint, int, int, void> _SendMessage;
11241124

11251125
public unsafe static void SendMessage(nint netmsg, int msgid, int playerid) {
1126+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
1127+
throw new InvalidOperationException("This method can only be called from the main thread.");
1128+
}
11261129
_SendMessage(netmsg, msgid, playerid);
11271130
}
11281131

@@ -1132,6 +1135,9 @@ public unsafe static void SendMessage(nint netmsg, int msgid, int playerid) {
11321135
/// each bit in player_mask represents a playerid
11331136
/// </summary>
11341137
public unsafe static void SendMessageToPlayers(nint netmsg, int msgid, ulong playermask) {
1138+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
1139+
throw new InvalidOperationException("This method can only be called from the main thread.");
1140+
}
11351141
_SendMessageToPlayers(netmsg, msgid, playermask);
11361142
}
11371143

managed/src/SwiftlyS2.Generated/Natives/Player.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal static class NativePlayer {
1414
private unsafe static delegate* unmanaged<int, int, byte*, int, void> _SendMessage;
1515

1616
public unsafe static void SendMessage(int playerid, int kind, string message, int htmlDuration) {
17+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
18+
throw new InvalidOperationException("This method can only be called from the main thread.");
19+
}
1720
var pool = ArrayPool<byte>.Shared;
1821
var messageLength = Encoding.UTF8.GetByteCount(message);
1922
var messageBuffer = pool.Rent(messageLength + 1);
@@ -91,6 +94,9 @@ public unsafe static ulong GetPressedButtons(int playerid) {
9194
private unsafe static delegate* unmanaged<int, byte*, void> _PerformCommand;
9295

9396
public unsafe static void PerformCommand(int playerid, string command) {
97+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
98+
throw new InvalidOperationException("This method can only be called from the main thread.");
99+
}
94100
var pool = ArrayPool<byte>.Shared;
95101
var commandLength = Encoding.UTF8.GetByteCount(command);
96102
var commandBuffer = pool.Rent(commandLength + 1);
@@ -119,6 +125,9 @@ public unsafe static string GetIPAddress(int playerid) {
119125
private unsafe static delegate* unmanaged<int, byte*, int, void> _Kick;
120126

121127
public unsafe static void Kick(int playerid, string reason, int gamereason) {
128+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
129+
throw new InvalidOperationException("This method can only be called from the main thread.");
130+
}
122131
var pool = ArrayPool<byte>.Shared;
123132
var reasonLength = Encoding.UTF8.GetByteCount(reason);
124133
var reasonBuffer = pool.Rent(reasonLength + 1);
@@ -152,24 +161,36 @@ public unsafe static void ClearTransmitEntityBlocked(int playerid) {
152161
private unsafe static delegate* unmanaged<int, int, void> _ChangeTeam;
153162

154163
public unsafe static void ChangeTeam(int playerid, int newteam) {
164+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
165+
throw new InvalidOperationException("This method can only be called from the main thread.");
166+
}
155167
_ChangeTeam(playerid, newteam);
156168
}
157169

158170
private unsafe static delegate* unmanaged<int, int, void> _SwitchTeam;
159171

160172
public unsafe static void SwitchTeam(int playerid, int newteam) {
173+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
174+
throw new InvalidOperationException("This method can only be called from the main thread.");
175+
}
161176
_SwitchTeam(playerid, newteam);
162177
}
163178

164179
private unsafe static delegate* unmanaged<int, nint, void> _TakeDamage;
165180

166181
public unsafe static void TakeDamage(int playerid, nint dmginfo) {
182+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
183+
throw new InvalidOperationException("This method can only be called from the main thread.");
184+
}
167185
_TakeDamage(playerid, dmginfo);
168186
}
169187

170188
private unsafe static delegate* unmanaged<int, Vector, QAngle, Vector, void> _Teleport;
171189

172190
public unsafe static void Teleport(int playerid, Vector pos, QAngle angle, Vector velocity) {
191+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
192+
throw new InvalidOperationException("This method can only be called from the main thread.");
193+
}
173194
_Teleport(playerid, pos, angle, velocity);
174195
}
175196

@@ -217,6 +238,9 @@ public unsafe static bool HasMenuShown(int playerid) {
217238
private unsafe static delegate* unmanaged<int, byte*, void> _ExecuteCommand;
218239

219240
public unsafe static void ExecuteCommand(int playerid, string command) {
241+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
242+
throw new InvalidOperationException("This method can only be called from the main thread.");
243+
}
220244
var pool = ArrayPool<byte>.Shared;
221245
var commandLength = Encoding.UTF8.GetByteCount(command);
222246
var commandBuffer = pool.Rent(commandLength + 1);

managed/src/SwiftlyS2.Generated/Natives/PlayerManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public unsafe static int GetPlayerCap() {
3535
private unsafe static delegate* unmanaged<int, byte*, int, void> _SendMessage;
3636

3737
public unsafe static void SendMessage(int kind, string message, int duration) {
38+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
39+
throw new InvalidOperationException("This method can only be called from the main thread.");
40+
}
3841
var pool = ArrayPool<byte>.Shared;
3942
var messageLength = Encoding.UTF8.GetByteCount(message);
4043
var messageBuffer = pool.Rent(messageLength + 1);

managed/src/SwiftlyS2.Generated/Natives/Schema.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ internal static class NativeSchema {
1414
private unsafe static delegate* unmanaged<nint, ulong, void> _SetStateChanged;
1515

1616
public unsafe static void SetStateChanged(nint entity, ulong hash) {
17+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
18+
throw new InvalidOperationException("This method can only be called from the main thread.");
19+
}
1720
_SetStateChanged(entity, hash);
1821
}
1922

@@ -79,6 +82,9 @@ public unsafe static nint GetPropPtr(nint entity, ulong hash) {
7982
private unsafe static delegate* unmanaged<nint, ulong, nint, uint, void> _WritePropPtr;
8083

8184
public unsafe static void WritePropPtr(nint entity, ulong hash, nint value, uint size) {
85+
if (Thread.CurrentThread.ManagedThreadId != _MainThreadID) {
86+
throw new InvalidOperationException("This method can only be called from the main thread.");
87+
}
8288
_WritePropPtr(entity, hash, value, size);
8389
}
8490

0 commit comments

Comments
 (0)