Skip to content

Commit 53a6c0a

Browse files
committed
Merge branch 'beta' of https://github.com/swiftly-solution/swiftlys2 into beta
2 parents e875b38 + 55128cb commit 53a6c0a

File tree

5 files changed

+230
-96
lines changed

5 files changed

+230
-96
lines changed

managed/src/SwiftlyS2.Core/Modules/Menus/MenuManagerAPI.cs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private void KeyStateChange( IOnClientKeyStateChangedEvent @event )
145145
if (menu.Configuration.PlaySound)
146146
{
147147
scrollSound.Recipients.AddRecipient(@event.PlayerId);
148-
scrollSound.Emit();
148+
_ = scrollSound.Emit();
149149
scrollSound.Recipients.RemoveRecipient(@event.PlayerId);
150150
}
151151
}
@@ -156,18 +156,18 @@ private void KeyStateChange( IOnClientKeyStateChangedEvent @event )
156156
if (menu.Configuration.PlaySound)
157157
{
158158
scrollSound.Recipients.AddRecipient(@event.PlayerId);
159-
scrollSound.Emit();
159+
_ = scrollSound.Emit();
160160
scrollSound.Recipients.RemoveRecipient(@event.PlayerId);
161161
}
162162
}
163163
else if (exitKey.HasFlag(@event.Key.ToKeyBind()))
164164
{
165-
CloseMenuForPlayer(player, menu);
165+
CloseMenuForPlayerInternal(player, menu, true);
166166

167167
if (menu.Configuration.PlaySound)
168168
{
169169
exitSound.Recipients.AddRecipient(@event.PlayerId);
170-
exitSound.Emit();
170+
_ = exitSound.Emit();
171171
exitSound.Recipients.RemoveRecipient(@event.PlayerId);
172172
}
173173
}
@@ -181,7 +181,7 @@ private void KeyStateChange( IOnClientKeyStateChangedEvent @event )
181181
if (menu.Configuration.PlaySound && option.PlaySound)
182182
{
183183
useSound.Recipients.AddRecipient(@event.PlayerId);
184-
useSound.Emit();
184+
_ = useSound.Emit();
185185
useSound.Recipients.RemoveRecipient(@event.PlayerId);
186186
}
187187
}
@@ -196,7 +196,7 @@ private void KeyStateChange( IOnClientKeyStateChangedEvent @event )
196196
if (menu.Configuration.PlaySound)
197197
{
198198
scrollSound.Recipients.AddRecipient(@event.PlayerId);
199-
scrollSound.Emit();
199+
_ = scrollSound.Emit();
200200
scrollSound.Recipients.RemoveRecipient(@event.PlayerId);
201201
}
202202
}
@@ -207,17 +207,17 @@ private void KeyStateChange( IOnClientKeyStateChangedEvent @event )
207207
if (menu.Configuration.PlaySound)
208208
{
209209
scrollSound.Recipients.AddRecipient(@event.PlayerId);
210-
scrollSound.Emit();
210+
_ = scrollSound.Emit();
211211
scrollSound.Recipients.RemoveRecipient(@event.PlayerId);
212212
}
213213
}
214214
else if (KeyBind.A.HasFlag(@event.Key.ToKeyBind()))
215215
{
216-
CloseMenuForPlayer(player, menu);
216+
CloseMenuForPlayerInternal(player, menu, true);
217217
if (menu.Configuration.PlaySound)
218218
{
219219
exitSound.Recipients.AddRecipient(@event.PlayerId);
220-
exitSound.Emit();
220+
_ = exitSound.Emit();
221221
exitSound.Recipients.RemoveRecipient(@event.PlayerId);
222222
}
223223
}
@@ -231,7 +231,7 @@ private void KeyStateChange( IOnClientKeyStateChangedEvent @event )
231231
if (menu.Configuration.PlaySound && option.PlaySound)
232232
{
233233
useSound.Recipients.AddRecipient(@event.PlayerId);
234-
useSound.Emit();
234+
_ = useSound.Emit();
235235
useSound.Recipients.RemoveRecipient(@event.PlayerId);
236236
}
237237
}
@@ -247,7 +247,7 @@ private void OnClientDisconnected( IOnClientDisconnectedEvent @event )
247247
openMenus
248248
.Where(kvp => kvp.Key == player)
249249
.ToList()
250-
.ForEach(kvp => CloseMenuForPlayer(player, kvp.Value));
250+
.ForEach(kvp => CloseMenuForPlayerInternal(player, kvp.Value, true));
251251
}
252252
}
253253

