Skip to content

Commit

Permalink
downloadFile: downloading from another resource (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNormalnij committed Mar 30, 2020
1 parent fa6bdb6 commit 302f512
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
15 changes: 13 additions & 2 deletions Client/mods/deathmatch/logic/CSingularFileDownload.cpp
Expand Up @@ -11,16 +11,17 @@

#include <StdInc.h>

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;

// 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;
Expand Down Expand Up @@ -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();
Expand All @@ -74,6 +84,7 @@ void CSingularFileDownload::Cancel()
{
m_bBeingDeleted = true;
m_pResource = NULL;
m_pRequestResource = NULL;

// TODO: Cancel also in Net
}
Expand Down
3 changes: 2 additions & 1 deletion Client/mods/deathmatch/logic/CSingularFileDownload.h
Expand Up @@ -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);
Expand All @@ -49,6 +49,7 @@ class CSingularFileDownload
SString m_strNameShort;

CResource* m_pResource;
CResource* m_pRequestResource;

bool m_bComplete;
bool m_bBeingDeleted;
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/CSingularFileDownloadManager.cpp
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp
Expand Up @@ -213,15 +213,15 @@ 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);

// 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;
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h
Expand Up @@ -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);
Expand Down
25 changes: 17 additions & 8 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp
Expand Up @@ -342,9 +342,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())
{
Expand All @@ -353,15 +353,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<CResourceFile*>::const_iterator iter = pResource->IterBeginResourceFiles();
for (; iter != pResource->IterEndResourceFiles(); iter++)
std::list<CResourceFile*>::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;
Expand All @@ -370,6 +375,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);
Expand Down

0 comments on commit 302f512

Please sign in to comment.