Skip to content

Commit

Permalink
Expose preferences to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed Sep 20, 2016
1 parent 740acec commit 701b189
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog
Expand Up @@ -126,6 +126,9 @@ Version 1.13.5+dev:
it would produce a new object that did not compare equal to any others.)
* Lua dialog functions now support the stacked widget and the unit preview pane
* New wesnoth.show_menu function shows a dropdown menu at the mouse location
* New table wesnoth.preferences that gives read-write access to game preferences.
The table is not iterable, but you can read and change preferences with
predefined names.
* Lua attack proxy has new read_only field which is true for unit_type attacks
If true, attempts to change the attack will result in an error.
* The name field in Lua attack proxy is now writable
Expand Down
8 changes: 8 additions & 0 deletions projectfiles/VC12/wesnoth.vcxproj
Expand Up @@ -2916,6 +2916,13 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Scripting\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Scripting\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\scripting\lua_preferences.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Scripting\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Scripting\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Scripting\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug_with_VLD|Win32'">$(IntDir)Scripting\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Scripting\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\scripting\lua_race.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug_with_VLD|Win32'">$(IntDir)Scripting\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Scripting\</ObjectFileName>
Expand Down Expand Up @@ -4370,6 +4377,7 @@
<ClInclude Include="..\..\src\scripting\lua_kernel_base.hpp" />
<ClInclude Include="..\..\src\scripting\lua_map_location_ops.hpp" />
<ClInclude Include="..\..\src\scripting\lua_pathfind_cost_calculator.hpp" />
<ClInclude Include="..\..\src\scripting\lua_preferences.hpp" />
<ClInclude Include="..\..\src\scripting\lua_race.hpp" />
<ClInclude Include="..\..\src\scripting\lua_rng.hpp" />
<ClInclude Include="..\..\src\scripting\lua_team.hpp" />
Expand Down
6 changes: 6 additions & 0 deletions projectfiles/VC12/wesnoth.vcxproj.filters
Expand Up @@ -1528,6 +1528,9 @@
</ClCompile>
<ClCompile Include="..\..\src\gui\dialogs\multiplayer\mp_staging.cpp">
<Filter>Gui\Dialogs\Multiplayer</Filter>
</ClCompile>
<ClCompile Include="..\..\src\scripting\lua_preferences.cpp">
<Filter>Scripting</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -2935,6 +2938,9 @@
</ClInclude>
<ClInclude Include="..\..\src\gui\dialogs\multiplayer\mp_staging.hpp">
<Filter>Gui\Dialogs\Multiplayer</Filter>
</ClInclude>
<ClInclude Include="..\..\src\scripting\lua_preferences.hpp">
<Filter>Scripting</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -920,6 +920,7 @@ set(wesnoth-main_SRC
scripting/lua_gui2.cpp
scripting/lua_kernel_base.cpp
scripting/lua_map_location_ops.cpp
scripting/lua_preferences.cpp
scripting/lua_race.cpp
scripting/lua_rng.cpp
scripting/lua_team.cpp
Expand Down
1 change: 1 addition & 0 deletions src/SConscript
Expand Up @@ -525,6 +525,7 @@ wesnoth_sources = Split("""
scripting/lua_gui2.cpp
scripting/lua_kernel_base.cpp
scripting/lua_map_location_ops.cpp
scripting/lua_preferences.cpp
scripting/lua_race.cpp
scripting/lua_rng.cpp
scripting/lua_team.cpp
Expand Down
6 changes: 5 additions & 1 deletion src/scripting/lua_kernel_base.cpp
Expand Up @@ -33,6 +33,7 @@
#include "scripting/lua_formula_bridge.hpp"
#include "scripting/lua_gui2.hpp"
#include "scripting/lua_map_location_ops.hpp"
#include "scripting/lua_preferences.hpp"
#include "scripting/lua_rng.hpp"
#include "scripting/push_check.hpp"

Expand Down Expand Up @@ -397,7 +398,10 @@ lua_kernel_base::lua_kernel_base(CVideo * video)
lua_newtable(L);
luaL_setfuncs(L, map_callbacks, 0);
lua_setfield(L, -2, "map_location");
lua_pop(L,1);
lua_pop(L, 1);

// Create the preferences table.
cmd_log_ << lua_preferences::register_table(L);

// Add mersenne twister rng wrapper
cmd_log_ << "Adding rng tables...\n";
Expand Down
78 changes: 78 additions & 0 deletions src/scripting/lua_preferences.cpp
@@ -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";
}
}
27 changes: 27 additions & 0 deletions src/scripting/lua_preferences.hpp
@@ -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

0 comments on commit 701b189

Please sign in to comment.