Expose preferences to Lua
This commit is contained in:
parent
99a0819ff0
commit
76d407e733
8 changed files with 129 additions and 1 deletions
|
@ -127,6 +127,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
|
||||
|
|
|
@ -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>
|
||||
|
@ -4362,6 +4369,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" />
|
||||
|
|
|
@ -1526,6 +1526,9 @@
|
|||
<ClCompile Include="..\..\src\wesnoth.cpp" />
|
||||
<ClCompile Include="..\..\src\wesnothd_connection.cpp" />
|
||||
<ClCompile Include="..\..\src\wml_exception.cpp" />
|
||||
<ClCompile Include="..\..\src\scripting\lua_preferences.cpp">
|
||||
<Filter>Scripting</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\src\addon\client.hpp">
|
||||
|
@ -2935,6 +2938,9 @@
|
|||
<ClInclude Include="..\..\src\wesnothd_connection_error.hpp" />
|
||||
<ClInclude Include="..\..\src\wml_exception.hpp" />
|
||||
<ClInclude Include="..\..\src\wml_separators.hpp" />
|
||||
<ClInclude Include="..\..\src\scripting\lua_preferences.hpp">
|
||||
<Filter>Scripting</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">
|
||||
|
|
|
@ -919,6 +919,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
|
||||
|
|
|
@ -524,6 +524,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
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
@ -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";
|
||||
|
|
78
src/scripting/lua_preferences.cpp
Normal file
78
src/scripting/lua_preferences.cpp
Normal file
|
@ -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
src/scripting/lua_preferences.hpp
Normal file
27
src/scripting/lua_preferences.hpp
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue