diff --git a/CMakeLists.txt b/CMakeLists.txt index ac41b7dadb..91d84d4e56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -233,6 +233,8 @@ add_library(${PROJECT_NAME} OBJECT src/input_source.h src/instrumentation.cpp src/instrumentation.h + src/json_helper.cpp + src/json_helper.h src/keys.h src/main_data.cpp src/main_data.h diff --git a/Makefile.am b/Makefile.am index 016e1fa74a..31d94c25d5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -211,6 +211,8 @@ libeasyrpg_player_a_SOURCES = \ src/input_source.h \ src/instrumentation.cpp \ src/instrumentation.h \ + src/json_helper.cpp \ + src/json_helper.h \ src/keys.h \ src/main_data.cpp \ src/main_data.h \ diff --git a/src/game_interpreter.cpp b/src/game_interpreter.cpp index 84dc8748fd..398c41fed1 100644 --- a/src/game_interpreter.cpp +++ b/src/game_interpreter.cpp @@ -44,6 +44,7 @@ #include "game_screen.h" #include "game_interpreter_control_variables.h" #include "game_windows.h" +#include "json_helper.h" #include "maniac_patch.h" #include "spriteset_map.h" #include "sprite_character.h" @@ -835,6 +836,8 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) { return CommandManiacCallCommand(com); case static_cast(2053): //Cmd::EasyRpg_SetInterpreterFlag return CommandEasyRpgSetInterpreterFlag(com); + case static_cast(2055): //EasyRPG_ProcessJson + return CommandProcessJson(com); default: return true; } @@ -5141,8 +5144,86 @@ bool Game_Interpreter::CommandEasyRpgSetInterpreterFlag(lcf::rpg::EventCommand c if (flag_name == "rpg2k-battle") lcf::Data::system.easyrpg_use_rpg2k_battle_system = flag_value; + + return true; +} +bool Game_Interpreter::CommandProcessJson(lcf::rpg::EventCommand const& com) { +#ifndef HAVE_NLOHMANN_JSON + Output::Warning("CommandProcessJson: JSON not supported on this platform"); return true; +#else + + if (!Player::IsPatchManiac()) { + Output::Warning("CommandProcessJson: This command needs Maniac Patch support"); + return true; + } + + int operation = ValueOrVariable(com.parameters[0], com.parameters[1]); + int source_var_id = ValueOrVariable(com.parameters[2], com.parameters[3]); + int target_var_type = ValueOrVariable(com.parameters[4], com.parameters[5]); + int target_var_id = ValueOrVariable(com.parameters[6], com.parameters[7]); + std::string json_path = ToString(CommandStringOrVariable(com, 8, 9)); + std::string json_data = ToString(Main_Data::game_strings->Get(source_var_id)); + + std::string result; + + if (operation == 0) { // Get operation: Extract a value from JSON data + result = Json_Helper::GetValue(json_data, json_path); + + if (result != "<>") { + int output_value = 0; + + std::istringstream iss(result); + int temp; + if (iss >> temp) { + output_value = temp; + } + else { + output_value = 0; + } + + switch (target_var_type) { + case 0: // Switch + Main_Data::game_switches->Set({ target_var_id }, output_value); + break; + case 1: // Variable + Main_Data::game_variables->Set({ target_var_id }, output_value); + break; + default: // String + Main_Data::game_strings->Asg({ target_var_id }, result); + break; + } + } + } + else if (operation == 1) { // Set operation: Update JSON data with a new value + std::string new_value; + + switch (target_var_type) { + case 0: // Switch + new_value = std::to_string(Main_Data::game_switches->Get(target_var_id)); + break; + case 1: // Variable + new_value = std::to_string(Main_Data::game_variables->Get(target_var_id)); + break; + default: // String + new_value = ToString(Main_Data::game_strings->Get(target_var_id)); + break; + } + + result = Json_Helper::SetValue(json_data, json_path, new_value); + + if (result != "<>") { + Main_Data::game_strings->Asg({ source_var_id }, result); + } + } + else { + Output::Warning("Process JSON - Invalid Operation: {}", operation); + } + + return true; + +#endif // !HAVE_NLOHMANN_JSON } Game_Interpreter& Game_Interpreter::GetForegroundInterpreter() { diff --git a/src/game_interpreter.h b/src/game_interpreter.h index 731aadf5cb..8ea918ee4f 100644 --- a/src/game_interpreter.h +++ b/src/game_interpreter.h @@ -297,6 +297,7 @@ class Game_Interpreter bool CommandManiacControlStrings(lcf::rpg::EventCommand const& com); bool CommandManiacCallCommand(lcf::rpg::EventCommand const& com); bool CommandEasyRpgSetInterpreterFlag(lcf::rpg::EventCommand const& com); + bool CommandProcessJson(lcf::rpg::EventCommand const& com); int DecodeInt(lcf::DBArray::const_iterator& it); const std::string DecodeString(lcf::DBArray::const_iterator& it); diff --git a/src/json_helper.cpp b/src/json_helper.cpp new file mode 100644 index 0000000000..1548fb15a3 --- /dev/null +++ b/src/json_helper.cpp @@ -0,0 +1,172 @@ +/* + * This file is part of EasyRPG Player. + * + * EasyRPG Player is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Player is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Player. If not, see . + */ + +#include "json_helper.h" + +#ifdef HAVE_NLOHMANN_JSON + +#include "output.h" +#include +#include + +namespace Json_Helper { + + const std::string k_invalid_str = "<>"; + + std::unordered_map json_cache; + + nlohmann::json* GetObjectAtPath(nlohmann::json& json_obj, std::string_view json_path, bool allow_path_creation = false) { + nlohmann::json* current_obj = &json_obj; + + std::stringstream path_stream(json_path.data()); + std::string token; + + while (std::getline(path_stream, token, '.')) { + if (token.back() == ']') { + size_t bracket_pos = token.find('['); + std::string array_name = token.substr(0, bracket_pos); + int index = std::stoi(token.substr(bracket_pos + 1, token.length() - bracket_pos - 2)); + + if (!current_obj->contains(array_name)) { + if (allow_path_creation) { + (*current_obj)[array_name] = nlohmann::json::array(); + } + else { + return nullptr; + } + } + + nlohmann::json& array_obj = (*current_obj)[array_name]; + + if (index >= array_obj.size()) { + if (allow_path_creation) { + array_obj.get_ref().resize(index + 1); + } + else { + return nullptr; + } + } + + current_obj = &(array_obj[index]); + } + else { + if (!current_obj->contains(token)) { + if (allow_path_creation) { + (*current_obj)[token] = nlohmann::json::object(); + } + else { + return nullptr; + } + } + + current_obj = &((*current_obj)[token]); + } + } + + return current_obj; + } + + std::string GetValueAsString(const nlohmann::json& json_obj) { + std::string result; + + if (json_obj.is_string()) { + result = json_obj.get(); + } + else if (json_obj.is_number_integer()) { + result = std::to_string(json_obj.get()); + } + else if (json_obj.is_number_float()) { + result = std::to_string(json_obj.get()); + } + else if (json_obj.is_boolean()) { + result = json_obj.get() ? "true" : "false"; + } + else { + result = json_obj.dump(); + } + + return result; + } + + std::string GetValue(std::string_view json_data, std::string_view json_path) { + try { + nlohmann::json json_obj; + auto it = json_cache.find(json_data.data()); + + if (it != json_cache.end()) { + json_obj = it->second; + } + else { + json_obj = nlohmann::json::parse(json_data); + json_cache[json_data.data()] = json_obj; + } + + nlohmann::json* current_obj = GetObjectAtPath(json_obj, json_path); + + if (current_obj == nullptr) { + Output::Warning("JSON_ERROR - Invalid path: {}", json_path); + return k_invalid_str; + } + + return GetValueAsString(*current_obj); + } + catch (const std::exception& e) { + Output::Warning("JSON_ERROR - {}", e.what()); + return k_invalid_str; + } + } + + std::string SetValue(std::string_view json_data, std::string_view json_path, std::string_view value) { + try { + nlohmann::json json_obj; + auto it = json_cache.find(json_data.data()); + + if (it != json_cache.end()) { + json_obj = it->second; + } + else { + json_obj = nlohmann::json::parse(json_data); + json_cache[json_data.data()] = json_obj; + } + + nlohmann::json* current_obj = GetObjectAtPath(json_obj, json_path, true); + + if (current_obj == nullptr) { + Output::Warning("JSON_ERROR - Invalid path: {}", json_path); + return std::string(json_data); + } + + // Parse the value string and set the appropriate JSON type + try { + *current_obj = nlohmann::json::parse(value); + } + catch (const nlohmann::json::parse_error&) { + // If parsing fails, treat it as a string value + *current_obj = value; + } + + return json_obj.dump(); + } + catch (const std::exception& e) { + Output::Warning("JSON_ERROR - {}", e.what()); + return std::string(json_data); + } + } + +} + +#endif // HAVE_NLOHMANN_JSON diff --git a/src/json_helper.h b/src/json_helper.h new file mode 100644 index 0000000000..5a07aa9022 --- /dev/null +++ b/src/json_helper.h @@ -0,0 +1,34 @@ +/* + * This file is part of EasyRPG Player. + * + * EasyRPG Player is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EasyRPG Player is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EasyRPG Player. If not, see . + */ + +#ifndef JSON_HELPER_H +#define JSON_HELPER_H + +#include "system.h" + +#ifdef HAVE_NLOHMANN_JSON + +#include +#include + +namespace Json_Helper { + std::string GetValue(std::string_view json_data, std::string_view json_path); + std::string SetValue(std::string_view json_data, std::string_view json_path, std::string_view value); +} + +#endif // HAVE_NLOHMANN_JSON +#endif // JSON_HELPER_H