mirror of
https://codeberg.org/spire/sm_whitelist.git
synced 2026-06-03 10:14:07 +02:00
don't download existing whitelists, download to tmp file
This commit is contained in:
parent
6a1837dbd4
commit
333c506551
2 changed files with 29 additions and 15 deletions
Binary file not shown.
|
|
@ -7,7 +7,7 @@ public Plugin:myinfo = {
|
||||||
name = "whitelist.tf downloader",
|
name = "whitelist.tf downloader",
|
||||||
author = "Icewind",
|
author = "Icewind",
|
||||||
description = "Download whitelists from whitelist.tf",
|
description = "Download whitelists from whitelist.tf",
|
||||||
version = "0.2",
|
version = "0.3",
|
||||||
url = "https://whitelist.tf"
|
url = "https://whitelist.tf"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -35,14 +35,14 @@ public OnPluginStart() {
|
||||||
|
|
||||||
public Action:DownloadWhiteListAction(client, args) {
|
public Action:DownloadWhiteListAction(client, args) {
|
||||||
new String:arg[128];
|
new String:arg[128];
|
||||||
|
|
||||||
if(args != 1) {
|
if(args != 1) {
|
||||||
PrintToServer("Usage: sm_whitelist_tf %whitelistid%");
|
PrintToServer("Usage: sm_whitelist_tf %whitelistid%");
|
||||||
return Plugin_Handled;
|
return Plugin_Handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetCmdArg(1, arg, sizeof(arg));
|
GetCmdArg(1, arg, sizeof(arg));
|
||||||
|
|
||||||
new intId = StringToInt(arg);
|
new intId = StringToInt(arg);
|
||||||
if(intId > 0) {
|
if(intId > 0) {
|
||||||
Format(arg, sizeof(arg), "custom_whitelist_%s", arg);
|
Format(arg, sizeof(arg), "custom_whitelist_%s", arg);
|
||||||
|
|
@ -54,19 +54,25 @@ public Action:DownloadWhiteListAction(client, args) {
|
||||||
|
|
||||||
public DownloadWhiteList(String:whiteListId[128], bool:exec) {
|
public DownloadWhiteList(String:whiteListId[128], bool:exec) {
|
||||||
decl String:fullUrl[512];
|
decl String:fullUrl[512];
|
||||||
decl String:targetPath[128];
|
decl String:targetPath[PLATFORM_MAX_PATH];
|
||||||
|
decl String:downloadPath[PLATFORM_MAX_PATH];
|
||||||
decl String:BaseUrl[128];
|
decl String:BaseUrl[128];
|
||||||
GetConVarString(g_hCvarUrl, BaseUrl, sizeof(BaseUrl));
|
GetConVarString(g_hCvarUrl, BaseUrl, sizeof(BaseUrl));
|
||||||
new Handle:curl = curl_easy_init();
|
new Handle:curl = curl_easy_init();
|
||||||
CURL_DEFAULT_OPT(curl);
|
CURL_DEFAULT_OPT(curl);
|
||||||
|
|
||||||
Format(fullUrl, sizeof(fullUrl), "%s/%s.txt", BaseUrl, whiteListId);
|
Format(fullUrl, sizeof(fullUrl), "%s/%s.txt", BaseUrl, whiteListId);
|
||||||
|
|
||||||
Format(targetPath, sizeof(targetPath), "cfg/%s.txt", whiteListId);
|
|
||||||
|
|
||||||
|
Format(targetPath, sizeof(targetPath), "cfg/%s.txt", whiteListId);
|
||||||
|
if (FileExists(targetPath)) {
|
||||||
|
execWhiteList(whiteListId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Format(downloadPath, sizeof(downloadPath), "cfg/%s.txt.part", whiteListId);
|
||||||
new Handle:output_file = INVALID_HANDLE;
|
new Handle:output_file = INVALID_HANDLE;
|
||||||
|
|
||||||
output_file = curl_OpenFile(targetPath, "w");
|
output_file = curl_OpenFile(downloadPath, "w");
|
||||||
|
|
||||||
new Handle:hDLPack = CreateDataPack();
|
new Handle:hDLPack = CreateDataPack();
|
||||||
WritePackCell(hDLPack, _:output_file);
|
WritePackCell(hDLPack, _:output_file);
|
||||||
|
|
@ -75,16 +81,25 @@ public DownloadWhiteList(String:whiteListId[128], bool:exec) {
|
||||||
|
|
||||||
curl_easy_setopt_handle(curl, CURLOPT_WRITEDATA, output_file);
|
curl_easy_setopt_handle(curl, CURLOPT_WRITEDATA, output_file);
|
||||||
curl_easy_setopt_string(curl, CURLOPT_URL, fullUrl);
|
curl_easy_setopt_string(curl, CURLOPT_URL, fullUrl);
|
||||||
|
|
||||||
|
WritePackString(hDLPack, downloadPath);
|
||||||
|
WritePackString(hDLPack, targetPath);
|
||||||
|
|
||||||
|
|
||||||
curl_easy_perform_thread(curl, onComplete, hDLPack);
|
curl_easy_perform_thread(curl, onComplete, hDLPack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public onComplete(Handle:hndl, CURLcode:code, any hDLPack) {
|
public onComplete(Handle:hndl, CURLcode:code, any hDLPack) {
|
||||||
decl String:whiteListId[128];
|
decl String:whiteListId[128];
|
||||||
|
decl String:targetPath[PLATFORM_MAX_PATH];
|
||||||
|
decl String:downloadPath[PLATFORM_MAX_PATH];
|
||||||
|
|
||||||
ResetPack(hDLPack);
|
ResetPack(hDLPack);
|
||||||
CloseHandle(Handle:ReadPackCell(hDLPack)); // output_file
|
CloseHandle(Handle:ReadPackCell(hDLPack)); // output_file
|
||||||
ReadPackString(hDLPack, whiteListId, sizeof(whiteListId));
|
ReadPackString(hDLPack, whiteListId, sizeof(whiteListId));
|
||||||
bool exec = ReadPackCell(hDLPack);
|
bool exec = ReadPackCell(hDLPack);
|
||||||
|
ReadPackString(hDLPack, downloadPath, sizeof(downloadPath));
|
||||||
|
ReadPackString(hDLPack, targetPath, sizeof(targetPath));
|
||||||
|
|
||||||
CloseHandle(hDLPack);
|
CloseHandle(hDLPack);
|
||||||
|
|
||||||
|
|
@ -94,10 +109,11 @@ public onComplete(Handle:hndl, CURLcode:code, any hDLPack) {
|
||||||
PrintToChatAll("cURL error: %d", code);
|
PrintToChatAll("cURL error: %d", code);
|
||||||
} else {
|
} else {
|
||||||
new status_code;
|
new status_code;
|
||||||
curl_easy_getinfo_int(hndl, CURLINFO_RESPONSE_CODE, status_code);
|
curl_easy_getinfo_int(hndl, CURLINFO_RESPONSE_CODE, status_code);
|
||||||
|
|
||||||
if (status_code == 200) {
|
if (status_code == 200) {
|
||||||
ok = true;
|
ok = true;
|
||||||
|
RenameFile(downloadPath, targetPath);
|
||||||
if (exec) {
|
if (exec) {
|
||||||
execWhiteList(whiteListId);
|
execWhiteList(whiteListId);
|
||||||
}
|
}
|
||||||
|
|
@ -108,15 +124,13 @@ public onComplete(Handle:hndl, CURLcode:code, any hDLPack) {
|
||||||
PrintToChatAll("HTTP error: %d", status_code);
|
PrintToChatAll("HTTP error: %d", status_code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseHandle(hndl);
|
CloseHandle(hndl);
|
||||||
|
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
decl String:targetPath[128];
|
DeleteFile(downloadPath);
|
||||||
Format(targetPath, sizeof(targetPath), "cfg/%s.txt", whiteListId);
|
|
||||||
DeleteFile(targetPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue