ddnet/src/engine/client/updater.cpp

375 lines
9 KiB
C++
Raw Normal View History

#include "updater.h"
2014-12-31 14:29:34 +00:00
#include <base/system.h>
#include <engine/engine.h>
2014-12-31 14:29:34 +00:00
#include <engine/storage.h>
#include <engine/client.h>
#include <engine/external/json-parser/json.h>
#include <game/version.h>
2015-03-13 14:13:19 +00:00
#include <stdlib.h> // system
2014-12-31 14:29:34 +00:00
using std::string;
using std::map;
2014-12-31 14:29:34 +00:00
class CUpdaterFetchTask : public CGetFile
{
char m_aBuf[256];
char m_aBuf2[256];
CUpdater *m_pUpdater;
void OnCompletion();
void OnProgress();
public:
CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, const char *pDestPath);
};
static const char *GetUpdaterUrl(char *pBuf, int BufSize, const char *pFile)
{
str_format(pBuf, BufSize, "https://update4.ddnet.tw/%s", pFile);
return pBuf;
}
static const char *GetUpdaterDestPath(char *pBuf, int BufSize, const char *pFile, const char *pDestPath)
{
if(!pDestPath)
{
pDestPath = pFile;
}
str_format(pBuf, BufSize, "update/%s", pDestPath);
return pBuf;
}
CUpdaterFetchTask::CUpdaterFetchTask(CUpdater *pUpdater, const char *pFile, const char *pDestPath) :
CGetFile(pUpdater->m_pStorage, GetUpdaterUrl(m_aBuf, sizeof(m_aBuf), pFile), GetUpdaterDestPath(m_aBuf2, sizeof(m_aBuf), pFile, pDestPath), -2, false),
m_pUpdater(pUpdater)
{
}
void CUpdaterFetchTask::OnProgress()
{
lock_wait(m_pUpdater->m_Lock);
str_copy(m_pUpdater->m_aStatus, Dest(), sizeof(m_pUpdater->m_aStatus));
m_pUpdater->m_Percent = Progress();
lock_unlock(m_pUpdater->m_Lock);
}
void CUpdaterFetchTask::OnCompletion()
{
const char *b = 0;
for(const char *a = Dest(); *a; a++)
if(*a == '/')
b = a + 1;
b = b ? b : Dest();
if(!str_comp(b, "update.json"))
{
if(State() == HTTP_DONE)
m_pUpdater->SetCurrentState(IUpdater::GOT_MANIFEST);
else if(State() == HTTP_ERROR)
m_pUpdater->SetCurrentState(IUpdater::FAIL);
}
else if(!str_comp(b, 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()
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
m_pClient = NULL;
m_pStorage = NULL;
m_pEngine = NULL;
2015-03-13 14:13:19 +00:00
m_State = CLEAN;
m_Percent = 0;
m_Lock = lock_create();
2014-12-31 14:29:34 +00:00
}
void CUpdater::Init()
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
m_pClient = Kernel()->RequestInterface<IClient>();
m_pStorage = Kernel()->RequestInterface<IStorage>();
m_pEngine = Kernel()->RequestInterface<IEngine>();
m_IsWinXP = os_is_winxp_or_lower();
2014-12-31 14:29:34 +00:00
}
CUpdater::~CUpdater()
2014-12-31 14:29:34 +00:00
{
lock_destroy(m_Lock);
2014-12-31 14:29:34 +00:00
}
void CUpdater::SetCurrentState(int NewState)
2014-12-31 14:29:34 +00:00
{
lock_wait(m_Lock);
m_State = NewState;
lock_unlock(m_Lock);
}
int CUpdater::GetCurrentState()
{
lock_wait(m_Lock);
int Result = m_State;
lock_unlock(m_Lock);
return Result;
}
void CUpdater::GetCurrentFile(char *pBuf, int BufSize)
{
lock_wait(m_Lock);
str_copy(pBuf, m_aStatus, BufSize);
lock_unlock(m_Lock);
}
int CUpdater::GetCurrentPercent()
{
lock_wait(m_Lock);
int Result = m_Percent;
lock_unlock(m_Lock);
return Result;
2014-12-31 14:29:34 +00:00
}
void CUpdater::FetchFile(const char *pFile, const char *pDestPath)
2014-12-31 14:29:34 +00:00
{
m_pEngine->AddJob(std::make_shared<CUpdaterFetchTask>(this, pFile, pDestPath));
}
2017-02-21 16:10:08 +00:00
bool CUpdater::MoveFile(const char *pFile)
{
char aBuf[256];
size_t len = str_length(pFile);
bool Success = true;
2016-05-01 09:23:56 +00:00
if(!str_comp_nocase(pFile + len - 4, ".dll") || !str_comp_nocase(pFile + len - 4, ".ttf"))
{
str_format(aBuf, sizeof(aBuf), "%s.old", pFile);
Success &= m_pStorage->RenameBinaryFile(pFile, aBuf);
str_format(aBuf, sizeof(aBuf), "update/%s", pFile);
Success &= m_pStorage->RenameBinaryFile(aBuf, pFile);
}
else
{
str_format(aBuf, sizeof(aBuf), "update/%s", pFile);
Success &= m_pStorage->RenameBinaryFile(aBuf, pFile);
}
return Success;
2014-12-31 14:29:34 +00:00
}
void CUpdater::Update()
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
switch(m_State)
{
case IUpdater::GOT_MANIFEST:
2015-03-13 14:13:19 +00:00
PerformUpdate();
break;
case IUpdater::MOVE_FILES:
CommitUpdate();
break;
2015-03-13 14:13:19 +00:00
default:
return;
}
2014-12-31 14:29:34 +00:00
}
void CUpdater::AddFileJob(const char *pFile, bool Job)
2014-12-31 14:29:34 +00:00
{
m_FileJobs[string(pFile)] = Job;
2014-12-31 14:29:34 +00:00
}
bool CUpdater::ReplaceClient()
2014-12-31 14:29:34 +00:00
{
dbg_msg("updater", "replacing " PLAT_CLIENT_EXEC);
bool Success = true;
2015-03-13 14:13:19 +00:00
// Replace running executable by renaming twice...
2015-05-08 18:40:47 +00:00
if(!m_IsWinXP)
{
m_pStorage->RemoveBinaryFile("DDNet.old");
Success &= m_pStorage->RenameBinaryFile(PLAT_CLIENT_EXEC, "DDNet.old");
Success &= m_pStorage->RenameBinaryFile("update/DDNet.tmp", PLAT_CLIENT_EXEC);
}
2015-03-13 14:13:19 +00:00
#if !defined(CONF_FAMILY_WINDOWS)
char aPath[512];
m_pStorage->GetBinaryPath(PLAT_CLIENT_EXEC, aPath, sizeof aPath);
char aBuf[512];
str_format(aBuf, sizeof aBuf, "chmod +x %s", aPath);
if(system(aBuf))
{
dbg_msg("updater", "ERROR: failed to set client executable bit");
Success &= false;
}
2015-03-13 14:13:19 +00:00
#endif
return Success;
2015-03-13 14:13:19 +00:00
}
bool CUpdater::ReplaceServer()
2015-03-13 14:13:19 +00:00
{
dbg_msg("updater", "replacing " PLAT_SERVER_EXEC);
bool Success = true;
2015-03-13 14:13:19 +00:00
//Replace running executable by renaming twice...
m_pStorage->RemoveBinaryFile("DDNet-Server.old");
Success &= m_pStorage->RenameBinaryFile(PLAT_SERVER_EXEC, "DDNet-Server.old");
Success &= m_pStorage->RenameBinaryFile("update/DDNet-Server.tmp", PLAT_SERVER_EXEC);
2015-03-13 14:13:19 +00:00
#if !defined(CONF_FAMILY_WINDOWS)
char aPath[512];
m_pStorage->GetBinaryPath(PLAT_SERVER_EXEC, aPath, sizeof aPath);
char aBuf[512];
str_format(aBuf, sizeof aBuf, "chmod +x %s", aPath);
if (system(aBuf))
{
dbg_msg("updater", "ERROR: failed to set server executable bit");
Success &= false;
}
2015-03-13 14:13:19 +00:00
#endif
return Success;
2014-12-31 14:29:34 +00:00
}
void CUpdater::ParseUpdate()
2014-12-31 14:29:34 +00:00
{
char aPath[512];
IOHANDLE File = m_pStorage->OpenFile(m_pStorage->GetBinaryPath("update/update.json", aPath, sizeof aPath), IOFLAG_READ, IStorage::TYPE_ALL);
if(!File)
return;
char aBuf[4096*4];
mem_zero(aBuf, sizeof (aBuf));
io_read(File, aBuf, sizeof(aBuf));
io_close(File);
2015-03-13 14:13:19 +00:00
json_value *pVersions = json_parse(aBuf, sizeof(aBuf));
2015-03-13 14:13:19 +00:00
if(pVersions && pVersions->type == json_array)
{
for(int i = 0; i < json_array_length(pVersions); i++)
2015-03-13 14:13:19 +00:00
{
const json_value *pTemp;
const json_value *pCurrent = json_array_get(pVersions, i);
if(str_comp(json_string_get(json_object_get(pCurrent, "version")), GAME_RELEASE_VERSION))
2015-03-13 14:13:19 +00:00
{
if(json_boolean_get(json_object_get(pCurrent, "client")))
m_ClientUpdate = true;
if(json_boolean_get(json_object_get(pCurrent, "server")))
m_ServerUpdate = true;
if((pTemp = json_object_get(pCurrent, "download"))->type == json_array)
{
for(int j = 0; j < json_array_length(pTemp); j++)
AddFileJob(json_string_get(json_array_get(pTemp, j)), true);
}
if((pTemp = json_object_get(pCurrent, "remove"))->type == json_array)
2015-03-13 14:13:19 +00:00
{
for(int j = 0; j < json_array_length(pTemp); j++)
AddFileJob(json_string_get(json_array_get(pTemp, j)), false);
2015-03-13 14:13:19 +00:00
}
}
else
break;
2015-03-13 14:13:19 +00:00
}
}
2014-12-31 14:29:34 +00:00
}
void CUpdater::InitiateUpdate()
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
m_State = GETTING_MANIFEST;
FetchFile("update.json");
2014-12-31 14:29:34 +00:00
}
void CUpdater::PerformUpdate()
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
m_State = PARSING_UPDATE;
dbg_msg("updater", "parsing update.json");
2015-03-13 14:13:19 +00:00
ParseUpdate();
m_State = DOWNLOADING;
const char *aLastFile;
aLastFile = "";
2017-03-04 14:43:49 +00:00
for(map<string, bool>::reverse_iterator it = m_FileJobs.rbegin(); it != m_FileJobs.rend(); ++it)
{
if(it->second)
{
aLastFile = it->first.c_str();
break;
}
2015-03-13 14:13:19 +00:00
}
for(map<string, bool>::iterator it = m_FileJobs.begin(); it != m_FileJobs.end(); ++it)
2015-03-13 14:13:19 +00:00
{
if(it->second)
{
const char *pFile = it->first.c_str();
size_t len = str_length(pFile);
if(!str_comp_nocase(pFile + len - 4, ".dll"))
{
#if defined(CONF_FAMILY_WINDOWS)
char aBuf[512];
str_copy(aBuf, pFile, sizeof(aBuf)); // SDL
str_copy(aBuf + len - 4, "-" PLAT_NAME, sizeof(aBuf) - len + 4); // -win32
str_append(aBuf, pFile + len - 4, sizeof(aBuf)); // .dll
FetchFile(aBuf, pFile);
#endif
// Ignore DLL downloads on other platforms, on Linux we statically link anyway
}
else
{
FetchFile(pFile);
}
aLastFile = pFile;
}
else
m_pStorage->RemoveBinaryFile(it->first.c_str());
2015-03-13 14:13:19 +00:00
}
2015-03-13 14:13:19 +00:00
if(m_ServerUpdate)
{
2015-03-13 14:13:19 +00:00
FetchFile(PLAT_SERVER_DOWN, "DDNet-Server.tmp");
aLastFile = "DDNet-Server.tmp";
}
2015-03-13 14:13:19 +00:00
if(m_ClientUpdate)
{
2015-03-13 14:13:19 +00:00
FetchFile(PLAT_CLIENT_DOWN, "DDNet.tmp");
aLastFile = "DDNet.tmp";
}
str_copy(m_aLastFile, aLastFile, sizeof(m_aLastFile));
}
void CUpdater::CommitUpdate()
{
bool Success = true;
for(map<std::string, bool>::iterator it = m_FileJobs.begin(); it != m_FileJobs.end(); ++it)
if(it->second)
Success &= MoveFile(it->first.c_str());
if(m_ClientUpdate)
Success &= ReplaceClient();
if(m_ServerUpdate)
Success &= ReplaceServer();
if(!Success)
m_State = FAIL;
else if(m_pClient->State() == IClient::STATE_ONLINE || m_pClient->EditorHasUnsavedData())
m_State = NEED_RESTART;
else
{
if(!m_IsWinXP)
m_pClient->Restart();
else
WinXpRestart();
}
2015-03-13 14:13:19 +00:00
}
void CUpdater::WinXpRestart()
2015-07-09 00:08:14 +00:00
{
char aBuf[512];
IOHANDLE bhFile = io_open(m_pStorage->GetBinaryPath("du.bat", aBuf, sizeof aBuf), IOFLAG_WRITE);
if(!bhFile)
return;
char bBuf[512];
2016-05-01 18:12:17 +00:00
str_format(bBuf, sizeof(bBuf), ":_R\r\ndel \"DDNet.exe\"\r\nif exist \"DDNet.exe\" goto _R\r\n:_T\r\nmove /y \"update\\DDNet.tmp\" \"DDNet.exe\"\r\nif not exist \"DDNet.exe\" goto _T\r\nstart DDNet.exe\r\ndel \"du.bat\"\r\n");
io_write(bhFile, bBuf, str_length(bBuf));
io_close(bhFile);
shell_execute(aBuf);
m_pClient->Quit();
}