3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 #include <unordered_map>
27 #include "common/helper.h"
28 #include "util/basic_macros.h"
35 #include "irrlichttypes.h"
36 #include "common/c_types.h"
37 #include "common/c_internal.h"
41 #define SCRIPTAPI_LOCK_DEBUG
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*"
47 #define PCALL_RES(RES) { \
48 int result_ = (RES); \
50 scriptError(result_, __FUNCTION__); \
54 #define runCallbacks(nargs, mode) \
55 runCallbacksRaw((nargs), (mode), __FUNCTION__)
57 #define setOriginFromTable(index) \
58 setOriginFromTableRaw(index, __FUNCTION__)
60 enum class ScriptingType: u8 {
74 class ServerActiveObject;
75 struct PlayerHPChangeReason;
77 class ScriptApiBase : protected LuaHelper {
79 ScriptApiBase(ScriptingType type);
80 // fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
83 FATAL_ERROR("ScriptApiBase created without ScriptingType!");
85 virtual ~ScriptApiBase();
86 DISABLE_CLASS_COPY(ScriptApiBase);
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);
93 void loadModFromMemory(const std::string &mod_name);
96 void runCallbacksRaw(int nargs,
97 RunCallbacksMode mode, const char *fxn);
100 void addObjectReference(ServerActiveObject *cobj);
101 void removeObjectReference(ServerActiveObject *cobj);
103 IGameDef *getGameDef() { return m_gamedef; }
105 ScriptingType getType() { return m_type; }
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);
116 void clientOpenLibs(lua_State *L);
122 friend class ObjectRef;
123 friend class NodeMetaRef;
124 friend class ModApiBase;
125 friend class ModApiEnvMod;
126 friend class LuaVoxelManip;
128 lua_State* getStack()
129 { return m_luastack; }
132 void scriptError(int result, const char *fxn);
133 void stackDump(std::ostream &o);
135 void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
137 Environment* getEnv() { return m_environment; }
138 void setEnv(Environment* env) { m_environment = env; }
141 GUIEngine* getGuiEngine() { return m_guiengine; }
142 void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
145 void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
147 void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
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;
158 static int luaPanic(lua_State *L);
160 lua_State *m_luastack = nullptr;
162 IGameDef *m_gamedef = nullptr;
163 Environment *m_environment = nullptr;
165 GUIEngine *m_guiengine = nullptr;
167 ScriptingType m_type;