Skip to content

Commit

Permalink
feat(*): get rid of SteamWorks dependency (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc authored Jul 5, 2022
1 parent 8a6c55b commit 27b2de9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
wget "https://forums.alliedmods.net/attachment.php?attachmentid=188744&d=1618607414" -O system2.zip
unzip -o system2.zip -d addons/sourcemod/
wget "https://github.com/KyleSanderson/SteamWorks/releases/download/1.2.3c/package-lin.tgz" -O steamworks.tgz
tar -xf steamworks.tgz --strip-components=1
- name: Compile
run: |
spcomp \
Expand Down
5 changes: 1 addition & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ FROM melkortf/tf2-sourcemod:latest

RUN wget "https://forums.alliedmods.net/attachment.php?attachmentid=188744&d=1618607414" -O system2.zip \
&& unzip -o system2.zip -d "${SERVER_DIR}/tf/addons/sourcemod/" \
&& rm -f system2.zip \
&& wget "https://github.com/KyleSanderson/SteamWorks/releases/download/1.2.3c/package-lin.tgz" -O steamworks.tgz \
&& tar -xf steamworks.tgz --strip-components=1 -C "${SERVER_DIR}/tf/" \
&& rm -f steamworks.tgz
&& rm -f system2.zip

COPY connector.smx "$SERVER_DIR/tf/addons/sourcemod/plugins/connector.smx"
40 changes: 34 additions & 6 deletions scripting/connector.sp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include <sourcemod>
#include <system2>
#include <SteamWorks>

#define PLUGIN_VERSION "0.4.1"
#define PUBLIC_IP_API_ADDRESS "https://api.ipify.org"

ConVar tf2pickupOrgApiAddress = null;
ConVar tf2pickupOrgSecret = null;
ConVar tf2pickupOrgPriority = null;
ConVar tf2pickupOrgOverrideInternalAddress = null;
Handle timer = null;
char publicIpAddress[64];

public Plugin myinfo =
{
Expand All @@ -33,13 +34,39 @@ public void OnPluginStart()
tf2pickupOrgOverrideInternalAddress.AddChangeHook(OnApiAddressOrSecretChange);

RegServerCmd("sm_tf2pickuporg_heartbeat", CommandHeartbeat);
ResolvePublicIpAddress();
}

public void OnPluginEnd()
{

}

public void ResolvePublicIpAddress()
{
System2HTTPRequest request = new System2HTTPRequest(PublicIpCallback, PUBLIC_IP_API_ADDRESS);
request.SetUserAgent("tf2pickup.org connector plugin/%s", PLUGIN_VERSION);
request.GET();
delete request;

PrintToServer("Querying https://api.ipify.org...");
}

public void PublicIpCallback(bool success, const char[] error, System2HTTPRequest request, System2HTTPResponse response, HTTPRequestMethod method)
{
if (!success) {
char url[128];
request.GetURL(url, sizeof(url));
PrintToServer("ERROR: %s failed: %s", url, error);
return;
}

publicIpAddress[0] = '\0';
response.GetContent(publicIpAddress, sizeof(publicIpAddress));
TrimString(publicIpAddress);
PrintToServer("Gameserver public IP address: %s", publicIpAddress);
}

public void OnApiAddressOrSecretChange(ConVar convar, char[] oldValue, char[] newValue)
{
if (timer != null) {
Expand All @@ -65,6 +92,11 @@ public Action CommandHeartbeat(int args)

public Action HeartbeatGameServer(Handle timerHandle)
{
if (strlen(publicIpAddress) == 0) {
PrintToServer("Gameserver public IP address unknown; heartbeat impossible");
return Plugin_Stop;
}

char apiAddress[128];
tf2pickupOrgApiAddress.GetString(apiAddress, sizeof(apiAddress));

Expand All @@ -79,12 +111,8 @@ public Action HeartbeatGameServer(Handle timerHandle)
request.SetHeader("Authorization", "secret %s", secret);
request.SetHeader("Content-Type", "application/x-www-form-urlencoded");

int ipAddr[4];
SteamWorks_GetPublicIP(ipAddr);

char address[64];
Format(address, sizeof(address), "%d.%d.%d.%d", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
System2_URLEncode(address, sizeof(address), address);
System2_URLEncode(address, sizeof(address), publicIpAddress);

char port[6];
GetConVarString(FindConVar("hostport"), port, sizeof(port));
Expand Down

0 comments on commit 27b2de9

Please sign in to comment.