]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_base.cpp
Add meshnode drawtype.
[minetest.git] / src / script / cpp_api / s_base.cpp
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 #include "cpp_api/s_base.h"
21 #include "cpp_api/s_internal.h"
22 #include "lua_api/l_object.h"
23 #include "serverobject.h"
24 #include "debug.h"
25 #include "filesys.h"
26 #include "log.h"
27 #include "mods.h"
28 #include "util/string.h"
29
30
31 extern "C" {
32 #include "lualib.h"
33 #if USE_LUAJIT
34         #include "luajit.h"
35 #endif
36 }
37
38 #include <stdio.h>
39 #include <cstdarg>
40
41
42 class ModNameStorer
43 {
44 private:
45         lua_State *L;
46 public:
47         ModNameStorer(lua_State *L_, const std::string &modname):
48                 L(L_)
49         {
50                 // Store current modname in registry
51                 lua_pushstring(L, modname.c_str());
52                 lua_setfield(L, LUA_REGISTRYINDEX, "current_modname");
53         }
54         ~ModNameStorer()
55         {
56                 // Clear current modname in registry
57                 lua_pushnil(L);
58                 lua_setfield(L, LUA_REGISTRYINDEX, "current_modname");
59         }
60 };
61
62
63 /*
64         ScriptApiBase
65 */
66
67 ScriptApiBase::ScriptApiBase()
68 {
69         #ifdef SCRIPTAPI_LOCK_DEBUG
70         m_locked = false;
71         #endif
72
73         m_luastack = luaL_newstate();
74         assert(m_luastack);
75
76         luaL_openlibs(m_luastack);
77
78         // Add and save an error handler
79         lua_pushcfunction(m_luastack, script_error_handler);
80         m_errorhandler = lua_gettop(m_luastack);
81
82         // Make the ScriptApiBase* accessible to ModApiBase
83         lua_pushlightuserdata(m_luastack, this);
84         lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
85
86         // If we are using LuaJIT add a C++ wrapper function to catch
87         // exceptions thrown in Lua -> C++ calls
88 #if USE_LUAJIT
89         lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
90         luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
91         lua_pop(m_luastack, 1);
92 #endif
93
94         // Add basic globals
95         lua_newtable(m_luastack);
96         lua_setglobal(m_luastack, "core");
97
98         lua_pushstring(m_luastack, DIR_DELIM);
99         lua_setglobal(m_luastack, "DIR_DELIM");
100
101         m_server = NULL;
102         m_environment = NULL;
103         m_guiengine = NULL;
104 }
105
106 ScriptApiBase::~ScriptApiBase()
107 {
108         lua_close(m_luastack);
109 }
110
111 bool ScriptApiBase::loadMod(const std::string &scriptpath,
112                 const std::string &modname)
113 {
114         ModNameStorer modnamestorer(getStack(), modname);
115
116         if (!string_allowed(modname, MODNAME_ALLOWED_CHARS)) {
117                 errorstream<<"Error loading mod \""<<modname
118                                 <<"\": modname does not follow naming conventions: "
119                                 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
120                 return false;
121         }
122
123         return loadScript(scriptpath);
124 }
125
126 bool ScriptApiBase::loadScript(const std::string &scriptpath)
127 {
128         verbosestream<<"Loading and running script from "<<scriptpath<<std::endl;
129
130         lua_State *L = getStack();
131
132         int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, m_errorhandler);
133         if (ret) {
134                 errorstream << "========== ERROR FROM LUA ===========" << std::endl;
135                 errorstream << "Failed to load and run script from " << std::endl;
136                 errorstream << scriptpath << ":" << std::endl;
137                 errorstream << std::endl;
138                 errorstream << lua_tostring(L, -1) << std::endl;
139                 errorstream << std::endl;
140                 errorstream << "======= END OF ERROR FROM LUA ========" << std::endl;
141                 lua_pop(L, 1); // Pop error message from stack
142                 return false;
143         }
144         return true;
145 }
146
147 void ScriptApiBase::realityCheck()
148 {
149         int top = lua_gettop(m_luastack);
150         if(top >= 30){
151                 dstream<<"Stack is over 30:"<<std::endl;
152                 stackDump(dstream);
153                 std::string traceback = script_get_backtrace(m_luastack);
154                 throw LuaError("Stack is over 30 (reality check)\n" + traceback);
155         }
156 }
157
158 void ScriptApiBase::scriptError()
159 {
160         throw LuaError(lua_tostring(m_luastack, -1));
161 }
162
163 void ScriptApiBase::stackDump(std::ostream &o)
164 {
165         int i;
166         int top = lua_gettop(m_luastack);
167         for (i = 1; i <= top; i++) {  /* repeat for each level */
168                 int t = lua_type(m_luastack, i);
169                 switch (t) {
170
171                         case LUA_TSTRING:  /* strings */
172                                 o<<"\""<<lua_tostring(m_luastack, i)<<"\"";
173                                 break;
174
175                         case LUA_TBOOLEAN:  /* booleans */
176                                 o<<(lua_toboolean(m_luastack, i) ? "true" : "false");
177                                 break;
178
179                         case LUA_TNUMBER:  /* numbers */ {
180                                 char buf[10];
181                                 snprintf(buf, 10, "%g", lua_tonumber(m_luastack, i));
182                                 o<<buf;
183                                 break; }
184
185                         default:  /* other values */
186                                 o<<lua_typename(m_luastack, t);
187                                 break;
188
189                 }
190                 o<<" ";
191         }
192         o<<std::endl;
193 }
194
195 void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
196 {
197         SCRIPTAPI_PRECHECKHEADER
198         //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
199
200         // Create object on stack
201         ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
202         int object = lua_gettop(L);
203
204         // Get core.object_refs table
205         lua_getglobal(L, "core");
206         lua_getfield(L, -1, "object_refs");
207         luaL_checktype(L, -1, LUA_TTABLE);
208         int objectstable = lua_gettop(L);
209
210         // object_refs[id] = object
211         lua_pushnumber(L, cobj->getId()); // Push id
212         lua_pushvalue(L, object); // Copy object to top of stack
213         lua_settable(L, objectstable);
214 }
215
216 void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
217 {
218         SCRIPTAPI_PRECHECKHEADER
219         //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
220
221         // Get core.object_refs table
222         lua_getglobal(L, "core");
223         lua_getfield(L, -1, "object_refs");
224         luaL_checktype(L, -1, LUA_TTABLE);
225         int objectstable = lua_gettop(L);
226
227         // Get object_refs[id]
228         lua_pushnumber(L, cobj->getId()); // Push id
229         lua_gettable(L, objectstable);
230         // Set object reference to NULL
231         ObjectRef::set_null(L);
232         lua_pop(L, 1); // pop object
233
234         // Set object_refs[id] = nil
235         lua_pushnumber(L, cobj->getId()); // Push id
236         lua_pushnil(L);
237         lua_settable(L, objectstable);
238 }
239
240 // Creates a new anonymous reference if cobj=NULL or id=0
241 void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
242                 ServerActiveObject *cobj)
243 {
244         if(cobj == NULL || cobj->getId() == 0){
245                 ObjectRef::create(L, cobj);
246         } else {
247                 objectrefGet(L, cobj->getId());
248         }
249 }
250
251 void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
252 {
253         // Get core.object_refs[i]
254         lua_getglobal(L, "core");
255         lua_getfield(L, -1, "object_refs");
256         luaL_checktype(L, -1, LUA_TTABLE);
257         lua_pushnumber(L, id);
258         lua_gettable(L, -2);
259         lua_remove(L, -2); // object_refs
260         lua_remove(L, -2); // core
261 }
262