From c884e7181fd6cc0e19e9c5abb4dce102f1485233 Mon Sep 17 00:00:00 2001
From: SmallJoker <SmallJoker@users.noreply.github.com>
Date: Sun, 3 Nov 2024 15:10:39 +0100
Subject: [PATCH] JsonCPP: restore '1.0.0+' compatibility (#15368)

Previously, compiling on Ubuntu 20.04 would fail with the system-provided JsonCPP
version (1.7.4). Which would satisfy the documented requirement of "1.0.0+".
---
 CMakeLists.txt                  |  2 +-
 lib/tiniergltf/tiniergltf.hpp   |  9 ++++++++-
 src/script/common/c_content.cpp | 10 +---------
 3 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index df45fb9d8..282fea4df 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -277,7 +277,7 @@ endif()
 
 # Library pack
 find_package(GMP REQUIRED)
-find_package(Json REQUIRED)
+find_package(Json 1.0.0 REQUIRED)
 find_package(Lua REQUIRED)
 if(NOT USE_LUAJIT)
 	add_subdirectory(lib/bitop)
diff --git a/lib/tiniergltf/tiniergltf.hpp b/lib/tiniergltf/tiniergltf.hpp
index 35440f5dd..06e2f5356 100644
--- a/lib/tiniergltf/tiniergltf.hpp
+++ b/lib/tiniergltf/tiniergltf.hpp
@@ -13,10 +13,17 @@
 #include <array>
 #include <optional>
 #include <limits>
+#include <memory> // unique_ptr
 #include <stdexcept>
 #include <unordered_map>
 #include <unordered_set>
 
+#if JSONCPP_VERSION_HEXA < 0x01090000 /* 1.9.0 */
+namespace Json {
+	using String = JSONCPP_STRING; // Polyfill
+}
+#endif
+
 namespace tiniergltf {
 
 static inline void check(bool cond) {
@@ -1381,7 +1388,7 @@ static Json::Value readJson(Span<const char> span) {
 	Json::CharReaderBuilder builder;
 	const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
 	Json::Value json;
-	JSONCPP_STRING err;
+	Json::String err;
 	if (!reader->parse(span.ptr, span.end(), &json, &err))
 		throw std::runtime_error(std::string("invalid JSON: ") + err);
 	return json;
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index 5355668f0..6367e53f3 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -2048,9 +2048,6 @@ bool read_tree_def(lua_State *L, int idx, const NodeDefManager *ndef,
 }
 
 /******************************************************************************/
-#if defined(JSONCPP_STRING) || !(JSONCPP_VERSION_MAJOR < 1 || JSONCPP_VERSION_MINOR < 9)
-#define HAVE_JSON_STRING
-#endif
 
 // Returns depth of json value tree
 static int push_json_value_getdepth(const Json::Value &value)
@@ -2079,13 +2076,8 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
 			lua_pushnumber(L, value.asDouble());
 			break;
 		case Json::stringValue: {
-#ifdef HAVE_JSON_STRING
 			const auto &str = value.asString();
 			lua_pushlstring(L, str.c_str(), str.size());
-#else
-			const char *str = value.asCString();
-			lua_pushstring(L, str ? str : "");
-#endif
 			break;
 		}
 		case Json::booleanValue:
@@ -2101,7 +2093,7 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value,
 		case Json::objectValue:
 			lua_createtable(L, 0, value.size());
 			for (auto it = value.begin(); it != value.end(); ++it) {
-#ifdef HAVE_JSON_STRING
+#if JSONCPP_VERSION_HEXA >= 0x01060000 /* 1.6.0 */
 				const auto &str = it.name();
 				lua_pushlstring(L, str.c_str(), str.size());
 #else