Skip to content

Commit e632a37

Browse files
committed
PROCESS JSON COMMAND - New Features
The Operations were updated to: 0: GET 1: SET 2: GET LENGTH 3: GET KEYS 4: GET VAR TYPE There are also new Flags: - Extract data from json path - similar to how stringvars extracts. - Prettify json - to make its output easier to read.
1 parent e07493f commit e632a37

File tree

3 files changed

+380
-62
lines changed

3 files changed

+380
-62
lines changed

src/game_interpreter.cpp

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5363,6 +5363,17 @@ bool Game_Interpreter::CommandEasyRpgProcessJson(lcf::rpg::EventCommand const& c
53635363
int target_var_id = ValueOrVariable(com.parameters[6], com.parameters[7]);
53645364

53655365
std::string json_path = ToString(CommandStringOrVariable(com, 8, 9));
5366+
5367+
int extract_data_from_string = com.parameters[11];
5368+
bool pretty_print = com.parameters[12] == 1;
5369+
5370+
if (extract_data_from_string == 1) { // as string
5371+
json_path = Game_Strings::Extract(json_path, false);
5372+
}
5373+
if (extract_data_from_string == 2) { // as hex
5374+
json_path = Game_Strings::Extract(json_path, true);
5375+
}
5376+
53665377
auto* json_data = Main_Data::game_strings->ParseJson(source_var_id);
53675378

53685379
if (!json_data) {
@@ -5377,7 +5388,8 @@ bool Game_Interpreter::CommandEasyRpgProcessJson(lcf::rpg::EventCommand const& c
53775388

53785389
std::optional<std::string> result;
53795390

5380-
if (operation == 0) { // Get operation: Extract a value from JSON data
5391+
switch (operation) {
5392+
case 0: { // Get operation: Extract a value from JSON data
53815393
result = Json_Helper::GetValue(*json_data, json_path);
53825394

53835395
if (result) {
@@ -5392,12 +5404,13 @@ bool Game_Interpreter::CommandEasyRpgProcessJson(lcf::rpg::EventCommand const& c
53925404
Main_Data::game_strings->Asg({ target_var_id }, *result);
53935405
break;
53945406
default:
5395-
Output::Warning("CommandEasyRpgProcessJson: Unsupported target_var_type {}", operation);
5407+
Output::Warning("CommandEasyRpgProcessJson: Unsupported target_var_type {}", target_var_type);
53965408
return true;
53975409
}
53985410
}
5411+
break;
53995412
}
5400-
else if (operation == 1) { // Set operation: Update JSON data with a new value
5413+
case 1: { // Set operation: Update JSON data with a new value
54015414
std::string new_value;
54025415

54035416
switch (target_var_type) {
@@ -5420,11 +5433,76 @@ bool Game_Interpreter::CommandEasyRpgProcessJson(lcf::rpg::EventCommand const& c
54205433
if (result) {
54215434
Main_Data::game_strings->Asg({ source_var_id }, *result);
54225435
}
5436+
break;
5437+
}
5438+
case 2: { // GetLength operation
5439+
auto length = Json_Helper::GetLength(*json_data, json_path);
5440+
if (length) {
5441+
switch (target_var_type) {
5442+
case 0: // Switch
5443+
Main_Data::game_switches->Set(target_var_id, *length > 0);
5444+
break;
5445+
case 1: // Variable
5446+
Main_Data::game_variables->Set(target_var_id, static_cast<int>(*length));
5447+
break;
5448+
case 2: // String
5449+
Main_Data::game_strings->Asg({ target_var_id }, std::to_string(*length));
5450+
break;
5451+
}
5452+
}
5453+
break;
5454+
}
5455+
case 3: { // GetKeys operation
5456+
auto keys = Json_Helper::GetKeys(*json_data, json_path);
5457+
if (keys && target_var_type == 2) { // Keys can only be stored in strings
5458+
std::string keys_str;
5459+
for (size_t i = 0; i < keys->size(); ++i) {
5460+
if (i > 0) keys_str += ",";
5461+
keys_str += "\"" + (*keys)[i] + "\"";
5462+
}
5463+
Main_Data::game_strings->Asg({ target_var_id }, "{ \"keys\": [" + keys_str + "] }");
5464+
}
5465+
break;
54235466
}
5424-
else {
5467+
case 4: { // GetType operation
5468+
auto type = Json_Helper::GetType(*json_data, json_path);
5469+
if (type) {
5470+
int type_code = 0;
5471+
switch (target_var_type) {
5472+
case 0: // Switch
5473+
// For switches, true if it's an object or array (for backward compatibility)
5474+
Main_Data::game_switches->Set(target_var_id, *type == "object" || *type == "array");
5475+
break;
5476+
case 1: // Variable
5477+
// For variables, return a numeric code for the type:
5478+
// 1=object, 2=array, 3=string, 4=number, 5=boolean, 6=null
5479+
if (*type == "object") type_code = 1;
5480+
else if (*type == "array") type_code = 2;
5481+
else if (*type == "string") type_code = 3;
5482+
else if (*type == "number") type_code = 4;
5483+
else if (*type == "boolean") type_code = 5;
5484+
else if (*type == "null") type_code = 6;
5485+
Main_Data::game_variables->Set(target_var_id, type_code);
5486+
break;
5487+
case 2: // String
5488+
Main_Data::game_strings->Asg({ target_var_id }, *type);
5489+
break;
5490+
}
5491+
}
5492+
break;
5493+
}
5494+
default:
54255495
Output::Warning("CommandEasyRpgProcessJson: Invalid Operation {}", operation);
54265496
}
54275497

5498+
if (target_var_type == 2 && pretty_print == 1) { // Only works with strings
5499+
std::string target_str = ToString(Main_Data::game_strings->Get(target_var_id));
5500+
if (auto parsed_json = Json_Helper::Parse(target_str)) {
5501+
std::string formatted = Json_Helper::PrettyPrint(*parsed_json, 2);
5502+
Main_Data::game_strings->Asg({ target_var_id }, formatted);
5503+
}
5504+
}
5505+
54285506
return true;
54295507

54305508
#endif // !HAVE_NLOHMANN_JSON

0 commit comments

Comments
 (0)