Skip to content

Commit 302f512

Browse files
authored
downloadFile: downloading from another resource (#945)
1 parent fa6bdb6 commit 302f512

File tree

7 files changed

+38
-17
lines changed

7 files changed

+38
-17
lines changed

Client/mods/deathmatch/logic/CSingularFileDownload.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
#include <StdInc.h>
1313

14-
CSingularFileDownload::CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CChecksum serverChecksum)
14+
CSingularFileDownload::CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CResource* pRequestResource, CChecksum serverChecksum)
1515
{
1616
// Store the name
1717
m_strName = szName;
1818

1919
// Store the name (short)
2020
m_strNameShort = szNameShort;
2121

22-
// store the resource
22+
// store resources
2323
m_pResource = pResource;
24+
m_pRequestResource = pRequestResource;
2425

2526
// Store the server checksum
2627
m_ServerChecksum = serverChecksum;
@@ -65,6 +66,15 @@ void CSingularFileDownload::CallFinished(bool bSuccess)
6566
CLuaArguments Arguments;
6667
Arguments.PushString(GetShortName()); // file name
6768
Arguments.PushBoolean(bSuccess); // Completed successfully?
69+
if (m_pRequestResource)
70+
{
71+
Arguments.PushResource(m_pRequestResource); // Resource that called downloadFile
72+
}
73+
else
74+
{
75+
Arguments.PushBoolean(false); // or false
76+
}
77+
6878
m_pResource->GetResourceEntity()->CallEvent("onClientFileDownloadComplete", Arguments, false);
6979
}
7080
SetComplete();
@@ -74,6 +84,7 @@ void CSingularFileDownload::Cancel()
7484
{
7585
m_bBeingDeleted = true;
7686
m_pResource = NULL;
87+
m_pRequestResource = NULL;
7788

7889
// TODO: Cancel also in Net
7990
}

Client/mods/deathmatch/logic/CSingularFileDownload.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class CSingularFileDownload
2525
{
2626
public:
27-
CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CChecksum serverChecksum);
27+
CSingularFileDownload(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CResource* pRequestResource, CChecksum serverChecksum);
2828
~CSingularFileDownload();
2929

3030
static void DownloadFinishedCallBack(const SHttpDownloadResult& result);
@@ -49,6 +49,7 @@ class CSingularFileDownload
4949
SString m_strNameShort;
5050

5151
CResource* m_pResource;
52+
CResource* m_pRequestResource;
5253

5354
bool m_bComplete;
5455
bool m_bBeingDeleted;

Client/mods/deathmatch/logic/CSingularFileDownloadManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ CSingularFileDownloadManager::~CSingularFileDownloadManager()
2323
}
2424

2525
CSingularFileDownload* CSingularFileDownloadManager::AddFile(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL,
26-
CChecksum checksum)
26+
CResource* pRequestResource, CChecksum checksum)
2727
{
28-
CSingularFileDownload* pFile = new CSingularFileDownload(pResource, szName, szNameShort, strHTTPURL, checksum);
28+
CSingularFileDownload* pFile = new CSingularFileDownload(pResource, szName, szNameShort, strHTTPURL, pRequestResource, checksum);
2929
m_Downloads.push_back(pFile);
3030
return NULL;
3131
}

Client/mods/deathmatch/logic/CSingularFileDownloadManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CSingularFileDownloadManager
2828
CSingularFileDownloadManager();
2929
~CSingularFileDownloadManager();
3030

31-
CSingularFileDownload* AddFile(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CChecksum checksum = CChecksum());
31+
CSingularFileDownload* AddFile(CResource* pResource, const char* szName, const char* szNameShort, SString strHTTPURL, CResource* pRequestResource, CChecksum checksum = CChecksum());
3232
void CancelResourceDownloads(CResource* pResource);
3333

3434
void ClearList();

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,15 @@ bool CStaticFunctionDefinitions::WasEventCancelled()
213213
return m_pEvents->WasEventCancelled();
214214
}
215215

