Fix lots of weirdness in updater

This commit is contained in:
Learath2 2024-01-08 18:01:37 +03:00
parent 9b3ebf3f1b
commit 32e968335b
2 changed files with 56 additions and 39 deletions

View file

@ -13,7 +13,6 @@
#include <cstdlib> // system #include <cstdlib> // system
using std::map;
using std::string; using std::string;
class CUpdaterFetchTask : public CHttpRequest class CUpdaterFetchTask : public CHttpRequest
@ -57,7 +56,6 @@ CUpdaterFetchTask::CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, cons
void CUpdaterFetchTask::OnProgress() void CUpdaterFetchTask::OnProgress()
{ {
CLockScope ls(m_pUpdater->m_Lock); CLockScope ls(m_pUpdater->m_Lock);
str_copy(m_pUpdater->m_aStatus, Dest());
m_pUpdater->m_Percent = Progress(); m_pUpdater->m_Percent = Progress();
} }
@ -75,13 +73,6 @@ void CUpdaterFetchTask::OnCompletion()
else if(State() == HTTP_ERROR) else if(State() == HTTP_ERROR)
m_pUpdater->SetCurrentState(IUpdater::FAIL); m_pUpdater->SetCurrentState(IUpdater::FAIL);
} }
else if(!str_comp(pFileName, m_pUpdater->m_aLastFile))
{
if(State() == HTTP_DONE)
m_pUpdater->SetCurrentState(IUpdater::MOVE_FILES);
else if(State() == HTTP_ERROR)
m_pUpdater->SetCurrentState(IUpdater::FAIL);
}
} }
CUpdater::CUpdater() CUpdater::CUpdater()
@ -92,6 +83,7 @@ CUpdater::CUpdater()
m_pHttp = nullptr; m_pHttp = nullptr;
m_State = CLEAN; m_State = CLEAN;
m_Percent = 0; m_Percent = 0;
m_pCurrentTask = nullptr;
IStorage::FormatTmpPath(m_aClientExecTmp, sizeof(m_aClientExecTmp), CLIENT_EXEC); IStorage::FormatTmpPath(m_aClientExecTmp, sizeof(m_aClientExecTmp), CLIENT_EXEC);
IStorage::FormatTmpPath(m_aServerExecTmp, sizeof(m_aServerExecTmp), SERVER_EXEC); IStorage::FormatTmpPath(m_aServerExecTmp, sizeof(m_aServerExecTmp), SERVER_EXEC);
@ -131,7 +123,10 @@ int CUpdater::GetCurrentPercent()
void CUpdater::FetchFile(const char *pFile, const char *pDestPath) void CUpdater::FetchFile(const char *pFile, const char *pDestPath)
{ {
m_pHttp->Run(std::make_shared<CUpdaterFetchTask>(this, pFile, pDestPath)); CLockScope ls(m_Lock);
m_pCurrentTask = std::make_shared<CUpdaterFetchTask>(this, pFile, pDestPath);
str_copy(m_aStatus, m_pCurrentTask->Dest());
m_pHttp->Run(m_pCurrentTask);
} }
bool CUpdater::MoveFile(const char *pFile) bool CUpdater::MoveFile(const char *pFile)
@ -173,6 +168,9 @@ void CUpdater::Update()
case IUpdater::GOT_MANIFEST: case IUpdater::GOT_MANIFEST:
PerformUpdate(); PerformUpdate();
break; break;
case IUpdater::DOWNLOADING:
RunningUpdate();
break;
case IUpdater::MOVE_FILES: case IUpdater::MOVE_FILES:
CommitUpdate(); CommitUpdate();
break; break;
@ -183,7 +181,7 @@ void CUpdater::Update()
void CUpdater::AddFileJob(const char *pFile, bool Job) void CUpdater::AddFileJob(const char *pFile, bool Job)
{ {
m_FileJobs[string(pFile)] = Job; m_FileJobs.emplace_front(std::make_pair(pFile, Job));
} }
bool CUpdater::ReplaceClient() bool CUpdater::ReplaceClient()
@ -276,33 +274,39 @@ void CUpdater::ParseUpdate()
void CUpdater::InitiateUpdate() void CUpdater::InitiateUpdate()
{ {
m_State = GETTING_MANIFEST; SetCurrentState(IUpdater::GETTING_MANIFEST);
FetchFile("update.json"); FetchFile("update.json");
} }
void CUpdater::PerformUpdate() void CUpdater::PerformUpdate()
{ {
m_State = PARSING_UPDATE; SetCurrentState(IUpdater::PARSING_UPDATE);
dbg_msg("updater", "parsing update.json"); dbg_msg("updater", "parsing update.json");
ParseUpdate(); ParseUpdate();
m_State = DOWNLOADING; m_CurrentJob = m_FileJobs.begin();
SetCurrentState(IUpdater::DOWNLOADING);
}
const char *pLastFile; void CUpdater::RunningUpdate()
pLastFile = ""; {
for(map<string, bool>::reverse_iterator it = m_FileJobs.rbegin(); it != m_FileJobs.rend(); ++it) if(m_pCurrentTask)
{ {
if(it->second) if(!m_pCurrentTask->Done())
{ {
pLastFile = it->first.c_str(); return;
break; }
else if(m_pCurrentTask->State() == HTTP_ERROR || m_pCurrentTask->State() == HTTP_ABORTED)
{
SetCurrentState(IUpdater::FAIL);
} }
} }
for(auto &FileJob : m_FileJobs) if(m_CurrentJob != m_FileJobs.end())
{ {
if(FileJob.second) auto &Job = *m_CurrentJob;
if(Job.second)
{ {
const char *pFile = FileJob.first.c_str(); const char *pFile = Job.first.c_str();
size_t len = str_length(pFile); size_t len = str_length(pFile);
if(!str_comp_nocase(pFile + len - 4, ".dll")) if(!str_comp_nocase(pFile + len - 4, ".dll"))
{ {
@ -330,24 +334,32 @@ void CUpdater::PerformUpdate()
{ {
FetchFile(pFile); FetchFile(pFile);
} }
pLastFile = pFile;
} }
else else
m_pStorage->RemoveBinaryFile(FileJob.first.c_str()); {
} m_pStorage->RemoveBinaryFile(Job.first.c_str());
}
if(m_ServerUpdate) m_CurrentJob++;
{
FetchFile(PLAT_SERVER_DOWN, m_aServerExecTmp);
pLastFile = m_aServerExecTmp;
} }
if(m_ClientUpdate) else
{ {
FetchFile(PLAT_CLIENT_DOWN, m_aClientExecTmp); if(m_ServerUpdate)
pLastFile = m_aClientExecTmp; {
} FetchFile(PLAT_SERVER_DOWN, m_aServerExecTmp);
m_ServerUpdate = false;
return;
}
str_copy(m_aLastFile, pLastFile); if(m_ClientUpdate)
{
FetchFile(PLAT_SERVER_DOWN, m_aServerExecTmp);
m_ClientUpdate = false;
return;
}
SetCurrentState(IUpdater::MOVE_FILES);
}
} }
void CUpdater::CommitUpdate() void CUpdater::CommitUpdate()

View file

@ -5,7 +5,8 @@
#include <engine/updater.h> #include <engine/updater.h>
#include <map> #include <forward_list>
#include <memory>
#include <string> #include <string>
#define CLIENT_EXEC "DDNet" #define CLIENT_EXEC "DDNet"
@ -34,6 +35,8 @@
#define PLAT_CLIENT_EXEC CLIENT_EXEC PLAT_EXT #define PLAT_CLIENT_EXEC CLIENT_EXEC PLAT_EXT
#define PLAT_SERVER_EXEC SERVER_EXEC PLAT_EXT #define PLAT_SERVER_EXEC SERVER_EXEC PLAT_EXT
class CUpdaterFetchTask;
class CUpdater : public IUpdater class CUpdater : public IUpdater
{ {
friend class CUpdaterFetchTask; friend class CUpdaterFetchTask;
@ -48,21 +51,23 @@ class CUpdater : public IUpdater
int m_State; int m_State;
char m_aStatus[256] GUARDED_BY(m_Lock); char m_aStatus[256] GUARDED_BY(m_Lock);
int m_Percent GUARDED_BY(m_Lock); int m_Percent GUARDED_BY(m_Lock);
char m_aLastFile[256];
char m_aClientExecTmp[64]; char m_aClientExecTmp[64];
char m_aServerExecTmp[64]; char m_aServerExecTmp[64];
std::forward_list<std::pair<std::string, bool>> m_FileJobs;
std::shared_ptr<CUpdaterFetchTask> m_pCurrentTask;
decltype(m_FileJobs)::iterator m_CurrentJob;
bool m_ClientUpdate; bool m_ClientUpdate;
bool m_ServerUpdate; bool m_ServerUpdate;
std::map<std::string, bool> m_FileJobs;
void AddFileJob(const char *pFile, bool Job); void AddFileJob(const char *pFile, bool Job);
void FetchFile(const char *pFile, const char *pDestPath = nullptr); void FetchFile(const char *pFile, const char *pDestPath = nullptr);
bool MoveFile(const char *pFile); bool MoveFile(const char *pFile);
void ParseUpdate(); void ParseUpdate();
void PerformUpdate(); void PerformUpdate();
void RunningUpdate();
void CommitUpdate(); void CommitUpdate();
bool ReplaceClient(); bool ReplaceClient();