@@ -322,7 +322,7 @@ public void OpenMenuForPlayer( IPlayer player, IMenuAPI menu )
322322
if (menu.Parent.ParentMenu == currentMenu)
323323
{
324324
// We are transitioning from the current menu to one of its submenus.
325-
// To show the submenu, we first need to close the current (parent) menu, see CloseMenuForPlayer.
325+
// To show the submenu, we first need to close the current (parent) menu.
326326
// The parent menu may have an onClosed callback registered in onClosedCallbacks.
327327
// If we do not remove that callback temporarily, closing the parent menu here
328328
// would incorrectly invoke the callback even though the user is only navigating
@@ -333,12 +333,12 @@ public void OpenMenuForPlayer( IPlayer player, IMenuAPI menu )
333333
// 3. Re-register the callback so it will only be invoked later, when the
334334
// logical end of the menu flow is reached and the menu is truly closed.
335335
_ = onClosedCallbacks.TryRemove((player, currentMenu), out var callback);
336-
CloseMenuForPlayer(player, currentMenu);
336+
CloseMenuForPlayerInternal(player, currentMenu, false);
337337
_ = onClosedCallbacks.AddOrUpdate((player, currentMenu), callback, ( _, _ ) => callback);
338338
}
339339
else
340340
{
341-
CloseMenuForPlayer(player, currentMenu);
341+
CloseMenuForPlayerInternal(player, currentMenu, false);
342342
}
343343
}
344344

@@ -358,15 +358,36 @@ public void CloseMenu( IMenuAPI menu )
358358
Core.PlayerManager
359359
.GetAllPlayers()
360360
.ToList()
361-
.ForEach(player => CloseMenuForPlayer(player, menu));
361+
.ForEach(player => CloseMenuForPlayerInternal(player, menu, true));
362362
}
363363

364364
public void CloseMenuForPlayer( IPlayer player, IMenuAPI menu )
365+
{
366+
CloseMenuForPlayerInternal(player, menu, true);
367+
}
368+
369+
public void CloseAllMenus()
370+
{
371+
openMenus.ToList().ForEach(kvp =>
372+
{
373+
var currentMenu = kvp.Value;
374+
while (currentMenu != null)
375+
{
376+
currentMenu.HideForPlayer(kvp.Key);
377+
MenuClosed?.Invoke(this, new MenuManagerEventArgs { Player = kvp.Key, Menu = currentMenu });
378+
currentMenu = currentMenu.Parent.ParentMenu;
379+
}
380+
_ = openMenus.TryRemove(kvp.Key, out _);
381+
});
382+
}
383+
384+
private void CloseMenuForPlayerInternal( IPlayer player, IMenuAPI menu, bool reopenParent )
365385
{
366386
if (!openMenus.TryGetValue(player, out var currentMenu) || currentMenu != menu)
367387
{
368388
return;
369389
}
390+
370391
if (onClosedCallbacks.TryRemove((player, menu), out var onClosed) && onClosed != null)
371392
{
372393
onClosed(player, menu);
@@ -377,25 +398,10 @@ public void CloseMenuForPlayer( IPlayer player, IMenuAPI menu )
377398
menu.HideForPlayer(player);
378399
MenuClosed?.Invoke(this, new MenuManagerEventArgs { Player = player, Menu = menu });
379400

380-
if (menu.Parent.ParentMenu != null)
401+
if (reopenParent && menu.Parent.ParentMenu != null)
381402
{
382403
OpenMenuForPlayer(player, menu.Parent.ParentMenu);
383404
}
384405
}
385406
}
386-
387-
public void CloseAllMenus()
388-
{
389-
openMenus.ToList().ForEach(kvp =>
390-
{
391-
var currentMenu = kvp.Value;
392-
while (currentMenu != null)
393-
{
394-
currentMenu.HideForPlayer(kvp.Key);
395-
MenuClosed?.Invoke(this, new MenuManagerEventArgs { Player = kvp.Key, Menu = currentMenu });
396-
currentMenu = currentMenu.Parent.ParentMenu;
397-
}
398-
_ = openMenus.TryRemove(kvp.Key, out _);
399-
});
400-
}
401407
}

managed/src/SwiftlyS2.Core/Natives/GameFunctions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
using System.Text;
33
using Spectre.Console;
44
using SwiftlyS2.Shared.Natives;
5-
using SwiftlyS2.Shared.SchemaDefinitions;
65

76
namespace SwiftlyS2.Core.Natives;
87

