]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_base.h
Async-related script cleanups
[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 #pragma once
21
22 #include <iostream>
23 #include <string>
24 #include <thread>
25 #include <mutex>
26 #include <unordered_map>
27 #include "common/helper.h"
28 #include "util/basic_macros.h"
29
30 extern "C" {
31 #include <lua.h>
32 #include <lualib.h>
33 }
34
35 #include "irrlichttypes.h"
36 #include "common/c_types.h"
37 #include "common/c_internal.h"
38 #include "debug.h"
39 #include "config.h"
40
41 #define SCRIPTAPI_LOCK_DEBUG
42
43 // MUST be an invalid mod name so that mods can't
44 // use that name to bypass security!
45 #define BUILTIN_MOD_NAME "*builtin*"
46
47 #define PCALL_RES(RES) {                    \
48         int result_ = (RES);                    \
49         if (result_ != 0) {                     \
50                 scriptError(result_, __FUNCTION__); \
51         }                                       \
52 }
53
54 #define runCallbacks(nargs, mode) \
55         runCallbacksRaw((nargs), (mode), __FUNCTION__)
56
57 #define setOriginFromTable(index) \
58         setOriginFromTableRaw(index, __FUNCTION__)
59
60 enum class ScriptingType: u8 {
61         Async,
62         Client,
63         MainMenu,
64         Server
65 };
66
67 class Server;
68 #ifndef SERVER
69 class Client;
70 #endif
71 class IGameDef;
72 class Environment;
73 class GUIEngine;
74 class ServerActiveObject;
75 struct PlayerHPChangeReason;
76
77 class ScriptApiBase : protected LuaHelper {
78 public:
79         ScriptApiBase(ScriptingType type);
80         // fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
81         ScriptApiBase()
82         {
83                 FATAL_ERROR("ScriptApiBase created without ScriptingType!");
84         }
85         virtual ~ScriptApiBase();
86         DISABLE_CLASS_COPY(ScriptApiBase);
87
88         // These throw a ModError on failure
89         void loadMod(const std::string &script_path, const std::string &mod_name);
90         void loadScript(const std::string &script_path);
91
92 #ifndef SERVER
93         void loadModFromMemory(const std::string &mod_name);
94 #endif
95
96         void runCallbacksRaw(int nargs,
97                 RunCallbacksMode mode, const char *fxn);
98
99         /* object */
100         void addObjectReference(ServerActiveObject *cobj);
101         void removeObjectReference(ServerActiveObject *cobj);
102
103         IGameDef *getGameDef() { return m_gamedef; }
104         Server* getServer();
105         ScriptingType getType() { return m_type; }
106 #ifndef SERVER
107         Client* getClient();
108 #endif
109
110         // IMPORTANT: these cannot be used for any security-related uses, they exist
111         // only to enrich error messages
112         const std::string &getOrigin() { return m_last_run_mod; }
113         void setOriginDirect(const char *origin);
114         void setOriginFromTableRaw(int index, const char *fxn);
115
116         void clientOpenLibs(lua_State *L);
117
118 protected:
119         friend class LuaABM;
120         friend class LuaLBM;
121         friend class InvRef;
122         friend class ObjectRef;
123         friend class NodeMetaRef;
124         friend class ModApiBase;
125         friend class ModApiEnvMod;
126         friend class LuaVoxelManip;
127
128         lua_State* getStack()
129                 { return m_luastack; }
130
131         void realityCheck();
132         void scriptError(int result, const char *fxn);
133         void stackDump(std::ostream &o);
134
135         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
136
137         Environment* getEnv() { return m_environment; }
138         void setEnv(Environment* env) { m_environment = env; }
139
140 #ifndef SERVER
141         GUIEngine* getGuiEngine() { return m_guiengine; }
142         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
143 #endif
144
145         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
146
147         void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
148
149         std::recursive_mutex m_luastackmutex;
150         std::string     m_last_run_mod;
151         bool            m_secure = false;
152 #ifdef SCRIPTAPI_LOCK_DEBUG
153         int             m_lock_recursion_count{};
154         std::thread::id m_owning_thread;
155 #endif
156
157 private:
158         static int luaPanic(lua_State *L);
159
160         lua_State      *m_luastack = nullptr;
161
162         IGameDef       *m_gamedef = nullptr;
163         Environment    *m_environment = nullptr;
164 #ifndef SERVER
165         GUIEngine      *m_guiengine = nullptr;
166 #endif
167         ScriptingType  m_type;
168 };