mirror of
https://codeberg.org/spire/mapdownloader.git
synced 2026-11-30 13:20:48 +01:00
Merge pull request 'Prevent incomplete downloads' (#2) from Kanarie/mapdownloader:master into master
Reviewed-on: https://codeberg.org/spire/mapdownloader/pulls/2
This commit is contained in:
commit
ef51d4c9fc
2 changed files with 92 additions and 26 deletions
Binary file not shown.
|
|
@ -8,7 +8,7 @@ public Plugin:myinfo =
|
|||
name = "map downloader",
|
||||
author = "Icewind",
|
||||
description = "Automatically download missing maps",
|
||||
version = "0.2",
|
||||
version = "0.3",
|
||||
url = "https://spire.tf"
|
||||
};
|
||||
|
||||
|
|
@ -25,13 +25,40 @@ new CURL_Default_opt[][2] = {
|
|||
|
||||
#define CURL_DEFAULT_OPT(%1) curl_easy_setopt_int_array(%1, CURL_Default_opt, sizeof(CURL_Default_opt))
|
||||
|
||||
#define TEMP_PREFIX ".mapdownloader"
|
||||
|
||||
new Handle:g_hCvarUrl = INVALID_HANDLE;
|
||||
new Handle:g_hDownloads = INVALID_HANDLE;
|
||||
|
||||
public OnPluginStart() {
|
||||
g_hCvarUrl = CreateConVar("sm_map_download_base", "https://fastdl.serveme.tf/maps", "map download url", FCVAR_PROTECTED);
|
||||
|
||||
RegAdminCmd("sm_map", HandleSmMapAction, ADMFLAG_CHANGEMAP, "sm_map <map>");
|
||||
RegServerCmd("changelevel", HandleChangeLevelAction);
|
||||
|
||||
g_hDownloads = CreateTrie();
|
||||
DeleteStaleTempFiles();
|
||||
}
|
||||
|
||||
public DeleteStaleTempFiles() {
|
||||
decl String:prefix[64];
|
||||
Format(prefix, sizeof(prefix), "%s_%d_", TEMP_PREFIX, GetConVarInt(FindConVar("hostport")));
|
||||
|
||||
new Handle:dir = OpenDirectory("maps");
|
||||
if (dir == INVALID_HANDLE) {
|
||||
return;
|
||||
}
|
||||
|
||||
decl String:name[PLATFORM_MAX_PATH];
|
||||
decl String:path[PLATFORM_MAX_PATH];
|
||||
new FileType:type;
|
||||
while (ReadDirEntry(dir, name, sizeof(name), type)) {
|
||||
if (type == FileType_File && StrContains(name, prefix) == 0) {
|
||||
Format(path, sizeof(path), "maps/%s", name);
|
||||
DeleteFile(path);
|
||||
}
|
||||
}
|
||||
CloseHandle(dir);
|
||||
}
|
||||
|
||||
public Action:HandleSmMapAction(_client, args) {
|
||||
|
|
@ -61,6 +88,8 @@ public Action:HandleChangeLevelAction(args) {
|
|||
|
||||
if (FindMap(arg, displayName, sizeof(displayName)) != FindMap_NotFound) {
|
||||
return Plugin_Continue;
|
||||
} else if (AnnounceDownloadInProgress(arg)) {
|
||||
return Plugin_Handled;
|
||||
} else {
|
||||
decl String:path[PLATFORM_MAX_PATH];
|
||||
Format(path, sizeof(path), "maps/%s.bsp", arg);
|
||||
|
|
@ -93,6 +122,10 @@ public ChangeLevelUrl(String:url[PLATFORM_MAX_PATH]) {
|
|||
SplitString(mapFull, ".", map, sizeof(map));
|
||||
}
|
||||
|
||||
if (AnnounceDownloadInProgress(map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Format(path, sizeof(path), "maps/%s.bsp", map);
|
||||
|
||||
PrintToServer("Saving %s to %s", fullUrl, path);
|
||||
|
|
@ -100,6 +133,16 @@ public ChangeLevelUrl(String:url[PLATFORM_MAX_PATH]) {
|
|||
DownloadMapUrl(map, fullUrl, path);
|
||||
}
|
||||
|
||||
public bool:AnnounceDownloadInProgress(const String:map[]) {
|
||||
decl String:tempPath[PLATFORM_MAX_PATH];
|
||||
if (!GetTrieString(g_hDownloads, map, tempPath, sizeof(tempPath))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PrintToChatAll("Still downloading %s, %d MB so far", map, FileSize(tempPath) / (1024 * 1024));
|
||||
return true;
|
||||
}
|
||||
|
||||
public DownloadMap(String:map[PLATFORM_MAX_PATH], String:targetPath[PLATFORM_MAX_PATH]) {
|
||||
decl String:fullUrl[512];
|
||||
decl String:BaseUrl[PLATFORM_MAX_PATH];
|
||||
|
|
@ -109,8 +152,17 @@ public DownloadMap(String:map[PLATFORM_MAX_PATH], String:targetPath[PLATFORM_MAX
|
|||
}
|
||||
|
||||
public DownloadMapUrl(String:map[PLATFORM_MAX_PATH], String:fullUrl[512], String:targetPath[PLATFORM_MAX_PATH]) {
|
||||
decl String:tempPath[PLATFORM_MAX_PATH];
|
||||
Format(tempPath, sizeof(tempPath), "maps/%s_%d_%08x.bsp.part", TEMP_PREFIX, GetConVarInt(FindConVar("hostport")), GetURandomInt());
|
||||
|
||||
new Handle:output_file = curl_OpenFile(tempPath, "wb");
|
||||
if (output_file == INVALID_HANDLE) {
|
||||
PrintToChatAll("Error downloading map %s: can't write to the maps directory", map);
|
||||
return;
|
||||
}
|
||||
SetTrieString(g_hDownloads, map, tempPath);
|
||||
|
||||
new Handle:curl = curl_easy_init();
|
||||
new Handle:output_file = curl_OpenFile(targetPath, "wb");
|
||||
CURL_DEFAULT_OPT(curl);
|
||||
|
||||
PrintToChatAll("Trying to download %s from %s", map, fullUrl);
|
||||
|
|
@ -119,6 +171,7 @@ public DownloadMapUrl(String:map[PLATFORM_MAX_PATH], String:fullUrl[512], String
|
|||
WritePackCell(hDLPack, _:output_file);
|
||||
WritePackString(hDLPack, map);
|
||||
WritePackString(hDLPack, targetPath);
|
||||
WritePackString(hDLPack, tempPath);
|
||||
|
||||
curl_easy_setopt_handle(curl, CURLOPT_WRITEDATA, output_file);
|
||||
curl_easy_setopt_string(curl, CURLOPT_URL, fullUrl);
|
||||
|
|
@ -129,12 +182,15 @@ public DownloadMapUrl(String:map[PLATFORM_MAX_PATH], String:fullUrl[512], String
|
|||
public onComplete(Handle:hndl, CURLcode:code, any hDLPack) {
|
||||
decl String:map[PLATFORM_MAX_PATH];
|
||||
decl String:targetPath[PLATFORM_MAX_PATH];
|
||||
decl String:tempPath[PLATFORM_MAX_PATH];
|
||||
|
||||
ResetPack(hDLPack);
|
||||
CloseHandle(Handle:ReadPackCell(hDLPack)); // output_file
|
||||
ReadPackString(hDLPack, map, sizeof(map));
|
||||
ReadPackString(hDLPack, targetPath, sizeof(targetPath));
|
||||
ReadPackString(hDLPack, tempPath, sizeof(tempPath));
|
||||
CloseHandle(hDLPack);
|
||||
RemoveFromTrie(g_hDownloads, map);
|
||||
|
||||
if (code != CURLE_OK) {
|
||||
CloseHandle(hndl);
|
||||
|
|
@ -143,28 +199,38 @@ public onComplete(Handle:hndl, CURLcode:code, any hDLPack) {
|
|||
curl_easy_strerror(code, sError, sizeof(sError));
|
||||
PrintToChatAll("cURL error: %s", sError);
|
||||
PrintToChatAll("cURL error code: %d", code);
|
||||
DeleteFile(targetPath);
|
||||
} else {
|
||||
DeleteFile(tempPath);
|
||||
return;
|
||||
}
|
||||
|
||||
new status_code;
|
||||
curl_easy_getinfo_int(hndl, CURLINFO_RESPONSE_CODE, status_code);
|
||||
CloseHandle(hndl);
|
||||
|
||||
if (status_code == 200) {
|
||||
if (FileSize(targetPath) < 1024) {
|
||||
PrintToChatAll("Map file to small, discarding");
|
||||
DeleteFile(targetPath);
|
||||
if (status_code == 404) {
|
||||
PrintToChatAll("Map %s not found", map);
|
||||
DeleteFile(tempPath);
|
||||
return;
|
||||
}
|
||||
PrintToChatAll("Successfully downloaded map %s", map);
|
||||
ForceChangeLevel(map, "changelevel");
|
||||
} else if (status_code == 404) {
|
||||
PrintToChatAll("Map %s not found", map);
|
||||
DeleteFile(targetPath);
|
||||
} else {
|
||||
DeleteFile(targetPath);
|
||||
if (status_code != 200) {
|
||||
PrintToChatAll("Error downloading map %s", map);
|
||||
PrintToChatAll("HTTP error: %d", status_code);
|
||||
}
|
||||
}
|
||||
DeleteFile(tempPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (FileSize(tempPath) < 1024) {
|
||||
PrintToChatAll("Map file too small, discarding");
|
||||
DeleteFile(tempPath);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!RenameFile(targetPath, tempPath)) {
|
||||
PrintToChatAll("Error downloading map %s: can't move it into the maps directory", map);
|
||||
DeleteFile(tempPath);
|
||||
return;
|
||||
}
|
||||
|
||||
PrintToChatAll("Successfully downloaded map %s", map);
|
||||
ForceChangeLevel(map, "changelevel");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue