]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_base.h
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / script / cpp_api / s_base.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 #pragma once
21
22 #include <iostream>
23 #include <string>
24 #include <thread>
25 #include <mutex>
26 #include <unordered_map>
27 #include "common/helper.h"
28 #include "util/basic_macros.h"
29
30 extern "C" {
31 #include <lua.h>
32 #include <lualib.h>
33 }
34
35 #include "irrlichttypes.h"
36 #include "common/c_types.h"
37 #include "common/c_internal.h"
38 #include "debug.h"
39 #include "config.h"
40
41 #define SCRIPTAPI_LOCK_DEBUG
42
43 // MUST be an invalid mod name so that mods can't
44 // use that name to bypass security!
45 #define BUILTIN_MOD_NAME "*builtin*"
46
47 #define PCALL_RES(RES) {                    \
48         int result_ = (RES);                    \
49         if (result_ != 0) {                     \
50                 scriptError(result_, __FUNCTION__); \
51         }                                       \
52 }
53
54 #define runCallbacks(nargs, mode) \
55         runCallbacksRaw((nargs), (mode), __FUNCTION__)
56
57 #define setOriginFromTable(index) \
58         setOriginFromTableRaw(index, __FUNCTION__)
59
60 enum class ScriptingType: u8 {
61         Async,
62         Client,
63         MainMenu,
64         Server
65 };
66
67 class Server;
68 #ifndef SERVER
69 class Client;
70 class Game;
71 #endif
72 class IGameDef;
73 class Environment;
74 class GUIEngine;
75 class ActiveObject;
76 class ServerActiveObject;
77 struct PlayerHPChangeReason;
78
79 class ScriptApiBase : protected LuaHelper {
80 public:
81         ScriptApiBase(ScriptingType type);
82         // fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
83         ScriptApiBase()
84         {
85                 FATAL_ERROR("ScriptApiBase created without ScriptingType!");
86         }
87         virtual ~ScriptApiBase();
88         DISABLE_CLASS_COPY(ScriptApiBase);
89
90         // These throw a ModError on failure
91         void loadMod(const std::string &script_path, const std::string &mod_name);
92         void loadScript(const std::string &script_path);
93
94 #ifndef SERVER
95         void loadModFromMemory(const std::string &mod_name);
96 #endif
97
98         void runCallbacksRaw(int nargs,
99                 RunCallbacksMode mode, const char *fxn);
100
101         /* object */
102         void addObjectReference(ActiveObject *cobj);
103         void removeObjectReference(ActiveObject *cobj);
104
105         IGameDef *getGameDef() { return m_gamedef; }
106         Server* getServer();
107         ScriptingType getType() { return m_type; }
108 #ifndef SERVER
109         Client* getClient();
110         Game *getGame() { return m_game; }
111 #endif
112
113         // IMPORTANT: these cannot be used for any security-related uses, they exist
114         // only to enrich error messages
115         const std::string &getOrigin() { return m_last_run_mod; }
116         void setOriginDirect(const char *origin);
117         void setOriginFromTableRaw(int index, const char *fxn);
118
119         void clientOpenLibs(lua_State *L);
120
121 protected:
122         friend class LuaABM;
123         friend class LuaLBM;
124         friend class InvRef;
125         friend class ObjectRef;
126         friend class NodeMetaRef;
127         friend class ModApiBase;
128         friend class ModApiEnvMod;
129         friend class LuaVoxelManip;
130
131         /*
132                 Subtle edge case with coroutines: If for whatever reason you have a
133                 method in a subclass that's called from existing lua_CFunction
134                 (any of the l_*.cpp files) then make it static and take the lua_State*
135                 as an argument. This is REQUIRED because getStack() will not return the
136                 correct state if called inside coroutines.
137
138                 Also note that src/script/common/ is the better place for such helpers.
139         */
140         lua_State* getStack()
141                 { return m_luastack; }
142
143         // Checks that stack size is sane
144         void realityCheck();
145         // Takes an error from lua_pcall and throws it as a LuaError
146         void scriptError(int result, const char *fxn);
147         // Dumps stack contents for debugging
148         void stackDump(std::ostream &o);
149
150         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
151 #ifndef SERVER
152         void setGame(Game *game) { m_game = game; }
153 #endif
154
155         Environment* getEnv() { return m_environment; }
156         void setEnv(Environment* env) { m_environment = env; }
157
158 #ifndef SERVER
159         GUIEngine* getGuiEngine() { return m_guiengine; }
160         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
161 #endif
162
163         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
164
165         void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
166
167         std::recursive_mutex m_luastackmutex;
168         std::string     m_last_run_mod;
169         bool            m_secure = false;
170 #ifdef SCRIPTAPI_LOCK_DEBUG
171         int             m_lock_recursion_count{};
172         std::thread::id m_owning_thread;
173 #endif
174
175 private:
176         static int luaPanic(lua_State *L);
177
178         lua_State      *m_luastack = nullptr;
179
180         IGameDef       *m_gamedef = nullptr;
181 #ifndef SERVER
182         Game       *m_game = nullptr;
183 #endif
184         Environment    *m_environment = nullptr;
185 #ifndef SERVER
186         GUIEngine      *m_guiengine = nullptr;
187 #endif
188         ScriptingType  m_type;
189 };