The Battle for Wesnoth  1.17.0-dev
lua_preferences.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
3  by Jyrki Vesterinen <sandgtx@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
18 #include "config.hpp"
19 #include "lua/lua.h"
20 #include "lua/lauxlib.h"
21 #include "preferences/general.hpp"
22 #include "scripting/lua_common.hpp"
23 
24 /**
25  * The __index metamethod.
26  * Parameter 1: the preference table.
27  * Parameter 2: preference name, must be a string.
28  * Returns: preference value. If there isn't such a preference, returns nil.
29  */
31 {
32  std::string preference_name = luaL_checkstring(L, 2);
34  return 1;
35 }
36 
37 /**
38  * The __newindex metamethod.
39  * Parameter 1: the preference table.
40  * Parameter 2: preference name, must be a string.
41  * Parameter 3: preference value.
42  * Returns nothing.
43  */
45 {
46  std::string preference_name = luaL_checkstring(L, 2);
48  luaW_toscalar(L, 3, value);
49  preferences::set(preference_name, value);
50  return 0;
51 }
52 
53 namespace lua_preferences
54 {
55  std::string register_table(lua_State* L)
56  {
57  // Push the wesnoth table to the stack
58  lua_getglobal(L, "wesnoth");
59 
60  // Create the preferences table
61  lua_newtable(L);
63  lua_setfield(L, -2, "__index");
65  lua_setfield(L, -2, "__newindex");
66  lua_pushstring(L, "src/scripting/lua_preferences.cpp");
67  lua_setfield(L, -2, "__metatable");
68 
69  // Set the table as its own metatable
70  lua_pushvalue(L, -1);
71  lua_setmetatable(L, -2);
72 
73  // Assign the table to wesnoth.preferences
74  lua_setfield(L, -2, "preferences");
75 
76  // Pop the wesnoth table from the stack
77  lua_pop(L, 1);
78 
79  return "Adding preferences table...\n";
80  }
81 }
#define lua_pushcfunction(L, f)
Definition: lua.h:370
Variant for storing WML attributes.
LUA_API int lua_getglobal(lua_State *L, const char *name)
Definition: lapi.cpp:632
void set(const std::string &key, bool value)
Definition: general.cpp:161
Definitions for the interface to Wesnoth Markup Language (WML).
#define lua_pop(L, n)
Definition: lua.h:364
void luaW_pushscalar(lua_State *L, const config::attribute_value &v)
Converts an attribute value into a Lua object pushed at the top of the stack.
Definition: lua_common.cpp:570
static int impl_preferences_get(lua_State *L)
The __index metamethod.
config::attribute_value get_as_attribute(const std::string &key)
Definition: general.cpp:222
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:901
#define lua_newtable(L)
Definition: lua.h:366
static int impl_preferences_set(lua_State *L)
The __newindex metamethod.
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:246
std::string register_table(lua_State *L)
bool luaW_toscalar(lua_State *L, int index, config::attribute_value &v)
Converts the value at the top of the stack to an attribute value.
Definition: lua_common.cpp:575
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:514
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:837
#define luaL_checkstring(L, n)
Definition: lauxlib.h:138