mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-22 05:40:01 -06:00
UICommon: Add AutoUpdate module + placeholder Qt implementation
The AutoUpdate module is a generic update checker mechanism which can be used by UI backends to trigger an auto-update check as well as the actual update process. Currently only configurable through .ini and the Qt implementation is completely placeholder-y -- blocking the main thread on a network request on startup, etc.
This commit is contained in:
139
Source/Core/UICommon/AutoUpdate.cpp
Normal file
139
Source/Core/UICommon/AutoUpdate.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
// Copyright 2018 Dolphin Emulator Project
|
||||
// Licensed under GPLv2+
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "UICommon/AutoUpdate.h"
|
||||
|
||||
#include <picojson/picojson.h>
|
||||
|
||||
#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
|
||||
|
||||
namespace
|
||||
{
|
||||
bool SystemSupportsAutoUpdates()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
const char UPDATER_FILENAME[] = "Updater.exe";
|
||||
const char UPDATER_RELOC_FILENAME[] = "Updater.2.exe";
|
||||
const char UPDATER_LOG_FILE[] = "Updater.log";
|
||||
|
||||
std::wstring MakeUpdaterCommandLine(const std::map<std::string, std::string>& flags)
|
||||
{
|
||||
std::wstring cmdline = UTF8ToUTF16(UPDATER_FILENAME) + L" "; // Start with a fake argv[0].
|
||||
for (const auto& pair : flags)
|
||||
{
|
||||
std::string value = "--" + pair.first + "=" + pair.second;
|
||||
value = ReplaceAll(value, "\"", "\\\""); // Escape double quotes.
|
||||
value = "\"" + value + "\" ";
|
||||
cmdline += UTF8ToUTF16(value);
|
||||
}
|
||||
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;
|
||||
File::Delete(reloc_updater_path);
|
||||
}
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
void AutoUpdateChecker::CheckForUpdate()
|
||||
{
|
||||
// Don't bother checking if updates are not supported or not enabled.
|
||||
if (SConfig::GetInstance().m_auto_update_track.empty() || !SystemSupportsAutoUpdates())
|
||||
return;
|
||||
|
||||
#ifdef _WIN32
|
||||
CleanupFromPreviousUpdate();
|
||||
#endif
|
||||
|
||||
std::string version_hash = SConfig::GetInstance().m_auto_update_hash_override.empty() ?
|
||||
SCM_REV_STR :
|
||||
SConfig::GetInstance().m_auto_update_hash_override;
|
||||
std::string url = "https://dolphin-emu.org/update/check/v0/" +
|
||||
SConfig::GetInstance().m_auto_update_track + "/" + version_hash;
|
||||
|
||||
Common::HttpRequest req{std::chrono::seconds{10}};
|
||||
auto resp = req.Get(url);
|
||||
if (!resp)
|
||||
{
|
||||
ERROR_LOG(COMMON, "Auto-update request failed");
|
||||
return;
|
||||
}
|
||||
std::string contents(reinterpret_cast<char*>(resp->data()), resp->size());
|
||||
INFO_LOG(COMMON, "Auto-update JSON response: %s", contents.c_str());
|
||||
|
||||
picojson::value json;
|
||||
std::string err = picojson::parse(json, contents);
|
||||
if (!err.empty())
|
||||
{
|
||||
ERROR_LOG(COMMON, "Invalid JSON received from auto-update service: %s", err.c_str());
|
||||
return;
|
||||
}
|
||||
picojson::object obj = json.get<picojson::object>();
|
||||
|
||||
if (obj["status"].get<std::string>() != "outdated")
|
||||
{
|
||||
INFO_LOG(COMMON, "Auto-update status: we are up to date.");
|
||||
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>();
|
||||
// TODO: generate the HTML changelog from the JSON information.
|
||||
OnUpdateAvailable(nvi);
|
||||
}
|
||||
|
||||
void AutoUpdateChecker::TriggerUpdate(const AutoUpdateChecker::NewVersionInformation& info)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
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;
|
||||
updater_flags["parent-pid"] = std::to_string(GetCurrentProcessId());
|
||||
updater_flags["install-base-path"] = File::GetExeDirectory();
|
||||
updater_flags["log-file"] = File::GetExeDirectory() + DIR_SEP + UPDATER_LOG_FILE;
|
||||
|
||||
// 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;
|
||||
File::Copy(updater_path, reloc_updater_path);
|
||||
|
||||
// Run the updater!
|
||||
std::wstring command_line = MakeUpdaterCommandLine(updater_flags);
|
||||
STARTUPINFO sinfo = {sizeof(info)};
|
||||
PROCESS_INFORMATION pinfo;
|
||||
INFO_LOG(COMMON, "Updater command line: %s", UTF16ToUTF8(command_line).c_str());
|
||||
if (!CreateProcessW(UTF8ToUTF16(reloc_updater_path).c_str(),
|
||||
const_cast<wchar_t*>(command_line.c_str()), nullptr, nullptr, FALSE, 0,
|
||||
nullptr, nullptr, &sinfo, &pinfo))
|
||||
{
|
||||
ERROR_LOG(COMMON, "Could not start updater process: error=%d", GetLastError());
|
||||
}
|
||||
#endif
|
||||
}
|
Reference in New Issue
Block a user