diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2020-07-28 13:51:32 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2020-07-28 13:51:32 +0200 |
commit | f7a042223f9c82c7764c0df3a3531bda04e9ad64 (patch) | |
tree | 1d227816dc680ea883ba16a385c2151714e65961 /src/script/cpp_api | |
parent | 344fddc1727f637b3c4e2a9b88844d952b91fe4f (diff) | |
download | dragonfireclient-f7a042223f9c82c7764c0df3a3531bda04e9ad64.tar.xz |
Added cheat Menu
Diffstat (limited to 'src/script/cpp_api')
-rw-r--r-- | src/script/cpp_api/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/script/cpp_api/s_cheats.cpp | 121 | ||||
-rw-r--r-- | src/script/cpp_api/s_cheats.h | 57 |
3 files changed, 179 insertions, 0 deletions
diff --git a/src/script/cpp_api/CMakeLists.txt b/src/script/cpp_api/CMakeLists.txt index 3cfd7709a..d2c55b7c5 100644 --- a/src/script/cpp_api/CMakeLists.txt +++ b/src/script/cpp_api/CMakeLists.txt @@ -15,6 +15,7 @@ set(common_SCRIPT_CPP_API_SRCS set(client_SCRIPT_CPP_API_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/s_client.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/s_cheats.cpp ${CMAKE_CURRENT_SOURCE_DIR}/s_mainmenu.cpp PARENT_SCOPE) diff --git a/src/script/cpp_api/s_cheats.cpp b/src/script/cpp_api/s_cheats.cpp new file mode 100644 index 000000000..82a314265 --- /dev/null +++ b/src/script/cpp_api/s_cheats.cpp @@ -0,0 +1,121 @@ +/* +Dragonfire +Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 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; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "cpp_api/s_cheats.h" +#include "cpp_api/s_base.h" +#include "cpp_api/s_internal.h" +#include "settings.h" + +ScriptApiCheatsCheat::ScriptApiCheatsCheat(const std::string &name, const std::string &setting) : + m_name(name), + m_setting(setting), + m_function_ref(0) +{ +} + + +ScriptApiCheatsCheat::ScriptApiCheatsCheat(const std::string &name, const int &function) : + m_name(name), + m_setting(""), + m_function_ref(function) +{ +} + +bool ScriptApiCheatsCheat::is_enabled() +{ + return ! m_function_ref && g_settings->getBool(m_setting); +} + +void ScriptApiCheatsCheat::toggle(lua_State *L, int error_handler) +{ + if (m_function_ref) { + lua_rawgeti(L, LUA_REGISTRYINDEX, m_function_ref); + lua_pcall(L, 0, 0, error_handler); + } else + g_settings->setBool(m_setting, !g_settings->getBool(m_setting)); +} + +ScriptApiCheatsCategory::ScriptApiCheatsCategory(const std::string &name) : + m_name(name) +{ +} + +ScriptApiCheatsCategory::~ScriptApiCheatsCategory() +{ + for (auto i = m_cheats.begin(); i != m_cheats.end(); i++) + delete *i; +} + +void ScriptApiCheatsCategory::read_cheats(lua_State *L) +{ + lua_pushnil(L); + while (lua_next(L, -2)) { + ScriptApiCheatsCheat *cheat = nullptr; + std::string name = lua_tostring(L, -2); + if (lua_isstring(L, -1)) + cheat = new ScriptApiCheatsCheat(name, lua_tostring(L, -1)); + else if (lua_isfunction(L, -1)) { + cheat = new ScriptApiCheatsCheat(name, luaL_ref(L, LUA_REGISTRYINDEX)); + lua_pushnil(L); + } + if (cheat) + m_cheats.push_back(cheat); + lua_pop(L, 1); + } +} + +ScriptApiCheats::~ScriptApiCheats() +{ + for (auto i = m_cheat_categories.begin(); i != m_cheat_categories.end(); i++) + delete *i; +} + +void ScriptApiCheats::init_cheats() +{ + SCRIPTAPI_PRECHECKHEADER + + lua_getglobal(L, "core"); + lua_getfield(L, -1, "cheats"); + if (! lua_istable(L, -1)) { + lua_pop(L, 2); + return; + } + lua_pushnil(L); + while (lua_next(L, -2)) { + if (lua_istable(L, -1)) { + ScriptApiCheatsCategory *category = new ScriptApiCheatsCategory(lua_tostring(L, -2)); + category->read_cheats(L); + m_cheat_categories.push_back(category); + } + lua_pop(L, 1); + } + lua_pop(L, 2); + m_cheats_loaded = true; +} + +void ScriptApiCheats::toggle_cheat(ScriptApiCheatsCheat *cheat) +{ + SCRIPTAPI_PRECHECKHEADER + + PUSH_ERROR_HANDLER(L); + int error_handler = lua_gettop(L) - 1; + lua_insert(L, error_handler); + + cheat->toggle(L, error_handler); +} diff --git a/src/script/cpp_api/s_cheats.h b/src/script/cpp_api/s_cheats.h new file mode 100644 index 000000000..a93768659 --- /dev/null +++ b/src/script/cpp_api/s_cheats.h @@ -0,0 +1,57 @@ +/* +Dragonfire +Copyright (C) 2020 Elias Fleckenstein <eliasfleckenstein@web.de> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 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; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#pragma once + +#include "cpp_api/s_base.h" +#include <vector> +#include <string> + +class ScriptApiCheatsCheat +{ +public: + ScriptApiCheatsCheat(const std::string &name, const std::string &setting); + ScriptApiCheatsCheat(const std::string &name, const int &function); + std::string m_name; + bool is_enabled(); + void toggle(lua_State *L, int error_handler); +private: + std::string m_setting; + int m_function_ref; +}; + +class ScriptApiCheatsCategory +{ +public: + ScriptApiCheatsCategory(const std::string &name); + ~ScriptApiCheatsCategory(); + std::string m_name; + void read_cheats(lua_State *L); + std::vector<ScriptApiCheatsCheat*> m_cheats; +}; + +class ScriptApiCheats : virtual public ScriptApiBase +{ +public: + virtual ~ScriptApiCheats(); + void init_cheats(); + void toggle_cheat(ScriptApiCheatsCheat *cheat); + bool m_cheats_loaded = false; + std::vector<ScriptApiCheatsCategory*> m_cheat_categories; +}; |