2018-03-18 00:22:05 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "UICommon/AutoUpdate.h"
|
|
|
|
|
2020-04-29 09:45:59 +00:00
|
|
|
#include <picojson.h>
|
2018-03-23 00:08:25 +00:00
|
|
|
#include <string>
|
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"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Common/scmrev.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined _WIN32 || defined __APPLE__
|
|
|
|
#define OS_SUPPORTS_UPDATER
|
|
|
|
#endif
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
const char UPDATER_FILENAME[] = "Updater.exe";
|
|
|
|
const char UPDATER_RELOC_FILENAME[] = "Updater.2.exe";
|
2019-01-20 01:24:26 +00:00
|
|
|
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
|
|
|
|
const char UPDATER_FILENAME[] = "Dolphin Updater.app";
|
|
|
|
const char UPDATER_RELOC_FILENAME[] = ".Dolphin Updater.2.app";
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-03-23 08:28:25 +00:00
|
|
|
#ifdef OS_SUPPORTS_UPDATER
|
2018-03-18 00:22:05 +00:00
|
|
|
const char UPDATER_LOG_FILE[] = "Updater.log";
|
|
|
|
|
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__
|
|
|
|
std::string cmdline = "\"" + File::GetExeDirectory() + DIR_SEP + UPDATER_RELOC_FILENAME +
|
|
|
|
"/Contents/MacOS/Dolphin Updater\"";
|
|
|
|
#else
|
|
|
|
std::string cmdline = File::GetExeDirectory() + DIR_SEP + UPDATER_RELOC_FILENAME;
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to remove the relocated updater file once we don't need it anymore.
|
|
|
|
void CleanupFromPreviousUpdate()
|
|
|
|
{
|
|
|
|
std::string reloc_updater_path = File::GetExeDirectory() + DIR_SEP + UPDATER_RELOC_FILENAME;
|
2019-01-20 01:24:26 +00:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
File::DeleteDirRecursively(reloc_updater_path);
|
|
|
|
#else
|
2018-03-18 00:22:05 +00:00
|
|
|
File::Delete(reloc_updater_path);
|
2019-01-20 01:24:26 +00:00
|
|
|
#endif
|
2018-03-18 00:22:05 +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>();
|
|
|
|
}
|
|
|
|
|
|
|
|
changelog += " by <a href = \"" + ver_obj["author_url"].get<std::string>() + "\">" +
|
|
|
|
ver_obj["author"].get<std::string>() + "</a> — " +
|
|
|
|
ver_obj["short_descr"].get<std::string>();
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
{
|
2019-01-20 01:24:26 +00:00
|
|
|
#if defined _WIN32 || defined __APPLE__
|
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()
|
|
|
|
{
|
|
|
|
#if defined _WIN32
|
|
|
|
return "win";
|
|
|
|
#elif defined __APPLE__
|
|
|
|
return "macos";
|
|
|
|
#else
|
|
|
|
return "unknown";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
void AutoUpdateChecker::CheckForUpdate()
|
|
|
|
{
|
|
|
|
// Don't bother checking if updates are not supported or not enabled.
|
2018-03-22 11:20:15 +00:00
|
|
|
if (!SystemSupportsAutoUpdates() || SConfig::GetInstance().m_auto_update_track.empty())
|
2018-03-18 00:22:05 +00:00
|
|
|
return;
|
|
|
|
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef OS_SUPPORTS_UPDATER
|
2018-03-18 00:22:05 +00:00
|
|
|
CleanupFromPreviousUpdate();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string version_hash = SConfig::GetInstance().m_auto_update_hash_override.empty() ?
|
|
|
|
SCM_REV_STR :
|
|
|
|
SConfig::GetInstance().m_auto_update_hash_override;
|
2019-01-20 01:24:26 +00:00
|
|
|
std::string url = "https://dolphin-emu.org/update/check/v1/" +
|
|
|
|
SConfig::GetInstance().m_auto_update_track + "/" + version_hash + "/" +
|
|
|
|
GetPlatformID();
|
2018-03-18 00:22:05 +00:00
|
|
|
|
|
|
|
Common::HttpRequest req{std::chrono::seconds{10}};
|
|
|
|
auto resp = req.Get(url);
|
|
|
|
if (!resp)
|
|
|
|
{
|
2020-11-09 07:35:49 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "Auto-update request failed");
|
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())
|
|
|
|
{
|
2020-11-09 07:35:49 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "Invalid JSON received from auto-update service: {}", err);
|
2018-03-18 00:22:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
picojson::object obj = json.get<picojson::object>();
|
|
|
|
|
|
|
|
if (obj["status"].get<std::string>() != "outdated")
|
|
|
|
{
|
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
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
OnUpdateAvailable(nvi);
|
|
|
|
}
|
|
|
|
|
2018-03-22 23:29:03 +00:00
|
|
|
void AutoUpdateChecker::TriggerUpdate(const AutoUpdateChecker::NewVersionInformation& info,
|
|
|
|
AutoUpdateChecker::RestartMode restart_mode)
|
2018-03-18 00:22:05 +00:00
|
|
|
{
|
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;
|
2019-01-20 01:24:26 +00:00
|
|
|
#ifdef _WIN32
|
2018-03-18 00:22:05 +00:00
|
|
|
updater_flags["parent-pid"] = std::to_string(GetCurrentProcessId());
|
2019-01-20 01:24:26 +00:00
|
|
|
#else
|
|
|
|
updater_flags["parent-pid"] = std::to_string(getpid());
|
|
|
|
#endif
|
2018-03-18 00:22:05 +00:00
|
|
|
updater_flags["install-base-path"] = File::GetExeDirectory();
|
|
|
|
updater_flags["log-file"] = File::GetExeDirectory() + DIR_SEP + UPDATER_LOG_FILE;
|
|
|
|
|
2018-03-22 23:29:03 +00:00
|
|
|
if (restart_mode == RestartMode::RESTART_AFTER_UPDATE)
|
|
|
|
updater_flags["binary-to-restart"] = File::GetExePath();
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
// Copy the updater so it can update itself if needed.
|
|
|
|
std::string updater_path = File::GetExeDirectory() + DIR_SEP + UPDATER_FILENAME;
|
|
|
|
std::string reloc_updater_path = File::GetExeDirectory() + DIR_SEP + UPDATER_RELOC_FILENAME;
|
2019-01-20 01:24:26 +00:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
File::CopyDir(updater_path, reloc_updater_path);
|
|
|
|
chmod((reloc_updater_path + "/Contents/MacOS/Dolphin Updater").c_str(), 0700);
|
|
|
|
#else
|
2018-03-18 00:22:05 +00:00
|
|
|
File::Copy(updater_path, reloc_updater_path);
|
2019-01-20 01:24:26 +00:00
|
|
|
#endif
|
2018-03-18 00:22:05 +00:00
|
|
|
|
|
|
|
// Run the updater!
|
2020-11-09 07:35:49 +00:00
|
|
|
const std::string command_line = MakeUpdaterCommandLine(updater_flags);
|
|
|
|
INFO_LOG_FMT(COMMON, "Updater command line: {}", command_line);
|
2019-01-20 01:24:26 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2019-06-24 19:02:30 +00:00
|
|
|
STARTUPINFO sinfo = {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;
|
2020-06-28 16:15:30 +00:00
|
|
|
if (CreateProcessW(UTF8ToWString(reloc_updater_path).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
|
|
|
{
|
2020-11-09 07:35:49 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "Could not start updater process: error={}", GetLastError());
|
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)
|
|
|
|
{
|
2020-11-09 07:35:49 +00:00
|
|
|
ERROR_LOG_FMT(COMMON, "Could not start updater process: error={}", errno);
|
2019-01-20 01:24:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-18 00:22:05 +00:00
|
|
|
#endif
|
|
|
|
}
|