Skip to content

Commit f0c4cb6

Browse files
authored
Merge pull request #9 from oscar-wos/workshop
Workshop
2 parents ecbd417 + 04c8225 commit f0c4cb6

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

CSSharpUtils/Extensions/ConfigExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public static class ConfigExtensions
3636
if (configPath == null)
3737
return false;
3838

39+
// get newest config version
40+
var newCfgVersion = new T().Version;
41+
42+
// loaded config is up-to-date
43+
if (checkVersion && config.Version == newCfgVersion)
44+
return false;
45+
3946
if (backup)
4047
{
4148
// get counter of backup file
@@ -45,13 +52,6 @@ public static class ConfigExtensions
4552
File.Copy($"{configPath}.json", $"{configPath}-{backupCount}.bak", true);
4653
}
4754

48-
// get newest config version
49-
var newCfgVersion = new T().Version;
50-
51-
// loaded config is up-to-date
52-
if (checkVersion && config.Version == newCfgVersion)
53-
return false;
54-
5555
// update the version
5656
config.Version = newCfgVersion;
5757

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

README.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ CsTeam randomTeam = CsTeamUtils.GetRandomTeam();
111111
Console.WriteLine($"Randomly selected team: {randomTeam}");
112112
```
113113

114+
### Workshop
115+
```csharp
116+
using CSSharpUtils.Utils;
117+
118+
// Get the workshop id
119+
string workshopId = WorkshopUtils.GetID();
120+
```
121+
114122
### Game
115123
```csharp
116124
using CSSharpUtils.Utils;

0 commit comments

Comments
 (0)