diff --git a/Source/Core/Core/ConfigManager.cpp b/Source/Core/Core/ConfigManager.cpp index a59dc8a126..3776dec2bc 100644 --- a/Source/Core/Core/ConfigManager.cpp +++ b/Source/Core/Core/ConfigManager.cpp @@ -23,6 +23,7 @@ #include "Common/MsgHandler.h" #include "Common/NandPaths.h" #include "Common/StringUtil.h" +#include "Common/scmrev.h" #include "Core/Analytics.h" #include "Core/Boot/Boot.h" @@ -90,6 +91,7 @@ void SConfig::SaveSettings() SaveNetworkSettings(ini); SaveBluetoothPassthroughSettings(ini); SaveUSBPassthroughSettings(ini); + SaveAutoUpdateSettings(ini); ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX)); @@ -370,6 +372,14 @@ void SConfig::SaveUSBPassthroughSettings(IniFile& ini) section->Set("Devices", devices_string); } +void SConfig::SaveAutoUpdateSettings(IniFile& ini) +{ + IniFile::Section* section = ini.GetOrCreateSection("AutoUpdate"); + + section->Set("TrackForTesting", m_auto_update_track); + section->Set("HashOverride", m_auto_update_hash_override); +} + void SConfig::LoadSettings() { Config::Load(); @@ -391,6 +401,7 @@ void SConfig::LoadSettings() LoadAnalyticsSettings(ini); LoadBluetoothPassthroughSettings(ini); LoadUSBPassthroughSettings(ini); + LoadAutoUpdateSettings(ini); } void SConfig::LoadGeneralSettings(IniFile& ini) @@ -671,6 +682,15 @@ void SConfig::LoadUSBPassthroughSettings(IniFile& ini) } } +void SConfig::LoadAutoUpdateSettings(IniFile& ini) +{ + IniFile::Section* section = ini.GetOrCreateSection("AutoUpdate"); + + // TODO: Rename and default to SCM_UPDATE_TRACK_STR when ready for general consumption. + section->Get("TrackForTesting", &m_auto_update_track, ""); + section->Get("HashOverride", &m_auto_update_hash_override, ""); +} + void SConfig::ResetRunningGameMetadata() { SetRunningGameMetadata("00000000", 0, 0, Core::TitleDatabase::TitleType::Other); diff --git a/Source/Core/Core/ConfigManager.h b/Source/Core/Core/ConfigManager.h index a95c8bdc95..8b045250a6 100644 --- a/Source/Core/Core/ConfigManager.h +++ b/Source/Core/Core/ConfigManager.h @@ -316,6 +316,10 @@ struct SConfig bool m_SSLDumpRootCA; bool m_SSLDumpPeerCert; + // Auto-update settings + std::string m_auto_update_track; + std::string m_auto_update_hash_override; + SConfig(const SConfig&) = delete; SConfig& operator=(const SConfig&) = delete; SConfig(SConfig&&) = delete; @@ -349,6 +353,7 @@ private: void SaveAnalyticsSettings(IniFile& ini); void SaveBluetoothPassthroughSettings(IniFile& ini); void SaveUSBPassthroughSettings(IniFile& ini); + void SaveAutoUpdateSettings(IniFile& ini); void LoadGeneralSettings(IniFile& ini); void LoadInterfaceSettings(IniFile& ini); @@ -363,6 +368,7 @@ private: void LoadAnalyticsSettings(IniFile& ini); void LoadBluetoothPassthroughSettings(IniFile& ini); void LoadUSBPassthroughSettings(IniFile& ini); + void LoadAutoUpdateSettings(IniFile& ini); void SetRunningGameMetadata(const std::string& game_id, u64 title_id, u16 revision, Core::TitleDatabase::TitleType type); diff --git a/Source/Core/DolphinQt2/Main.cpp b/Source/Core/DolphinQt2/Main.cpp index e3cc6f00ee..bde128bbc0 100644 --- a/Source/Core/DolphinQt2/Main.cpp +++ b/Source/Core/DolphinQt2/Main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/MsgHandler.h" #include "Core/Analytics.h" @@ -20,6 +21,7 @@ #include "DolphinQt2/Resources.h" #include "DolphinQt2/Settings.h" #include "DolphinQt2/Translation.h" +#include "UICommon/AutoUpdate.h" #include "UICommon/CommandLineParse.h" #include "UICommon/UICommon.h" @@ -50,6 +52,35 @@ static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no }); } +// TODO: This should be replaced with something in a background thread, it performs a blocking +// HTTP query. It also needs a proper UI, and many other things. But right now it needs to be +// manually enabled through INI, so all these problems are ignored :) +class QtAutoUpdateChecker : public AutoUpdateChecker +{ +public: + explicit QtAutoUpdateChecker(QWidget* parent) : m_parent(parent) {} +protected: + void OnUpdateAvailable(const NewVersionInformation& info) override + { + QMessageBox prompt(m_parent); + + prompt.setIcon(QMessageBox::Question); + prompt.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + prompt.setText(QString::fromUtf8("Update Dolphin to version %1?") + .arg(QString::fromStdString(info.new_shortrev))); + + const int answer = prompt.exec(); + if (answer == QMessageBox::Yes) + { + TriggerUpdate(info); + m_parent->close(); + } + } + +private: + QWidget* m_parent; +}; + // N.B. On Windows, this should be called from WinMain. Link against qtmain and specify // /SubSystem:Windows int main(int argc, char* argv[]) @@ -152,6 +183,9 @@ int main(int argc, char* argv[]) } #endif + QtAutoUpdateChecker updater(&win); + updater.CheckForUpdate(); + retval = app.exec(); } diff --git a/Source/Core/UICommon/AutoUpdate.cpp b/Source/Core/UICommon/AutoUpdate.cpp new file mode 100644 index 0000000000..3313b706fd --- /dev/null +++ b/Source/Core/UICommon/AutoUpdate.cpp @@ -0,0 +1,139 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#include "UICommon/AutoUpdate.h" + +#include + +#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 +#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& 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(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(); + + if (obj["status"].get() != "outdated") + { + INFO_LOG(COMMON, "Auto-update status: we are up to date."); + return; + } + + NewVersionInformation nvi; + nvi.this_manifest_url = obj["old"].get()["manifest"].get(); + nvi.next_manifest_url = obj["new"].get()["manifest"].get(); + nvi.content_store_url = obj["content-store"].get(); + nvi.new_shortrev = obj["new"].get()["name"].get(); + nvi.new_hash = obj["new"].get()["hash"].get(); + // TODO: generate the HTML changelog from the JSON information. + OnUpdateAvailable(nvi); +} + +void AutoUpdateChecker::TriggerUpdate(const AutoUpdateChecker::NewVersionInformation& info) +{ +#ifdef _WIN32 + std::map 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(command_line.c_str()), nullptr, nullptr, FALSE, 0, + nullptr, nullptr, &sinfo, &pinfo)) + { + ERROR_LOG(COMMON, "Could not start updater process: error=%d", GetLastError()); + } +#endif +} diff --git a/Source/Core/UICommon/AutoUpdate.h b/Source/Core/UICommon/AutoUpdate.h new file mode 100644 index 0000000000..6266b6f9dc --- /dev/null +++ b/Source/Core/UICommon/AutoUpdate.h @@ -0,0 +1,38 @@ +// Copyright 2018 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include + +// This class defines all the logic for Dolphin auto-update checking. UI-specific elements have to +// be defined in a backend specific subclass. +class AutoUpdateChecker +{ +public: + // Initiates a check for updates in the background. Calls the OnUpdateAvailable callback if an + // update is available, does "nothing" otherwise. + void CheckForUpdate(); + + struct NewVersionInformation + { + // Name (5.0-1234) and revision hash of the new version. + std::string new_shortrev; + std::string new_hash; + + // The full changelog in HTML format. + std::string changelog_html; + + // Internals, to be passed to the updater binary. + std::string this_manifest_url; + std::string next_manifest_url; + std::string content_store_url; + }; + + // Starts the updater process, which will wait in the background until the current process exits. + void TriggerUpdate(const NewVersionInformation& info); + +protected: + virtual void OnUpdateAvailable(const NewVersionInformation& info) = 0; +}; diff --git a/Source/Core/UICommon/CMakeLists.txt b/Source/Core/UICommon/CMakeLists.txt index bb5439badd..cbd59b1a17 100644 --- a/Source/Core/UICommon/CMakeLists.txt +++ b/Source/Core/UICommon/CMakeLists.txt @@ -1,4 +1,5 @@ set(SRCS + AutoUpdate.cpp CommandLineParse.cpp Disassembler.cpp GameFile.cpp diff --git a/Source/Core/UICommon/UICommon.vcxproj b/Source/Core/UICommon/UICommon.vcxproj index 164cb4f884..4ee5a357c0 100644 --- a/Source/Core/UICommon/UICommon.vcxproj +++ b/Source/Core/UICommon/UICommon.vcxproj @@ -42,8 +42,12 @@ {E54CF649-140E-4255-81A5-30A673C1FB36} + + {2c0d058e-de35-4471-ad99-e68a2caf9e18} + + @@ -55,6 +59,7 @@ + @@ -70,4 +75,4 @@ - + \ No newline at end of file