]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_base.h
Line_of_sight: Improve using VoxelLineIterator
[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 #pragma once
21
22 #include <iostream>
23 #include <string>
24 #include <thread>
25 #include <mutex>
26 #include "util/basic_macros.h"
27
28 extern "C" {
29 #include <lua.h>
30 }
31
32 #include "irrlichttypes.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) {                    \
44         int result_ = (RES);                    \
45         if (result_ != 0) {                     \
46                 scriptError(result_, __FUNCTION__); \
47         }                                       \
48 }
49
50 #define runCallbacks(nargs, mode) \
51         runCallbacksRaw((nargs), (mode), __FUNCTION__)
52
53 #define setOriginFromTable(index) \
54         setOriginFromTableRaw(index, __FUNCTION__)
55
56 enum class ScriptingType: u8 {
57         Client,
58         Server,
59         MainMenu
60 };
61
62 class Server;
63 #ifndef SERVER
64 class Client;
65 #endif
66 class IGameDef;
67 class Environment;
68 class GUIEngine;
69 class ServerActiveObject;
70
71 class ScriptApiBase {
72 public:
73         ScriptApiBase();
74         virtual ~ScriptApiBase();
75         DISABLE_CLASS_COPY(ScriptApiBase);
76
77         // These throw a ModError on failure
78         void loadMod(const std::string &script_path, const std::string &mod_name);
79         void loadScript(const std::string &script_path);
80
81 #ifndef SERVER
82         void loadModFromMemory(const std::string &mod_name);
83 #endif
84
85         void runCallbacksRaw(int nargs,
86                 RunCallbacksMode mode, const char *fxn);
87
88         /* object */
89         void addObjectReference(ServerActiveObject *cobj);
90         void removeObjectReference(ServerActiveObject *cobj);
91
92         IGameDef *getGameDef() { return m_gamedef; }
93         Server* getServer();
94         void setType(ScriptingType type) { m_type = type; }
95         ScriptingType getType() { return m_type; }
96 #ifndef SERVER
97         Client* getClient();
98 #endif
99
100         std::string getOrigin() { return m_last_run_mod; }
101         void setOriginDirect(const char *origin);
102         void setOriginFromTableRaw(int index, const char *fxn);
103
104 protected:
105         friend class LuaABM;
106         friend class LuaLBM;
107         friend class InvRef;
108         friend class ObjectRef;
109         friend class NodeMetaRef;
110         friend class ModApiBase;
111         friend class ModApiEnvMod;
112         friend class LuaVoxelManip;
113
114         lua_State* getStack()
115                 { return m_luastack; }
116
117         void realityCheck();
118         void scriptError(int result, const char *fxn);
119         void stackDump(std::ostream &o);
120
121         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
122
123         Environment* getEnv() { return m_environment; }
124         void setEnv(Environment* env) { m_environment = env; }
125
126         GUIEngine* getGuiEngine() { return m_guiengine; }
127         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
128
129         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
130
131         std::recursive_mutex m_luastackmutex;
132         std::string     m_last_run_mod;
133         bool            m_secure = false;
134 #ifdef SCRIPTAPI_LOCK_DEBUG
135         int             m_lock_recursion_count;
136         std::thread::id m_owning_thread;
137 #endif
138
139 private:
140         static int luaPanic(lua_State *L);
141
142         lua_State      *m_luastack = nullptr;
143
144         IGameDef       *m_gamedef = nullptr;
145         Environment    *m_environment = nullptr;
146         GUIEngine      *m_guiengine = nullptr;
147         ScriptingType  m_type;
148 };