From 971bc9421f86f1e092451ac95b305bf5b569cdba Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Sun, 22 Dec 2024 00:28:13 +0200 Subject: [PATCH] fix settings docs --- mods/settings.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/mods/settings.md b/mods/settings.md index b0238e8..1c25a17 100644 --- a/mods/settings.md +++ b/mods/settings.md @@ -590,7 +590,7 @@ struct MyComplexSettingValue { std::string value; // Make sure your value type is comparable - bool operator==(MyComplexSettingValue& other) const = default; + bool operator==(MyComplexSettingValue const& other) const = default; // If your value type is a thin wrapper around another type, you can allow // implicit conversions from your type to the wrapped type @@ -602,7 +602,7 @@ struct MyComplexSettingValue { MyComplexSettingValue(std::string_view value) : value(value) {} // Setting values must be copyable! - MyComplexSettingValue(MyComplexSettingValue&) = default; + MyComplexSettingValue(MyComplexSettingValue const&) = default; }; // You'll have to manually implement JSON serialization for the value @@ -610,21 +610,14 @@ template<> struct matjson::Serialize { // Serialize the value into JSON. In this case, we just return the value, // as strings are inherently JSON-serializable - static matjson::Value to_json(MyComplexSettingValue& settingValue) { + static matjson::Value toJson(MyComplexSettingValue const& settingValue) { return settingValue.value; } - // Deserialize the value from JSON, again taking advantage of strings being // inherently JSON-serializable - static MyComplexSettingValue from_json(matjson::Value const& json) { - return MyComplexSettingValue(json.as_string()); - } - - // Validate that the JSON value is the type we expect. You can do more - // complex validation here, but in practice most implementations just check - // if it's roughly the correct type (usually object, array, or string) - static bool is_json(matjson::Value const& json) { - return json.is_string(); + static Result fromJson(matjson::Value const& json) { + GEODE_UNWRAP_INTO(auto str, json.asString()); + return Ok(MyComplexSettingValue(str)); } }; ``` @@ -639,6 +632,11 @@ Custom settings do not necessarily need to inherit from the `SettingValueNodeV3< #include #include +// If you use PCH these are most likely not necessary +#include +#include +#include + using namespace geode::prelude; // Inherit from SettingV3 directly over SettingBaseValueV3, as our setting @@ -646,7 +644,7 @@ using namespace geode::prelude; class MyButtonSettingV3 : public SettingV3 { public: // Once again implement the parse function - static Result> parse(std::string const& key, std::string const& modID, matjson::Value const& json) { + static Result> parse(std::string const& key, std::string const& modID, matjson::Value const& json) { auto res = std::make_shared(); auto root = checkJson(json, "MyButtonSettingV3"); @@ -660,7 +658,7 @@ public: res->parseEnableIf(root); root.checkUnknownKeys(); - return root.ok(res); + return root.ok(std::static_pointer_cast(res)); } // Since there's no data to save or load, these can just return true