diff --git a/src/preferences.cpp b/src/preferences.cpp index 6628b814251..f8a4b2aebeb 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -194,6 +194,11 @@ void set(const std::string &key, const std::string &value) prefs[key] = value; } +void set(const std::string &key, const config::attribute_value &value) +{ + prefs[key] = value; +} + void clear(const std::string& key) { prefs.recursive_clear_value(key); @@ -230,6 +235,11 @@ bool get(const std::string &key, bool def) return prefs[key].to_bool(def); } +config::attribute_value get_as_attribute(const std::string &key) +{ + return prefs[key]; +} + void disable_preferences_save() { no_preferences_save = true; } diff --git a/src/preferences.hpp b/src/preferences.hpp index e4c3a5440bb..f3c862966b9 100644 --- a/src/preferences.hpp +++ b/src/preferences.hpp @@ -17,9 +17,9 @@ #ifndef PREFERENCES_HPP_INCLUDED #define PREFERENCES_HPP_INCLUDED -class config; class display; +#include "config.hpp" #include "terrain/translation.hpp" #include @@ -52,14 +52,16 @@ namespace preferences { void set(const std::string& key, const std::string &value); void set(const std::string& key, char const *value); - void set(const std::string &key, bool value); - void set(const std::string &key, int value); + void set(const std::string& key, bool value); + void set(const std::string& key, int value); + void set(const std::string& key, const config::attribute_value& value); void clear(const std::string& key); void set_child(const std::string& key, const config& val); const config &get_child(const std::string &key); std::string get(const std::string& key); std::string get(const std::string& key, const std::string& def); - bool get(const std::string &key, bool def); + bool get(const std::string& key, bool def); + config::attribute_value get_as_attribute(const std::string& key); void erase(const std::string& key); bool have_setting(const std::string& key); diff --git a/src/scripting/lua_preferences.cpp b/src/scripting/lua_preferences.cpp index e10c78cccd3..4547f36e0f4 100644 --- a/src/scripting/lua_preferences.cpp +++ b/src/scripting/lua_preferences.cpp @@ -1,20 +1,21 @@ #include "lua_preferences.hpp" +#include #include #include #include +#include /** * The __index metamethod. * Parameter 1: the preference table. * Parameter 2: preference name, must be a string. - * Returns: preference value. Returned as a string regardless of the type of the preference. - * If there isn't such a preference, returns an empty string. + * Returns: preference value. If there isn't such a preference, returns nil. */ static int impl_preferences_get(lua_State* L) { std::string preference_name = luaL_checkstring(L, 2); - lua_pushstring(L, preferences::get(preference_name).c_str()); + luaW_pushscalar(L, preferences::get_as_attribute(preference_name)); return 1; } @@ -22,30 +23,15 @@ static int impl_preferences_get(lua_State* L) * The __newindex metamethod. * Parameter 1: the preference table. * Parameter 2: preference name, must be a string. - * Parameter 3: preference value. Can be a string, boolean or integer. + * Parameter 3: preference value. * Returns nothing. */ static int impl_preferences_set(lua_State* L) { std::string preference_name = luaL_checkstring(L, 2); - int type = lua_type(L, 3); - - switch (type) - { - case LUA_TSTRING: - preferences::set(preference_name, luaL_checkstring(L, 3)); - break; - case LUA_TBOOLEAN: - preferences::set(preference_name, lua_toboolean(L, 3) == 1); - break; - case LUA_TNUMBER: - preferences::set(preference_name, luaL_checkint(L, 3)); - break; - default: - return luaL_typerror(L, 3, "string/boolean/number"); - break; - } - + config::attribute_value value; + luaW_toscalar(L, 3, value); + preferences::set(preference_name, value); return 0; } @@ -62,6 +48,8 @@ namespace lua_preferences lua_setfield(L, -2, "__index"); lua_pushcfunction(L, impl_preferences_set); lua_setfield(L, -2, "__newindex"); + lua_pushstring(L, "src/scripting/lua_preferences.cpp"); + lua_setfield(L, -2, "__metatable"); // Set the table as its own metatable lua_pushvalue(L, -1); @@ -75,4 +63,4 @@ namespace lua_preferences return "Adding preferences table...\n"; } -} \ No newline at end of file +} diff --git a/src/scripting/lua_preferences.hpp b/src/scripting/lua_preferences.hpp index d6f4045e7a7..9d409e41822 100644 --- a/src/scripting/lua_preferences.hpp +++ b/src/scripting/lua_preferences.hpp @@ -24,4 +24,4 @@ namespace lua_preferences std::string register_table(lua_State* L); } -#endif \ No newline at end of file +#endif