]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_base.h
Add LuaEntity on_death callback (#6177)
[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 #ifndef S_BASE_H_
21 #define S_BASE_H_
22
23 #include <iostream>
24 #include <string>
25 #include <thread>
26 #include "util/basic_macros.h"
27
28 extern "C" {
29 #include <lua.h>
30 }
31
32 #include "irrlichttypes.h"
33 #include "threading/mutex_auto_lock.h"
34 #include "common/c_types.h"
35 #include "common/c_internal.h"
36
37 #define SCRIPTAPI_LOCK_DEBUG
38 #define SCRIPTAPI_DEBUG
39
40 // MUST be an invalid mod name so that mods can't
41 // use that name to bypass security!
42 #define BUILTIN_MOD_NAME "*builtin*"
43
44 #define PCALL_RES(RES) do {                 \
45         int result_ = (RES);                    \
46         if (result_ != 0) {                     \
47                 scriptError(result_, __FUNCTION__); \
48         }                                       \
49 } while (0)
50
51 #define runCallbacks(nargs, mode) \
52         runCallbacksRaw((nargs), (mode), __FUNCTION__)
53
54 #define setOriginFromTable(index) \
55         setOriginFromTableRaw(index, __FUNCTION__)
56
57 enum class ScriptingType: u8 {
58         Client,
59         Server,
60         MainMenu
61 };
62
63 class Server;
64 #ifndef SERVER
65 class Client;
66 #endif
67 class IGameDef;
68 class Environment;
69 class GUIEngine;
70 class ServerActiveObject;
71
72 class ScriptApiBase {
73 public:
74         ScriptApiBase();
75         virtual ~ScriptApiBase();
76         DISABLE_CLASS_COPY(ScriptApiBase);
77
78         // These throw a ModError on failure
79         void loadMod(const std::string &script_path, const std::string &mod_name);
80         void loadScript(const std::string &script_path);
81
82 #ifndef SERVER
83         void loadModFromMemory(const std::string &mod_name);
84 #endif
85
86         void runCallbacksRaw(int nargs,
87                 RunCallbacksMode mode, const char *fxn);
88
89         /* object */
90         void addObjectReference(ServerActiveObject *cobj);
91         void removeObjectReference(ServerActiveObject *cobj);
92
93         IGameDef *getGameDef() { return m_gamedef; }
94         Server* getServer();
95         void setType(ScriptingType type) { m_type = type; }
96         ScriptingType getType() { return m_type; }
97 #ifndef SERVER
98         Client* getClient();
99 #endif
100
101         std::string getOrigin() { return m_last_run_mod; }
102         void setOriginDirect(const char *origin);
103         void setOriginFromTableRaw(int index, const char *fxn);
104
105 protected:
106         friend class LuaABM;
107         friend class LuaLBM;
108         friend class InvRef;
109         friend class ObjectRef;
110         friend class NodeMetaRef;
111         friend class ModApiBase;
112         friend class ModApiEnvMod;
113         friend class LuaVoxelManip;
114
115         lua_State* getStack()
116                 { return m_luastack; }
117
118         void realityCheck();
119         void scriptError(int result, const char *fxn);
120         void stackDump(std::ostream &o);
121
122         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
123
124         Environment* getEnv() { return m_environment; }
125         void setEnv(Environment* env) { m_environment = env; }
126
127         GUIEngine* getGuiEngine() { return m_guiengine; }
128         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
129
130         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
131
132         std::recursive_mutex m_luastackmutex;
133         std::string     m_last_run_mod;
134         bool            m_secure = false;
135 #ifdef SCRIPTAPI_LOCK_DEBUG
136         int             m_lock_recursion_count;
137         std::thread::id m_owning_thread;
138 #endif
139
140 private:
141         static int luaPanic(lua_State *L);
142
143         lua_State      *m_luastack = nullptr;
144
145         IGameDef       *m_gamedef = nullptr;
146         Environment    *m_environment = nullptr;
147         GUIEngine      *m_guiengine = nullptr;
148         ScriptingType  m_type;
149 };
150
151 #endif /* S_BASE_H_ */