|
| 1 | +#include "GameSettings.hpp" |
| 2 | + |
| 3 | +#include <godot_cpp/classes/config_file.hpp> |
| 4 | +#include <godot_cpp/classes/dir_access.hpp> |
| 5 | +#include <godot_cpp/classes/file_access.hpp> |
| 6 | +#include <godot_cpp/classes/global_constants.hpp> |
| 7 | +#include <godot_cpp/classes/resource_loader.hpp> |
| 8 | +#include <godot_cpp/core/error_macros.hpp> |
| 9 | +#include <godot_cpp/core/object.hpp> |
| 10 | +#include <godot_cpp/core/print_string.hpp> |
| 11 | +#include <godot_cpp/core/property_info.hpp> |
| 12 | +#include <godot_cpp/variant/dictionary.hpp> |
| 13 | +#include <godot_cpp/variant/packed_string_array.hpp> |
| 14 | +#include <godot_cpp/variant/variant.hpp> |
| 15 | + |
| 16 | +#include "openvic-extension/utility/ClassBindings.hpp" |
| 17 | + |
| 18 | +using namespace OpenVic; |
| 19 | +using namespace godot; |
| 20 | + |
| 21 | +GameSettings::GameSettings() { |
| 22 | + config.instantiate(); |
| 23 | + defaults.instantiate(); |
| 24 | +} |
| 25 | + |
| 26 | +void GameSettings::reset_settings() { |
| 27 | + config = defaults; |
| 28 | + emit_changed(); |
| 29 | +} |
| 30 | + |
| 31 | +Error GameSettings::save(String path) { |
| 32 | + if (path.is_empty()) { |
| 33 | + path = get_path(); |
| 34 | + } |
| 35 | + for (String const& section : defaults->get_sections()) { |
| 36 | + for (String const& key : defaults->get_section_keys(section)) { |
| 37 | + if (!has_section_key(section, key)) { |
| 38 | + config->set_value(section, key, defaults->get_value(section, key)); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + Error err = config->save(path); |
| 43 | + return err; |
| 44 | +} |
| 45 | + |
| 46 | +Error GameSettings::load(String path) { |
| 47 | + if (path.is_empty()) { |
| 48 | + path = get_path(); |
| 49 | + } else { |
| 50 | + set_path(path); |
| 51 | + } |
| 52 | + |
| 53 | + Error err = config->load(path); |
| 54 | + emit_changed(); |
| 55 | + return err; |
| 56 | +} |
| 57 | + |
| 58 | +Ref<GameSettings> GameSettings::load_from_file(String path) { |
| 59 | + Ref<GameSettings> settings; |
| 60 | + if (ResourceLoader::get_singleton()->has_cached(path)) { |
| 61 | + settings = ResourceLoader::get_singleton()->get_cached_ref(path); |
| 62 | + if (settings.is_valid()) { |
| 63 | + return settings; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + settings.instantiate(); |
| 68 | + if (!FileAccess::file_exists(path)) { |
| 69 | + if (String dir = path.get_base_dir(); !DirAccess::dir_exists_absolute(dir)) { |
| 70 | + DirAccess::make_dir_recursive_absolute(dir); |
| 71 | + } |
| 72 | + |
| 73 | + ERR_FAIL_COND_V(settings->save(path) != OK, Ref<GameSettings>()); |
| 74 | + } else { |
| 75 | + ERR_FAIL_COND_V(settings->load(path) != OK, Ref<GameSettings>()); |
| 76 | + } |
| 77 | + return settings; |
| 78 | +} |
| 79 | + |
| 80 | +void GameSettings::set_value(String section, String key, Variant value) { |
| 81 | + if (value == Variant()) { |
| 82 | + config->set_value(section, key, defaults->get_value(section, key)); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + config->set_value(section, key, value); |
| 87 | +} |
| 88 | + |
| 89 | +Variant GameSettings::get_value(String section, String key, Variant default_value) { |
| 90 | + if (default_value != Variant()) { |
| 91 | + if (defaults->has_section_key(section, key)) { |
| 92 | + ERR_FAIL_COND_V_MSG( |
| 93 | + defaults->get_value(section, key) != default_value, nullptr, |
| 94 | + vformat("Setting '%s.%s' default value does match previously set default value.", section, key) |
| 95 | + ); |
| 96 | + } else { |
| 97 | + defaults->set_value(section, key, default_value); |
| 98 | + } |
| 99 | + } else if (defaults->has_section_key(section, key)) { |
| 100 | + default_value = defaults->get_value(section, key); |
| 101 | + } |
| 102 | + |
| 103 | + if (!has_section_key(section, key)) { |
| 104 | + set_value(section, key, default_value); |
| 105 | + return default_value; |
| 106 | + } |
| 107 | + |
| 108 | + return config->get_value(section, key); |
| 109 | +} |
| 110 | + |
| 111 | +bool GameSettings::has_section(String section) const { |
| 112 | + return config->has_section(section); |
| 113 | +} |
| 114 | + |
| 115 | +bool GameSettings::has_section_key(String section, String key) const { |
| 116 | + return config->has_section_key(section, key); |
| 117 | +} |
| 118 | + |
| 119 | +void GameSettings::reset_section(String section) { |
| 120 | + set_block_signals(true); |
| 121 | + for (String const& key : config->get_section_keys(section)) { |
| 122 | + reset_section_key(section, key); |
| 123 | + } |
| 124 | + set_block_signals(false); |
| 125 | + emit_changed(); |
| 126 | +} |
| 127 | + |
| 128 | +void GameSettings::reset_section_key(String section, String key) { |
| 129 | + config->set_value(section, key, defaults->get_value(section, key)); |
| 130 | + emit_changed(); |
| 131 | +} |
| 132 | + |
| 133 | +PackedStringArray GameSettings::get_sections() const { |
| 134 | + return config->get_sections(); |
| 135 | +} |
| 136 | + |
| 137 | +PackedStringArray GameSettings::get_section_keys(String section) const { |
| 138 | + return config->get_section_keys(section); |
| 139 | +} |
| 140 | + |
| 141 | +Error GameSettings::load_deprecated_file(String path, Dictionary section_keys) { |
| 142 | + Ref<ConfigFile> config; |
| 143 | + config.instantiate(); |
| 144 | + if (Error err = config->load(path); err != OK) { |
| 145 | + return err; |
| 146 | + } |
| 147 | + |
| 148 | + Array sections = section_keys.keys(); |
| 149 | + for (size_t i = 0; i < sections.size(); i++) { |
| 150 | + Variant section = sections[i]; |
| 151 | + const Variant::Type section_type = section.get_type(); |
| 152 | + ERR_FAIL_COND_V(section_type != Variant::STRING && section_type != Variant::STRING_NAME, ERR_INVALID_PARAMETER); |
| 153 | + |
| 154 | + Variant key = section_keys[section]; |
| 155 | + switch (key.get_type()) { |
| 156 | + using enum Variant::Type; |
| 157 | + case NIL: |
| 158 | + for (String const& key : config->get_section_keys(section)) { |
| 159 | + set_value(section, key, config->get_value(section, key)); |
| 160 | + } |
| 161 | + break; |
| 162 | + case STRING: |
| 163 | + case STRING_NAME: |
| 164 | + if (config->has_section_key(section, key) && !has_section_key(section, key)) { |
| 165 | + set_value(section, key, config->get_value(section, key)); |
| 166 | + } |
| 167 | + break; |
| 168 | + |
| 169 | + case PACKED_STRING_ARRAY: { |
| 170 | + PackedStringArray array = key; |
| 171 | + for (String const& k : array) { |
| 172 | + if (config->has_section_key(section, k) && !has_section_key(section, k)) { |
| 173 | + set_value(section, k, config->get_value(section, k)); |
| 174 | + } |
| 175 | + } |
| 176 | + break; |
| 177 | + } |
| 178 | + |
| 179 | + case ARRAY: { |
| 180 | + Array array = key; |
| 181 | + for (size_t arr_i = 0; arr_i < array.size(); arr_i++) { |
| 182 | + Variant const& inner_key = array[arr_i]; |
| 183 | + Variant::Type inner_key_type = inner_key.get_type(); |
| 184 | + ERR_FAIL_COND_V( |
| 185 | + inner_key_type != Variant::STRING && inner_key_type != Variant::STRING_NAME, ERR_INVALID_PARAMETER |
| 186 | + ); |
| 187 | + |
| 188 | + String const& k = inner_key; |
| 189 | + if (config->has_section_key(section, k) && !has_section_key(section, k)) { |
| 190 | + set_value(section, k, config->get_value(section, k)); |
| 191 | + } |
| 192 | + } |
| 193 | + break; |
| 194 | + } |
| 195 | + |
| 196 | + default: // |
| 197 | + ERR_FAIL_V(ERR_INVALID_PARAMETER); |
| 198 | + } |
| 199 | + } |
| 200 | + emit_changed(); |
| 201 | + |
| 202 | + return OK; |
| 203 | +} |
| 204 | + |
| 205 | +void GameSettings::_bind_methods() { |
| 206 | + OV_BIND_METHOD(GameSettings::reset_settings); |
| 207 | + |
| 208 | + OV_BIND_METHOD(GameSettings::save, { "path" }, DEFVAL(String())); |
| 209 | + OV_BIND_METHOD(GameSettings::load, { "path" }, DEFVAL(String())); |
| 210 | + |
| 211 | + OV_BIND_SMETHOD(GameSettings::load_from_file, { "path" }); |
| 212 | + |
| 213 | + OV_BIND_METHOD(GameSettings::set_value, { "section", "key", "value" }); |
| 214 | + OV_BIND_METHOD(GameSettings::get_value, { "section", "key", "default" }, DEFVAL(nullptr)); |
| 215 | + |
| 216 | + OV_BIND_METHOD(GameSettings::has_section, { "section" }); |
| 217 | + OV_BIND_METHOD(GameSettings::has_section_key, { "section", "key" }); |
| 218 | + |
| 219 | + OV_BIND_METHOD(GameSettings::reset_section, { "section" }); |
| 220 | + OV_BIND_METHOD(GameSettings::reset_section_key, { "section", "key" }); |
| 221 | + |
| 222 | + OV_BIND_METHOD(GameSettings::get_sections); |
| 223 | + OV_BIND_METHOD(GameSettings::get_section_keys, { "section" }); |
| 224 | + |
| 225 | + OV_BIND_METHOD(GameSettings::load_deprecated_file, { "path", "section_keys" }); |
| 226 | +} |
0 commit comments