Skip to content

Commit ffcb673

Browse files
authored
chore: get workshop map id (#73)
1 parent 489a3c3 commit ffcb673

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

src/FiveStack.Entities/Map.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ namespace FiveStack.Entities;
33
public class Map
44
{
55
public string name { get; set; } = "";
6-
public string label { get; set; } = "";
76
public string workshop_map_id { get; set; } = "";
87
}

src/FiveStack.Services/MatchManager.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Runtime.InteropServices;
12
using CounterStrikeSharp.API;
23
using CounterStrikeSharp.API.Core;
34
using CounterStrikeSharp.API.Modules.Cvars;
@@ -10,6 +11,9 @@
1011

1112
namespace FiveStack;
1213

14+
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
15+
delegate IntPtr GetAddonNameDelegate(IntPtr self);
16+
1317
public class MatchManager
1418
{
1519
private MatchData? _matchData;
@@ -28,6 +32,7 @@ public class MatchManager
2832
// public CoachSystem _coachSystem;
2933
public KnifeSystem knifeSystem;
3034
public CaptainSystem captainSystem;
35+
public INetworkServerService _networkServerService;
3136

3237
public MatchManager(
3338
ILogger<MatchManager> logger,
@@ -38,7 +43,8 @@ public MatchManager(
3843
KnifeSystem KnifeSystem,
3944
ReadySystem ReadySystem,
4045
CaptainSystem CaptainSystem,
41-
EnvironmentService environmentService
46+
EnvironmentService environmentService,
47+
INetworkServerService networkServerService
4248
)
4349
{
4450
_logger = logger;
@@ -50,6 +56,7 @@ EnvironmentService environmentService
5056
captainSystem = CaptainSystem;
5157
_backUpManagement = backUpManagement;
5258
_environmentService = environmentService;
59+
_networkServerService = networkServerService;
5360
}
5461

5562
public void Init(MatchData match)
@@ -241,10 +248,22 @@ public void SetupMatch(MatchData match)
241248
return;
242249
}
243250

244-
_logger.LogInformation($"Game State {_currentMap.status} on {_currentMap.map.label} {_currentMap.map.name} / {Server.MapName}");
251+
_logger.LogInformation(
252+
$"Game State {_currentMap.status} on ({_currentMap.map.name}) / {Server.MapName}"
253+
);
245254

246-
// attempt to reduce the number of bad map naming issues
247-
if (!new[] { _currentMap.map.name, _currentMap.map.label.ToLower() }.Contains(Server.MapName.ToLower()))
255+
if (_currentMap.map.workshop_map_id is not null)
256+
{
257+
string currentWorkshopID = GetWorkshopID();
258+
_logger.LogInformation($"Checking Workshop Map {_currentMap.map.workshop_map_id} / {currentWorkshopID}");
259+
260+
if (_currentMap.map.workshop_map_id != currentWorkshopID)
261+
{
262+
ChangeMap(_currentMap.map);
263+
return;
264+
}
265+
}
266+
else if (!Server.MapName.ToLower().Contains(_currentMap.map.name.ToLower()))
248267
{
249268
ChangeMap(_currentMap.map);
250269
return;
@@ -270,6 +289,16 @@ public void SetupMatch(MatchData match)
270289
}
271290
}
272291

292+
private string GetWorkshopID()
293+
{
294+
IntPtr networkGameServer = _networkServerService.GetIGameServerHandle();
295+
IntPtr vtablePtr = Marshal.ReadIntPtr(networkGameServer);
296+
IntPtr functionPtr = Marshal.ReadIntPtr(vtablePtr + (25 * IntPtr.Size));
297+
var getAddonName = Marshal.GetDelegateForFunctionPointer<GetAddonNameDelegate>(functionPtr);
298+
IntPtr result = getAddonName(networkGameServer);
299+
return Marshal.PtrToStringAnsi(result)!.Split(',')[0];
300+
}
301+
273302
public void SetupTeamNames()
274303
{
275304
MatchMap? _currentMap = GetCurrentMap();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Runtime.InteropServices;
2+
using CounterStrikeSharp.API;
3+
using CounterStrikeSharp.API.Core;
4+
using CounterStrikeSharp.API.Modules.Memory;
5+
6+
public class INetworkServerService : NativeObject
7+
{
8+
private readonly VirtualFunctionWithReturn<nint, nint> GetIGameServerFunc;
9+
10+
public INetworkServerService()
11+
: base(NativeAPI.GetValveInterface(0, "NetworkServerService_001"))
12+
{
13+
int offset = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 23 : 24;
14+
this.GetIGameServerFunc = new VirtualFunctionWithReturn<nint, nint>(this.Handle, offset);
15+
}
16+
17+
public nint GetIGameServerHandle()
18+
{
19+
return this.GetIGameServerFunc.Invoke(this.Handle);
20+
}
21+
}

src/FiveStackServiceCollection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
1515
serviceCollection.AddSingleton<GameBackUpRounds>();
1616
serviceCollection.AddSingleton<SurrenderSystem>();
1717
serviceCollection.AddSingleton<EnvironmentService>();
18+
serviceCollection.AddSingleton<INetworkServerService>();
1819

1920
serviceCollection.AddTransient<MatchManager>();
2021
serviceCollection.AddTransient<VoteSystem>();

0 commit comments

Comments
 (0)