98
internal static class GameFunctions
109
{
10+
private static readonly bool IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
1111
public static unsafe delegate* unmanaged< CTakeDamageInfo*, nint, nint, nint, Vector*, Vector*, float, int, int, void*, void > pCTakeDamageInfo_Constructor;
1212
public static unsafe delegate* unmanaged< nint, Ray_t*, Vector, Vector, CTraceFilter*, CGameTrace*, void > pTraceShape;
1313
public static unsafe delegate* unmanaged< Vector, Vector, BBox_t, CTraceFilter*, CGameTrace*, void > pTracePlayerBBox;
@@ -16,7 +16,8 @@ internal static class GameFunctions
1616
public static unsafe delegate* unmanaged< nint, nint, float, void > pSetOrAddAttribute;
1717
public static unsafe delegate* unmanaged< int, nint, nint > pGetWeaponCSDataFromKey;
1818
public static unsafe delegate* unmanaged< nint, uint, nint, byte, CUtlSymbolLarge, byte, int, nint, nint, void > pDispatchParticleEffect;
19-
public static unsafe delegate* unmanaged< nint, uint, float, nint, byte, void > pTerminateRound;
19+
public static unsafe delegate* unmanaged< nint, uint, float, nint, byte, void > pTerminateRoundLinux;
20+
public static unsafe delegate* unmanaged< nint, float, uint, nint, byte, void > pTerminateRoundWindows;
2021
public static unsafe delegate* unmanaged< nint, Vector*, QAngle*, Vector*, void > pTeleport;
2122
public static int TeleportOffset => NativeOffsets.Fetch("CBaseEntity::Teleport");
2223
public static int CommitSuicideOffset => NativeOffsets.Fetch("CBasePlayerPawn::CommitSuicide");
@@ -43,7 +44,14 @@ public static void Initialize()
4344
pSetOrAddAttribute = (delegate* unmanaged< nint, IntPtr, float, void >)NativeSignatures.Fetch("CAttributeList::SetOrAddAttributeValueByName");
4445
pGetWeaponCSDataFromKey = (delegate* unmanaged< int, nint, nint >)NativeSignatures.Fetch("GetWeaponCSDataFromKey");
4546
pDispatchParticleEffect = (delegate* unmanaged< nint, uint, nint, byte, CUtlSymbolLarge, byte, int, nint, nint, void >)NativeSignatures.Fetch("DispatchParticleEffect");
46-
pTerminateRound = (delegate* unmanaged< nint, uint, float, nint, byte, void >)NativeSignatures.Fetch("CGameRules::TerminateRound");
47+
if (IsWindows)
48+
{
49+
pTerminateRoundWindows = (delegate* unmanaged< nint, float, uint, nint, byte, void >)NativeSignatures.Fetch("CGameRules::TerminateRound");
50+
}
51+
else
52+
{
53+
pTerminateRoundLinux = (delegate* unmanaged< nint, uint, float, nint, byte, void >)NativeSignatures.Fetch("CGameRules::TerminateRound");
54+
}
4755
pTeleport = (delegate* unmanaged< nint, Vector*, QAngle*, Vector*, void >)((void**)NativeMemoryHelpers.GetVirtualTableAddress("server", "CBaseEntity"))[TeleportOffset];
4856
}
4957
}
@@ -60,7 +68,14 @@ public static void TerminateRound( nint gameRules, uint reason, float delay )
6068
{
6169
unsafe
6270
{
63-
pTerminateRound(gameRules, reason, delay, 0, 0);
71+
if (IsWindows)
72+
{
73+
pTerminateRoundWindows(gameRules, delay, reason, 0, 0);
74+
}
75+
else
76+
{
77+
pTerminateRoundLinux(gameRules, reason, delay, 0, 0);
78+
}
6479
}
6580
}
6681
catch (Exception e)

managed/src/TestPlugin/TestPlugin.cs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using System.Collections.Concurrent;
3737
using Dia2Lib;
3838
using System.Reflection.Metadata;
39+
using Microsoft.Diagnostics.Tracing.Parsers.MicrosoftWindowsTCPIP;
3940

4041
namespace TestPlugin;
4142

@@ -631,6 +632,12 @@ public void GetIpCommand( ICommandContext context )
631632

632633
// Core.Menus.OpenMenu(player, settingsMenu);
633634
// }
635+
[Command("ed")]
636+
public void EndRoundCommand( ICommandContext _ )
637+
{
638+
var gameRules = Core.EntitySystem.GetGameRules()!;
639+
gameRules.TerminateRound(RoundEndReason.CTsWin, 10.0f);
640+
}
634641

635642
[Command("tm")]
636643
public void TestMenuCommand( ICommandContext context )
@@ -656,16 +663,42 @@ public void TestMenuCommand( ICommandContext context )
656663
.AddOption(new ButtonMenuOption("Cancel") { CloseAfterClick = true })
657664
.Build();
658665

