]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_cheats.cpp
82a314265c331d997cdacda4c8b538f8784e9c95
[dragonfireclient.git] / src / script / cpp_api / s_cheats.cpp
1 /*
2 Dragonfire
3 Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */  
19
20 #include "cpp_api/s_cheats.h"
21 #include "cpp_api/s_base.h"
22 #include "cpp_api/s_internal.h"
23 #include "settings.h"
24
25 ScriptApiCheatsCheat::ScriptApiCheatsCheat(const std::string &name, const std::string &setting) : 
26         m_name(name),
27         m_setting(setting),
28         m_function_ref(0)
29 {
30 }
31
32
33 ScriptApiCheatsCheat::ScriptApiCheatsCheat(const std::string &name, const int &function) : 
34         m_name(name),
35         m_setting(""),
36         m_function_ref(function)
37 {
38 }
39
40 bool ScriptApiCheatsCheat::is_enabled()
41 {
42         return ! m_function_ref && g_settings->getBool(m_setting);
43 }
44
45 void ScriptApiCheatsCheat::toggle(lua_State *L, int error_handler)
46 {       
47         if (m_function_ref) {
48                 lua_rawgeti(L, LUA_REGISTRYINDEX, m_function_ref);
49                 lua_pcall(L, 0, 0, error_handler);
50         } else 
51                 g_settings->setBool(m_setting, !g_settings->getBool(m_setting));
52 }
53
54 ScriptApiCheatsCategory::ScriptApiCheatsCategory(const std::string &name) :
55         m_name(name)
56 {
57 }
58
59 ScriptApiCheatsCategory::~ScriptApiCheatsCategory()
60 {
61         for (auto i = m_cheats.begin(); i != m_cheats.end(); i++)
62                 delete *i;
63 }
64
65 void ScriptApiCheatsCategory::read_cheats(lua_State *L)
66 {
67         lua_pushnil(L);
68         while (lua_next(L, -2)) {
69                 ScriptApiCheatsCheat *cheat = nullptr;
70                 std::string name = lua_tostring(L, -2);
71                 if (lua_isstring(L, -1))
72                         cheat = new ScriptApiCheatsCheat(name, lua_tostring(L, -1));
73                 else if (lua_isfunction(L, -1)) {
74                         cheat = new ScriptApiCheatsCheat(name, luaL_ref(L, LUA_REGISTRYINDEX));
75                         lua_pushnil(L);
76                 }
77                 if (cheat)
78                         m_cheats.push_back(cheat);
79                 lua_pop(L, 1);
80         }
81 }
82
83 ScriptApiCheats::~ScriptApiCheats()
84 {
85         for (auto i = m_cheat_categories.begin(); i != m_cheat_categories.end(); i++)
86                 delete *i;
87 }
88
89 void ScriptApiCheats::init_cheats()
90 {
91         SCRIPTAPI_PRECHECKHEADER
92         
93         lua_getglobal(L, "core");
94         lua_getfield(L, -1, "cheats");
95         if (! lua_istable(L, -1)) {
96                 lua_pop(L, 2);
97                 return;
98         }
99         lua_pushnil(L);
100         while (lua_next(L, -2)) {
101                 if (lua_istable(L, -1)) {
102                         ScriptApiCheatsCategory *category = new ScriptApiCheatsCategory(lua_tostring(L, -2));
103                         category->read_cheats(L);
104                         m_cheat_categories.push_back(category);
105                 }
106                 lua_pop(L, 1);
107         }
108         lua_pop(L, 2);
109         m_cheats_loaded = true;
110 }
111
112 void ScriptApiCheats::toggle_cheat(ScriptApiCheatsCheat *cheat)
113 {
114         SCRIPTAPI_PRECHECKHEADER
115         
116         PUSH_ERROR_HANDLER(L);
117         int error_handler = lua_gettop(L) - 1;
118         lua_insert(L, error_handler);
119         
120         cheat->toggle(L, error_handler);
121 }