Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "lua_preferences.hpp" | ||
|
||
#include <lua/lua.h> | ||
#include <lua/lauxlib.h> | ||
#include <preferences.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. | ||
*/ | ||
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()); | ||
return 1; | ||
} | ||
|
||
/** | ||
* 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. | ||
* 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; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
namespace lua_preferences | ||
{ | ||
std::string register_table(lua_State* L) | ||
{ | ||
// Push the wesnoth table to the stack | ||
lua_getglobal(L, "wesnoth"); | ||
|
||
// Create the preferences table | ||
lua_newtable(L); | ||
lua_pushcfunction(L, impl_preferences_get); | ||
lua_setfield(L, -2, "__index"); | ||
lua_pushcfunction(L, impl_preferences_set); | ||
lua_setfield(L, -2, "__newindex"); | ||
|
||
// Set the table as its own metatable | ||
lua_pushvalue(L, -1); | ||
lua_setmetatable(L, -2); | ||
|
||
// Assign the table to wesnoth.preferences | ||
lua_setfield(L, -2, "preferences"); | ||
|
||
// Pop the wesnoth table from the stack | ||
lua_pop(L, 1); | ||
|
||
return "Adding preferences table...\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Copyright (C) 2016 by Jyrki Vesterinen <sandgtx@gmail.com> | ||
Part of the Battle for Wesnoth Project http://www.wesnoth.org/ | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 2 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY. | ||
See the COPYING file for more details. | ||
*/ | ||
|
||
#ifndef SCRIPTING_LUA_UNIT_PREFERENCES_HPP | ||
#define SCRIPTING_LUA_UNIT_PREFERENCES_HPP | ||
|
||
#include <string> | ||
|
||
struct lua_State; | ||
|
||
namespace lua_preferences | ||
{ | ||
std::string register_table(lua_State* L); | ||
} | ||
|
||
#endif |