diff options
Diffstat (limited to 'src/squelch')
-rw-r--r-- | src/squelch/automatizer.cc | 35 | ||||
-rw-r--r-- | src/squelch/condition.cc | 350 | ||||
-rw-r--r-- | src/squelch/condition_metadata.cc | 88 | ||||
-rw-r--r-- | src/squelch/rule.cc | 84 | ||||
-rw-r--r-- | src/squelch/tree_printer.cc | 5 |
5 files changed, 290 insertions, 272 deletions
diff --git a/src/squelch/automatizer.cc b/src/squelch/automatizer.cc index c3c52b1b..b06367a8 100644 --- a/src/squelch/automatizer.cc +++ b/src/squelch/automatizer.cc @@ -12,20 +12,21 @@ namespace squelch { /** * Parse rules from JSON array */ -static std::vector< std::shared_ptr < Rule > > parse_rules(json_t *rules_j) +static std::vector< std::shared_ptr < Rule > > parse_rules(jsoncons::json const &rules_json) { std::vector< std::shared_ptr < Rule > > rules; - if (!json_is_array(rules_j)) + if (!rules_json.is_array()) { msg_format("Error 'rules' is not an array"); return rules; } - for (size_t i = 0; i < json_array_size(rules_j); i++) + auto rules_array = rules_json.array_value(); + + for (auto const &rule_value : rules_array) { - json_t *rule_j = json_array_get(rules_j, i); - auto rule = Rule::parse_rule(rule_j); + auto rule = Rule::parse_rule(rule_value); if (rule) { rules.push_back(rule); @@ -63,25 +64,25 @@ bool Automatizer::apply_rules(object_type *o_ptr, int item_idx) const return false; } -std::shared_ptr<json_t> Automatizer::to_json() const +jsoncons::json Automatizer::to_json() const { - auto rules_json = std::shared_ptr<json_t>(json_array(), &json_decref); + auto document = jsoncons::json::array(); for (auto rule : m_rules) { - json_array_append_new(rules_json.get(), rule->to_json()); + document.push_back(rule->to_json()); } - return rules_json; + return document; } -void Automatizer::load_json(json_t *json) +void Automatizer::load_json(jsoncons::json const &document) { // Go through all the found rules - auto rules = parse_rules(json); + auto rules = parse_rules(document); // Load rule - for (auto rule : rules) + for (auto rule: rules) { append_rule(rule); } @@ -204,13 +205,15 @@ void Automatizer::add_new_condition(std::function<std::shared_ptr<Condition> ()> factory); } -void Automatizer::get_rule_names(std::vector<const char *> *names) const +std::vector<std::string> Automatizer::get_rule_names() const { - names->resize(m_rules.size()); - for (size_t i = 0; i < m_rules.size(); i++) + std::vector<std::string> names; + names.reserve(m_rules.size()); + for (auto const &rule: m_rules) { - (*names)[i] = m_rules.at(i)->get_name(); + names.push_back(rule->get_name()); } + return names; } int Automatizer::rules_count() const diff --git a/src/squelch/condition.cc b/src/squelch/condition.cc index c3b8c3f5..cd7f879c 100644 --- a/src/squelch/condition.cc +++ b/src/squelch/condition.cc @@ -6,6 +6,7 @@ #include "tome/squelch/cursor.hpp" #include "tome/squelch/tree_printer.hpp" #include "../ability_type.hpp" +#include "../game.hpp" #include "../object1.hpp" #include "../object2.hpp" #include "../object_kind.hpp" @@ -16,7 +17,6 @@ #include "../player_type.hpp" #include "../skills.hpp" #include "../skill_type.hpp" -#include "../quark.hpp" #include "../util.hpp" #include "../variable.hpp" @@ -58,12 +58,15 @@ EnumStringMap<identification_state> &identification_state_mapping() return *m; } -json_t *Condition::to_json() const +jsoncons::json Condition::to_json() const { - json_t *j = json_object(); - json_object_set_new(j, "type", - json_string(match_mapping().stringify(match))); + // Start with an object with only 'type' property + jsoncons::json j; + j["type"] = match_mapping().stringify(match); + + // Add sub-class properties to_json(j); + // Return the completed JSON return j; } @@ -86,11 +89,11 @@ void Condition::display(TreePrinter *tree_printer, Cursor *cursor) const tree_printer->dedent(); } -std::shared_ptr<Condition> Condition::parse_condition(json_t *condition_json) +std::shared_ptr<Condition> Condition::parse_condition(jsoncons::json const &condition_json) { // Parsers for concrete types of conditions. static std::map< match_type, - std::function< std::shared_ptr< Condition > ( json_t * ) > > parsers { + std::function<std::shared_ptr<Condition>(jsoncons::json const &)>> parsers { { match_type::AND, &AndCondition::from_json }, { match_type::OR, &OrCondition::from_json }, { match_type::NOT, &NotCondition::from_json }, @@ -112,15 +115,13 @@ std::shared_ptr<Condition> Condition::parse_condition(json_t *condition_json) { match_type::SKILL, &SkillCondition::from_json }, { match_type::ABILITY, &AbilityCondition::from_json } }; - if ((condition_json == nullptr) || json_is_null(condition_json)) + if (condition_json.is_null()) { return nullptr; } - cptr type_s = nullptr; - if (json_unpack(condition_json, - "{s:s}", - "type", &type_s) < 0) + cptr type_s = condition_json.get("type").as<cptr>(); + if (!type_s) { msg_print("Missing/invalid 'type' in condition"); return nullptr; @@ -144,11 +145,11 @@ std::shared_ptr<Condition> Condition::parse_condition(json_t *condition_json) return nullptr; } -json_t *Condition::optional_to_json(std::shared_ptr<Condition> condition) +jsoncons::json Condition::optional_to_json(std::shared_ptr<Condition> condition) { return condition ? condition->to_json() - : json_null(); + : jsoncons::json::null_type(); } bool TvalCondition::is_match(object_type *o_ptr) const @@ -156,22 +157,23 @@ bool TvalCondition::is_match(object_type *o_ptr) const return (o_ptr->tval == m_tval); } -std::shared_ptr<Condition> TvalCondition::from_json(json_t *j) +std::shared_ptr<Condition> TvalCondition::from_json(jsoncons::json const &j) { - int tval; - - if (json_unpack(j, "{s:i}", "tval", &tval) < 0) + auto tval_j = j.get("tval"); + if (!tval_j.is_uinteger()) { msg_print("Missing/invalid 'tval' property"); return nullptr; } + int tval = tval_j.as_uint(); + return std::make_shared<TvalCondition>(tval); } -void TvalCondition::to_json(json_t *j) const +void TvalCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "tval", json_integer(m_tval)); + j["tval"] = m_tval; } void TvalCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t bcol) const @@ -193,14 +195,16 @@ bool NameCondition::is_match(object_type *o_ptr) const return boost::algorithm::iequals(m_name, buf1); } -std::shared_ptr<Condition> NameCondition::from_json(json_t *j) +std::shared_ptr<Condition> NameCondition::from_json(jsoncons::json const &j) { - cptr s = nullptr; - if (json_unpack(j, "{s:s}", "name", &s) < 0) + cptr s = j.get("name").as<cptr>(); + + if (!s) { msg_print("Missing/invalid 'name' property"); return nullptr; } + return std::make_shared<NameCondition>(s); } @@ -214,9 +218,9 @@ void NameCondition::write_tree(TreePrinter *p, Cursor *cursor, uint8_t ecol, uin p->write(TERM_WHITE, "\n"); } -void NameCondition::to_json(json_t *j) const +void NameCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "name", json_string(m_name.c_str())); + j["name"] = m_name; } bool ContainCondition::is_match(object_type *o_ptr) const @@ -226,14 +230,16 @@ bool ContainCondition::is_match(object_type *o_ptr) const return boost::algorithm::icontains(buf1, m_contain); } -std::shared_ptr<Condition> ContainCondition::from_json(json_t *j) +std::shared_ptr<Condition> ContainCondition::from_json(jsoncons::json const &j) { - cptr s = nullptr; - if (json_unpack(j, "{s:s}", "contain", &s) < 0) + cptr s = j.get("contain").as<cptr>(); + + if (!s) { msg_print("Missing/invalid 'contain' property"); return nullptr; } + return std::make_shared<ContainCondition>(s); } @@ -247,9 +253,9 @@ void ContainCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_ p->write(TERM_WHITE, "\n"); } -void ContainCondition::to_json(json_t *j) const +void ContainCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "contain", json_string(m_contain.c_str())); + j["contain"] = m_contain; } bool SvalCondition::is_match(object_type *o_ptr) const @@ -259,19 +265,25 @@ bool SvalCondition::is_match(object_type *o_ptr) const (o_ptr->sval <= m_max)); } -std::shared_ptr<Condition> SvalCondition::from_json(json_t *j) +std::shared_ptr<Condition> SvalCondition::from_json(jsoncons::json const &j) { - int min, max; + auto min_j = j.get("min"); + if (!min_j.is_uinteger()) + { + msg_print("Missing/invalid 'min' property"); + return nullptr; + } - if (json_unpack(j, "{s:i,s:i}", - "min", &min, - "max", &max) < 0) + auto max_j = j.get("max"); + if (!max_j.is_uinteger()) { - msg_print("Missing/invalid 'min'/'max' properties"); + msg_print("Missing/invalid 'max' property"); return nullptr; } - return std::make_shared<SvalCondition>(min, max); + return std::make_shared<SvalCondition>( + min_j.as_uint(), + max_j.as_uint()); } void SvalCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t bcol) const @@ -285,10 +297,10 @@ void SvalCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t b p->write(TERM_WHITE, "\n"); } -void SvalCondition::to_json(json_t *j) const +void SvalCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "min", json_integer(m_min)); - json_object_set_new(j, "max", json_integer(m_max)); + j["min"] = m_min; + j["max"] = m_max; } void GroupingCondition::add_child(ConditionFactory const &factory) @@ -370,16 +382,15 @@ std::shared_ptr<Condition> GroupingCondition::next_child(Condition *current) return nullptr; } -std::vector< std::shared_ptr<Condition> > GroupingCondition::parse_conditions(json_t *j) +std::vector< std::shared_ptr<Condition> > GroupingCondition::parse_conditions(jsoncons::json const &j) { - json_t *conditions_j = json_object_get(j, "conditions"); + auto conditions_j = j.get("conditions"); - if ((conditions_j == nullptr) || - (json_is_null(conditions_j))) + if (conditions_j.is_null()) { return std::vector< std::shared_ptr<Condition> >(); } - else if (!json_is_array(conditions_j)) + else if (!conditions_j.is_array()) { msg_print("'conditions' property has invalid type"); return std::vector< std::shared_ptr<Condition> >(); @@ -387,11 +398,8 @@ std::vector< std::shared_ptr<Condition> > GroupingCondition::parse_conditions(js else { std::vector< std::shared_ptr<Condition> > subconditions; - for (size_t i = 0; i < json_array_size(conditions_j); i++) + for (auto const &subcondition_j: conditions_j.array_value()) { - json_t *subcondition_j = - json_array_get(conditions_j, i); - std::shared_ptr<Condition> subcondition = parse_condition(subcondition_j); @@ -404,14 +412,16 @@ std::vector< std::shared_ptr<Condition> > GroupingCondition::parse_conditions(js } } -void GroupingCondition::to_json(json_t *j) const +void GroupingCondition::to_json(jsoncons::json &j) const { - json_t *ja = json_array(); + // Put all the sub-conditions into an array + jsoncons::json::array ja; for (auto condition_p : m_conditions) { - json_array_append_new(ja, optional_to_json(condition_p)); + ja.push_back(optional_to_json(condition_p)); } - json_object_set_new(j, "conditions", ja); + // Add to JSON object + j["conditions"] = ja; } bool AndCondition::is_match(object_type *o_ptr) const @@ -426,7 +436,7 @@ bool AndCondition::is_match(object_type *o_ptr) const return true; } -std::shared_ptr<Condition> AndCondition::from_json(json_t *j) +std::shared_ptr<Condition> AndCondition::from_json(jsoncons::json const &j) { auto condition = std::make_shared<AndCondition>(); for (auto subcondition : parse_conditions(j)) @@ -459,7 +469,7 @@ bool OrCondition::is_match(object_type *o_ptr) const return false; } -std::shared_ptr<Condition> OrCondition::from_json(json_t *j) +std::shared_ptr<Condition> OrCondition::from_json(jsoncons::json const &j) { std::shared_ptr<OrCondition> condition = std::make_shared<OrCondition>(); @@ -488,10 +498,11 @@ bool StatusCondition::is_match(object_type *o_ptr) const return m_status == object_status(o_ptr); } -std::shared_ptr<Condition> StatusCondition::from_json(json_t *j) +std::shared_ptr<Condition> StatusCondition::from_json(jsoncons::json const &j) { - cptr s; - if (json_unpack(j, "{s:s}", "status", &s) < 0) + cptr s = j.get("status").as<cptr>(); + + if (!s) { msg_print("Missing/invalid 'status' property"); return nullptr; @@ -518,9 +529,9 @@ void StatusCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t p->write(TERM_WHITE, "\n"); } -void StatusCondition::to_json(json_t *j) const +void StatusCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "status", json_string(status_mapping().stringify(m_status))); + j["status"] = status_mapping().stringify(m_status); } bool RaceCondition::is_match(object_type *o_ptr) const @@ -528,11 +539,11 @@ bool RaceCondition::is_match(object_type *o_ptr) const return boost::algorithm::iequals(m_race, rp_ptr->title); } -std::shared_ptr<Condition> RaceCondition::from_json(json_t *j) +std::shared_ptr<Condition> RaceCondition::from_json(jsoncons::json const &j) { - cptr s; + cptr s = j.get("race").as<cptr>(); - if (json_unpack(j, "{s:s}", "race", &s) < 0) + if (!s) { msg_print("Missing/invalid 'race' property"); return nullptr; @@ -552,9 +563,9 @@ void RaceCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t b p->write(TERM_WHITE, "\n"); } -void RaceCondition::to_json(json_t *j) const +void RaceCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "race", json_string(m_race.c_str())); + j["race"] = m_race; } bool SubraceCondition::is_match(object_type *o_ptr) const @@ -562,11 +573,11 @@ bool SubraceCondition::is_match(object_type *o_ptr) const return boost::algorithm::iequals(m_subrace, rmp_ptr->title); } -std::shared_ptr<Condition> SubraceCondition::from_json(json_t *j) +std::shared_ptr<Condition> SubraceCondition::from_json(jsoncons::json const &j) { - cptr s; + cptr s = j.get("subrace").as<cptr>(); - if (json_unpack(j, "{s:s}", "subrace", &s) < 0) + if (!s) { msg_print("Missing/invalid 'subrace' property"); return nullptr; @@ -586,9 +597,9 @@ void SubraceCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_ p->write(TERM_WHITE, "\n"); } -void SubraceCondition::to_json(json_t *j) const +void SubraceCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "subrace", json_string(m_subrace.c_str())); + j["subrace"] = m_subrace; } bool ClassCondition::is_match(object_type *o_ptr) const @@ -596,11 +607,11 @@ bool ClassCondition::is_match(object_type *o_ptr) const return boost::algorithm::iequals(m_class, spp_ptr->title); } -std::shared_ptr<Condition> ClassCondition::from_json(json_t *j) +std::shared_ptr<Condition> ClassCondition::from_json(jsoncons::json const &j) { - cptr s; + cptr s = j.get("class").as<cptr>(); - if (json_unpack(j, "{s:s}", "class", &s) < 0) + if (!s) { msg_print("Missing/invalid 'class' property"); return nullptr; @@ -620,30 +631,28 @@ void ClassCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t p->write(TERM_WHITE, "\n"); } -void ClassCondition::to_json(json_t *j) const +void ClassCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "class", json_string(m_class.c_str())); + j["class"] = m_class; } bool InscriptionCondition::is_match(object_type *o_ptr) const { - if (o_ptr->note == 0) - { - return false; - } return boost::algorithm::icontains( - quark_str(o_ptr->note), + o_ptr->inscription, m_inscription); } -std::shared_ptr<Condition> InscriptionCondition::from_json(json_t *j) +std::shared_ptr<Condition> InscriptionCondition::from_json(jsoncons::json const &j) { - cptr s = nullptr; - if (json_unpack(j, "{s:s}", "inscription", &s) < 0) + cptr s = j.get("inscription").as<cptr>(); + + if (!s) { msg_print("Missing/invalid 'inscription' property"); return nullptr; } + return std::make_shared<InscriptionCondition>(s); } @@ -658,9 +667,9 @@ void InscriptionCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, ui p->write(TERM_WHITE, "\n"); } -void InscriptionCondition::to_json(json_t *j) const +void InscriptionCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "inscription", json_string(m_inscription.c_str())); + j["inscription"] = m_inscription; } bool DiscountCondition::is_match(object_type *o_ptr) const @@ -670,17 +679,23 @@ bool DiscountCondition::is_match(object_type *o_ptr) const (o_ptr->discount <= m_max)); } -std::shared_ptr<Condition> DiscountCondition::from_json(json_t *j) +std::shared_ptr<Condition> DiscountCondition::from_json(jsoncons::json const &j) { - int min, max; + auto min_j = j.get("min"); + if (!min_j.is_integer()) + { + msg_print("Missing/invalid 'min' property"); + return nullptr; + } + int min = min_j.as_int(); - if (json_unpack(j, "{s:i,s:i}", - "min", &min, - "max", &max) < 0) + auto max_j = j.get("max"); + if (!max_j.is_integer()) { - msg_print("Missing/invalid 'min'/'max' properties"); + msg_print("Missing/invalid 'max' property"); return nullptr; } + int max = max_j.as_int(); return std::make_shared<DiscountCondition>(min, max); } @@ -696,10 +711,10 @@ void DiscountCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8 p->write(TERM_WHITE, "\n"); } -void DiscountCondition::to_json(json_t *j) const +void DiscountCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "min", json_integer(m_min)); - json_object_set_new(j, "max", json_integer(m_max)); + j["min"] = m_min; + j["max"] = m_max; } bool LevelCondition::is_match(object_type *) const @@ -708,16 +723,23 @@ bool LevelCondition::is_match(object_type *) const (p_ptr->lev <= m_max)); } -std::shared_ptr<Condition> LevelCondition::from_json(json_t *j) +std::shared_ptr<Condition> LevelCondition::from_json(jsoncons::json const &j) { - int min, max; - if (json_unpack(j, "{s:i,s:i}", - "min", &min, - "max", &max) < 0) + auto min_j = j.get("min"); + if (!min_j.is_integer()) { - msg_print("Missing/invalid 'min'/'max' properties"); + msg_print("Missing/invalid 'min' property"); return nullptr; } + int min = min_j.as_int(); + + auto max_j = j.get("max"); + if (!max_j.is_integer()) + { + msg_print("Missing/invalid 'max' property"); + return nullptr; + } + int max = max_j.as_int(); return std::make_shared<LevelCondition>(min, max); } @@ -734,10 +756,10 @@ void LevelCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t p->write(TERM_WHITE, "\n"); } -void LevelCondition::to_json(json_t *j) const +void LevelCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "min", json_integer(m_min)); - json_object_set_new(j, "max", json_integer(m_max)); + j["min"] = m_min; + j["max"] = m_max; } bool SkillCondition::is_match(object_type *) const @@ -747,16 +769,28 @@ bool SkillCondition::is_match(object_type *) const (sk <= m_max)); } -std::shared_ptr<Condition> SkillCondition::from_json(json_t *j) +std::shared_ptr<Condition> SkillCondition::from_json(jsoncons::json const &j) { - cptr s; - int min, max; - if (json_unpack(j, "{s:i,s:i,s:s}", - "min", &min, - "max", &max, - "name", &s) < 0) + auto min_j = j.get("min"); + if (!min_j.is_integer()) + { + msg_print("Missing/invalid 'min' property"); + return nullptr; + } + int min = min_j.as_int(); + + auto max_j = j.get("max"); + if (!max_j.is_integer()) { - msg_print("Missing/invalid 'min'/'max'/'name' properties"); + msg_print("Missing/invalid 'max' property"); + return nullptr; + } + int max = max_j.as_int(); + + auto s = j.get("name").as<cptr>(); + if (!s) + { + msg_print("Missing/invalid 'name' property"); return nullptr; } @@ -772,8 +806,10 @@ std::shared_ptr<Condition> SkillCondition::from_json(json_t *j) void SkillCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t bcol) const { + auto const &s_descriptors = game->edit_data.s_descriptors; + p->write(ecol, "Your skill in "); - p->write(bcol, s_info[m_skill_idx].name); + p->write(bcol, s_descriptors[m_skill_idx].name); p->write(ecol, " is from "); p->write(TERM_WHITE, format("%d", (int) m_min)); p->write(ecol, " to "); @@ -781,14 +817,13 @@ void SkillCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t p->write(TERM_WHITE, "\n"); } -void SkillCondition::to_json(json_t *j) const +void SkillCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "name", - json_string(s_info[m_skill_idx].name)); - json_object_set_new(j, "min", - json_integer(m_min)); - json_object_set_new(j, "max", - json_integer(m_max)); + auto const &s_descriptors = game->edit_data.s_descriptors; + + j["name"] = s_descriptors[m_skill_idx].name; + j["min"] = m_min; + j["max"] = m_max; } bool StateCondition::is_match(object_type *o_ptr) const @@ -805,10 +840,11 @@ bool StateCondition::is_match(object_type *o_ptr) const return false; } -std::shared_ptr<Condition> StateCondition::from_json(json_t *j) +std::shared_ptr<Condition> StateCondition::from_json(jsoncons::json const &j) { - cptr s; - if (json_unpack(j, "{s:s}", "state", &s) < 0) + cptr s = j.get("state").as<cptr>(); + + if (!s) { msg_print("Missing/invalid 'state' property"); return nullptr; @@ -835,34 +871,28 @@ void StateCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t p->write(TERM_WHITE, "\n"); } -void StateCondition::to_json(json_t *j) const +void StateCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "state", - json_string(identification_state_mapping(). - stringify(m_state))); + j["state"] = identification_state_mapping().stringify(m_state); } bool SymbolCondition::is_match(object_type *o_ptr) const { - object_kind *k_ptr = &k_info[o_ptr->k_idx]; - return k_ptr->d_char == m_symbol; + auto const &k_info = game->edit_data.k_info; + + return k_info[o_ptr->k_idx].d_char == m_symbol; } -std::shared_ptr<Condition> SymbolCondition::from_json(json_t *j) +std::shared_ptr<Condition> SymbolCondition::from_json(jsoncons::json const &j) { - cptr s_ = nullptr; - if (json_unpack(j, "{s:s}", "symbol", &s_) < 0) - { - msg_print("Missing/invalid 'symbol' property"); - return nullptr; - } + auto s = j.get("symbol").as<std::string>(); - std::string s(s_); if (s.empty()) { - msg_print("Invalid 'symbol' property: Too short"); + msg_print("Missing/invalid 'symbol' property"); return nullptr; } + if (s.size() > 1) { msg_print("Invalid 'symbol' property: Too long"); @@ -883,21 +913,21 @@ void SymbolCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t p->write(TERM_WHITE, "\n"); } -void SymbolCondition::to_json(json_t *j) const +void SymbolCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "symbol", - json_string(format("%c", m_symbol))); + j["symbol"] = format("%c", m_symbol); } bool AbilityCondition::is_match(object_type *) const { - return has_ability(m_ability_idx); + return p_ptr->has_ability(m_ability_idx); } -std::shared_ptr<Condition> AbilityCondition::from_json(json_t *j) +std::shared_ptr<Condition> AbilityCondition::from_json(jsoncons::json const &j) { - cptr a; - if (json_unpack(j, "{s:s}", "ability", &a) < 0) + cptr a = j.get("ability").as<cptr>(); + + if (!a) { msg_print("Missing/invalid 'ability' property"); return nullptr; @@ -915,18 +945,19 @@ std::shared_ptr<Condition> AbilityCondition::from_json(json_t *j) void AbilityCondition::write_tree(TreePrinter *p, Cursor *, uint8_t ecol, uint8_t bcol) const { - cptr ability_s = ab_info[m_ability_idx].name; + auto const &ab_info = game->edit_data.ab_info; p->write(ecol, "You have the "); - p->write(bcol, ability_s); + p->write(bcol, ab_info[m_ability_idx].name); p->write(ecol, " ability"); p->write(TERM_WHITE, "\n"); } -void AbilityCondition::to_json(json_t *j) const +void AbilityCondition::to_json(jsoncons::json &j) const { - cptr ability_s = ab_info[m_ability_idx].name; - json_object_set_new(j, "ability", json_string(ability_s)); + auto const &ab_info = game->edit_data.ab_info; + + j["ability"] = ab_info[m_ability_idx].name; } void SingleSubconditionCondition::add_child(std::function< std::shared_ptr<Condition> () > const &factory) @@ -951,23 +982,20 @@ std::shared_ptr<Condition> SingleSubconditionCondition::first_child() return m_subcondition; } -void SingleSubconditionCondition::to_json(json_t *j) const +void SingleSubconditionCondition::to_json(jsoncons::json &j) const { - json_object_set_new(j, "condition", - optional_to_json(m_subcondition)); + j["condition"] = optional_to_json(m_subcondition); } -std::shared_ptr<Condition> SingleSubconditionCondition::parse_single_subcondition(json_t *in_json) +std::shared_ptr<Condition> SingleSubconditionCondition::parse_single_subcondition(jsoncons::json const &in_json) { - json_t *condition_j = - json_object_get(in_json, "condition"); + auto condition_j = in_json.get("condition"); - if ((condition_j == nullptr) || - (json_is_null(condition_j))) + if (condition_j.is_null()) { return nullptr; } - else if (!json_is_object(condition_j)) + else if (!condition_j.is_object()) { msg_format("Invalid 'condition' property"); return nullptr; @@ -988,7 +1016,7 @@ bool NotCondition::is_match(object_type *o_ptr) const return !m_subcondition->is_match(o_ptr); } -std::shared_ptr<Condition> NotCondition::from_json(json_t *j) +std::shared_ptr<Condition> NotCondition::from_json(jsoncons::json const &j) { return std::make_shared<NotCondition>(parse_single_subcondition(j)); } @@ -1021,7 +1049,7 @@ bool InventoryCondition::is_match(object_type *) const return false; } -std::shared_ptr<Condition> InventoryCondition::from_json(json_t *j) +std::shared_ptr<Condition> InventoryCondition::from_json(jsoncons::json const &j) { return std::make_shared<InventoryCondition>( parse_single_subcondition(j)); @@ -1057,7 +1085,7 @@ bool EquipmentCondition::is_match(object_type *) const return false; } -std::shared_ptr<Condition> EquipmentCondition::from_json(json_t *j) +std::shared_ptr<Condition> EquipmentCondition::from_json(jsoncons::json const &j) { return std::make_shared<EquipmentCondition>( parse_single_subcondition(j)); diff --git a/src/squelch/condition_metadata.cc b/src/squelch/condition_metadata.cc index 62a90e58..f6d4370c 100644 --- a/src/squelch/condition_metadata.cc +++ b/src/squelch/condition_metadata.cc @@ -14,8 +14,8 @@ namespace squelch { static std::shared_ptr<Condition> create_condition_name() { - cptr s = lua_input_box("Object name to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Object name to match?", 79); + if (s.empty()) { return nullptr; } @@ -25,8 +25,8 @@ static std::shared_ptr<Condition> create_condition_name() static std::shared_ptr<Condition> create_condition_contain() { - cptr s = lua_input_box("Word to find in object name?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Word to find in object name?", 79); + if (s.empty()) { return nullptr; } @@ -36,8 +36,8 @@ static std::shared_ptr<Condition> create_condition_contain() static std::shared_ptr<Condition> create_condition_inscribed() { - cptr s = lua_input_box("Word to find in object inscription?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Word to find in object inscription?", 79); + if (s.empty() == 0) { return nullptr; } @@ -50,16 +50,16 @@ static std::shared_ptr<Condition> create_condition_discount() int min, max; { - cptr s = lua_input_box("Min discount?", 79); - if (sscanf(s, "%d", &min) < 1) + auto s = input_box_auto("Min discount?", 79); + if (sscanf(s.c_str(), "%d", &min) < 1) { return nullptr; } } { - cptr s = lua_input_box("Max discount?", 79); - if (sscanf(s, "%d", &max) < 1) + auto s = input_box_auto("Max discount?", 79); + if (sscanf(s.c_str(), "%d", &max) < 1) { return nullptr; } @@ -70,22 +70,20 @@ static std::shared_ptr<Condition> create_condition_discount() static std::shared_ptr<Condition> create_condition_symbol() { - char c; - cptr s = lua_input_box("Symbol to match?", 1); - if (sscanf(s, "%c", &c) < 1) + auto s = input_box_auto("Symbol to match?", 1); + if (s.empty()) { return nullptr; } - return std::make_shared<SymbolCondition>(c); + return std::make_shared<SymbolCondition>(s[0]); } static std::shared_ptr<Condition> create_condition_status() { status_type status; - char c; - c = lua_msg_box("[t]errible, [v]ery bad, [b]ad, " + auto c = msg_box_auto("[t]errible, [v]ery bad, [b]ad, " "[a]verage, [G]ood, [V]ery good, [S]pecial?"); switch (c) @@ -105,7 +103,7 @@ static std::shared_ptr<Condition> create_condition_status() static std::shared_ptr<Condition> create_condition_state() { - char c = lua_msg_box("[i]dentified, [n]on identified?"); + char c = msg_box_auto("[i]dentified, [n]on identified?"); identification_state s; switch (c) @@ -125,9 +123,9 @@ static bool in_byte_range(int x) static std::shared_ptr<Condition> create_condition_tval() { - cptr s = lua_input_box("Tval to match?", 79); + auto s = input_box_auto("Tval to match?", 79); int tval; - if (sscanf(s, "%d", &tval) < 1) + if (sscanf(s.c_str(), "%d", &tval) < 1) { return nullptr; } @@ -145,8 +143,8 @@ static std::shared_ptr<Condition> create_condition_sval() int sval_min, sval_max; { - cptr s = lua_input_box("Min sval?", 79); - if ((sscanf(s, "%d", &sval_min) < 1) || + auto s = input_box_auto("Min sval?", 79); + if ((sscanf(s.c_str(), "%d", &sval_min) < 1) || (!in_byte_range(sval_min))) { return nullptr; @@ -154,8 +152,8 @@ static std::shared_ptr<Condition> create_condition_sval() } { - cptr s = lua_input_box("Max sval?", 79); - if ((sscanf(s, "%d", &sval_max) < 1) || + auto s = input_box_auto("Max sval?", 79); + if ((sscanf(s.c_str(), "%d", &sval_max) < 1) || (!in_byte_range(sval_max))) { return nullptr; @@ -167,8 +165,8 @@ static std::shared_ptr<Condition> create_condition_sval() static std::shared_ptr<Condition> create_condition_race() { - cptr s = lua_input_box("Player race to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Player race to match?", 79); + if (s.empty()) { return nullptr; } @@ -178,8 +176,8 @@ static std::shared_ptr<Condition> create_condition_race() static std::shared_ptr<Condition> create_condition_subrace() { - cptr s = lua_input_box("Player subrace to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Player subrace to match?", 79); + if (s.empty()) { return nullptr; } @@ -189,8 +187,8 @@ static std::shared_ptr<Condition> create_condition_subrace() static std::shared_ptr<Condition> create_condition_class() { - cptr s = lua_input_box("Player class to match?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Player class to match?", 79); + if (s.empty()) { return nullptr; } @@ -203,16 +201,16 @@ static std::shared_ptr<Condition> create_condition_level() int min, max; { - cptr s = lua_input_box("Min player level?", 79); - if (sscanf(s, "%d", &min) < 1) + auto s = input_box_auto("Min player level?", 79); + if (sscanf(s.c_str(), "%d", &min) < 1) { return nullptr; } } { - cptr s = lua_input_box("Max player level?", 79); - if (sscanf(s, "%d", &max) < 1) + auto s = input_box_auto("Max player level?", 79); + if (sscanf(s.c_str(), "%d", &max) < 1) { return nullptr; } @@ -226,16 +224,16 @@ static std::shared_ptr<Condition> create_condition_skill() int min, max; { - cptr s = lua_input_box("Min skill level?", 79); - if (sscanf(s, "%d", &min) < 1) + auto s = input_box_auto("Min skill level?", 79); + if (sscanf(s.c_str(), "%d", &min) < 1) { return nullptr; } } { - cptr s = lua_input_box("Max skill level?", 79); - if (sscanf(s, "%d", &max) < 1) + auto s = input_box_auto("Max skill level?", 79); + if (sscanf(s.c_str(), "%d", &max) < 1) { return nullptr; } @@ -243,13 +241,13 @@ static std::shared_ptr<Condition> create_condition_skill() s16b skill_idx; { - cptr s = lua_input_box("Skill name?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Skill name?", 79); + if (s.empty() == 0) { return nullptr; } - skill_idx = find_skill_i(s); + skill_idx = find_skill_i(s.c_str()); if (skill_idx < 0) { return nullptr; @@ -261,13 +259,13 @@ static std::shared_ptr<Condition> create_condition_skill() static std::shared_ptr<Condition> create_condition_ability() { - cptr s = lua_input_box("Ability name?", 79); - if (strlen(s) == 0) + auto s = input_box_auto("Ability name?", 79); + if (s.empty() == 0) { return nullptr; } - s16b ai = find_ability(s); + s16b ai = find_ability(s.c_str()); if (ai < 0) { return nullptr; @@ -394,7 +392,7 @@ std::shared_ptr<Condition> new_condition_interactive() match_type::INVENTORY, match_type::EQUIPMENT }; - static std::vector<const char *> condition_type_names; + static std::vector<std::string> condition_type_names; // Fill in types names? if (condition_type_names.empty()) @@ -414,7 +412,7 @@ std::shared_ptr<Condition> new_condition_interactive() Term_clear(); Term_get_size(&wid, &hgt); - display_list(0, 0, hgt - 1, 15, "Rule types", condition_type_names.data(), condition_types.size(), begin, sel, TERM_L_GREEN); + display_list(0, 0, hgt - 1, 15, "Rule types", condition_type_names, begin, sel, TERM_L_GREEN); display_desc(condition_types[sel]); diff --git a/src/squelch/rule.cc b/src/squelch/rule.cc index 1c17d2fd..e35b3ce1 100644 --- a/src/squelch/rule.cc +++ b/src/squelch/rule.cc @@ -8,8 +8,8 @@ #include "../modules.hpp" #include "../object1.hpp" #include "../object2.hpp" +#include "../object_flag.hpp" #include "../object_type.hpp" -#include "../quark.hpp" #include "../tables.hpp" #include "../util.hpp" #include "../variable.hpp" @@ -25,15 +25,14 @@ EnumStringMap<action_type> &action_mapping() return *m; } -void Rule::set_name(const char *new_name) +void Rule::set_name(std::string const &new_name) { - assert(new_name != nullptr); m_name = new_name; } -const char *Rule::get_name() const +std::string Rule::get_name() const { - return m_name.c_str(); + return m_name; } std::shared_ptr<Condition> Rule::get_condition() const @@ -41,22 +40,16 @@ std::shared_ptr<Condition> Rule::get_condition() const return m_condition; } -json_t *Rule::to_json() const +jsoncons::json Rule::to_json() const { - json_t *rule_json = json_object(); - json_object_set_new(rule_json, - "name", - json_string(m_name.c_str())); - json_object_set_new(rule_json, - "action", - json_string(action_mapping().stringify(m_action))); - json_object_set_new(rule_json, - "module", - json_string(modules[m_module_idx].meta.name)); - json_object_set_new(rule_json, - "condition", - Condition::optional_to_json(m_condition)); - return rule_json; + jsoncons::json j; + + j["name"] = jsoncons::json::string_type(m_name); + j["action"] = action_mapping().stringify(m_action); + j["module"] = modules[m_module_idx].meta.name; + j["condition"] = Condition::optional_to_json(m_condition); + + return j; } void Rule::add_new_condition(Cursor *cursor, @@ -138,23 +131,19 @@ bool Rule::apply_rule(object_type *o_ptr, int item_idx) const return false; } -std::shared_ptr<Rule> Rule::parse_rule(json_t *rule_json) +std::shared_ptr<Rule> Rule::parse_rule(jsoncons::json const &rule_json) { - if (!json_is_object(rule_json)) + if (!rule_json.is_object()) { msg_print("Rule is not an object"); return nullptr; } // Retrieve the attributes - char *rule_name_s = nullptr; - char *rule_action_s = nullptr; - char *rule_module_s = nullptr; - if (json_unpack(rule_json, - "{s:s,s:s,s:s}", - "name", &rule_name_s, - "action", &rule_action_s, - "module", &rule_module_s) < 0) + char const *rule_name_s = rule_json.get("name").as<char const *>(); + char const *rule_action_s = rule_json.get("action").as<char const *>(); + char const *rule_module_s = rule_json.get("module").as<char const *>(); + if ((!rule_name_s) || (!rule_action_s) || (!rule_module_s)) { msg_print("Rule missing required field(s)"); return nullptr; @@ -178,28 +167,29 @@ std::shared_ptr<Rule> Rule::parse_rule(json_t *rule_json) // Parse condition std::shared_ptr<Condition> condition = - Condition::parse_condition(json_object_get(rule_json, "condition")); + Condition::parse_condition(rule_json.get("condition")); // Parse rule switch (action) { case action_type::AUTO_INSCRIBE: { - json_t *rule_inscription_j = json_object_get(rule_json, "inscription"); + auto rule_inscription_j = rule_json.get("inscription"); - if (rule_inscription_j == nullptr) + if (rule_inscription_j.is_null()) { msg_print("Inscription rule missing 'inscription' attribute"); return nullptr; } - if (!json_is_string(rule_inscription_j)) + + if (!rule_inscription_j.is_string()) { msg_print("Inscription rule 'inscription' attribute wrong type"); return nullptr; } - std::string inscription = - json_string_value(rule_inscription_j); + std::string inscription = rule_inscription_j.as<std::string>(); + return std::make_shared<InscribeRule>( rule_name_s, module_idx, condition, inscription); } @@ -237,7 +227,7 @@ bool DestroyRule::do_apply_rule(object_type *o_ptr, int item_idx) const } // Never destroy inscribed items - if (o_ptr->note) + if (!o_ptr->inscription.empty()) { return false; } @@ -250,10 +240,8 @@ bool DestroyRule::do_apply_rule(object_type *o_ptr, int item_idx) const // Cannot destroy CURSE_NO_DROP objects. { - u32b f1, f2, f3, f4, f5, esp; - object_flags(o_ptr, &f1, &f2, &f3, &f4, &f5, &esp); - - if ((f4 & TR4_CURSE_NO_DROP) != 0) + auto const f = object_flags(o_ptr); + if (f & TR_CURSE_NO_DROP) { return false; } @@ -292,14 +280,10 @@ bool PickUpRule::do_apply_rule(object_type *o_ptr, int item_idx) const return true; } -json_t *InscribeRule::to_json() const +jsoncons::json InscribeRule::to_json() const { - json_t *j = Rule::to_json(); - - json_object_set_new(j, - "inscription", - json_string(m_inscription.c_str())); - + jsoncons::json j; + j["inscription"] = m_inscription; return j; } @@ -318,14 +302,14 @@ void InscribeRule::do_write_tree(TreePrinter *p) const bool InscribeRule::do_apply_rule(object_type *o_ptr, int) const { // Already inscribed? - if (o_ptr->note != 0) + if (!o_ptr->inscription.empty()) { return false; } // Inscribe msg_format("<Auto-Inscribe {%s}>", m_inscription.c_str()); - o_ptr->note = quark_add(m_inscription.c_str()); + o_ptr->inscription = m_inscription; return true; } diff --git a/src/squelch/tree_printer.cc b/src/squelch/tree_printer.cc index 2be098dc..0dbceec9 100644 --- a/src/squelch/tree_printer.cc +++ b/src/squelch/tree_printer.cc @@ -86,4 +86,9 @@ void TreePrinter::write(uint8_t color, cptr line) } } +void TreePrinter::write(uint8_t color, std::string const &line) +{ + write(color, line.c_str()); +} + } // namespace |