From 6fbd4c19246bd9d59a2330e9805b7b525ccdd3b7 Mon Sep 17 00:00:00 2001 From: TheNormalnij Date: Sat, 18 May 2019 01:48:03 +0300 Subject: [PATCH] downloadFile: added the ability to download a file from another resource (Fix issue #711) --- .../logic/CSingularFileDownload.cpp | 15 +++++++++-- .../deathmatch/logic/CSingularFileDownload.h | 3 ++- .../logic/CSingularFileDownloadManager.cpp | 4 +-- .../logic/CSingularFileDownloadManager.h | 2 +- .../logic/CStaticFunctionDefinitions.cpp | 4 +-- .../logic/CStaticFunctionDefinitions.h | 2 +- .../logic/lua/CLuaFunctionDefs.Util.cpp | 25 +++++++++++++------ 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/Client/mods/deathmatch/logic/CSingularFileDownload.cpp b/Client/mods/deathmatch/logic/CSingularFileDownload.cpp index d8a61d09c6d..ef298f00910 100644 --- a/Client/mods/deathmatch/logic/CSingularFileDownload.cpp +++ b/Client/mods/deathmatch/logic/CSingularFileDownload.cpp @@ -11,7 +11,7 @@ #include -CSingularFileDownload::CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CChecksum serverChecksum) +CSingularFileDownload::CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CResource* pRequestResource, CChecksum serverChecksum) { // Store the name m_strName = szName; @@ -19,8 +19,9 @@ CSingularFileDownload::CSingularFileDownload(CResource* pResource, const char* s // Store the name (short) m_strNameShort = szNameShort; - // store the resource + // store resources m_pResource = pResource; + m_pRequestResource = pRequestResource; // Store the server checksum m_ServerChecksum = serverChecksum; @@ -65,6 +66,15 @@ void CSingularFileDownload::CallFinished(bool bSuccess) CLuaArguments Arguments; Arguments.PushString(GetShortName()); // file name Arguments.PushBoolean(bSuccess); // Completed successfully? + if (m_pRequestResource) + { + Arguments.PushResource(m_pRequestResource); // Resource that called downloadFile + } + else + { + Arguments.PushBoolean(false); // or false + } + m_pResource->GetResourceEntity()->CallEvent("onClientFileDownloadComplete", Arguments, false); } SetComplete(); @@ -74,6 +84,7 @@ void CSingularFileDownload::Cancel() { m_bBeingDeleted = true; m_pResource = NULL; + m_pRequestResource = NULL; // TODO: Cancel also in Net } diff --git a/Client/mods/deathmatch/logic/CSingularFileDownload.h b/Client/mods/deathmatch/logic/CSingularFileDownload.h index 330a305ce1d..cac3df21f27 100644 --- a/Client/mods/deathmatch/logic/CSingularFileDownload.h +++ b/Client/mods/deathmatch/logic/CSingularFileDownload.h @@ -24,7 +24,7 @@ class CSingularFileDownload { public: - CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CChecksum serverChecksum); + CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CResource* pRequestResource, CChecksum serverChecksum); ~CSingularFileDownload(); static void DownloadFinishedCallBack(const SHttpDownloadResult& result); @@ -49,6 +49,7 @@ class CSingularFileDownload SString m_strNameShort; CResource* m_pResource; + CResource* m_pRequestResource; bool m_bComplete; bool m_bBeingDeleted; diff --git a/Client/mods/deathmatch/logic/CSingularFileDownloadManager.cpp b/Client/mods/deathmatch/logic/CSingularFileDownloadManager.cpp index dd259c2a41a..953b61f880f 100644 --- a/Client/mods/deathmatch/logic/CSingularFileDownloadManager.cpp +++ b/Client/mods/deathmatch/logic/CSingularFileDownloadManager.cpp @@ -23,9 +23,9 @@ CSingularFileDownloadManager::~CSingularFileDownloadManager() } CSingularFileDownload* CSingularFileDownloadManager::AddFile(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, - CChecksum checksum) + CResource* pRequestResource, CChecksum checksum) { - CSingularFileDownload* pFile = new CSingularFileDownload(pResource, szName, szNameShort, strHTTPURL, checksum); + CSingularFileDownload* pFile = new CSingularFileDownload(pResource, szName, szNameShort, strHTTPURL, pRequestResource, checksum); m_Downloads.push_back(pFile); return NULL; } diff --git a/Client/mods/deathmatch/logic/CSingularFileDownloadManager.h b/Client/mods/deathmatch/logic/CSingularFileDownloadManager.h index 089b1745e1b..1f09581adef 100644 --- a/Client/mods/deathmatch/logic/CSingularFileDownloadManager.h +++ b/Client/mods/deathmatch/logic/CSingularFileDownloadManager.h @@ -28,7 +28,7 @@ class CSingularFileDownloadManager CSingularFileDownloadManager(); ~CSingularFileDownloadManager(); - CSingularFileDownload* AddFile(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CChecksum checksum = CChecksum()); + CSingularFileDownload* AddFile(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CResource* pRequestResource, CChecksum checksum = CChecksum()); void CancelResourceDownloads(CResource* pResource); void ClearList(); diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp index 17e43f2aaeb..1146018afb5 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp @@ -213,7 +213,7 @@ bool CStaticFunctionDefinitions::WasEventCancelled() return m_pEvents->WasEventCancelled(); } -bool CStaticFunctionDefinitions::DownloadFile(CResource* pResource, const char* szFile, CChecksum checksum) +bool CStaticFunctionDefinitions::DownloadFile(CResource* pResource, const char* szFile, CResource* pRequestResource, CChecksum checksum) { SString strHTTPDownloadURLFull("%s/%s/%s", g_pClientGame->GetHTTPURL().c_str(), pResource->GetName(), szFile); SString strPath("%s\\resources\\%s\\%s", g_pClientGame->GetFileCacheRoot(), pResource->GetName(), szFile); @@ -221,7 +221,7 @@ bool CStaticFunctionDefinitions::DownloadFile(CResource* pResource, const char* // Call SingularFileDownloadManager if (g_pClientGame->GetSingularFileDownloadManager()) { - g_pClientGame->GetSingularFileDownloadManager()->AddFile(pResource, strPath.c_str(), szFile, strHTTPDownloadURLFull, checksum); + g_pClientGame->GetSingularFileDownloadManager()->AddFile(pResource, strPath.c_str(), szFile, strHTTPDownloadURLFull, pRequestResource, checksum); return true; } return false; diff --git a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h index 62df78f7b20..63f9b473dbc 100644 --- a/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h +++ b/Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h @@ -35,7 +35,7 @@ class CStaticFunctionDefinitions static bool WasEventCancelled(); // Misc funcs - static bool DownloadFile(CResource* pResource, const char* szFile, CChecksum checksum = CChecksum()); + static bool DownloadFile(CResource* pResource, const char* szFile, CResource* pRequestResource, CChecksum checksum = CChecksum()); // Output funcs static bool OutputConsole(const char* szText); diff --git a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp index 6e1b094e5d0..5e6ed698393 100644 --- a/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp +++ b/Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp @@ -291,9 +291,9 @@ int CLuaFunctionDefs::GetDevelopmentMode(lua_State* luaVM) int CLuaFunctionDefs::DownloadFile(lua_State* luaVM) { - SString strFile = ""; + SString strFileInput = ""; CScriptArgReader argStream(luaVM); - argStream.ReadString(strFile); + argStream.ReadString(strFileInput); if (!argStream.HasErrors()) { @@ -302,15 +302,20 @@ int CLuaFunctionDefs::DownloadFile(lua_State* luaVM) if (pLuaMain) { // Grab its resource - CResource* pResource = pLuaMain->GetResource(); - if (pResource) + CResource* pThisResource = pLuaMain->GetResource(); + CResource* pOtherResource = pThisResource; + + SString strMetaPath; + + // Resolve other resource from name + if (CResourceManager::ParseResourcePathInput(strFileInput, pOtherResource, NULL, &strMetaPath)) { - std::list::const_iterator iter = pResource->IterBeginResourceFiles(); - for (; iter != pResource->IterEndResourceFiles(); iter++) + std::list::const_iterator iter = pOtherResource->IterBeginResourceFiles(); + for (; iter != pOtherResource->IterEndResourceFiles(); iter++) { - if (strcmp(strFile, (*iter)->GetShortName()) == 0) + if (strcmp(strMetaPath, (*iter)->GetShortName()) == 0) { - if (CStaticFunctionDefinitions::DownloadFile(pResource, strFile, (*iter)->GetServerChecksum())) + if (CStaticFunctionDefinitions::DownloadFile(pOtherResource, strMetaPath, pThisResource, (*iter)->GetServerChecksum())) { lua_pushboolean(luaVM, true); return 1; @@ -319,6 +324,10 @@ int CLuaFunctionDefs::DownloadFile(lua_State* luaVM) } m_pScriptDebugging->LogCustom(luaVM, 255, 255, 255, "%s: File doesn't exist", lua_tostring(luaVM, lua_upvalueindex(1))); } + else + { + m_pScriptDebugging->LogCustom(luaVM, 255, 255, 255, "%s: Invalid path", lua_tostring(luaVM, lua_upvalueindex(1))); + } } } lua_pushboolean(luaVM, false);