diff --git a/plugin/mapdownloader.smx b/plugin/mapdownloader.smx index 0bd8df0..d233cf4 100644 Binary files a/plugin/mapdownloader.smx and b/plugin/mapdownloader.smx differ diff --git a/plugin/mapdownloader.sp b/plugin/mapdownloader.sp index 59410d4..ee5d35e 100644 --- a/plugin/mapdownloader.sp +++ b/plugin/mapdownloader.sp @@ -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 "); 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,42 +182,55 @@ 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); + CloseHandle(hndl); PrintToChatAll("Error downloading map %s", map); decl String:sError[256]; curl_easy_strerror(code, sError, sizeof(sError)); PrintToChatAll("cURL error: %s", sError); PrintToChatAll("cURL error code: %d", code); - DeleteFile(targetPath); - } else { - 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); - 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); - PrintToChatAll("Error downloading map %s", map); - PrintToChatAll("HTTP error: %d", status_code); - } + DeleteFile(tempPath); + return; } - return; + + new status_code; + curl_easy_getinfo_int(hndl, CURLINFO_RESPONSE_CODE, status_code); + CloseHandle(hndl); + + if (status_code == 404) { + PrintToChatAll("Map %s not found", map); + DeleteFile(tempPath); + return; + } + 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"); }