216-
bool CStaticFunctionDefinitions::DownloadFile(CResource* pResource, const char* szFile, CChecksum checksum)
216+
bool CStaticFunctionDefinitions::DownloadFile(CResource* pResource, const char* szFile, CResource* pRequestResource, CChecksum checksum)
217217
{
218218
SString strHTTPDownloadURLFull("%s/%s/%s", g_pClientGame->GetHTTPURL().c_str(), pResource->GetName(), szFile);
219219
SString strPath("%s\\resources\\%s\\%s", g_pClientGame->GetFileCacheRoot(), pResource->GetName(), szFile);
220220

221221
// Call SingularFileDownloadManager
222222
if (g_pClientGame->GetSingularFileDownloadManager())
223223
{
224-
g_pClientGame->GetSingularFileDownloadManager()->AddFile(pResource, strPath.c_str(), szFile, strHTTPDownloadURLFull, checksum);
224+
g_pClientGame->GetSingularFileDownloadManager()->AddFile(pResource, strPath.c_str(), szFile, strHTTPDownloadURLFull, pRequestResource, checksum);
225225
return true;
226226
}
227227
return false;

Client/mods/deathmatch/logic/CStaticFunctionDefinitions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CStaticFunctionDefinitions
3535
static bool WasEventCancelled();
3636

3737
// Misc funcs
38-
static bool DownloadFile(CResource* pResource, const char* szFile, CChecksum checksum = CChecksum());
38+
static bool DownloadFile(CResource* pResource, const char* szFile, CResource* pRequestResource, CChecksum checksum = CChecksum());
3939

4040
// Output funcs
4141
static bool OutputConsole(const char* szText);

Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ int CLuaFunctionDefs::GetDevelopmentMode(lua_State* luaVM)
342342

343343
int CLuaFunctionDefs::DownloadFile(lua_State* luaVM)
344344
{
345-
SString strFile = "";
345+
SString strFileInput = "";
346346
CScriptArgReader argStream(luaVM);
347-
argStream.ReadString(strFile);
347+
argStream.ReadString(strFileInput);
348348

349349
if (!argStream.HasErrors())
350350
{
@@ -353,15 +353,20 @@ int CLuaFunctionDefs::DownloadFile(lua_State* luaVM)
353353
if (pLuaMain)
354354
{
355355
// Grab its resource
356-
CResource* pResource = pLuaMain->GetResource();
357-
if (pResource)
356+
CResource* pThisResource = pLuaMain->GetResource();
357+
CResource* pOtherResource = pThisResource;
358+
359+
SString strMetaPath;
360+
361+
// Resolve other resource from name
362+
if (CResourceManager::ParseResourcePathInput(strFileInput, pOtherResource, NULL, &strMetaPath))
358363
{
359-
std::list<CResourceFile*>::const_iterator iter = pResource->IterBeginResourceFiles();
360-
for (; iter != pResource->IterEndResourceFiles(); iter++)
364+
std::list<CResourceFile*>::const_iterator iter = pOtherResource->IterBeginResourceFiles();
365+
for (; iter != pOtherResource->IterEndResourceFiles(); iter++)
361366
{
362-
if (strcmp(strFile, (*iter)->GetShortName()) == 0)
367+
if (strcmp(strMetaPath, (*iter)->GetShortName()) == 0)
363368
{
364-
if (CStaticFunctionDefinitions::DownloadFile(pResource, strFile, (*iter)->GetServerChecksum()))
369+
if (CStaticFunctionDefinitions::DownloadFile(pOtherResource, strMetaPath, pThisResource, (*iter)->GetServerChecksum()))
365370
{
366371
lua_pushboolean(luaVM, true);
367372
return 1;
@@ -370,6 +375,10 @@ int CLuaFunctionDefs::DownloadFile(lua_State* luaVM)
370375
}
371376
m_pScriptDebugging->LogCustom(luaVM, 255, 255, 255, "%s: File doesn't exist", lua_tostring(luaVM, lua_upvalueindex(1)));
372377
}
378+
else
379+
{
380+
m_pScriptDebugging->LogCustom(luaVM, 255, 255, 255, "%s: Invalid path", lua_tostring(luaVM, lua_upvalueindex(1)));
381+
}
373382
}
374383
}
375384
lua_pushboolean(luaVM, false);

0 commit comments

Comments
 (0)