-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.sp
More file actions
312 lines (257 loc) · 8.55 KB
/
updater.sp
File metadata and controls
312 lines (257 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#pragma semicolon 1
/* SM Includes */
#include <sourcemod>
#undef REQUIRE_EXTENSIONS
#include <cURL>
#include <socket>
#include <steamtools>
#include <SteamWorks>
#define REQUIRE_EXTENSIONS
/* Plugin Info */
#define PLUGIN_NAME "Updater"
#define PLUGIN_VERSION "1.2.2"
public Plugin:myinfo =
{
name = PLUGIN_NAME,
author = "GoD-Tony",
description = "Automatically updates SourceMod plugins and files",
version = PLUGIN_VERSION,
url = "http://forums.alliedmods.net/showthread.php?t=169095"
};
/* Globals */
//#define DEBUG // This will enable verbose logging. Useful for developers testing their updates.
#define CURL_AVAILABLE() (GetFeatureStatus(FeatureType_Native, "curl_easy_init") == FeatureStatus_Available)
#define SOCKET_AVAILABLE() (GetFeatureStatus(FeatureType_Native, "SocketCreate") == FeatureStatus_Available)
#define STEAMTOOLS_AVAILABLE() (GetFeatureStatus(FeatureType_Native, "Steam_CreateHTTPRequest") == FeatureStatus_Available)
#define STEAMWORKS_AVAILABLE() (GetFeatureStatus(FeatureType_Native, "SteamWorks_WriteHTTPResponseBodyToFile") == FeatureStatus_Available)
#define EXTENSION_ERROR "This plugin requires one of the cURL, Socket, SteamTools, or SteamWorks extensions to function."
#define TEMP_FILE_EXT "temp" // All files are downloaded with this extension first.
#define MAX_URL_LENGTH 256
#define UPDATE_URL "http://godtony.mooo.com/updater/updater.txt"
enum UpdateStatus {
Status_Idle,
Status_Checking, // Checking for updates.
Status_Downloading, // Downloading an update.
Status_Updated, // Update is complete.
Status_Error, // An error occured while downloading.
};
new bool:g_bGetDownload, bool:g_bGetSource;
new Handle:g_hPluginPacks = INVALID_HANDLE;
new Handle:g_hDownloadQueue = INVALID_HANDLE;
new Handle:g_hRemoveQueue = INVALID_HANDLE;
new bool:g_bDownloading = false;
static Handle:_hUpdateTimer = INVALID_HANDLE;
static Float:_fLastUpdate = 0.0;
static String:_sDataPath[PLATFORM_MAX_PATH];
/* Core Includes */
#include "updater/plugins.sp"
#include "updater/filesys.sp"
#include "updater/download.sp"
#include "updater/api.sp"
/* Plugin Functions */
public APLRes:AskPluginLoad2(Handle:myself, bool:late, String:error[], err_max)
{
// cURL
MarkNativeAsOptional("curl_OpenFile");
MarkNativeAsOptional("curl_slist");
MarkNativeAsOptional("curl_slist_append");
MarkNativeAsOptional("curl_easy_init");
MarkNativeAsOptional("curl_easy_setopt_int_array");
MarkNativeAsOptional("curl_easy_setopt_handle");
MarkNativeAsOptional("curl_easy_setopt_string");
MarkNativeAsOptional("curl_easy_perform_thread");
MarkNativeAsOptional("curl_easy_strerror");
// Socket
MarkNativeAsOptional("SocketCreate");
MarkNativeAsOptional("SocketSetArg");
MarkNativeAsOptional("SocketSetOption");
MarkNativeAsOptional("SocketConnect");
MarkNativeAsOptional("SocketSend");
// SteamTools
MarkNativeAsOptional("Steam_CreateHTTPRequest");
MarkNativeAsOptional("Steam_SetHTTPRequestHeaderValue");
MarkNativeAsOptional("Steam_SendHTTPRequest");
MarkNativeAsOptional("Steam_WriteHTTPResponseBody");
MarkNativeAsOptional("Steam_ReleaseHTTPRequest");
API_Init();
RegPluginLibrary("updater");
return APLRes_Success;
}
public OnPluginStart()
{
if (!CURL_AVAILABLE() && !SOCKET_AVAILABLE() && !STEAMTOOLS_AVAILABLE() && !STEAMWORKS_AVAILABLE())
{
SetFailState(EXTENSION_ERROR);
}
LoadTranslations("common.phrases");
// Convars.
new Handle:hCvar = INVALID_HANDLE;
hCvar = CreateConVar("sm_updater_version", PLUGIN_VERSION, PLUGIN_NAME, FCVAR_PLUGIN|FCVAR_NOTIFY|FCVAR_DONTRECORD);
OnVersionChanged(hCvar, "", "");
HookConVarChange(hCvar, OnVersionChanged);
hCvar = CreateConVar("sm_updater", "2", "Determines update functionality. (1 = Notify, 2 = Download, 3 = Include source code)", FCVAR_PLUGIN, true, 1.0, true, 3.0);
OnSettingsChanged(hCvar, "", "");
HookConVarChange(hCvar, OnSettingsChanged);
// Commands.
RegAdminCmd("sm_updater_check", Command_Check, ADMFLAG_RCON, "Forces Updater to check for updates.");
RegAdminCmd("sm_updater_status", Command_Status, ADMFLAG_RCON, "View the status of Updater.");
// Initialize arrays.
g_hPluginPacks = CreateArray();
g_hDownloadQueue = CreateArray();
g_hRemoveQueue = CreateArray();
// Temp path for checking update files.
BuildPath(Path_SM, _sDataPath, sizeof(_sDataPath), "data/updater.txt");
#if !defined DEBUG
// Add this plugin to the autoupdater.
Updater_AddPlugin(GetMyHandle(), UPDATE_URL);
#endif
// Check for updates every 24 hours.
_hUpdateTimer = CreateTimer(86400.0, Timer_CheckUpdates, _, TIMER_REPEAT);
}
public OnAllPluginsLoaded()
{
// Check for updates on startup.
TriggerTimer(_hUpdateTimer, true);
}
public Action:Timer_CheckUpdates(Handle:timer)
{
Updater_FreeMemory();
// Update everything!
new maxPlugins = GetMaxPlugins();
for (new i = 0; i < maxPlugins; i++)
{
if (Updater_GetStatus(i) == Status_Idle)
{
Updater_Check(i);
}
}
_fLastUpdate = GetTickedTime();
return Plugin_Continue;
}
public Action:Command_Check(client, args)
{
new Float:fNextUpdate = _fLastUpdate + 3600.0;
if (fNextUpdate > GetTickedTime())
{
ReplyToCommand(client, "[Updater] Updates can only be checked once per hour. %.1f minutes remaining.", (fNextUpdate - GetTickedTime()) / 60.0);
}
else
{
ReplyToCommand(client, "[Updater] Checking for updates.");
TriggerTimer(_hUpdateTimer, true);
}
return Plugin_Handled;
}
public Action:Command_Status(client, args)
{
decl String:sFilename[64];
new Handle:hPlugin = INVALID_HANDLE;
new maxPlugins = GetMaxPlugins();
ReplyToCommand(client, "[Updater] -- Status Begin --");
ReplyToCommand(client, "Plugins being monitored for updates:");
for (new i = 0; i < maxPlugins; i++)
{
hPlugin = IndexToPlugin(i);
if (IsValidPlugin(hPlugin))
{
GetPluginFilename(hPlugin, sFilename, sizeof(sFilename));
ReplyToCommand(client, " [%i] %s", i, sFilename);
}
}
ReplyToCommand(client, "Last update check was %.1f minutes ago.", (GetTickedTime() - _fLastUpdate) / 60.0);
ReplyToCommand(client, "[Updater] --- Status End ---");
return Plugin_Handled;
}
public OnVersionChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
if (!StrEqual(newValue, PLUGIN_VERSION))
{
SetConVarString(convar, PLUGIN_VERSION);
}
}
public OnSettingsChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
switch (GetConVarInt(convar))
{
case 1: // Notify only.
{
g_bGetDownload = false;
g_bGetSource = false;
}
case 2: // Download updates.
{
g_bGetDownload = true;
g_bGetSource = false;
}
case 3: // Download with source code.
{
g_bGetDownload = true;
g_bGetSource = true;
}
}
}
#if !defined DEBUG
public Updater_OnPluginUpdated()
{
Updater_Log("Reloading Updater plugin... updates will resume automatically.");
// Reload this plugin.
decl String:filename[64];
GetPluginFilename(INVALID_HANDLE, filename, sizeof(filename));
ServerCommand("sm plugins reload %s", filename);
}
#endif
Updater_Check(index)
{
if (Fwd_OnPluginChecking(IndexToPlugin(index)) == Plugin_Continue)
{
decl String:url[MAX_URL_LENGTH];
Updater_GetURL(index, url, sizeof(url));
Updater_SetStatus(index, Status_Checking);
AddToDownloadQueue(index, url, _sDataPath);
}
}
Updater_FreeMemory()
{
// Make sure that no threads are active.
if (g_bDownloading || GetArraySize(g_hDownloadQueue))
{
return;
}
// Remove all queued plugins.
new index;
new maxPlugins = GetArraySize(g_hRemoveQueue);
for (new i = 0; i < maxPlugins; i++)
{
index = PluginToIndex(GetArrayCell(g_hRemoveQueue, i));
if (index != -1)
{
Updater_RemovePlugin(index);
}
}
ClearArray(g_hRemoveQueue);
// Remove plugins that have been unloaded.
for (new i = 0; i < GetMaxPlugins(); i++)
{
if (!IsValidPlugin(IndexToPlugin(i)))
{
Updater_RemovePlugin(i);
i--;
}
}
}
Updater_Log(const String:format[], any:...)
{
decl String:buffer[256], String:path[PLATFORM_MAX_PATH];
VFormat(buffer, sizeof(buffer), format, 2);
BuildPath(Path_SM, path, sizeof(path), "logs/Updater.log");
LogToFileEx(path, "%s", buffer);
}
#if defined DEBUG
Updater_DebugLog(const String:format[], any:...)
{
decl String:buffer[256], String:path[PLATFORM_MAX_PATH];
VFormat(buffer, sizeof(buffer), format, 2);
BuildPath(Path_SM, path, sizeof(path), "logs/Updater_Debug.log");
LogToFileEx(path, "%s", buffer);
}
#endif