]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_base.h
ed056db31b7f49f30b7859fbcfe5d58ea9e75e5b
[minetest.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 class Server;
58 #ifndef SERVER
59 class Client;
60 #endif
61 class IGameDef;
62 class Environment;
63 class GUIEngine;
64 class ServerActiveObject;
65
66 class ScriptApiBase {
67 public:
68         ScriptApiBase();
69         virtual ~ScriptApiBase();
70         DISABLE_CLASS_COPY(ScriptApiBase);
71
72         // These throw a ModError on failure
73         void loadMod(const std::string &script_path, const std::string &mod_name);
74         void loadScript(const std::string &script_path);
75
76         void runCallbacksRaw(int nargs,
77                 RunCallbacksMode mode, const char *fxn);
78
79         /* object */
80         void addObjectReference(ServerActiveObject *cobj);
81         void removeObjectReference(ServerActiveObject *cobj);
82
83         IGameDef *getGameDef() { return m_gamedef; }
84         Server* getServer();
85 #ifndef SERVER
86         Client* getClient();
87 #endif
88
89         std::string getOrigin() { return m_last_run_mod; }
90         void setOriginDirect(const char *origin);
91         void setOriginFromTableRaw(int index, const char *fxn);
92
93 protected:
94         friend class LuaABM;
95         friend class LuaLBM;
96         friend class InvRef;
97         friend class ObjectRef;
98         friend class NodeMetaRef;
99         friend class ModApiBase;
100         friend class ModApiEnvMod;
101         friend class LuaVoxelManip;
102
103         lua_State* getStack()
104                 { return m_luastack; }
105
106         void realityCheck();
107         void scriptError(int result, const char *fxn);
108         void stackDump(std::ostream &o);
109
110         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
111
112         Environment* getEnv() { return m_environment; }
113         void setEnv(Environment* env) { m_environment = env; }
114
115         GUIEngine* getGuiEngine() { return m_guiengine; }
116         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
117
118         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
119
120         std::recursive_mutex m_luastackmutex;
121         std::string     m_last_run_mod;
122         bool            m_secure;
123 #ifdef SCRIPTAPI_LOCK_DEBUG
124         int             m_lock_recursion_count;
125         std::thread::id m_owning_thread;
126 #endif
127
128 private:
129         static int luaPanic(lua_State *L);
130
131         lua_State*      m_luastack;
132
133         IGameDef*       m_gamedef;
134         Environment*    m_environment;
135         GUIEngine*      m_guiengine;
136 };
137
138 #endif /* S_BASE_H_ */