1+ using CounterStrikeSharp . API . Core ;
2+ using System . Runtime . InteropServices ;
3+
4+ namespace CSSharpUtils . Utils ;
5+
6+ /// <summary>
7+ /// Provides utility methods for accessing workshop-related information.
8+ /// </summary>
9+ public static class WorkshopUtils
10+ {
11+ private static readonly IntPtr _networkServerService ;
12+
13+ private delegate IntPtr GetGameServerHandle ( IntPtr networkServerService ) ;
14+ private static readonly GetGameServerHandle _getGameServerHandleDelegate ;
15+
16+ private delegate IntPtr GetWorkshopId ( IntPtr gameServer ) ;
17+ private static readonly GetWorkshopId _getWorkshopIdDelegate ;
18+
19+ static unsafe WorkshopUtils ( )
20+ {
21+ _networkServerService = NativeAPI . GetValveInterface ( 0 , "NetworkServerService_001" ) ;
22+
23+ var gameServerOffset = RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) ? 24 : 23 ;
24+ IntPtr * gameServerHandle = ( * ( IntPtr * * ) _networkServerService + gameServerOffset ) ;
25+ _getGameServerHandleDelegate = Marshal . GetDelegateForFunctionPointer < GetGameServerHandle > ( * gameServerHandle ) ;
26+
27+ var networkGameServer = _getGameServerHandleDelegate ( _networkServerService ) ;
28+ IntPtr * workshopHandle = ( * ( IntPtr * * ) networkGameServer + 25 ) ;
29+ _getWorkshopIdDelegate = Marshal . GetDelegateForFunctionPointer < GetWorkshopId > ( * workshopHandle ) ;
30+ }
31+
32+ /// <summary>
33+ /// Gets the workshop ID of the current server.
34+ /// </summary>
35+ /// <returns>The workshop ID as a string.</returns>
36+ /// <exception cref="InvalidOperationException">Thrown if the workshop ID cannot be retrieved.</exception>
37+ public static unsafe string GetID ( )
38+ {
39+ IntPtr networkGameServer = _getGameServerHandleDelegate ( _networkServerService ) ;
40+ IntPtr result = _getWorkshopIdDelegate ( networkGameServer ) ;
41+
42+ var workshopString = Marshal . PtrToStringAnsi ( result ) ;
43+ return workshopString ? . Split ( ',' ) [ 0 ] ?? throw new InvalidOperationException ( "Failed to retrieve the workshop ID." ) ;
44+ }
45+ }
0 commit comments