diff --git a/src/Update.cpp b/src/Update.cpp deleted file mode 100644 index f33ca0128..000000000 --- a/src/Update.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include "Update.h" - -#include -#include -#ifndef WIN -#include -#endif -#if !defined(MACOSX) && !defined(BSD) -#include -#endif -#include -#include - -#ifdef WIN -# ifndef NOMINMAX -# define NOMINMAX -# endif -# include -#else -# include -# include -#endif -#ifdef MACOSX -# include -# include -#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 -} diff --git a/src/Update.h b/src/Update.h deleted file mode 100644 index 32b79f4a6..000000000 --- a/src/Update.h +++ /dev/null @@ -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(); diff --git a/src/client/Client.cpp b/src/client/Client.cpp index d180e7393..115dd6b0e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -17,7 +17,6 @@ #include "Config.h" #include "Format.h" #include "MD5.h" -#include "Update.h" #include "client/GameSave.h" #include "client/SaveFile.h" @@ -63,7 +62,7 @@ void Client::Initialize() if (prefs.Get("version.update", false)) { prefs.Set("version.update", false); - update_finish(); + Platform::UpdateFinish(); } //Read stamps library diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index fee96aa29..d5c683f6c 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -615,4 +615,118 @@ bool Install() #endif return ok; } + +bool UpdateStart(const std::vector &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 +} } diff --git a/src/common/Platform.h b/src/common/Platform.h index 53d290aba..f42ac2f03 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -49,4 +49,8 @@ namespace Platform bool Install(); bool ChangeDir(ByteString toDir); + + bool UpdateStart(const std::vector &data); + bool UpdateFinish(); + void UpdateCleanup(); } diff --git a/src/gui/update/UpdateActivity.cpp b/src/gui/update/UpdateActivity.cpp index 3ef0cd7dc..df66faf0f 100644 --- a/src/gui/update/UpdateActivity.cpp +++ b/src/gui/update/UpdateActivity.cpp @@ -6,7 +6,6 @@ #include #include "Config.h" -#include "Update.h" #include "prefs/GlobalPrefs.h" #include "client/Client.h" @@ -94,10 +93,10 @@ private: notifyProgress(-1); prefs.Set("version.update", true); - if (update_start(&res[0], uncompressedLength)) + if (!Platform::UpdateStart(res)) { prefs.Set("version.update", false); - update_cleanup(); + Platform::UpdateCleanup(); notifyError("Update failed - try downloading a new version."); return false; } diff --git a/src/meson.build b/src/meson.build index bfdd33730..2583a4c77 100644 --- a/src/meson.build +++ b/src/meson.build @@ -6,7 +6,6 @@ configure_file( powder_files = files( 'SDLCompat.cpp', - 'Update.cpp', 'PowderToySDL.cpp', 'lua/CommandInterface.cpp', 'lua/TPTScriptInterface.cpp',