]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_base.h
0c2dfafd1f9f9fdec4cd97d82d3965e0f1591cb4
[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 "jthread/jmutex.h"
32 #include "jthread/jmutexautolock.h"
33 #include "common/c_types.h"
34 #include "common/c_internal.h"
35
36 #define SCRIPTAPI_LOCK_DEBUG
37
38 #define SCRIPT_MOD_NAME_FIELD "current_mod_name"
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 class Server;
51 class Environment;
52 class GUIEngine;
53 class ServerActiveObject;
54
55 class ScriptApiBase {
56 public:
57         ScriptApiBase();
58         virtual ~ScriptApiBase();
59
60         bool loadMod(const std::string &script_path, const std::string &mod_name,
61                 std::string *error=NULL);
62         bool loadScript(const std::string &script_path, std::string *error=NULL);
63
64         /* object */
65         void addObjectReference(ServerActiveObject *cobj);
66         void removeObjectReference(ServerActiveObject *cobj);
67
68         Server* getServer() { return m_server; }
69
70 protected:
71         friend class LuaABM;
72         friend class InvRef;
73         friend class ObjectRef;
74         friend class NodeMetaRef;
75         friend class ModApiBase;
76         friend class ModApiEnvMod;
77         friend class LuaVoxelManip;
78
79         lua_State* getStack()
80                 { return m_luastack; }
81
82         void realityCheck();
83         void scriptError(int result, const char *fxn);
84         void stackDump(std::ostream &o);
85
86         void setServer(Server* server) { m_server = server; }
87
88         Environment* getEnv() { return m_environment; }
89         void setEnv(Environment* env) { m_environment = env; }
90
91         GUIEngine* getGuiEngine() { return m_guiengine; }
92         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
93
94         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
95         void objectrefGet(lua_State *L, u16 id);
96
97         JMutex          m_luastackmutex;
98         // Stack index of Lua error handler
99         int             m_errorhandler;
100         bool            m_secure;
101 #ifdef SCRIPTAPI_LOCK_DEBUG
102         bool            m_locked;
103 #endif
104
105 private:
106         lua_State*      m_luastack;
107
108         Server*         m_server;
109         Environment*    m_environment;
110         GUIEngine*      m_guiengine;
111 };
112
113 #endif /* S_BASE_H_ */