settings: enable dynamic reload of some emu values

This commit is contained in:
Megamouse
2019-11-14 22:45:07 +01:00
parent f2b530823b
commit d91f8193b0
9 changed files with 100 additions and 63 deletions
+13 -8
View File
@@ -17,8 +17,8 @@ namespace cfg
}
}
_base::_base(type _type, node* owner, const std::string& name)
: m_type(_type)
_base::_base(type _type, node* owner, const std::string& name, bool dynamic)
: m_type(_type), m_dynamic(dynamic)
{
for (const auto& pair : owner->m_nodes)
{
@@ -31,7 +31,7 @@ namespace cfg
owner->m_nodes.emplace_back(name, this);
}
bool _base::from_string(const std::string&)
bool _base::from_string(const std::string&, bool)
{
fmt::throw_exception("from_string() purecall" HERE);
}
@@ -46,7 +46,7 @@ namespace cfg
// Incrementally load config entries from YAML::Node.
// The config value is preserved if the corresponding YAML node doesn't exist.
static void decode(const YAML::Node& data, class _base& rhs);
static void decode(const YAML::Node& data, class _base& rhs, bool dynamic = false);
}
std::vector<std::string> cfg::make_int_range(s64 min, s64 max)
@@ -212,8 +212,13 @@ void cfg::encode(YAML::Emitter& out, const cfg::_base& rhs)
}
}
void cfg::decode(const YAML::Node& data, cfg::_base& rhs)
void cfg::decode(const YAML::Node& data, cfg::_base& rhs, bool dynamic)
{
if (dynamic && !rhs.get_is_dynamic())
{
return;
}
switch (rhs.get_type())
{
case type::node:
@@ -232,7 +237,7 @@ void cfg::decode(const YAML::Node& data, cfg::_base& rhs)
{
if (_pair.first == pair.first.Scalar())
{
decode(pair.second, *_pair.second);
decode(pair.second, *_pair.second, dynamic);
}
}
}
@@ -295,9 +300,9 @@ std::string cfg::node::to_string() const
return {out.c_str(), out.size()};
}
bool cfg::node::from_string(const std::string& value) try
bool cfg::node::from_string(const std::string& value, bool dynamic) try
{
cfg::decode(YAML::Load(value), *this);
cfg::decode(YAML::Load(value), *this, dynamic);
return true;
}
catch (const std::exception& e)
+24 -19
View File
@@ -42,11 +42,13 @@ namespace cfg
const type m_type;
protected:
bool m_dynamic = true;
// Ownerless entry constructor
_base(type _type);
// Owned entry constructor
_base(type _type, class node* owner, const std::string& name);
_base(type _type, class node* owner, const std::string& name, bool dynamic = true);
public:
_base(const _base&) = delete;
@@ -56,6 +58,9 @@ namespace cfg
// Get type
type get_type() const { return m_type; }
// Get dynamic property for reloading configs during games
bool get_is_dynamic() const { return m_dynamic; };
// Reset defaults
virtual void from_default() = 0;
@@ -66,7 +71,7 @@ namespace cfg
}
// Try to convert from string (optional)
virtual bool from_string(const std::string&);
virtual bool from_string(const std::string&, bool /*dynamic*/ = false);
// Get string list (optional)
virtual std::vector<std::string> to_list() const
@@ -93,8 +98,8 @@ namespace cfg
}
// Registered node constructor
node(node* owner, const std::string& name)
: _base(type::node, owner, name)
node(node* owner, const std::string& name, bool dynamic = true)
: _base(type::node, owner, name, dynamic)
{
}
@@ -108,7 +113,7 @@ namespace cfg
std::string to_string() const override;
// Deserialize node
bool from_string(const std::string& value) override;
bool from_string(const std::string& value, bool dynamic = false) override;
// Set default values
void from_default() override;
@@ -121,8 +126,8 @@ namespace cfg
public:
bool def;
_bool(node* owner, const std::string& name, bool def = false)
: _base(type::_bool, owner, name)
_bool(node* owner, const std::string& name, bool def = false, bool dynamic = false)
: _base(type::_bool, owner, name, dynamic)
, m_value(def)
, def(def)
{
@@ -145,7 +150,7 @@ namespace cfg
return m_value ? "true" : "false";
}
bool from_string(const std::string& value) override
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
if (value == "false")
m_value = false;
@@ -172,8 +177,8 @@ namespace cfg
public:
const T def;
_enum(node* owner, const std::string& name, T value = {})
: _base(type::_enum, owner, name)
_enum(node* owner, const std::string& name, T value = {}, bool dynamic = false)
: _base(type::_enum, owner, name, dynamic)
, m_value(value)
, def(value)
{
@@ -201,7 +206,7 @@ namespace cfg
return result; // TODO: ???
}
bool from_string(const std::string& value) override
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
u64 result;
@@ -239,8 +244,8 @@ namespace cfg
static const s64 max = Max;
static const s64 min = Min;
_int(node* owner, const std::string& name, int_type def = std::min<int_type>(Max, std::max<int_type>(Min, 0)))
: _base(type::_int, owner, name)
_int(node* owner, const std::string& name, int_type def = std::min<int_type>(Max, std::max<int_type>(Min, 0)), bool dynamic = false)
: _base(type::_int, owner, name, dynamic)
, m_value(def)
, def(def)
{
@@ -266,7 +271,7 @@ namespace cfg
return std::to_string(m_value);
}
bool from_string(const std::string& value) override
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
s64 result;
if (try_to_int64(&result, value, Min, Max))
@@ -304,8 +309,8 @@ namespace cfg
public:
std::string def;
string(node* owner, const std::string& name, const std::string& def = {})
: _base(type::string, owner, name)
string(node* owner, const std::string& name, const std::string& def = {}, bool dynamic = false)
: _base(type::string, owner, name, dynamic)
, m_name(name)
, m_value(def)
, def(def)
@@ -339,7 +344,7 @@ namespace cfg
return m_value;
}
bool from_string(const std::string& value) override
bool from_string(const std::string& value, bool /*dynamic*/ = false) override
{
m_value = value;
return true;
@@ -353,8 +358,8 @@ namespace cfg
public:
// Default value is empty list in current implementation
set_entry(node* owner, const std::string& name)
: _base(type::set, owner, name)
set_entry(node* owner, const std::string& name, bool dynamic = false)
: _base(type::set, owner, name, dynamic)
{
}