浏览代码

UI: Migrate Windows update check to nlohmann JSON

derrod 2 年之前
父节点
当前提交
01b21f67ff
共有 3 个文件被更改,包括 30 次插入44 次删除
  1. 3 1
      UI/cmake/legacy.cmake
  2. 5 2
      UI/cmake/os-windows.cmake
  3. 22 41
      UI/update/win-update.cpp

+ 3 - 1
UI/cmake/legacy.cmake

@@ -340,6 +340,7 @@ if(OS_WINDOWS)
   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/obs.rc.in ${CMAKE_BINARY_DIR}/obs.rc)
 
   find_package(Detours REQUIRED)
+  find_package(nlohmann_json REQUIRED)
 
   target_sources(
     obs
@@ -356,10 +357,11 @@ if(OS_WINDOWS)
             update/update-helpers.hpp
             update/crypto-helpers-mbedtls.cpp
             update/crypto-helpers.hpp
+            win-update/updater/manifest.hpp
             ${CMAKE_BINARY_DIR}/obs.rc)
 
   find_package(MbedTLS)
-  target_link_libraries(obs PRIVATE Mbedtls::Mbedtls OBS::blake2 Detours::Detours)
+  target_link_libraries(obs PRIVATE Mbedtls::Mbedtls nlohmann_json::nlohmann_json OBS::blake2 Detours::Detours)
 
   target_compile_features(obs PRIVATE cxx_std_17)
 

+ 5 - 2
UI/cmake/os-windows.cmake

@@ -8,6 +8,7 @@ endif()
 
 find_package(MbedTLS)
 find_package(Detours REQUIRED)
+find_package(nlohmann_json REQUIRED)
 
 configure_file(cmake/windows/obs.rc.in obs.rc)
 
@@ -26,9 +27,11 @@ target_sources(
           update/update-window.cpp
           update/update-window.hpp
           update/win-update.cpp
-          update/win-update.hpp)
+          update/win-update.hpp
+          win-update/updater/manifest.hpp)
 
-target_link_libraries(obs-studio PRIVATE crypt32 OBS::blake2 OBS::w32-pthreads MbedTLS::MbedTLS Detours::Detours)
+target_link_libraries(obs-studio PRIVATE crypt32 OBS::blake2 OBS::w32-pthreads MbedTLS::MbedTLS
+                                         nlohmann_json::nlohmann_json Detours::Detours)
 target_compile_definitions(obs-studio PRIVATE PSAPI_VERSION=2)
 target_link_options(obs-studio PRIVATE /IGNORE:4098 /IGNORE:4099)
 

+ 22 - 41
UI/update/win-update.cpp

@@ -1,3 +1,4 @@
+#include "../win-update/updater/manifest.hpp"
 #include "update-helpers.hpp"
 #include "shared-update.hpp"
 #include "update-window.hpp"
@@ -17,14 +18,13 @@
 
 #include <util/windows/WinHandle.hpp>
 #include <util/util.hpp>
-#include <json11.hpp>
 
 #ifdef BROWSER_AVAILABLE
 #include <browser-panel.hpp>
 #endif
 
 using namespace std;
-using namespace json11;
+using namespace updater;
 
 /* ------------------------------------------------------------------------ */
 
@@ -71,60 +71,41 @@ using namespace json11;
 #define CUR_COMMIT "00000000"
 #endif
 
-static bool ParseUpdateManifest(const char *manifest, bool *updatesAvailable,
-				string &notes_str, uint64_t &updateVer,
-				string &branch)
+static bool ParseUpdateManifest(const char *manifest_data,
+				bool *updatesAvailable, string &notes,
+				uint64_t &updateVer, const string &branch)
 try {
+	json manifestContents = json::parse(manifest_data);
+	Manifest manifest = manifestContents.get<Manifest>();
 
-	string error;
-	Json root = Json::parse(manifest, error);
-	if (!error.empty())
-		throw strprintf("Failed reading json string: %s",
-				error.c_str());
+	if (manifest.version_major == 0 && manifest.commit.empty())
+		throw strprintf("Invalid version number: %d.%d.%d",
+				manifest.version_major, manifest.version_minor,
+				manifest.version_patch);
 
-	if (!root.is_object())
-		throw string("Root of manifest is not an object");
-
-	int major = root["version_major"].int_value();
-	int minor = root["version_minor"].int_value();
-	int patch = root["version_patch"].int_value();
-	int rc = root["rc"].int_value();
-	int beta = root["beta"].int_value();
-	string commit_hash = root["commit"].string_value();
-
-	if (major == 0 && commit_hash.empty())
-		throw strprintf("Invalid version number: %d.%d.%d", major,
-				minor, patch);
-
-	const Json &notes = root["notes"];
-	if (!notes.is_string())
-		throw string("'notes' value invalid");
-
-	notes_str = notes.string_value();
-
-	const Json &packages = root["packages"];
-	if (!packages.is_array())
-		throw string("'packages' value invalid");
+	notes = manifest.notes;
 
 	uint64_t cur_ver;
 	uint64_t new_ver;
 
-	if (commit_hash.empty()) {
+	if (manifest.commit.empty()) {
 		cur_ver = CUR_VER;
-		new_ver = MAKE_SEMANTIC_VERSION(
-			(uint64_t)major, (uint64_t)minor, (uint64_t)patch);
+		new_ver =
+			MAKE_SEMANTIC_VERSION((uint64_t)manifest.version_major,
+					      (uint64_t)manifest.version_minor,
+					      (uint64_t)manifest.version_patch);
 		new_ver <<= 16;
 		/* RC builds are shifted so that rc1 and beta1 versions do not result
 		 * in the same new_ver. */
-		if (rc > 0)
-			new_ver |= (uint64_t)rc << 8;
-		else if (beta > 0)
-			new_ver |= (uint64_t)beta;
+		if (manifest.rc > 0)
+			new_ver |= (uint64_t)manifest.rc << 8;
+		else if (manifest.beta > 0)
+			new_ver |= (uint64_t)manifest.beta;
 	} else {
 		/* Test or nightly builds may not have a (valid) version number,
 		 * so compare commit hashes instead. */
 		cur_ver = stoul(CUR_COMMIT, nullptr, 16);
-		new_ver = stoul(commit_hash.substr(0, 8), nullptr, 16);
+		new_ver = stoul(manifest.commit.substr(0, 8), nullptr, 16);
 	}
 
 	updateVer = new_ver;