The Battle for Wesnoth  1.19.5+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"
20 #include "scripting/lua_common.hpp"
21 #include "scripting/push_check.hpp"
22 
23 /**
24  * The __index metamethod.
25  * Parameter 1: the preference table.
26  * Parameter 2: preference name, must be a string.
27  * Returns: preference value. If there isn't such a preference, returns nil.
28  */
29 int impl_preferences_get(lua_State* L)
30 {
31  std::string preference_name = luaL_checkstring(L, 2);
32  luaW_pushscalar(L, prefs::get().preferences_[preference_name]);
33  return 1;
34 }
35 
36 /**
37  * The __newindex metamethod.
38  * Parameter 1: the preference table.
39  * Parameter 2: preference name, must be a string.
40  * Parameter 3: preference value.
41  * Returns nothing.
42  */
43 int impl_preferences_set(lua_State* L)
44 {
45  std::string preference_name = luaL_checkstring(L, 2);
47  luaW_toscalar(L, 3, value);
48  prefs::get().preferences_["preferences_name"] = value;
49  return 0;
50 }
51 
52 static int impl_preferences_dir(lua_State* L)
53 {
54  std::vector<std::string> all_prefs;
55 
56  for(const std::string& attr : prefs::get().all_attributes()) {
57  all_prefs.push_back(attr);
58  }
59 
60  // This will result in some duplicates since the advanced preferences' values are stored in the same file as the regular preferences
61  // but dir() automatically strips those out so who cares
62  for(const auto& pref : prefs::get().get_advanced_preferences()) {
63  all_prefs.push_back(pref.field);
64  }
65  lua_push(L, all_prefs);
66  return 1;
67 }
68 
69 namespace lua_preferences
70 {
71  std::string register_table(lua_State* L)
72  {
73  // Push the wesnoth table to the stack
74  lua_getglobal(L, "wesnoth");
75 
76  // Create the preferences table
77  lua_newtable(L);
78  lua_pushcfunction(L, impl_preferences_get);
79  lua_setfield(L, -2, "__index");
80  lua_pushcfunction(L, impl_preferences_set);
81  lua_setfield(L, -2, "__newindex");
82  lua_pushcfunction(L, impl_preferences_dir);
83  lua_setfield(L, -2, "__dir");
84  lua_pushstring(L, "src/scripting/lua_preferences.cpp");
85  lua_setfield(L, -2, "__metatable");
86 
87  // Set the table as its own metatable
88  lua_pushvalue(L, -1);
89  lua_setmetatable(L, -2);
90 
91  // Assign the table to wesnoth.preferences
92  lua_setfield(L, -2, "preferences");
93 
94  // Pop the wesnoth table from the stack
95  lua_pop(L, 1);
96 
97  return "Adding preferences table...\n";
98  }
99 }
Variant for storing WML attributes.
static prefs & get()
config preferences_
Definitions for the interface to Wesnoth Markup Language (WML).
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:583
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:578
int impl_preferences_set(lua_State *L)
The __newindex metamethod.
static int impl_preferences_dir(lua_State *L)
int impl_preferences_get(lua_State *L)
The __index metamethod.
std::string register_table(lua_State *L)
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:425