Skip to content

Commit 4c0144c

Browse files
authored
feat(core): add demos.tf support (#13)
Adds support for sending the demos.tf url alongside the webhook request
1 parent cce55cf commit 4c0144c

File tree

2 files changed

+87
-24
lines changed

2 files changed

+87
-24
lines changed

scripting/include/demostf.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#if defined _demostf_included
2+
#endinput
3+
#endif
4+
#define _demostf_included
5+
6+
forward DemoUploaded(bool success, const char[] demoid, const char[] url);

scripting/payload-webhook.sp

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
#include <sdktools>
33
#include <sdkhooks>
44
#include <logstf>
5+
#include <demostf>
56
#include <SteamWorks>
67
#pragma newdecls required
78
#pragma semicolon 1
89

9-
#define VERSION "1.3.0"
10+
#define VERSION "1.4.0"
1011

1112
char g_sWebhookToken[128];
13+
bool g_bDemostfLoaded;
14+
char g_sLogId[128];
15+
char g_sDemoId[128];
1216

1317
ConVar g_hCvarWebhookToken;
1418
ConVar g_hCvarSendLogs;
@@ -36,6 +40,33 @@ public void OnPluginStart()
3640
PrintToServer("[Payload] Plugin loaded.");
3741
}
3842

43+
public void OnAllPluginsLoaded()
44+
{
45+
IsDemosTfPresent();
46+
}
47+
48+
public bool IsDemosTfPresent()
49+
{
50+
Handle h_demostf = FindPluginByFile("demostf.smx");
51+
if (h_demostf != null && GetPluginStatus(h_demostf) == Plugin_Running)
52+
{
53+
char version[64];
54+
GetPluginInfo(h_demostf, PlInfo_Version, version, sizeof(version));
55+
56+
// Crude way of making sure we're running a version newer than 0.2
57+
if (strcmp(version, "0.2") > 0) {
58+
g_bDemostfLoaded = true;
59+
PrintToServer("[Payload] Demos.tf plugin detected.");
60+
return true;
61+
}
62+
}
63+
else
64+
{
65+
g_bDemostfLoaded = false;
66+
return false;
67+
}
68+
}
69+
3970
public Action TestUpload(int client, int args)
4071
{
4172
bool sendRequest = GetConVarBool(g_hCvarSendLogs);
@@ -74,34 +105,53 @@ public int LogUploaded(bool success, const char[] logid, const char[] url)
74105
{
75106
if (success)
76107
{
77-
bool sendRequest = GetConVarBool(g_hCvarSendLogs);
78-
if (sendRequest == false)
79-
return;
108+
strcopy(g_sLogId, sizeof(g_sLogId), logid);
109+
// Prepare the request if we have received the demoid or if the plugin isn't loaded
110+
if (!StrEqual(g_sDemoId, "") || !g_bDemostfLoaded)
111+
PrepareRequest();
112+
}
113+
}
114+
115+
public int DemoUploaded(bool success, const char[] demoid, const char[] url)
116+
{
117+
if (success)
118+
{
119+
strcopy(g_sDemoId, sizeof(g_sDemoId), demoid);
120+
}
121+
// Prepare the request if we have received the logid
122+
if (!StrEqual(g_sLogId, ""))
123+
PrepareRequest();
124+
}
80125

81-
// Make sure we update the string value of the token
82-
GetConVarString(g_hCvarWebhookToken, g_sWebhookToken, sizeof(g_sWebhookToken));
83-
if (strlen(g_sWebhookToken) == 0)
84-
return;
126+
public void PrepareRequest()
127+
{
128+
bool sendRequest = GetConVarBool(g_hCvarSendLogs);
129+
if (sendRequest == false)
130+
return;
85131

86-
char BaseUrl[64];
87-
char FullUrl[128];
132+
// Make sure we update the string value of the token
133+
GetConVarString(g_hCvarWebhookToken, g_sWebhookToken, sizeof(g_sWebhookToken));
134+
if (strlen(g_sWebhookToken) == 0)
135+
return;
136+
137+
char BaseUrl[64];
138+
char FullUrl[128];
88139

89-
// Store convar for the api Url in BaseUrl
90-
GetConVarString(g_hCvarApiUrl, BaseUrl, sizeof(BaseUrl));
140+
// Store convar for the api Url in BaseUrl
141+
GetConVarString(g_hCvarApiUrl, BaseUrl, sizeof(BaseUrl));
91142

92-
// Complete the baseUrl
93-
Format(FullUrl, sizeof(FullUrl), "%s/webhooks/logs", BaseUrl);
143+
// Complete the baseUrl
144+
Format(FullUrl, sizeof(FullUrl), "%s/webhooks/logs", BaseUrl);
94145

95-
// For debug purposes:
96-
PrintToServer("FullURL: %s", FullUrl);
97-
PrintToServer("[Payload] Rendering logs preview...");
98-
PrintToChatAll("[Payload] Rendering logs preview...");
146+
// For debug purposes:
147+
PrintToServer("FullURL: %s", FullUrl);
148+
PrintToServer("[Payload] Rendering logs preview...");
149+
PrintToChatAll("[Payload] Rendering logs preview...");
99150

100-
SendRequest(logid, FullUrl);
101-
}
151+
SendRequest(FullUrl);
102152
}
103153

104-
public void SendRequest(const char[] logid, const char[] fullApiUrl)
154+
public void SendRequest(const char[] fullApiUrl)
105155
{
106156
Handle hRequest = SteamWorks_CreateHTTPRequest(k_EHTTPMethodPOST, fullApiUrl);
107157

@@ -110,8 +160,12 @@ public void SendRequest(const char[] logid, const char[] fullApiUrl)
110160
SteamWorks_SetHTTPRequestHeaderValue(hRequest, "Authorization", g_sWebhookToken);
111161

112162
// Body
113-
SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "logsId", logid);
114-
163+
SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "logsId", g_sLogId);
164+
165+
// Only add parameter if the demos.tf plugin was loaded and returned an id
166+
if (g_bDemostfLoaded && !StrEqual(g_sDemoId, ""))
167+
SteamWorks_SetHTTPRequestGetOrPostParameter(hRequest, "demosId", g_sDemoId);
168+
115169
SteamWorks_SetHTTPCallbacks(hRequest, OnSteamWorksHTTPComplete);
116170
SteamWorks_SendHTTPRequest(hRequest);
117171
}
@@ -141,7 +195,10 @@ public int OnSteamWorksHTTPComplete(Handle hRequest, bool bFailure, bool bReques
141195
PrintToChatAll("[Payload] Log preview uploaded.");
142196
PrintToServer("[Payload] Log preview uploaded.");
143197
}
144-
198+
// Clear the ids
199+
g_sDemoId = "";
200+
g_sLogId = "";
201+
145202
delete hRequest;
146203
}
147204

0 commit comments

Comments
 (0)