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>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
|
2015-02-27 21:09:04 +00:00
|
|
|
#define CLIENT_EXEC "DDNet"
|
|
|
|
#define SERVER_EXEC "DDNet-Server"
|
|
|
|
|
2014-12-31 14:29:34 +00:00
|
|
|
#if defined(CONF_FAMILY_WINDOWS)
|
2015-02-27 21:09:04 +00:00
|
|
|
#define PLAT_EXT ".exe"
|
2014-12-31 14:29:34 +00:00
|
|
|
#elif defined(CONF_FAMILY_UNIX)
|
2015-02-27 21:09:04 +00:00
|
|
|
#define PLAT_EXT ""
|
2014-12-31 14:29:34 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-27 21:09:04 +00:00
|
|
|
#define PLAT_CLIENT_EXEC CLIENT_EXEC PLAT_EXT
|
|
|
|
#define PLAT_SERVER_EXEC SERVER_EXEC PLAT_EXT
|
|
|
|
|
2014-12-31 14:29:34 +00:00
|
|
|
CAutoUpdate::CAutoUpdate()
|
|
|
|
{
|
|
|
|
m_pClient = NULL;
|
|
|
|
m_pStorage = NULL;
|
|
|
|
m_pFetcher = NULL;
|
|
|
|
m_State = CLEAN;
|
|
|
|
m_Percent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::Init()
|
|
|
|
{
|
|
|
|
m_pClient = Kernel()->RequestInterface<IClient>();
|
|
|
|
m_pStorage = Kernel()->RequestInterface<IStorage>();
|
|
|
|
m_pFetcher = Kernel()->RequestInterface<IFetcher>();
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:09:04 +00:00
|
|
|
void CAutoUpdate::ProgressCallback(CFetchTask *pTask, void *pUser)
|
2014-12-31 14:29:34 +00:00
|
|
|
{
|
|
|
|
CAutoUpdate *pUpdate = (CAutoUpdate *)pUser;
|
2015-02-27 21:09:04 +00:00
|
|
|
str_copy(pUpdate->m_Status, pTask->Dest(), sizeof(pUpdate->m_Status));
|
|
|
|
pUpdate->m_Percent = pTask->Progress();
|
2014-12-31 14:29:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 21:09:04 +00:00
|
|
|
void CAutoUpdate::CompletionCallback(CFetchTask *pTask, void *pUser)
|
2014-12-31 14:29:34 +00:00
|
|
|
{
|
|
|
|
CAutoUpdate *pUpdate = (CAutoUpdate *)pUser;
|
2015-02-27 21:09:04 +00:00
|
|
|
if(!str_comp(pTask->Dest(), "update.json")){
|
2014-12-31 14:29:34 +00:00
|
|
|
pUpdate->m_State = GOT_MANIFEST;
|
|
|
|
}
|
2015-02-27 21:09:04 +00:00
|
|
|
else if(!str_comp(pTask->Dest(), pUpdate->m_aLastFile)){
|
2014-12-31 14:29:34 +00:00
|
|
|
if(pUpdate->m_ClientUpdate)
|
|
|
|
pUpdate->ReplaceExecutable();
|
|
|
|
pUpdate->m_State = NEED_RESTART;
|
|
|
|
}
|
2015-02-27 21:09:04 +00:00
|
|
|
delete pTask;
|
2014-12-31 14:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::FetchFile(const char *pFile, const char *pDestPath)
|
|
|
|
{
|
|
|
|
char aBuf[256];
|
|
|
|
str_format(aBuf, sizeof(aBuf), "https://learath2.info/%s", pFile);
|
|
|
|
if(!pDestPath)
|
|
|
|
pDestPath = pFile;
|
2015-02-27 21:09:04 +00:00
|
|
|
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()
|
|
|
|
{
|
|
|
|
switch(m_State){
|
|
|
|
case GOT_MANIFEST:
|
|
|
|
PerformUpdate();
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::AddNewFile(const char *pFile)
|
|
|
|
{
|
2015-02-27 21:09:04 +00:00
|
|
|
//Check if already on the download list
|
2014-12-31 14:29:34 +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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::AddRemovedFile(const char *pFile)
|
|
|
|
{
|
|
|
|
//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));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::ReplaceExecutable()
|
|
|
|
{
|
2015-02-27 21:09:04 +00:00
|
|
|
dbg_msg("autoupdate", "Replacing" PLAT_CLIENT_EXEC);
|
|
|
|
|
|
|
|
//Replace running executable by renaming twice...
|
2015-03-08 17:45:37 +00:00
|
|
|
m_pStorage->RemoveFile("DDNet.old", 2);
|
2015-02-27 21:09:04 +00:00
|
|
|
m_pStorage->RenameFile(PLAT_CLIENT_EXEC, "DDNet.old", 2);
|
|
|
|
m_pStorage->RenameFile("DDNet.tmp", PLAT_CLIENT_EXEC, 2);
|
2014-12-31 14:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::ParseUpdate()
|
|
|
|
{
|
|
|
|
IOHANDLE File = m_pStorage->OpenFile("update.json", IOFLAG_READ, IStorage::TYPE_ALL);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::InitiateUpdate()
|
|
|
|
{
|
|
|
|
m_State = GETTING_MANIFEST;
|
|
|
|
FetchFile("update.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::IgnoreUpdate()
|
|
|
|
{
|
|
|
|
m_State = IGNORED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAutoUpdate::PerformUpdate()
|
|
|
|
{
|
|
|
|
m_State = PARSING_UPDATE;
|
|
|
|
dbg_msg("autoupdate", "Parsing update.json");
|
|
|
|
ParseUpdate();
|
|
|
|
m_State = DOWNLOADING;
|
2015-01-03 21:28:08 +00:00
|
|
|
|
|
|
|
const char *aLastFile;
|
2014-12-31 14:29:34 +00:00
|
|
|
if(m_ClientUpdate)
|
2015-01-03 21:28:08 +00:00
|
|
|
aLastFile = "DDNet.tmp";
|
|
|
|
else if(!m_AddedFiles.empty())
|
|
|
|
aLastFile= m_AddedFiles.front().c_str();
|
2014-12-31 14:29:34 +00:00
|
|
|
else
|
2015-01-03 21:28:08 +00:00
|
|
|
aLastFile = "";
|
|
|
|
|
|
|
|
str_copy(m_aLastFile, aLastFile, sizeof(m_aLastFile));
|
2014-12-31 14:29:34 +00:00
|
|
|
|
|
|
|
while(!m_AddedFiles.empty()){
|
|
|
|
FetchFile(m_AddedFiles.back().c_str());
|
|
|
|
m_AddedFiles.pop_back();
|
|
|
|
}
|
|
|
|
while(!m_RemovedFiles.empty()){
|
|
|
|
m_pStorage->RemoveFile(m_RemovedFiles.back().c_str(), IStorage::TYPE_SAVE);
|
|
|
|
m_RemovedFiles.pop_back();
|
|
|
|
}
|
2015-02-27 21:09:04 +00:00
|
|
|
if(m_ServerUpdate)
|
|
|
|
FetchFile(PLAT_SERVER_EXEC);
|
2014-12-31 14:29:34 +00:00
|
|
|
if(m_ClientUpdate)
|
2015-02-27 21:09:04 +00:00
|
|
|
FetchFile(PLAT_CLIENT_EXEC, "DDNet.tmp");
|
2014-12-31 14:29:34 +00:00
|
|
|
}
|