ddnet/src/engine/client/autoupdate.cpp

250 lines
6.7 KiB
C++
Raw Normal View History

2014-12-31 14:29:34 +00:00
#include "autoupdate.h"
#include <base/system.h>
#include <engine/fetcher.h>
#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::vector;
CAutoUpdate::CAutoUpdate()
{
2015-03-13 14:13:19 +00:00
m_pClient = NULL;
m_pStorage = NULL;
m_pFetcher = NULL;
m_State = CLEAN;
m_Percent = 0;
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::Init()
{
2015-03-13 14:13:19 +00:00
m_pClient = Kernel()->RequestInterface<IClient>();
m_pStorage = Kernel()->RequestInterface<IStorage>();
m_pFetcher = Kernel()->RequestInterface<IFetcher>();
2015-04-18 14:23:43 +00:00
m_IsWinXP = os_compare_version(5, 1) <= 0;
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::ProgressCallback(CFetchTask *pTask, void *pUser)
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
CAutoUpdate *pUpdate = (CAutoUpdate *)pUser;
str_copy(pUpdate->m_Status, pTask->Dest(), sizeof(pUpdate->m_Status));
pUpdate->m_Percent = pTask->Progress();
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::CompletionCallback(CFetchTask *pTask, void *pUser)
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
CAutoUpdate *pUpdate = (CAutoUpdate *)pUser;
if(!str_comp(pTask->Dest(), "update.json"))
{
2015-03-13 19:18:00 +00:00
if(pTask->State() == CFetchTask::STATE_DONE)
pUpdate->m_State = GOT_MANIFEST;
else if(pTask->State() == CFetchTask::STATE_ERROR)
2015-03-13 19:27:37 +00:00
pUpdate->m_State = FAIL;
2015-03-13 14:13:19 +00:00
}
else if(!str_comp(pTask->Dest(), pUpdate->m_aLastFile))
{
2015-03-13 19:27:37 +00:00
if(pTask->State() == CFetchTask::STATE_DONE)
{
if(pUpdate->m_ClientUpdate)
pUpdate->ReplaceClient();
if(pUpdate->m_ServerUpdate)
pUpdate->ReplaceServer();
if(pUpdate->m_pClient->State() == IClient::STATE_ONLINE || pUpdate->m_pClient->EditorHasUnsavedData())
pUpdate->m_State = NEED_RESTART;
else{
if(!pUpdate->m_IsWinXP)
pUpdate->m_pClient->Restart();
else
pUpdate->WinXpRestart();
}
2015-03-13 19:27:37 +00:00
}
else if(pTask->State() == CFetchTask::STATE_ERROR)
pUpdate->m_State = FAIL;
2015-03-13 14:13:19 +00:00
}
delete pTask;
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::FetchFile(const char *pFile, const char *pDestPath)
{
2015-03-13 14:13:19 +00:00
char aBuf[256];
str_format(aBuf, sizeof(aBuf), "https://%s/%s", g_Config.m_ClDDNetUpdateServer, pFile);
if(!pDestPath)
pDestPath = pFile;
CFetchTask *Task = new CFetchTask;
m_pFetcher->QueueAdd(Task, aBuf, pDestPath, -2, this, &CAutoUpdate::CompletionCallback, &CAutoUpdate::ProgressCallback);
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::Update()
{
2015-03-13 14:13:19 +00:00
switch(m_State)
{
case GOT_MANIFEST:
PerformUpdate();
default:
return;
}
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::AddNewFile(const char *pFile)
{
//Check if already on the download list
2015-03-13 14:13:19 +00:00
for(vector<string>::iterator it = m_AddedFiles.begin(); it < m_AddedFiles.end(); ++it)
{
if(!str_comp(it->c_str(), pFile))
return;
}
m_AddedFiles.push_back(string(pFile));
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::AddRemovedFile(const char *pFile)
{
2015-03-13 14:13:19 +00:00
//First remove from to be downloaded list
for(vector<string>::iterator it = m_AddedFiles.begin(); it < m_AddedFiles.end(); ++it)
{
if(!str_comp(it->c_str(), pFile)){
m_AddedFiles.erase(it);
break;
}
}
m_RemovedFiles.push_back(string(pFile));
2014-12-31 14:29:34 +00:00
}
2015-03-13 14:13:19 +00:00
void CAutoUpdate::ReplaceClient()
2014-12-31 14:29:34 +00:00
{
2015-03-13 14:13:19 +00:00
dbg_msg("autoupdate", "Replacing " PLAT_CLIENT_EXEC);
//Replace running executable by renaming twice...
if(m_IsWinXP)
{
m_pStorage->RemoveBinaryFile("DDNet.old");
m_pStorage->RenameBinaryFile(PLAT_CLIENT_EXEC, "DDNet.old");
m_pStorage->RenameBinaryFile("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))
2015-03-13 14:13:19 +00:00
dbg_msg("autoupdate", "Error setting client executable bit");
#endif
}
2015-03-13 14:13:19 +00:00
void CAutoUpdate::ReplaceServer()
{
dbg_msg("autoupdate", "Replacing " PLAT_SERVER_EXEC);
//Replace running executable by renaming twice...
m_pStorage->RemoveBinaryFile("DDNet-Server.old");
m_pStorage->RenameBinaryFile(PLAT_SERVER_EXEC, "DDNet-Server.old");
m_pStorage->RenameBinaryFile("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))
2015-03-13 14:13:19 +00:00
dbg_msg("autoupdate", "Error setting server executable bit");
#endif
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::ParseUpdate()
{
char aPath[512];
IOHANDLE File = m_pStorage->OpenFile(m_pStorage->GetBinaryPath("update.json", aPath, sizeof aPath), IOFLAG_READ, IStorage::TYPE_ALL);
2015-03-13 14:13:19 +00:00
if(File)
{
char aBuf[4096*4];
mem_zero(aBuf, sizeof (aBuf));
io_read(File, aBuf, sizeof(aBuf));
io_close(File);
json_value *pVersions = json_parse(aBuf);
if(pVersions && pVersions->type == json_array)
{
for(int i = 0; i < json_array_length(pVersions); i++)
{
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))
{
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++)
AddNewFile(json_string_get(json_array_get(pTemp, j)));
}
if((pTemp = json_object_get(pCurrent, "remove"))->type == json_array)
{
for(int j = 0; j < json_array_length(pTemp); j++)
AddRemovedFile(json_string_get(json_array_get(pTemp, j)));
}
}
else
break;
}
}
}
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::InitiateUpdate()
{
2015-03-13 14:13:19 +00:00
m_State = GETTING_MANIFEST;
FetchFile("update.json");
2014-12-31 14:29:34 +00:00
}
void CAutoUpdate::PerformUpdate()
{
2015-03-13 14:13:19 +00:00
m_State = PARSING_UPDATE;
dbg_msg("autoupdate", "Parsing update.json");
ParseUpdate();
m_State = DOWNLOADING;
const char *aLastFile;
if(m_ClientUpdate)
aLastFile = "DDNet.tmp";
else if(!m_AddedFiles.empty())
aLastFile= m_AddedFiles.front().c_str();
else
aLastFile = "";
str_copy(m_aLastFile, aLastFile, sizeof(m_aLastFile));
while(!m_AddedFiles.empty())
{
FetchFile(m_AddedFiles.back().c_str());
m_AddedFiles.pop_back();
}
while(!m_RemovedFiles.empty())
{
m_pStorage->RemoveBinaryFile(m_RemovedFiles.back().c_str());
2015-03-13 14:13:19 +00:00
m_RemovedFiles.pop_back();
}
if(m_ServerUpdate)
FetchFile(PLAT_SERVER_DOWN, "DDNet-Server.tmp");
if(m_ClientUpdate)
FetchFile(PLAT_CLIENT_DOWN, "DDNet.tmp");
}
void CAutoUpdate::WinXpRestart()
{
char aBuf[512];
IOHANDLE bhFile = io_open(m_pStorage->GetBinaryPath("du.bat", aBuf, sizeof aBuf), IOFLAG_WRITE);
if(!bhFile)
return;
char bBuf[512];
str_format(bBuf, sizeof(bBuf), ":_R\r\ndel \"DDNet.exe\"\r\nif exist \"DDNet.exe\" goto _R\r\nrename \"DDNet.tmp\" \"DDNet.exe\"\r\n:_T\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();
}