Skip to content

Commit

Permalink
Emscripten: Migrate from picojson to nlohmann_json
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed May 20, 2024
1 parent c160ec9 commit a94d338
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1,250 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ src/external/* -diff linguist-vendored
src/midiprogram.h -diff linguist-vendored
src/midisequencer.* -diff linguist-vendored
src/midisynth.* -diff linguist-vendored
src/picojson.h -diff linguist-vendored
CMakePresets.json -diff linguist-generated
builds/android/app/src/main/java/org/libsdl/app/*.java -diff linguist-vendored
builds/android/app/src/main/java/org/libsdl/app/SDLActivity.java diff
Expand Down
24 changes: 12 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(PLAYER_JS_GAME_URL "games/" CACHE STRING "Game URL/directory where the web player searches for games")
set(PLAYER_JS_OUTPUT_NAME "easyrpg-player" CACHE STRING "Output name of the js, html and wasm files")
set_property(SOURCE src/async_handler.cpp APPEND PROPERTY COMPILE_DEFINITIONS "EM_GAME_URL=\"${PLAYER_JS_GAME_URL}\"")
target_sources(${PROJECT_NAME} PRIVATE src/external/picojson.h)
endif()

# Endianess check
Expand Down Expand Up @@ -885,20 +884,21 @@ player_find_package(NAME lhasa
)

# json support
option(PLAYER_WITH_NLOHMANN_JSON "Support processing of JSON files" ON)

set(NLOHMANN_JSON_REQUIRED "")
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
set(NLOHMANN_JSON_REQUIRED "REQUIRED")
player_find_package(NAME nlohmann_json
DEFINITION HAVE_NLOHMANN_JSON
TARGET nlohmann_json::nlohmann_json
REQUIRED
)
else()
option(PLAYER_WITH_NLOHMANN_JSON "Support processing of JSON files" ON)
player_find_package(NAME nlohmann_json
CONDITION PLAYER_WITH_NLOHMANN_JSON
DEFINITION HAVE_NLOHMANN_JSON
TARGET nlohmann_json::nlohmann_json
)
endif()

player_find_package(NAME nlohmann_json
CONDITION PLAYER_WITH_NLOHMANN_JSON
DEFINITION HAVE_NLOHMANN_JSON
TARGET nlohmann_json::nlohmann_json
${NLOHMANN_JSON_REQUIRED}
)

# Sound system to use
if(${PLAYER_TARGET_PLATFORM} STREQUAL "SDL2")
set(PLAYER_AUDIO_BACKEND "SDL2" CACHE STRING "Audio system to use. Options: SDL2 OFF")
Expand Down
1 change: 0 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,6 @@ EXTRA_DIST += \
bench/text.cpp \
bench/utils.cpp \
bench/variables.cpp \
src/external/picojson.h \
src/platform/3ds/audio.cpp \
src/platform/3ds/audio.h \
src/platform/3ds/clock.cpp \
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ EasyRPG Player makes use of the following 3rd party software:
(Yoshio Uno), provided under the (3-clause) BSD license
* [dr_wav] WAV audio loader and writer - Copyright (c) David Reid, provided
under public domain or MIT-0
* [PicoJSON] JSON parser/serializer - Copyright (c) 2009-2010 Cybozu Labs, Inc.
Copyright (c) 2011-2015 Kazuho Oku, provided under the (2-clause) BSD license

### 3rd party resources

Expand All @@ -123,7 +121,6 @@ EasyRPG Player makes use of the following 3rd party software:
[Logo2]: resources/logo2.png
[FMMidi]: http://unhaut.epizy.com/fmmidi
[dr_wav]: https://github.com/mackron/dr_libs
[PicoJSON]: https://github.com/kazuho/picojson
[baekmuk]: https://kldp.net/baekmuk
[Shinonome]: http://openlab.ring.gr.jp/efont/shinonome
[ttyp0]: https://people.mpi-inf.mpg.de/~uwe/misc/uw-ttyp0
Expand Down
53 changes: 25 additions & 28 deletions src/async_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
#ifdef EMSCRIPTEN
# include <emscripten.h>
# include <lcf/reader_util.h>
# define PICOJSON_USE_LOCALE 0
# define PICOJSON_ASSERT(e) do { if (! (e)) assert(false && #e); } while (0)
# include "external/picojson.h"
# include <nlohmann/json.hpp>
using json = nlohmann::json;
#endif

#include "async_handler.h"
Expand Down Expand Up @@ -148,49 +147,47 @@ void AsyncHandler::CreateRequestMapping(const std::string& file) {
return;
}

picojson::value v;
picojson::parse(v, f);
json j = json::parse(f, nullptr, false);
if (j.is_discarded()) {
Output::Error("Emscripten: index.json is not a valid JSON file");
return;
}

const auto& metadata = v.get("metadata");
if (metadata.is<picojson::object>()) {
for (const auto& value : metadata.get<picojson::object>()) {
if (value.first == "version") {
index_version = (int)value.second.get<double>();
}
if (j.contains("metadata") && j["metadata"].is_object()) {
const auto& metadata = j["metadata"];
if (metadata.contains("version") && metadata["version"].is_number()) {
index_version = metadata["version"].get<int>();
}
}

Output::Debug("Parsing index.json version {}", index_version);

if (index_version <= 1) {
// legacy format
for (const auto& value : v.get<picojson::object>()) {
file_mapping[value.first] = value.second.to_str();
for (const auto& value : j.items()) {
file_mapping[value.key()] = value.value().get<std::string>();
}
} else {
using fn = std::function<void(const picojson::object&,const std::string&)>;
fn parse = [&] (const picojson::object& obj, const std::string& path) {
using fn = std::function<void(const json&, const std::string&)>;
fn parse = [&] (const json& obj, const std::string& path) {
std::string dirname;
for (const auto& value : obj) {
if (value.first == "_dirname" && value.second.is<std::string>()) {
dirname = value.second.to_str();
}
if (obj.contains("_dirname") && obj["_dirname"].is_string()) {
dirname = obj["_dirname"].get<std::string>();
}
dirname = FileFinder::MakePath(path, dirname);

for (const auto& value : obj) {
const auto& second = value.second;
if (second.is<picojson::object>()) {
parse(second.get<picojson::object>(), dirname);
} else if (second.is<std::string>()){
file_mapping[FileFinder::MakePath(Utils::LowerCase(dirname), value.first)] = FileFinder::MakePath(dirname, second.to_str());
for (const auto& value : obj.items()) {
const auto& second = value.value();
if (second.is_object()) {
parse(second, dirname);
} else if (second.is_string()){
file_mapping[FileFinder::MakePath(Utils::LowerCase(dirname), value.key())] = FileFinder::MakePath(dirname, second.get<std::string>());
}
}
};

const auto& cache = v.get("cache");
if (cache.is<picojson::object>()) {
parse(cache.get<picojson::object>(), "");
if (j.contains("cache") && j["cache"].is_object()) {
parse(j["cache"], "");
}

// Create some empty DLL files. Engine & patch detection depend on them.
Expand Down
Loading

0 comments on commit a94d338

Please sign in to comment.