Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Command 2055 - Process JSON Code #3215

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
81 changes: 81 additions & 0 deletions src/game_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -835,6 +836,8 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) {
return CommandManiacCallCommand(com);
case static_cast<Game_Interpreter::Cmd>(2053): //Cmd::EasyRpg_SetInterpreterFlag
return CommandEasyRpgSetInterpreterFlag(com);
case static_cast<Game_Interpreter::Cmd>(2055): //EasyRPG_ProcessJson
return CommandProcessJson(com);
default:
return true;
}
Expand Down Expand Up @@ -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 != "<<INVALID_OUTPUT>>") {
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 != "<<INVALID_OUTPUT>>") {
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() {
Expand Down
1 change: 1 addition & 0 deletions src/game_interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t>::const_iterator& it);
const std::string DecodeString(lcf::DBArray<int32_t>::const_iterator& it);
Expand Down
172 changes: 172 additions & 0 deletions src/json_helper.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#include "json_helper.h"

#ifdef HAVE_NLOHMANN_JSON

#include "output.h"
#include <sstream>
#include <unordered_map>

namespace Json_Helper {

const std::string k_invalid_str = "<<INVALID_OUTPUT>>";

std::unordered_map<std::string, nlohmann::json> 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<nlohmann::json::array_t&>().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<std::string>();
}
else if (json_obj.is_number_integer()) {
result = std::to_string(json_obj.get<int>());
}
else if (json_obj.is_number_float()) {
result = std::to_string(json_obj.get<float>());
}
else if (json_obj.is_boolean()) {
result = json_obj.get<bool>() ? "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
34 changes: 34 additions & 0 deletions src/json_helper.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#ifndef JSON_HELPER_H
#define JSON_HELPER_H

#include "system.h"

#ifdef HAVE_NLOHMANN_JSON

#include <string>
#include <nlohmann/json.hpp>

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
Loading