]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_base.h
Fix code style from recent commits and add misc. optimizations
[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 "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
44 class Server;
45 class Environment;
46 class GUIEngine;
47 class ServerActiveObject;
48
49 class ScriptApiBase {
50 public:
51         ScriptApiBase();
52         virtual ~ScriptApiBase();
53
54         bool loadMod(const std::string &script_path, const std::string &mod_name,
55                 std::string *error=NULL);
56         bool loadScript(const std::string &script_path, std::string *error=NULL);
57
58         /* object */
59         void addObjectReference(ServerActiveObject *cobj);
60         void removeObjectReference(ServerActiveObject *cobj);
61
62         Server* getServer() { return m_server; }
63
64 protected:
65         friend class LuaABM;
66         friend class InvRef;
67         friend class ObjectRef;
68         friend class NodeMetaRef;
69         friend class ModApiBase;
70         friend class ModApiEnvMod;
71         friend class LuaVoxelManip;
72
73         lua_State* getStack()
74                 { return m_luastack; }
75
76         void realityCheck();
77         void scriptError();
78         void stackDump(std::ostream &o);
79
80         void setServer(Server* server) { m_server = server; }
81
82         Environment* getEnv() { return m_environment; }
83         void setEnv(Environment* env) { m_environment = env; }
84
85         GUIEngine* getGuiEngine() { return m_guiengine; }
86         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
87
88         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
89         void objectrefGet(lua_State *L, u16 id);
90
91         JMutex          m_luastackmutex;
92         // Stack index of Lua error handler
93         int             m_errorhandler;
94         bool            m_secure;
95 #ifdef SCRIPTAPI_LOCK_DEBUG
96         bool            m_locked;
97 #endif
98
99 private:
100         lua_State*      m_luastack;
101
102         Server*         m_server;
103         Environment*    m_environment;
104         GUIEngine*      m_guiengine;
105 };
106
107 #endif /* S_BASE_H_ */