mirror of
https://github.com/The-Powder-Toy/The-Powder-Toy.git
synced 2025-08-16 13:24:02 +02:00
Move update code to Platform
This commit is contained in:
155
src/Update.cpp
155
src/Update.cpp
@@ -1,155 +0,0 @@
|
|||||||
#include "Update.h"
|
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cstdlib>
|
|
||||||
#ifndef WIN
|
|
||||||
#include <sys/param.h>
|
|
||||||
#endif
|
|
||||||
#if !defined(MACOSX) && !defined(BSD)
|
|
||||||
#include <malloc.h>
|
|
||||||
#endif
|
|
||||||
#include <cstring>
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#ifdef WIN
|
|
||||||
# ifndef NOMINMAX
|
|
||||||
# define NOMINMAX
|
|
||||||
# endif
|
|
||||||
# include <windows.h>
|
|
||||||
#else
|
|
||||||
# include <unistd.h>
|
|
||||||
# include <sys/stat.h>
|
|
||||||
#endif
|
|
||||||
#ifdef MACOSX
|
|
||||||
# include <mach-o/dyld.h>
|
|
||||||
# include <errno.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "common/Platform.h"
|
|
||||||
|
|
||||||
// returns 1 on failure, 0 on success
|
|
||||||
int update_start(char *data, unsigned int len)
|
|
||||||
{
|
|
||||||
ByteString exeName = Platform::ExecutableName(), updName;
|
|
||||||
FILE *f;
|
|
||||||
|
|
||||||
if (!exeName.length())
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
#ifdef WIN
|
|
||||||
updName = exeName;
|
|
||||||
ByteString extension = exeName.substr(exeName.length() - 4);
|
|
||||||
if (extension == ".exe")
|
|
||||||
updName = exeName.substr(0, exeName.length() - 4);
|
|
||||||
updName = updName + "_upd.exe";
|
|
||||||
|
|
||||||
if (!MoveFile(Platform::WinWiden(exeName).c_str(), Platform::WinWiden(updName).c_str()))
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
f = fopen(exeName.c_str(), "wb");
|
|
||||||
if (!f)
|
|
||||||
return 1;
|
|
||||||
if (fwrite(data, 1, len, f) != len)
|
|
||||||
{
|
|
||||||
fclose(f);
|
|
||||||
Platform::RemoveFile(exeName);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if ((uintptr_t)ShellExecute(NULL, L"open", Platform::WinWiden(exeName).c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32)
|
|
||||||
{
|
|
||||||
Platform::RemoveFile(exeName);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
updName = exeName + "-update";
|
|
||||||
|
|
||||||
f = fopen(updName.c_str(), "w");
|
|
||||||
if (!f)
|
|
||||||
return 1;
|
|
||||||
if (fwrite(data, 1, len, f) != len)
|
|
||||||
{
|
|
||||||
fclose(f);
|
|
||||||
unlink(updName.c_str());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (chmod(updName.c_str(), 0755))
|
|
||||||
{
|
|
||||||
unlink(updName.c_str());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rename(updName.c_str(), exeName.c_str()))
|
|
||||||
{
|
|
||||||
unlink(updName.c_str());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
execl(exeName.c_str(), "powder-update", NULL);
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns 1 on failure, 0 on success
|
|
||||||
int update_finish()
|
|
||||||
{
|
|
||||||
#ifdef WIN
|
|
||||||
ByteString exeName = Platform::ExecutableName(), updName;
|
|
||||||
int timeout = 5, err;
|
|
||||||
if constexpr (DEBUG)
|
|
||||||
{
|
|
||||||
printf("Update: Current EXE name: %s\n", exeName.c_str());
|
|
||||||
}
|
|
||||||
updName = exeName;
|
|
||||||
ByteString extension = exeName.substr(exeName.length() - 4);
|
|
||||||
if (extension == ".exe")
|
|
||||||
updName = exeName.substr(0, exeName.length() - 4);
|
|
||||||
updName = updName + "_upd.exe";
|
|
||||||
if constexpr (DEBUG)
|
|
||||||
{
|
|
||||||
printf("Update: Temp EXE name: %s\n", updName.c_str());
|
|
||||||
}
|
|
||||||
while (!Platform::RemoveFile(updName))
|
|
||||||
{
|
|
||||||
err = GetLastError();
|
|
||||||
if (err == ERROR_FILE_NOT_FOUND)
|
|
||||||
{
|
|
||||||
if constexpr (DEBUG)
|
|
||||||
{
|
|
||||||
printf("Update: Temp file not deleted\n");
|
|
||||||
}
|
|
||||||
// Old versions of powder toy name their update files with _update.exe, delete that upgrade file here
|
|
||||||
updName = exeName;
|
|
||||||
ByteString extension = exeName.substr(exeName.length() - 4);
|
|
||||||
if (extension == ".exe")
|
|
||||||
updName = exeName.substr(0, exeName.length() - 4);
|
|
||||||
updName = updName + "_update.exe";
|
|
||||||
Platform::RemoveFile(updName);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Sleep(500);
|
|
||||||
timeout--;
|
|
||||||
if (timeout <= 0)
|
|
||||||
{
|
|
||||||
if constexpr (DEBUG)
|
|
||||||
{
|
|
||||||
printf("Update: Delete timeout\n");
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_cleanup()
|
|
||||||
{
|
|
||||||
#ifdef WIN
|
|
||||||
update_finish();
|
|
||||||
#endif
|
|
||||||
}
|
|
@@ -1,7 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include "Config.h"
|
|
||||||
|
|
||||||
//char *exe_name(void);
|
|
||||||
int update_start(char *data, unsigned int len);
|
|
||||||
int update_finish();
|
|
||||||
void update_cleanup();
|
|
@@ -17,7 +17,6 @@
|
|||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Format.h"
|
#include "Format.h"
|
||||||
#include "MD5.h"
|
#include "MD5.h"
|
||||||
#include "Update.h"
|
|
||||||
|
|
||||||
#include "client/GameSave.h"
|
#include "client/GameSave.h"
|
||||||
#include "client/SaveFile.h"
|
#include "client/SaveFile.h"
|
||||||
@@ -63,7 +62,7 @@ void Client::Initialize()
|
|||||||
if (prefs.Get("version.update", false))
|
if (prefs.Get("version.update", false))
|
||||||
{
|
{
|
||||||
prefs.Set("version.update", false);
|
prefs.Set("version.update", false);
|
||||||
update_finish();
|
Platform::UpdateFinish();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Read stamps library
|
//Read stamps library
|
||||||
|
@@ -615,4 +615,118 @@ bool Install()
|
|||||||
#endif
|
#endif
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool UpdateStart(const std::vector<char> &data)
|
||||||
|
{
|
||||||
|
ByteString exeName = Platform::ExecutableName(), updName;
|
||||||
|
|
||||||
|
if (!exeName.length())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
#ifdef WIN
|
||||||
|
updName = exeName;
|
||||||
|
ByteString extension = exeName.substr(exeName.length() - 4);
|
||||||
|
if (extension == ".exe")
|
||||||
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
|
updName = updName + "_upd.exe";
|
||||||
|
|
||||||
|
if (!MoveFile(Platform::WinWiden(exeName).c_str(), Platform::WinWiden(updName).c_str()))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!WriteFile(data, exeName))
|
||||||
|
{
|
||||||
|
Platform::RemoveFile(exeName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((uintptr_t)ShellExecute(NULL, L"open", Platform::WinWiden(exeName).c_str(), NULL, NULL, SW_SHOWNORMAL) <= 32)
|
||||||
|
{
|
||||||
|
Platform::RemoveFile(exeName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
updName = exeName + "-update";
|
||||||
|
|
||||||
|
if (!WriteFile(data, updName))
|
||||||
|
{
|
||||||
|
RemoveFile(updName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chmod(updName.c_str(), 0755))
|
||||||
|
{
|
||||||
|
RemoveFile(updName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!RenameFile(updName, exeName))
|
||||||
|
{
|
||||||
|
RemoveFile(updName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
execl(exeName.c_str(), "powder-update", NULL);
|
||||||
|
return false; // execl returned, we failed
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UpdateFinish()
|
||||||
|
{
|
||||||
|
#ifdef WIN
|
||||||
|
ByteString exeName = Platform::ExecutableName(), updName;
|
||||||
|
int timeout = 5, err;
|
||||||
|
if constexpr (DEBUG)
|
||||||
|
{
|
||||||
|
printf("Update: Current EXE name: %s\n", exeName.c_str());
|
||||||
|
}
|
||||||
|
updName = exeName;
|
||||||
|
ByteString extension = exeName.substr(exeName.length() - 4);
|
||||||
|
if (extension == ".exe")
|
||||||
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
|
updName = updName + "_upd.exe";
|
||||||
|
if constexpr (DEBUG)
|
||||||
|
{
|
||||||
|
printf("Update: Temp EXE name: %s\n", updName.c_str());
|
||||||
|
}
|
||||||
|
while (!Platform::RemoveFile(updName))
|
||||||
|
{
|
||||||
|
err = GetLastError();
|
||||||
|
if (err == ERROR_FILE_NOT_FOUND)
|
||||||
|
{
|
||||||
|
if constexpr (DEBUG)
|
||||||
|
{
|
||||||
|
printf("Update: Temp file not deleted\n");
|
||||||
|
}
|
||||||
|
// Old versions of powder toy name their update files with _update.exe, delete that upgrade file here
|
||||||
|
updName = exeName;
|
||||||
|
ByteString extension = exeName.substr(exeName.length() - 4);
|
||||||
|
if (extension == ".exe")
|
||||||
|
updName = exeName.substr(0, exeName.length() - 4);
|
||||||
|
updName = updName + "_update.exe";
|
||||||
|
Platform::RemoveFile(updName);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Sleep(500);
|
||||||
|
timeout--;
|
||||||
|
if (timeout <= 0)
|
||||||
|
{
|
||||||
|
if constexpr (DEBUG)
|
||||||
|
{
|
||||||
|
printf("Update: Delete timeout\n");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateCleanup()
|
||||||
|
{
|
||||||
|
#ifdef WIN
|
||||||
|
UpdateFinish();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -49,4 +49,8 @@ namespace Platform
|
|||||||
bool Install();
|
bool Install();
|
||||||
|
|
||||||
bool ChangeDir(ByteString toDir);
|
bool ChangeDir(ByteString toDir);
|
||||||
|
|
||||||
|
bool UpdateStart(const std::vector<char> &data);
|
||||||
|
bool UpdateFinish();
|
||||||
|
void UpdateCleanup();
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Update.h"
|
|
||||||
|
|
||||||
#include "prefs/GlobalPrefs.h"
|
#include "prefs/GlobalPrefs.h"
|
||||||
#include "client/Client.h"
|
#include "client/Client.h"
|
||||||
@@ -94,10 +93,10 @@ private:
|
|||||||
notifyProgress(-1);
|
notifyProgress(-1);
|
||||||
|
|
||||||
prefs.Set("version.update", true);
|
prefs.Set("version.update", true);
|
||||||
if (update_start(&res[0], uncompressedLength))
|
if (!Platform::UpdateStart(res))
|
||||||
{
|
{
|
||||||
prefs.Set("version.update", false);
|
prefs.Set("version.update", false);
|
||||||
update_cleanup();
|
Platform::UpdateCleanup();
|
||||||
notifyError("Update failed - try downloading a new version.");
|
notifyError("Update failed - try downloading a new version.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,6 @@ configure_file(
|
|||||||
|
|
||||||
powder_files = files(
|
powder_files = files(
|
||||||
'SDLCompat.cpp',
|
'SDLCompat.cpp',
|
||||||
'Update.cpp',
|
|
||||||
'PowderToySDL.cpp',
|
'PowderToySDL.cpp',
|
||||||
'lua/CommandInterface.cpp',
|
'lua/CommandInterface.cpp',
|
||||||
'lua/TPTScriptInterface.cpp',
|
'lua/TPTScriptInterface.cpp',
|
||||||
|
Reference in New Issue
Block a user