2018-03-18 00:22:05 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-03-18 00:22:05 +00:00
|
|
|
|
|
|
|
#include "UICommon/AutoUpdate.h"
|
|
|
|
|
2023-03-10 02:23:12 +00:00
|
|
|
#include <cstdlib>
|
2018-03-23 00:08:25 +00:00
|
|
|
#include <string>
|
2018-03-18 00:22:05 +00:00
|
|
|
|
2021-12-27 18:09:47 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
#include <picojson.h>
|
|
|
|
|
2022-11-01 06:08:35 +00:00
|
|
|
#include "Common/CommonFuncs.h"
|
2018-03-18 00:22:05 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/HttpRequest.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2022-11-01 06:08:35 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2018-03-18 00:22:05 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2021-05-21 11:57:59 +00:00
|
|
|
#include "Common/Version.h"
|
2018-03-18 00:22:05 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
2023-03-10 02:23:12 +00:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2018-03-18 00:22:05 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2022-10-21 23:08:20 +00:00
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
2019-01-20 01:24:26 +00:00
|
|
|
#define OS_SUPPORTS_UPDATER
|
|
|
|
#endif
|
|
|
|
|
2021-03-13 01:10:53 +00:00
|
|
|
// Refer to docs/autoupdate_overview.md for a detailed overview of the autoupdate process
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
namespace
|
|
|
|
{
|
2021-03-11 10:48:46 +00:00
|
|
|
bool s_update_triggered = false;
|
2019-01-20 01:24:26 +00:00
|
|
|
|
2022-10-21 23:08:20 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
const char UPDATER_CONTENT_PATH[] = "/Contents/MacOS/Dolphin Updater";
|
2019-01-20 01:24:26 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-23 08:28:25 +00:00
|
|
|
#ifdef OS_SUPPORTS_UPDATER
|
2022-10-21 23:08:20 +00:00
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
const char UPDATER_LOG_FILE[] = "Updater.log";
|
|
|
|
|
2022-10-21 23:08:20 +00:00
|
|
|
std::string UpdaterPath(bool relocated = false)
|
|
|
|
{
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (relocated)
|
2024-08-01 16:38:16 +00:00
|
|
|
return File::GetExeDirectory() + DIR_SEP + ".Dolphin Updater.2.app";
|
2022-10-21 23:08:20 +00:00
|
|
|
else
|
2024-08-01 16:38:16 +00:00
|
|
|
return File::GetBundleDirectory() + DIR_SEP + "Contents/Helpers/Dolphin Updater.app";
|
2022-10-21 23:08:20 +00:00
|
|
|
#else
|
2024-08-01 16:38:16 +00:00
|
|
|
return File::GetExeDirectory() + DIR_SEP + "Updater.exe";
|
2022-10-21 23:08:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-20 01:24:26 +00:00
|
|
|
std::string MakeUpdaterCommandLine(const std::map<std::string, std::string>& flags)
|
2018-03-18 00:22:05 +00:00
|
|
|
{
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef __APPLE__
|
2022-10-21 23:08:20 +00:00
|
|
|
std::string cmdline = "\"" + UpdaterPath(true) + UPDATER_CONTENT_PATH + "\"";
|
2019-01-20 01:24:26 +00:00
|
|
|
#else
|
2022-10-21 23:08:20 +00:00
|
|
|
std::string cmdline = UpdaterPath();
|
2019-01-20 01:24:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cmdline += " ";
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
for (const auto& pair : flags)
|
|
|
|
{
|
|
|
|
std::string value = "--" + pair.first + "=" + pair.second;
|
|
|
|
value = ReplaceAll(value, "\"", "\\\""); // Escape double quotes.
|
|
|
|
value = "\"" + value + "\" ";
|
2019-01-20 01:24:26 +00:00
|
|
|
cmdline += value;
|
2018-03-18 00:22:05 +00:00
|
|
|
}
|
|
|
|
return cmdline;
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:08:20 +00:00
|
|
|
#ifdef __APPLE__
|
2018-03-18 00:22:05 +00:00
|
|
|
void CleanupFromPreviousUpdate()
|
|
|
|
{
|
2022-10-21 23:08:20 +00:00
|
|
|
// Remove the relocated updater file.
|
|
|
|
File::DeleteDirRecursively(UpdaterPath(true));
|
2024-08-08 06:51:18 +00:00
|
|
|
|
|
|
|
// Remove the old (non-embedded) updater app bundle.
|
|
|
|
// While the update process will delete the files within the old bundle after updating to a
|
|
|
|
// version with an embedded updater, it won't delete the folder structure of the bundle, so
|
|
|
|
// we should clean those leftovers up.
|
|
|
|
File::DeleteDirRecursively(File::GetExeDirectory() + DIR_SEP + "Dolphin Updater.app");
|
2018-03-18 00:22:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-03-23 00:08:25 +00:00
|
|
|
|
2022-10-21 23:08:20 +00:00
|
|
|
#endif
|
|
|
|
|
2018-03-23 00:08:25 +00:00
|
|
|
// This ignores i18n because most of the text in there (change descriptions) is only going to be
|
|
|
|
// written in english anyway.
|
|
|
|
std::string GenerateChangelog(const picojson::array& versions)
|
|
|
|
{
|
|
|
|
std::string changelog;
|
|
|
|
for (const auto& ver : versions)
|
|
|
|
{
|
|
|
|
if (!ver.is<picojson::object>())
|
|
|
|
continue;
|
|
|
|
picojson::object ver_obj = ver.get<picojson::object>();
|
|
|
|
|
|
|
|
if (ver_obj["changelog_html"].is<picojson::null>())
|
|
|
|
{
|
|
|
|
if (!changelog.empty())
|
|
|
|
changelog += "<div style=\"margin-top: 0.4em;\"></div>"; // Vertical spacing.
|
|
|
|
|
|
|
|
// Try to link to the PR if we have this info. Otherwise just show shortrev.
|
|
|
|
if (ver_obj["pr_url"].is<std::string>())
|
|
|
|
{
|
|
|
|
changelog += "<a href=\"" + ver_obj["pr_url"].get<std::string>() + "\">" +
|
|
|
|
ver_obj["shortrev"].get<std::string>() + "</a>";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
changelog += ver_obj["shortrev"].get<std::string>();
|
|
|
|
}
|
2021-05-24 16:44:21 +00:00
|
|
|
const std::string escaped_description =
|
2023-05-16 18:21:19 +00:00
|
|
|
Common::GetEscapedHtml(ver_obj["short_descr"].get<std::string>());
|
2018-03-23 00:08:25 +00:00
|
|
|
changelog += " by <a href = \"" + ver_obj["author_url"].get<std::string>() + "\">" +
|
2021-05-24 16:44:21 +00:00
|
|
|
ver_obj["author"].get<std::string>() + "</a> — " + escaped_description;
|
2018-03-23 00:08:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!changelog.empty())
|
|
|
|
changelog += "<hr>";
|
|
|
|
changelog += "<b>Dolphin " + ver_obj["shortrev"].get<std::string>() + "</b>";
|
|
|
|
changelog += "<p>" + ver_obj["changelog_html"].get<std::string>() + "</p>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return changelog;
|
|
|
|
}
|
2018-03-18 00:22:05 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-03-22 11:20:15 +00:00
|
|
|
bool AutoUpdateChecker::SystemSupportsAutoUpdates()
|
|
|
|
{
|
2022-10-21 23:08:20 +00:00
|
|
|
#if defined(AUTOUPDATE) && defined(OS_SUPPORTS_UPDATER)
|
2018-03-22 11:20:15 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-01-20 01:24:26 +00:00
|
|
|
static std::string GetPlatformID()
|
|
|
|
{
|
2022-06-16 21:55:51 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#if defined(_M_ARM_64)
|
|
|
|
return "win-arm64";
|
|
|
|
#else
|
2019-01-20 01:24:26 +00:00
|
|
|
return "win";
|
2022-06-16 21:55:51 +00:00
|
|
|
#endif
|
|
|
|
#elif defined(__APPLE__)
|
2021-05-24 03:33:39 +00:00
|
|
|
#if defined(MACOS_UNIVERSAL_BUILD)
|
|
|
|
return "macos-universal";
|
|
|
|
#else
|
2019-01-20 01:24:26 +00:00
|
|
|
return "macos";
|
2021-05-24 03:33:39 +00:00
|
|
|
#endif
|
2019-01-20 01:24:26 +00:00
|
|
|
#else
|
|
|
|
return "unknown";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:23:12 +00:00
|
|
|
static std::string GetUpdateServerUrl()
|
|
|
|
{
|
|
|
|
auto server_url = std::getenv("DOLPHIN_UPDATE_SERVER_URL");
|
|
|
|
if (server_url)
|
|
|
|
return server_url;
|
|
|
|
return "https://dolphin-emu.org";
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 GetOwnProcessId()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return GetCurrentProcessId();
|
|
|
|
#else
|
|
|
|
return getpid();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-27 18:09:47 +00:00
|
|
|
void AutoUpdateChecker::CheckForUpdate(std::string_view update_track,
|
2022-11-01 06:08:35 +00:00
|
|
|
std::string_view hash_override, const CheckType check_type)
|
2018-03-18 00:22:05 +00:00
|
|
|
{
|
|
|
|
// Don't bother checking if updates are not supported or not enabled.
|
2021-12-27 18:09:47 +00:00
|
|
|
if (!SystemSupportsAutoUpdates() || update_track.empty())
|
2018-03-18 00:22:05 +00:00
|
|
|
return;
|
|
|
|
|
2022-10-21 23:08:20 +00:00
|
|
|
#ifdef __APPLE__
|
2018-03-18 00:22:05 +00:00
|
|
|
CleanupFromPreviousUpdate();
|
|
|
|
#endif
|
|
|
|
|
2022-01-13 23:04:22 +00:00
|
|
|
std::string_view version_hash = hash_override.empty() ? Common::GetScmRevGitStr() : hash_override;
|
2023-03-10 02:23:12 +00:00
|
|
|
std::string url = fmt::format("{}/update/check/v1/{}/{}/{}", GetUpdateServerUrl(), update_track,
|
2021-12-27 18:09:47 +00:00
|
|
|
version_hash, GetPlatformID());
|
2018-03-18 00:22:05 +00:00
|
|
|
|
2022-11-01 06:08:35 +00:00
|
|
|
const bool is_manual_check = check_type == CheckType::Manual;
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
Common::HttpRequest req{std::chrono::seconds{10}};
|
|
|
|
auto resp = req.Get(url);
|
|
|
|
if (!resp)
|
|
|
|
{
|
2022-11-01 06:08:35 +00:00
|
|
|
if (is_manual_check)
|
|
|
|
CriticalAlertFmtT("Unable to contact update server.");
|
2018-03-18 00:22:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-11-09 07:35:49 +00:00
|
|
|
const std::string contents(reinterpret_cast<char*>(resp->data()), resp->size());
|
|
|
|
INFO_LOG_FMT(COMMON, "Auto-update JSON response: {}", contents);
|
2018-03-18 00:22:05 +00:00
|
|
|
|
|
|
|
picojson::value json;
|
2020-11-09 07:35:49 +00:00
|
|
|
const std::string err = picojson::parse(json, contents);
|
2018-03-18 00:22:05 +00:00
|
|
|
if (!err.empty())
|
|
|
|
{
|
2022-11-01 06:08:35 +00:00
|
|
|
CriticalAlertFmtT("Invalid JSON received from auto-update service : {0}", err);
|
2018-03-18 00:22:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
picojson::object obj = json.get<picojson::object>();
|
|
|
|
|
|
|
|
if (obj["status"].get<std::string>() != "outdated")
|
|
|
|
{
|
2022-11-01 06:08:35 +00:00
|
|
|
if (is_manual_check)
|
|
|
|
SuccessAlertFmtT("You are running the latest version available on this update track.");
|
2020-11-09 07:35:49 +00:00
|
|
|
INFO_LOG_FMT(COMMON, "Auto-update status: we are up to date.");
|
2018-03-18 00:22:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NewVersionInformation nvi;
|
|
|
|
nvi.this_manifest_url = obj["old"].get<picojson::object>()["manifest"].get<std::string>();
|
|
|
|
nvi.next_manifest_url = obj["new"].get<picojson::object>()["manifest"].get<std::string>();
|
|
|
|
nvi.content_store_url = obj["content-store"].get<std::string>();
|
|
|
|
nvi.new_shortrev = obj["new"].get<picojson::object>()["name"].get<std::string>();
|
|
|
|
nvi.new_hash = obj["new"].get<picojson::object>()["hash"].get<std::string>();
|
2018-03-22 11:20:15 +00:00
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
// TODO: generate the HTML changelog from the JSON information.
|
2018-03-23 00:08:25 +00:00
|
|
|
nvi.changelog_html = GenerateChangelog(obj["changelog"].get<picojson::array>());
|
2018-03-22 11:20:15 +00:00
|
|
|
|
2023-03-10 02:23:12 +00:00
|
|
|
if (std::getenv("DOLPHIN_UPDATE_TEST_DONE"))
|
|
|
|
{
|
|
|
|
// We are at end of updater test flow, send a message to server, which will kill us.
|
|
|
|
req.Get(fmt::format("{}/update-test-done/{}", GetUpdateServerUrl(), GetOwnProcessId()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OnUpdateAvailable(nvi);
|
|
|
|
}
|
2018-03-18 00:22:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 23:29:03 +00:00
|
|
|
void AutoUpdateChecker::TriggerUpdate(const AutoUpdateChecker::NewVersionInformation& info,
|
2022-11-01 06:08:35 +00:00
|
|
|
const AutoUpdateChecker::RestartMode restart_mode)
|
2018-03-18 00:22:05 +00:00
|
|
|
{
|
2021-03-11 10:48:46 +00:00
|
|
|
// Check to make sure we don't already have an update triggered
|
|
|
|
if (s_update_triggered)
|
|
|
|
{
|
|
|
|
WARN_LOG_FMT(COMMON, "Auto-update: received a redundant trigger request, ignoring");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_update_triggered = true;
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef OS_SUPPORTS_UPDATER
|
2018-03-18 00:22:05 +00:00
|
|
|
std::map<std::string, std::string> updater_flags;
|
|
|
|
updater_flags["this-manifest-url"] = info.this_manifest_url;
|
|
|
|
updater_flags["next-manifest-url"] = info.next_manifest_url;
|
|
|
|
updater_flags["content-store-url"] = info.content_store_url;
|
2023-03-10 02:23:12 +00:00
|
|
|
updater_flags["parent-pid"] = std::to_string(GetOwnProcessId());
|
2018-03-18 00:22:05 +00:00
|
|
|
updater_flags["install-base-path"] = File::GetExeDirectory();
|
2020-06-29 18:40:12 +00:00
|
|
|
updater_flags["log-file"] = File::GetUserPath(D_LOGS_IDX) + UPDATER_LOG_FILE;
|
2018-03-18 00:22:05 +00:00
|
|
|
|
2018-03-22 23:29:03 +00:00
|
|
|
if (restart_mode == RestartMode::RESTART_AFTER_UPDATE)
|
|
|
|
updater_flags["binary-to-restart"] = File::GetExePath();
|
|
|
|
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef __APPLE__
|
2022-10-21 23:08:20 +00:00
|
|
|
// Copy the updater so it can update itself if needed.
|
|
|
|
const std::string reloc_updater_path = UpdaterPath(true);
|
2023-02-22 01:23:21 +00:00
|
|
|
if (!File::Copy(UpdaterPath(), reloc_updater_path))
|
2022-11-01 06:08:35 +00:00
|
|
|
{
|
|
|
|
CriticalAlertFmtT("Unable to create updater copy.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (chmod((reloc_updater_path + UPDATER_CONTENT_PATH).c_str(), 0700) != 0)
|
|
|
|
{
|
|
|
|
CriticalAlertFmtT("Unable to set permissions on updater copy.");
|
|
|
|
return;
|
|
|
|
}
|
2019-01-20 01:24:26 +00:00
|
|
|
#endif
|
2018-03-18 00:22:05 +00:00
|
|
|
|
|
|
|
// Run the updater!
|
2022-09-11 06:21:12 +00:00
|
|
|
std::string command_line = MakeUpdaterCommandLine(updater_flags);
|
2020-11-09 07:35:49 +00:00
|
|
|
INFO_LOG_FMT(COMMON, "Updater command line: {}", command_line);
|
2019-01-20 01:24:26 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2022-09-11 06:21:12 +00:00
|
|
|
STARTUPINFO sinfo{.cb = sizeof(sinfo)};
|
2018-03-22 23:31:58 +00:00
|
|
|
sinfo.dwFlags = STARTF_FORCEOFFFEEDBACK; // No hourglass cursor after starting the process.
|
2018-03-18 00:22:05 +00:00
|
|
|
PROCESS_INFORMATION pinfo;
|
2022-10-21 23:08:20 +00:00
|
|
|
if (CreateProcessW(UTF8ToWString(UpdaterPath()).c_str(), UTF8ToWString(command_line).data(),
|
2019-06-24 19:02:30 +00:00
|
|
|
nullptr, nullptr, FALSE, 0, nullptr, nullptr, &sinfo, &pinfo))
|
|
|
|
{
|
|
|
|
CloseHandle(pinfo.hThread);
|
|
|
|
CloseHandle(pinfo.hProcess);
|
|
|
|
}
|
|
|
|
else
|
2018-03-18 00:22:05 +00:00
|
|
|
{
|
2023-04-18 16:50:31 +00:00
|
|
|
const std::string error = Common::GetLastErrorString();
|
2022-11-01 06:08:35 +00:00
|
|
|
CriticalAlertFmtT("Could not start updater process: {0}", error);
|
2018-03-18 00:22:05 +00:00
|
|
|
}
|
2019-01-20 01:24:26 +00:00
|
|
|
#else
|
|
|
|
if (popen(command_line.c_str(), "r") == nullptr)
|
|
|
|
{
|
2023-04-18 16:50:31 +00:00
|
|
|
const std::string error = Common::LastStrerrorString();
|
2022-11-01 06:08:35 +00:00
|
|
|
CriticalAlertFmtT("Could not start updater process: {0}", error);
|
2019-01-20 01:24:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
#endif
|
|
|
|
}
|