diff options
author | hop311 <hop3114@gmail.com> | 2023-10-13 23:04:12 +0200 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-10-14 12:05:06 +0200 |
commit | d26f9c2fb5a9666822a0f702d76b764600a390d7 (patch) | |
tree | c2a13a44d4c8553f6e87193f5f437052cf1d7067 /src/openvic-simulation/Modifier.cpp | |
parent | ce6e70d079f4ab18cdfa082032dc3580ab233b0e (diff) |
Further CLI and modifier reading work
Diffstat (limited to 'src/openvic-simulation/Modifier.cpp')
-rw-r--r-- | src/openvic-simulation/Modifier.cpp | 64 |
1 files changed, 49 insertions, 15 deletions
diff --git a/src/openvic-simulation/Modifier.cpp b/src/openvic-simulation/Modifier.cpp index 1273fdd..fc5dcaf 100644 --- a/src/openvic-simulation/Modifier.cpp +++ b/src/openvic-simulation/Modifier.cpp @@ -154,38 +154,72 @@ bool ModifierManager::setup_modifier_effects() { ret &= add_modifier_effect("min_build_university", true, ModifierEffect::format_t::INT); ret &= add_modifier_effect("max_bank", true, ModifierEffect::format_t::INT); ret &= add_modifier_effect("min_build_bank", true, ModifierEffect::format_t::INT); - + modifier_effects.lock(); return ret; } -node_callback_t ModifierManager::expect_modifier_value_and_default(callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback) const { - return [this, modifier_callback, default_callback](ast::NodeCPtr root) -> bool { - ModifierValue modifier; - bool ret = expect_dictionary( - [this, &modifier, default_callback](std::string_view key, ast::NodeCPtr value) -> bool { - ModifierEffect const* effect = get_modifier_effect_by_identifier(key); - if (effect != nullptr) { - if (modifier.values.find(effect) == modifier.values.end()) { - return expect_fixed_point( - assign_variable_callback(modifier.values[effect]) - )(value); - } +key_value_callback_t ModifierManager::_modifier_effect_callback( + ModifierValue& modifier, key_value_callback_t default_callback, + ModifierEffectValidator auto effect_validator) const { + + return [this, &modifier, default_callback, effect_validator](std::string_view key, ast::NodeCPtr value) -> bool { + ModifierEffect const* effect = get_modifier_effect_by_identifier(key); + if (effect != nullptr) { + if (effect_validator(*effect)) { + if (modifier.values.find(effect) == modifier.values.end()) { + return expect_fixed_point( + assign_variable_callback(modifier.values[effect]) + )(value); + } else { Logger::error("Duplicate modifier effect: ", key); return false; } - return default_callback(key, value); + } else { + Logger::error("Failed to validate modifier effect: ", key); + return false; } - )(root); + } + return default_callback(key, value); + }; +} + +node_callback_t ModifierManager::expect_validated_modifier_value_and_default(callback_t<ModifierValue&&> modifier_callback, + key_value_callback_t default_callback, ModifierEffectValidator auto effect_validator) const { + return [this, modifier_callback, default_callback, effect_validator](ast::NodeCPtr root) -> bool { + ModifierValue modifier; + bool ret = expect_dictionary(_modifier_effect_callback(modifier, default_callback, effect_validator))(root); ret &= modifier_callback(std::move(modifier)); return ret; }; } +node_callback_t ModifierManager::expect_validated_modifier_value(callback_t<ModifierValue&&> modifier_callback, + ModifierEffectValidator auto effect_validator) const { + return expect_validated_modifier_value_and_default(modifier_callback, key_value_invalid_callback, effect_validator); +} + +node_callback_t ModifierManager::expect_modifier_value_and_default(callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback) const { + return expect_validated_modifier_value_and_default(modifier_callback, default_callback, + [](ModifierEffect const&) -> bool { return true; } + ); +} node_callback_t ModifierManager::expect_modifier_value(callback_t<ModifierValue&&> modifier_callback) const { return expect_modifier_value_and_default(modifier_callback, key_value_invalid_callback); } +node_callback_t ModifierManager::expect_whitelisted_modifier_value_and_default(callback_t<ModifierValue&&> modifier_callback, std::set<std::string, std::less<void>> const& whitelist, key_value_callback_t default_callback) const { + return expect_validated_modifier_value_and_default(modifier_callback, default_callback, + [&whitelist](ModifierEffect const& effect) -> bool { + return whitelist.contains(effect.get_identifier()); + } + ); +} + +node_callback_t ModifierManager::expect_whitelisted_modifier_value(callback_t<ModifierValue&&> modifier_callback, std::set<std::string, std::less<void>> const& whitelist) const { + return expect_whitelisted_modifier_value_and_default(modifier_callback, whitelist, key_value_invalid_callback); +} + node_callback_t ModifierManager::expect_modifier_value_and_key_map_and_default(callback_t<ModifierValue&&> modifier_callback, key_value_callback_t default_callback, key_map_t&& key_map) const { return [this, modifier_callback, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool { bool ret = expect_modifier_value_and_default( |