From dc348b48129ff0f977751c4f6906935608a36929 Mon Sep 17 00:00:00 2001 From: GenevensiS <66968533+G-e-n-e-v-e-n-s-i-S@users.noreply.github.com> Date: Thu, 7 Aug 2025 18:45:12 +0200 Subject: [PATCH] Add to_json and from_json script functions --- doc/function/new_card.txt | 2 +- src/data/field.cpp | 1 - src/data/format/clipboard.cpp | 5 +- src/data/keyword.hpp | 10 +- src/data/pack.cpp | 2 +- src/data/set.hpp | 2 +- src/gui/add_json_window.cpp | 53 +- src/gui/set/cards_panel.cpp | 50 +- src/script/functions/basic.cpp | 24 + src/script/functions/construction.cpp | 12 +- src/script/functions/construction_helper.hpp | 107 ++-- src/script/functions/json.hpp | 596 +++++++++++++++++++ src/script/to_value.hpp | 5 +- src/util/index_map.hpp | 2 +- src/util/io/reader.hpp | 3 + src/util/io/writer.cpp | 6 + src/util/io/writer.hpp | 9 +- src/util/locale.hpp | 1 + 18 files changed, 748 insertions(+), 142 deletions(-) create mode 100644 src/script/functions/json.hpp diff --git a/doc/function/new_card.txt b/doc/function/new_card.txt index c61630c8..ff20ea65 100644 --- a/doc/function/new_card.txt +++ b/doc/function/new_card.txt @@ -7,7 +7,7 @@ Creates a new [[type:card]] object. The card is not automatically added to a set The argument is a map from card field names to values, for example @new_card([name: "My Card"])@ creates a card with the name @"My Card"@, and all other fields at their default value. -The map can also contain the following built-in keys: notes, id, linked_card_1 to linked_card_4, linked_relation_1 to linked_relation_4, stylesheet, and styling_data. For styling_data, the value must itself be a map from styling field names to values. Be sure to define a stylesheet before styling_data. +The map can also contain the following built-in keys: notes, id, linked_card_1 to linked_card_4, linked_relation_1 to linked_relation_4, stylesheet, styling_data, and extra_data. For styling_data and extra_data, the value must itself be a map from field names to values. Be sure to define a stylesheet before these. NOTE: you should use underscores instead of spaces in field names. diff --git a/src/data/field.cpp b/src/data/field.cpp index 168b9f04..e0f5c4fc 100644 --- a/src/data/field.cpp +++ b/src/data/field.cpp @@ -40,7 +40,6 @@ Field::~Field() {} void Field::initDependencies(Context& ctx, const Dependency& dep) const { sort_script.initDependencies(ctx, dep); - import_script.initDependencies(ctx, dep); } IMPLEMENT_REFLECTION(Field) { diff --git a/src/data/format/clipboard.cpp b/src/data/format/clipboard.cpp index 7f6a1031..6574cfbc 100644 --- a/src/data/format/clipboard.cpp +++ b/src/data/format/clipboard.cpp @@ -62,9 +62,9 @@ wxDataFormat CardsDataObject::format = _("application/x-mse-cards"); CardsDataObject::CardsDataObject(const SetP& set, const vector& cards) { // set the stylesheet, so when deserializing we know whos style options we are reading - bool* has_styling = new bool[cards.size()]; + vector has_styling; for (size_t i = 0 ; i < cards.size() ; ++i) { - has_styling[i] = cards[i]->has_styling && !cards[i]->stylesheet; + has_styling.push_back(cards[i]->has_styling && !cards[i]->stylesheet); if (has_styling[i]) { cards[i]->stylesheet = set->stylesheet; } @@ -78,7 +78,6 @@ CardsDataObject::CardsDataObject(const SetP& set, const vector& cards) { } } SetFormat(format); - delete [] has_styling; } CardsDataObject::CardsDataObject() { diff --git a/src/data/keyword.hpp b/src/data/keyword.hpp index 5888a853..afd5a73a 100644 --- a/src/data/keyword.hpp +++ b/src/data/keyword.hpp @@ -91,11 +91,11 @@ public: Keyword() : fixed(false), valid(false) {} String keyword; ///< The keyword, only for human use - String rules; ///< Rules/explanation - String match; ///< String to match, tags are used for parameters - vector parameters; ///< The types of parameters - StringScript reminder; ///< Reminder text of the keyword - String mode; ///< Mode of use, can be used by scripts (only gives the name) + String rules; ///< Rules/explanation + String match; ///< String to match, tags are used for parameters + vector parameters; ///< The types of parameters + StringScript reminder; ///< Reminder text of the keyword + String mode; ///< Mode of use, can be used by scripts (only gives the name) /// Regular expression to match and split parameters, automatically generated. /** The regex has exactly 2 * parameters.size() + 1 captures (excluding the entire match, caputure 0), * captures 1,3,... capture the plain text of the match string diff --git a/src/data/pack.cpp b/src/data/pack.cpp index 18d83daa..0e184a30 100644 --- a/src/data/pack.cpp +++ b/src/data/pack.cpp @@ -94,7 +94,7 @@ bool PackType::update(Context& ctx) { bool PackItem::update(Context& ctx) { return amount.update(ctx) - | weight.update(ctx); + || weight.update(ctx); } diff --git a/src/data/set.hpp b/src/data/set.hpp index e6b24af7..0775953f 100644 --- a/src/data/set.hpp +++ b/src/data/set.hpp @@ -50,7 +50,7 @@ public: /// The values on the fields of the set /** The indices should correspond to the set_fields in the Game */ IndexMap data; - /// Extra values for specitic stylesheets, indexed by stylesheet name + /// Extra values for specific stylesheets, indexed by stylesheet name DelayedIndexMaps styling_data; vector cards; ///< The cards in the set vector keywords; ///< Additional keywords used in this set diff --git a/src/gui/add_json_window.cpp b/src/gui/add_json_window.cpp index 414c0c10..c17df358 100644 --- a/src/gui/add_json_window.cpp +++ b/src/gui/add_json_window.cpp @@ -19,10 +19,11 @@ #include #include #include +#include