]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_base.h
Move `setlocale` from Lua to C++.
[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 "util/basic_macros.h"
28
29 extern "C" {
30 #include <lua.h>
31 #include <lualib.h>
32 }
33
34 #include "irrlichttypes.h"
35 #include "common/c_types.h"
36 #include "common/c_internal.h"
37 #include "debug.h"
38 #include "config.h"
39
40 #define SCRIPTAPI_LOCK_DEBUG
41 #define SCRIPTAPI_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
76 class ScriptApiBase {
77 public:
78         ScriptApiBase(ScriptingType type);
79         // fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
80         ScriptApiBase()
81         {
82                 FATAL_ERROR("ScriptApiBase created without ScriptingType!");
83         }
84         virtual ~ScriptApiBase();
85         DISABLE_CLASS_COPY(ScriptApiBase);
86
87         // These throw a ModError on failure
88         void loadMod(const std::string &script_path, const std::string &mod_name);
89         void loadScript(const std::string &script_path);
90
91 #ifndef SERVER
92         void loadModFromMemory(const std::string &mod_name);
93 #endif
94
95         void runCallbacksRaw(int nargs,
96                 RunCallbacksMode mode, const char *fxn);
97
98         /* object */
99         void addObjectReference(ServerActiveObject *cobj);
100         void removeObjectReference(ServerActiveObject *cobj);
101
102         IGameDef *getGameDef() { return m_gamedef; }
103         Server* getServer();
104         ScriptingType getType() { return m_type; }
105 #ifndef SERVER
106         Client* getClient();
107 #endif
108
109         std::string getOrigin() { return m_last_run_mod; }
110         void setOriginDirect(const char *origin);
111         void setOriginFromTableRaw(int index, const char *fxn);
112
113         void clientOpenLibs(lua_State *L);
114
115 protected:
116         friend class LuaABM;
117         friend class LuaLBM;
118         friend class InvRef;
119         friend class ObjectRef;
120         friend class NodeMetaRef;
121         friend class ModApiBase;
122         friend class ModApiEnvMod;
123         friend class LuaVoxelManip;
124
125         lua_State* getStack()
126                 { return m_luastack; }
127
128         void realityCheck();
129         void scriptError(int result, const char *fxn);
130         void stackDump(std::ostream &o);
131
132         void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
133
134         Environment* getEnv() { return m_environment; }
135         void setEnv(Environment* env) { m_environment = env; }
136
137         GUIEngine* getGuiEngine() { return m_guiengine; }
138         void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
139
140         void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
141
142         std::recursive_mutex m_luastackmutex;
143         std::string     m_last_run_mod;
144         bool            m_secure = false;
145 #ifdef SCRIPTAPI_LOCK_DEBUG
146         int             m_lock_recursion_count;
147         std::thread::id m_owning_thread;
148 #endif
149
150 private:
151         static int luaPanic(lua_State *L);
152
153         lua_State      *m_luastack = nullptr;
154
155         IGameDef       *m_gamedef = nullptr;
156         Environment    *m_environment = nullptr;
157         GUIEngine      *m_guiengine = nullptr;
158         ScriptingType  m_type;
159 };