The Battle for Wesnoth  1.19.0-dev
lua_preferences.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
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/wrapper_lauxlib.h"
20 #include "preferences/advanced.hpp"
21 #include "preferences/general.hpp"
22 #include "scripting/lua_common.hpp"
23 #include "scripting/push_check.hpp"
24 
25 /**
26  * The __index metamethod.
27  * Parameter 1: the preference table.
28  * Parameter 2: preference name, must be a string.
29  * Returns: preference value. If there isn't such a preference, returns nil.
30  */
31 static int impl_preferences_get(lua_State* L)
32 {
33  std::string preference_name = luaL_checkstring(L, 2);
35  return 1;
36 }
37 
38 /**
39  * The __newindex metamethod.
40  * Parameter 1: the preference table.
41  * Parameter 2: preference name, must be a string.
42  * Parameter 3: preference value.
43  * Returns nothing.
44  */
45 static int impl_preferences_set(lua_State* L)
46 {
47  std::string preference_name = luaL_checkstring(L, 2);
49  luaW_toscalar(L, 3, value);
50  preferences::set(preference_name, value);
51  return 0;
52 }
53 
54 static int impl_preferences_dir(lua_State* L)
55 {
56  std::vector<std::string> all_prefs;
57  for(const auto& [key, value] : preferences::get_prefs()->attribute_range()) {
58  all_prefs.push_back(key);
59  }
60  // This will result in some duplicates, but dir() automatically strips those out so who cares,
61  for(const auto& pref : preferences::get_advanced_preferences()) {
62  all_prefs.push_back(pref.field);
63  }
64  lua_push(L, all_prefs);
65  return 1;
66 }
67 
68 namespace lua_preferences
69 {
70  std::string register_table(lua_State* L)
71  {
72  // Push the wesnoth table to the stack
73  lua_getglobal(L, "wesnoth");
74 
75  // Create the preferences table
76  lua_newtable(L);
77  lua_pushcfunction(L, impl_preferences_get);
78  lua_setfield(L, -2, "__index");
79  lua_pushcfunction(L, impl_preferences_set);
80  lua_setfield(L, -2, "__newindex");
81  lua_pushcfunction(L, impl_preferences_dir);
82  lua_setfield(L, -2, "__dir");
83  lua_pushstring(L, "src/scripting/lua_preferences.cpp");
84  lua_setfield(L, -2, "__metatable");
85 
86  // Set the table as its own metatable
87  lua_pushvalue(L, -1);
88  lua_setmetatable(L, -2);
89 
90  // Assign the table to wesnoth.preferences
91  lua_setfield(L, -2, "preferences");
92 
93  // Pop the wesnoth table from the stack
94  lua_pop(L, 1);
95 
96  return "Adding preferences table...\n";
97  }
98 }
Variant for storing WML attributes.
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:586
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:581
static int impl_preferences_set(lua_State *L)
The __newindex metamethod.
static int impl_preferences_get(lua_State *L)
The __index metamethod.
static int impl_preferences_dir(lua_State *L)
std::string register_table(lua_State *L)
void set(const std::string &key, bool value)
Definition: general.cpp:165
config::attribute_value get_as_attribute(const std::string &key)
Definition: general.cpp:226
config * get_prefs()
Definition: general.cpp:235
const advanced_pref_list & get_advanced_preferences()
Gets a list of the available advanced preferences.
Definition: advanced.cpp:87
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:373