Skip to content

Commit 60e258f

Browse files
committed
feat(API/Engine): WorkshopId
1 parent 816dd56 commit 60e258f

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

managed/src/SwiftlyS2.Core/Modules/Engine/EngineService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public EngineService( CommandTrackerManager commandTrackedManager )
2424

2525
public string Map => GlobalVars.MapName.Value;
2626

27+
public string WorkshopId => NativeEngineHelpers.GetWorkshopId();
28+
2729
public int MaxPlayers => GlobalVars.MaxClients;
2830

2931
public float CurrentTime => GlobalVars.CurrentTime;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,18 @@ public unsafe static string GetGameDirectoryPath() {
169169
return retString;
170170
}
171171
}
172+
173+
private unsafe static delegate* unmanaged<byte*, int> _GetWorkshopId;
174+
175+
public unsafe static string GetWorkshopId() {
176+
var ret = _GetWorkshopId(null);
177+
var pool = ArrayPool<byte>.Shared;
178+
var retBuffer = pool.Rent(ret + 1);
179+
fixed (byte* retBufferPtr = retBuffer) {
180+
ret = _GetWorkshopId(retBufferPtr);
181+
var retString = Encoding.UTF8.GetString(retBufferPtr, ret);
182+
pool.Return(retBuffer);
183+
return retString;
184+
}
185+
}
172186
}

managed/src/SwiftlyS2.Shared/Modules/Engine/IEngineService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public interface IEngineService
1616
[Obsolete("Use GlobalVars.MapName instead.")]
1717
public string Map { get; }
1818

19+
/// <summary>
20+
/// Gets the Workshop ID of the current map.
21+
/// </summary>
22+
public string WorkshopId { get; }
23+
1924
/// <summary>
2025
/// Gets a reference to the global variables structure.
2126
/// </summary>

natives/engine/helpers.native

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ string GetNativeVersion = void
1111
string GetMenuSettings = void
1212
ptr GetGlobalVars = void
1313
string GetCSGODirectoryPath = void
14-
string GetGameDirectoryPath = void
14+
string GetGameDirectoryPath = void
15+
string GetWorkshopId = void

src/scripting/engine/enginehelpers.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <public/filesystem.h>
2828
#include <public/steam/isteamgameserver.h>
2929
#include <public/tier0/platform.h>
30+
#include <public/iserver.h>
3031
#include <igamesystemfactory.h>
3132

3233
#include <core/bridge/metamod.h>
@@ -53,7 +54,7 @@ bool Bridge_EngineHelpers_IsMapValid(const char* map_name)
5354
engine->IsMapValid(map_name) ||
5455
Files::ExistsPath(fmt::format("{}steamapps/workshop/content/730/{}/{}.vpk", s_sWorkingDir.Get(), map_name, map_name)) ||
5556
Files::ExistsPath(fmt::format("{}steamapps/workshop/content/730/{}/{}_dir.vpk", s_sWorkingDir.Get(), map_name, map_name))
56-
);
57+
);
5758
}
5859

5960
void Bridge_EngineHelpers_ExecuteCommand(const char* command)
@@ -182,6 +183,21 @@ int Bridge_EngineHelpers_GetIP(char* out)
182183
return s.size();
183184
}
184185

186+
int Bridge_EngineHelpers_GetWorkshopId(char* out)
187+
{
188+
auto networkServerService = g_ifaceService.FetchInterface<INetworkServerService>(NETWORKSERVERSERVICE_INTERFACE_VERSION);
189+
if (!networkServerService) return 0;
190+
191+
auto server = networkServerService->GetIGameServer();
192+
if (!server) return 0;
193+
194+
std::string addonId = server->GetAddonName();
195+
196+
if (out != nullptr) strcpy(out, addonId.c_str());
197+
198+
return addonId.size();
199+
}
200+
185201
DEFINE_NATIVE("EngineHelpers.GetIP", Bridge_EngineHelpers_GetIP);
186202
DEFINE_NATIVE("EngineHelpers.IsMapValid", Bridge_EngineHelpers_IsMapValid);
187203
DEFINE_NATIVE("EngineHelpers.ExecuteCommand", Bridge_EngineHelpers_ExecuteCommand);
@@ -193,4 +209,5 @@ DEFINE_NATIVE("EngineHelpers.GetNativeVersion", Bridge_EngineHelpers_GetNativeVe
193209
DEFINE_NATIVE("EngineHelpers.GetMenuSettings", Bridge_EngineHelpers_GetMenuSettings);
194210
DEFINE_NATIVE("EngineHelpers.GetGlobalVars", Bridge_EngineHelpers_GetGlobalVars);
195211
DEFINE_NATIVE("EngineHelpers.GetCSGODirectoryPath", Bridge_EngineHelpers_GetCSGODirectoryPath);
196-
DEFINE_NATIVE("EngineHelpers.GetGameDirectoryPath", Bridge_EngineHelpers_GetGameDirectoryPath);
212+
DEFINE_NATIVE("EngineHelpers.GetGameDirectoryPath", Bridge_EngineHelpers_GetGameDirectoryPath);
213+
DEFINE_NATIVE("EngineHelpers.GetWorkshopId", Bridge_EngineHelpers_GetWorkshopId);

0 commit comments

Comments
 (0)