659-
var menu = Core.MenusAPI
666+
var shopMenu = Core.MenusAPI
660667
.CreateBuilder()
661668
.Design.SetMenuTitle("Shop Menu")
662-
.AddOption(new SubmenuMenuOption("Item 1", confirmMenu))
663-
.AddOption(new SubmenuMenuOption("Item 2", confirmMenu))
664-
.AddOption(new SubmenuMenuOption("Item 3", confirmMenu))
665-
.AddOption(new SubmenuMenuOption("Item 4", confirmMenu))
669+
.AddOption(new SubmenuMenuOption("Item 1", async () =>
670+
{
671+
await Task.Delay(100);
672+
return confirmMenu;
673+
}))
674+
.AddOption(new SubmenuMenuOption("Item 2", async () =>
675+
{
676+
await Task.Delay(100);
677+
return confirmMenu;
678+
}))
679+
.AddOption(new SubmenuMenuOption("Item 3", async () =>
680+
{
681+
await Task.Delay(100);
682+
return confirmMenu;
683+
}))
684+
.AddOption(new SubmenuMenuOption("Item 4", async () =>
685+
{
686+
await Task.Delay(100);
687+
return confirmMenu;
688+
}))
689+
.Build();
690+
691+
var mainMenu = Core.MenusAPI
692+
.CreateBuilder()
693+
.Design.SetMenuTitle("Menu")
694+
.AddOption(new SubmenuMenuOption("Shop", async () =>
695+
{
696+
await Task.Delay(100);
697+
return shopMenu;
698+
}))
666699
.Build();
667700

668-
Core.MenusAPI.OpenMenu(menu, ( player, menu ) =>
701+
Core.MenusAPI.OpenMenu(mainMenu, ( player, menu ) =>
669702
{
670703
Console.WriteLine($"{menu.Configuration.Title} closed for player: {player.Controller.PlayerName}");
671704
});

src/server/players/manager.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,14 @@ void CheckTransmitHook(void* _this, CCheckTransmitInfo** ppInfoList, int infoCou
210210
auto& pInfo = ppInfoList[i];
211211
int playerid = pInfo->m_nPlayerSlot.Get();
212212
if (!playermanager->IsPlayerOnline(playerid))
213+
{
213214
continue;
215+
}
214216
auto player = playermanager->GetPlayer(playerid);
217+
if (!player)
218+
{
219+
continue;
220+
}
215221

216222
auto& blockedBits = player->GetBlockedTransmittingBits();
217223

@@ -275,12 +281,21 @@ bool ClientConnectHook(void* _this, CPlayerSlot slot, const char* pszName, uint6
275281
static auto playermanager = g_ifaceService.FetchInterface<IPlayerManager>(PLAYERMANAGER_INTERFACE_VERSION);
276282
auto playerid = slot.Get();
277283
auto player = playermanager->RegisterPlayer(playerid);
278-
player->Initialize(playerid);
284+
// player->Initialize(playerid);
285+
if (!player)
286+
{
287+
return false;
288+
}
289+
279290
player->SetUnauthorizedSteamID(xuid);
280291

281292
if (g_pOnClientConnectCallback)
293+
{
282294
if (reinterpret_cast<bool (*)(int)>(g_pOnClientConnectCallback)(playerid) == false)
295+
{
283296
return false;
297+
}
298+
}
284299

285300
return reinterpret_cast<decltype(&ClientConnectHook)>(g_pClientConnectHook->GetOriginal())(_this, slot, pszName, xuid, pszNetworkID, unk1, pRejectReason);
286301
}
@@ -291,8 +306,8 @@ void OnClientConnectedHook(void* _this, CPlayerSlot slot, const char* pszName, u
291306
auto playerid = slot.Get();
292307
if (bFakePlayer)
293308
{
294-
auto player = playermanager->RegisterPlayer(playerid);
295-
player->Initialize(playerid);
309+
playermanager->RegisterPlayer(playerid);
310+
// player->Initialize(playerid);
296311
}
297312
else
298313
{
@@ -326,10 +341,11 @@ IPlayer* CPlayerManager::RegisterPlayer(int playerid)
326341
if (g_Players[playerid] != nullptr)
327342
UnregisterPlayer(playerid);
328343

329-
g_Players[playerid] = new CPlayer();
330-
g_Players[playerid]->Initialize(playerid);
344+
auto player = new CPlayer();
345+
player->Initialize(playerid);
346+
g_Players[playerid] = player;
331347

332-
return g_Players[playerid];
348+
return player;
333349
}
334350

335351
void CPlayerManager::UnregisterPlayer(int playerid)
@@ -341,11 +357,13 @@ void CPlayerManager::UnregisterPlayer(int playerid)
341357

342358
static auto vgui = g_ifaceService.FetchInterface<IVGUI>(VGUI_INTERFACE_VERSION);
343359

344-
vgui->UnregisterForPlayer(g_Players[playerid]);
345-
346-
g_Players[playerid]->Shutdown();
347-
delete g_Players[playerid];
360+
auto player = g_Players[playerid];
348361
g_Players[playerid] = nullptr;
362+
363+
vgui->UnregisterForPlayer(player);
364+
365+
player->Shutdown();
366+
delete player;
349367
}
350368

351369
IPlayer* CPlayerManager::GetPlayer(int playerid)

0 commit comments

Comments
 (0)