Address feedback

* Changed the plugin preference API to use luaW_pushscalar() and
luaW_toscalar() to handle preference values.
* Set the __metatable field in the "preferences" table, which blocks Lua
code from changing the metatable and allows developers to query where the
metatable originates.
* Added empty lines to the end of new files.
This commit is contained in:
Jyrki Vesterinen 2016-09-24 18:30:35 +03:00
parent c2d3607d49
commit 02b2ac5a17
4 changed files with 28 additions and 28 deletions

View File

@ -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;
}

View File

@ -17,9 +17,9 @@
#ifndef PREFERENCES_HPP_INCLUDED
#define PREFERENCES_HPP_INCLUDED
class config;
class display;
#include "config.hpp"
#include "terrain/translation.hpp"
#include <SDL.h>
@ -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);

View File

@ -1,20 +1,21 @@
#include "lua_preferences.hpp"
#include <config.hpp>
#include <lua/lua.h>
#include <lua/lauxlib.h>
#include <preferences.hpp>
#include <scripting/lua_common.hpp>
/**
* 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";
}
}
}

View File

@ -24,4 +24,4 @@ namespace lua_preferences
std::string register_table(lua_State* L);
}
#endif
#endif