]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_base.h
couple of memory leaks fixes.
[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
26 extern "C" {
27 #include <lua.h>
28 }
29
30 #include "irrlichttypes.h"
31 #include "threads.h"
32 #include "threading/mutex.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 class Environment;
59 class GUIEngine;
60 class ServerActiveObject;
61
62 class ScriptApiBase {
63 public:
64         ScriptApiBase();
65         virtual ~ScriptApiBase();
66
67         // These throw a ModError on failure
68         void loadMod(const std::string &script_path, const std::string &mod_name);
69         void loadScript(const std::string &script_path);
70
71         void runCallbacksRaw(int nargs,
72                 RunCallbacksMode mode, const char *fxn);
73
74         /* object */
75         void addObjectReference(ServerActiveObject *cobj);
76         void removeObjectReference(ServerActiveObject *cobj);
77
78         Server* getServer() { return m_server; }
79
80         std::string getOrigin() { return m_last_run_mod; }
81         void setOriginDirect(const char *origin);
82         void setOriginFromTableRaw(int index, const char *fxn);
83
84 protected:
85         friend class LuaABM;
86         friend class LuaLBM;
87         friend class InvRef;
88         friend class ObjectRef;
89         friend class NodeMetaRef;
90         friend class ModApiBase;
91         friend class ModApiEnvMod;
92         friend class LuaVoxelManip;
93
94         lua_State* getStack()
95                 { return m_luastack; }
96
97         void realityCheck();
98         void scriptError(int result, const char *fxn);
99         void stackDump(std::ostream &o);
100
101         void setServer(Server* server) { m_server = server; }
102
103         Environment* getEnv() { return m_environment; }
104         void setEnv(Environment* env) { m_environment = env; }
105
106         GUIEngine* getGuiEngine() { return m_guiengine; }
107         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
108
109         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
110         void objectrefGet(lua_State *L, u16 id);
111
112         RecursiveMutex  m_luastackmutex;
113         std::string     m_last_run_mod;
114         bool            m_secure;
115 #ifdef SCRIPTAPI_LOCK_DEBUG
116         int             m_lock_recursion_count;
117         threadid_t      m_owning_thread;
118 #endif
119
120 private:
121         lua_State*      m_luastack;
122
123         Server*         m_server;
124         Environment*    m_environment;
125         GUIEngine*      m_guiengine;
126 };
127
128 #endif /* S_BASE_H_ */