d490f7dfd9eaab141b34e1df56d91c0a44a91ad0
[oweals/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
26 extern "C" {
27 #include <lua.h>
28 }
29
30 #include "irrlichttypes.h"
31 #include "threading/mutex.h"
32 #include "threading/mutex_auto_lock.h"
33 #include "common/c_types.h"
34 #include "common/c_internal.h"
35
36 #define SCRIPTAPI_LOCK_DEBUG
37 #define SCRIPTAPI_DEBUG
38
39 // MUST be an invalid mod name so that mods can't
40 // use that name to bypass security!
41 #define BUILTIN_MOD_NAME "*builtin*"
42
43 #define PCALL_RES(RES) do {                 \
44         int result_ = (RES);                    \
45         if (result_ != 0) {                     \
46                 scriptError(result_, __FUNCTION__); \
47         }                                       \
48 } while (0)
49
50 #define runCallbacks(nargs, mode) \
51         runCallbacksRaw((nargs), (mode), __FUNCTION__)
52
53 #define setOriginFromTable(index) \
54         setOriginFromTableRaw(index, __FUNCTION__)
55
56 class Server;
57 class Environment;
58 class GUIEngine;
59 class ServerActiveObject;
60
61 class ScriptApiBase {
62 public:
63         ScriptApiBase();
64         virtual ~ScriptApiBase();
65
66         bool loadMod(const std::string &script_path, const std::string &mod_name,
67                 std::string *error=NULL);
68         bool loadScript(const std::string &script_path, std::string *error=NULL);
69
70         void runCallbacksRaw(int nargs,
71                 RunCallbacksMode mode, const char *fxn);
72
73         /* object */
74         void addObjectReference(ServerActiveObject *cobj);
75         void removeObjectReference(ServerActiveObject *cobj);
76
77         Server* getServer() { return m_server; }
78
79         std::string getOrigin() { return m_last_run_mod; }
80         void setOriginDirect(const char *origin);
81         void setOriginFromTableRaw(int index, const char *fxn);
82
83 protected:
84         friend class LuaABM;
85         friend class InvRef;
86         friend class ObjectRef;
87         friend class NodeMetaRef;
88         friend class ModApiBase;
89         friend class ModApiEnvMod;
90         friend class LuaVoxelManip;
91
92         lua_State* getStack()
93                 { return m_luastack; }
94
95         void realityCheck();
96         void scriptError(int result, const char *fxn);
97         void stackDump(std::ostream &o);
98
99         void setServer(Server* server) { m_server = server; }
100
101         Environment* getEnv() { return m_environment; }
102         void setEnv(Environment* env) { m_environment = env; }
103
104         GUIEngine* getGuiEngine() { return m_guiengine; }
105         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
106
107         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
108         void objectrefGet(lua_State *L, u16 id);
109
110         Mutex           m_luastackmutex;
111         std::string     m_last_run_mod;
112         bool            m_secure;
113 #ifdef SCRIPTAPI_LOCK_DEBUG
114         bool            m_locked;
115 #endif
116
117 private:
118         lua_State*      m_luastack;
119
120         Server*         m_server;
121         Environment*    m_environment;
122         GUIEngine*      m_guiengine;
123 };
124
125 #endif /* S_BASE_H_ */