| 1 | #include <Windows.h> |
| 2 | |
| 3 | #include <filesystem> |
| 4 | #include <map> |
| 5 | #include <optional> |
| 6 | |
| 7 | #include "Common/CommonPaths.h" |
| 8 | #include "Common/FileUtil.h" |
| 9 | #include "Common/HttpRequest.h" |
| 10 | #include "Common/IOFile.h" |
| 11 | #include "Common/ScopeGuard.h" |
| 12 | #include "Common/StringUtil.h" |
| 13 | #include "Common/WindowsRegistry.h" |
| 14 | |
| 15 | #include "UpdaterCommon/Platform.h" |
| 16 | #include "UpdaterCommon/UI.h" |
| 17 | #include "UpdaterCommon/UpdaterCommon.h" |
| 18 | |
| 19 | namespace Platform |
| 20 | { |
| 21 | struct BuildVersion |
| 22 | { |
| 23 | u32 major{}; |
| 24 | u32 minor{}; |
| 25 | u32 build{}; |
| 26 | auto operator<=>(BuildVersion const& rhs) const = default; |
| 27 | static std::optional<BuildVersion> from_string(const std::string& str) |
| 28 | { |
| 29 | auto components = SplitString(str, '.'); |
| 30 | // Allow variable number of components (truncating after "build"), but not |
| 31 | // empty. |
| 32 | if (components.size() == 0) |
| 33 | return {}; |
| 34 | BuildVersion version; |
| 35 | if (!TryParse(components[0], &version.major, 10)) |
| 36 | return {}; |
| 37 | if (components.size() > 1 && !TryParse(components[1], &version.minor, 10)) |
| 38 | return {}; |
| 39 | if (components.size() > 2 && !TryParse(components[2], &version.build, 10)) |
| 40 | return {}; |
| 41 | return version; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | enum class VersionCheckStatus |
| 46 | { |
| 47 | NothingToDo, |
| 48 | UpdateOptional, |
| 49 | UpdateRequired, |
| 50 | }; |
| 51 | |
| 52 | struct VersionCheckResult |
| 53 | { |
| 54 | VersionCheckStatus status{VersionCheckStatus::NothingToDo}; |
| 55 | std::optional<BuildVersion> current_version{}; |
| 56 | std::optional<BuildVersion> target_version{}; |
| 57 | }; |
| 58 | |
| 59 | class BuildInfo |
| 60 | { |
| 61 | using Map = std::map<std::string, std::string>; |
| 62 | |
| 63 | public: |
| 64 | BuildInfo() = default; |
| 65 | BuildInfo(const std::string& content) |
| 66 | { |
| 67 | map = {{"OSMinimumVersionWin10", ""}, |
| 68 | {"OSMinimumVersionWin11", ""}, |
| 69 | {"VCToolsVersion", ""}, |
| 70 | {"VCToolsUpdateURL", ""}}; |
| 71 | Parse(content); |
| 72 | } |
| 73 | |
| 74 | std::optional<std::string> GetString(const std::string& name) const |
| 75 | { |
| 76 | auto it = map.find(name); |
| 77 | if (it == map.end() || it->second.size() == 0) |
| 78 | return {}; |
| 79 | return it->second; |
| 80 | } |
| 81 | |
| 82 | std::optional<BuildVersion> GetVersion(const std::string& name) const |
| 83 | { |
| 84 | auto str = GetString(name); |
| 85 | if (!str.has_value()) |
| 86 | return {}; |
| 87 | return BuildVersion::from_string(str.value()); |
| 88 | } |
| 89 | |
| 90 | private: |
| 91 | void Parse(const std::string& content) |
| 92 | { |
| 93 | std::stringstream content_stream(content); |
| 94 | std::string line; |
| 95 | while (std::getline(content_stream, line)) |
| 96 | { |
| 97 | if (line.starts_with("//")) |
| 98 | continue; |
| 99 | const size_t equals_index = line.find('='); |
| 100 | if (equals_index == line.npos) |
| 101 | continue; |
| 102 | auto key = line.substr(0, equals_index); |
| 103 | auto key_it = map.find(key); |
| 104 | if (key_it == map.end()) |
| 105 | continue; |
| 106 | auto val_start = equals_index + 1; |
| 107 | auto eol = line.find('\r', val_start); |
| 108 | auto val_size = (eol == line.npos) ? line.npos : eol - val_start; |
| 109 | key_it->second = line.substr(val_start, val_size); |
| 110 | } |
| 111 | } |
| 112 | Map map; |
| 113 | }; |
| 114 | |
| 115 | struct BuildInfos |
| 116 | { |
| 117 | BuildInfo current; |
| 118 | BuildInfo next; |
| 119 | }; |
| 120 | |
| 121 | // This default value should be kept in sync with the value of VCToolsUpdateURL in |
| 122 | // build_info.txt.in |
| 123 | static const char* VCToolsUpdateURLDefault = "https://aka.ms/vs/17/release/vc_redist.x64.exe"; |
| 124 | #define VC_RUNTIME_REGKEY R"(SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\)" |
| 125 | |
| 126 | static const char* VCRuntimeRegistrySubkey() |
| 127 | { |
| 128 | return VC_RUNTIME_REGKEY |
| 129 | #ifdef _M_X86_64 |
| 130 | "x64"; |
| 131 | #elif _M_ARM_64 |
| 132 | "arm64"; |
| 133 | #else |
| 134 | #error unsupported architecture |
| 135 | #endif |
| 136 | } |
| 137 | |
| 138 | static bool ReadVCRuntimeVersionField(u32* value, const char* name) |
| 139 | { |
| 140 | return WindowsRegistry::ReadValue(value, VCRuntimeRegistrySubkey(), name); |
| 141 | } |
| 142 | |
| 143 | static std::optional<BuildVersion> GetInstalledVCRuntimeVersion() |
| 144 | { |
| 145 | u32 installed; |
| 146 | if (!ReadVCRuntimeVersionField(&installed, "Installed") || !installed) |
| 147 | return {}; |
| 148 | BuildVersion version; |
| 149 | if (!ReadVCRuntimeVersionField(&version.major, "Major") || |
| 150 | !ReadVCRuntimeVersionField(&version.minor, "Minor") || |
| 151 | !ReadVCRuntimeVersionField(&version.build, "Bld")) |
| 152 | { |
| 153 | return {}; |
| 154 | } |
| 155 | return version; |
| 156 | } |
| 157 | |
| 158 | static VersionCheckResult VCRuntimeVersionCheck(const BuildInfos& build_infos) |
| 159 | { |
| 160 | VersionCheckResult result; |
| 161 | result.current_version = GetInstalledVCRuntimeVersion(); |
| 162 | result.target_version = build_infos.next.GetVersion("VCToolsVersion"); |
| 163 | |
| 164 | auto existing_version = build_infos.current.GetVersion("VCToolsVersion"); |
| 165 | |
| 166 | if (!result.target_version.has_value()) |
| 167 | result.status = VersionCheckStatus::UpdateOptional; |
| 168 | else if (!result.current_version.has_value() || result.current_version < result.target_version) |
| 169 | result.status = VersionCheckStatus::UpdateRequired; |
| 170 | |
| 171 | // See if the current build was already running on acceptable version of the runtime. This could |
| 172 | // happen if the user manually copied the redist DLLs and got Dolphin running that way. |
| 173 | if (existing_version.has_value() && existing_version >= result.target_version) |
| 174 | result.status = VersionCheckStatus::NothingToDo; |
| 175 | |
| 176 | return result; |
| 177 | } |
| 178 | |
| 179 | static bool VCRuntimeUpdate(const BuildInfo& build_info) |
| 180 | { |
| 181 | UI::SetDescription("Updating VC++ Redist, please wait..."); |
| 182 | |
| 183 | Common::HttpRequest req; |
| 184 | req.FollowRedirects(10); |
| 185 | auto resp = req.Get(build_info.GetString("VCToolsUpdateURL").value_or(VCToolsUpdateURLDefault)); |
| 186 | if (!resp) |
| 187 | return false; |
| 188 | |
| 189 | // Write it to current working directory. |
| 190 | auto redist_path = std::filesystem::current_path() / L"vc_redist.x64.exe"; |
| 191 | auto redist_path_u8 = WStringToUTF8(redist_path.wstring()); |
| 192 | File::IOFile redist_file; |
| 193 | redist_file.Open(redist_path_u8, "wb"); |
| 194 | if (!redist_file.WriteBytes(resp->data(), resp->size())) |
| 195 | return false; |
| 196 | redist_file.Close(); |
| 197 | |
| 198 | Common::ScopeGuard redist_deleter([&] { File::Delete(redist_path_u8); }); |
| 199 | |
| 200 | // The installer also supports /passive and /quiet. We normally pass neither (the |
| 201 | // exception being test automation) to allow the user to see and interact with the installer. |
| 202 | std::wstring cmdline = redist_path.filename().wstring() + L" /install /norestart"; |
| 203 | if (UI::IsTestMode()) |
| 204 | cmdline += L" /passive /quiet"; |
| 205 | |
| 206 | STARTUPINFOW startup_info{.cb = sizeof(startup_info)}; |
| 207 | PROCESS_INFORMATION process_info; |
| 208 | if (!CreateProcessW(redist_path.c_str(), cmdline.data(), nullptr, nullptr, TRUE, 0, nullptr, |
| 209 | nullptr, &startup_info, &process_info)) |
| 210 | { |
| 211 | return false; |
| 212 | } |
| 213 | CloseHandle(process_info.hThread); |
| 214 | |
| 215 | // Wait for it to run |
| 216 | WaitForSingleObject(process_info.hProcess, INFINITE); |
| 217 | DWORD exit_code; |
| 218 | bool has_exit_code = GetExitCodeProcess(process_info.hProcess, &exit_code); |
| 219 | CloseHandle(process_info.hProcess); |
| 220 | // NOTE: Some nonzero exit codes can still be considered success (e.g. if installation was |
| 221 | // bypassed because the same version already installed). |
| 222 | return has_exit_code && |
| 223 | (exit_code == ERROR_SUCCESS || exit_code == ERROR_SUCCESS_REBOOT_REQUIRED); |
| 224 | } |
| 225 | |
| 226 | static BuildVersion CurrentOSVersion() |
| 227 | { |
| 228 | OSVERSIONINFOW info = WindowsRegistry::GetOSVersion(); |
| 229 | return {.major = info.dwMajorVersion, .minor = info.dwMinorVersion, .build = info.dwBuildNumber}; |
| 230 | } |
| 231 | |
| 232 | static VersionCheckResult OSVersionCheck(const BuildInfo& build_info) |
| 233 | { |
| 234 | VersionCheckResult result; |
| 235 | result.current_version = CurrentOSVersion(); |
| 236 | |
| 237 | constexpr BuildVersion WIN11_BASE{10, 0, 22000}; |
| 238 | const char* version_name = |
| 239 | (result.current_version >= WIN11_BASE) ? "OSMinimumVersionWin11" : "OSMinimumVersionWin10"; |
| 240 | result.target_version = build_info.GetVersion(version_name); |
| 241 | |
| 242 | if (!result.target_version.has_value() || result.current_version >= result.target_version) |
| 243 | result.status = VersionCheckStatus::NothingToDo; |
| 244 | else |
| 245 | result.status = VersionCheckStatus::UpdateRequired; |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | static std::optional<BuildInfos> InitBuildInfos(const std::vector<TodoList::UpdateOp>& to_update, |
| 250 | const std::string& install_base_path, |
| 251 | const std::string& temp_dir) |
| 252 | { |
| 253 | const auto op_it = std::ranges::find(to_update, "build_info.txt", &TodoList::UpdateOp::filename); |
| 254 | if (op_it == to_update.cend()) |
| 255 | return {}; |
| 256 | |
| 257 | const auto op = *op_it; |
| 258 | std::string build_info_path = |
| 259 | temp_dir + DIR_SEP + HexEncode(op.new_hash.data(), op.new_hash.size()); |
| 260 | std::string build_info_content; |
| 261 | if (!File::ReadFileToString(build_info_path, build_info_content) || |
| 262 | op.new_hash != ComputeHash(build_info_content)) |
| 263 | { |
| 264 | LogToFile("Failed to read %s\n.", build_info_path.c_str()); |
| 265 | return {}; |
| 266 | } |
| 267 | BuildInfos build_infos; |
| 268 | build_infos.next = Platform::BuildInfo(build_info_content); |
| 269 | |
| 270 | build_info_path = install_base_path + DIR_SEP + "build_info.txt"; |
| 271 | build_infos.current = Platform::BuildInfo(); |
| 272 | if (File::ReadFileToString(build_info_path, build_info_content)) |
| 273 | { |
| 274 | if (op.old_hash != ComputeHash(build_info_content)) |
| 275 | LogToFile("Using modified existing BuildInfo %s.\n", build_info_path.c_str()); |
| 276 | build_infos.current = Platform::BuildInfo(build_info_content); |
| 277 | } |
| 278 | return build_infos; |
| 279 | } |
| 280 | |
| 281 | static bool CheckBuildInfo(const BuildInfos& build_infos) |
| 282 | { |
| 283 | // The existing BuildInfo may have been modified. Be careful not to overly trust its contents! |
| 284 | |
| 285 | // If the binary requires more recent OS, inform the user. |
| 286 | auto os_check = OSVersionCheck(build_infos.next); |
| 287 | if (os_check.status == VersionCheckStatus::UpdateRequired) |
| 288 | { |
| 289 | UI::Error("Please update Windows in order to update Dolphin."); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | // Check if application being launched needs more recent version of VC Redist. If so, download |
| 294 | // latest updater and execute it. |
| 295 | auto vc_check = VCRuntimeVersionCheck(build_infos); |
| 296 | const auto is_test_mode = UI::IsTestMode(); |
| 297 | if (vc_check.status != VersionCheckStatus::NothingToDo || is_test_mode) |
| 298 | { |
| 299 | auto update_ok = VCRuntimeUpdate(build_infos.next); |
| 300 | if (!update_ok && is_test_mode) |
| 301 | { |
| 302 | // For now, only check return value when test automation is running. |
| 303 | // The vc_redist exe may return other non-zero status that we don't check for, yet. |
| 304 | return false; |
| 305 | } |
| 306 | vc_check = VCRuntimeVersionCheck(build_infos); |
| 307 | if (vc_check.status == VersionCheckStatus::UpdateRequired) |
| 308 | { |
| 309 | // The update is required and the install failed for some reason. |
| 310 | UI::Error("Please update VC++ Runtime in order to update Dolphin."); |
| 311 | return false; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | bool VersionCheck(const std::vector<TodoList::UpdateOp>& to_update, |
| 319 | const std::string& install_base_path, const std::string& temp_dir) |
| 320 | { |
| 321 | auto build_infos = InitBuildInfos(to_update, install_base_path, temp_dir); |
| 322 | // If there's no build info, it means the check should be skipped. |
| 323 | if (!build_infos.has_value()) |
| 324 | { |
| 325 | return true; |
| 326 | } |
| 327 | return CheckBuildInfo(build_infos.value()); |
| 328 | } |
| 329 | |
| 330 | } // namespace Platform |