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.
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"
27 #include "util/string.h"
46 ModNameStorer(lua_State *L_, const std::string &modname):
49 // Store current modname in registry
50 lua_pushstring(L, modname.c_str());
51 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
55 // Clear current modname in registry
57 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
66 ScriptApiBase::ScriptApiBase()
68 #ifdef SCRIPTAPI_LOCK_DEBUG
72 m_luastack = luaL_newstate();
75 // Add and save an error handler
76 lua_pushcfunction(m_luastack, script_error_handler);
77 m_errorhandler = lua_gettop(m_luastack);
79 // Make the ScriptApiBase* accessible to ModApiBase
80 lua_pushlightuserdata(m_luastack, this);
81 lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
83 // If we are using LuaJIT add a C++ wrapper function to catch
84 // exceptions thrown in Lua -> C++ calls
86 lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
87 luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
88 lua_pop(m_luastack, 1);
96 ScriptApiBase::~ScriptApiBase()
98 lua_close(m_luastack);
101 bool ScriptApiBase::loadMod(const std::string &scriptpath,
102 const std::string &modname)
104 ModNameStorer modnamestorer(getStack(), modname);
106 if (!string_allowed(modname, MODNAME_ALLOWED_CHARS)) {
107 errorstream<<"Error loading mod \""<<modname
108 <<"\": modname does not follow naming conventions: "
109 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
113 return loadScript(scriptpath);
116 bool ScriptApiBase::loadScript(const std::string &scriptpath)
118 verbosestream<<"Loading and running script from "<<scriptpath<<std::endl;
120 lua_State *L = getStack();
122 int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, m_errorhandler);
124 errorstream << "========== ERROR FROM LUA ===========" << std::endl;
125 errorstream << "Failed to load and run script from " << std::endl;
126 errorstream << scriptpath << ":" << std::endl;
127 errorstream << std::endl;
128 errorstream << lua_tostring(L, -1) << std::endl;
129 errorstream << std::endl;
130 errorstream << "======= END OF ERROR FROM LUA ========" << std::endl;
131 lua_pop(L, 1); // Pop error message from stack
137 void ScriptApiBase::realityCheck()
139 int top = lua_gettop(m_luastack);
141 dstream<<"Stack is over 30:"<<std::endl;
143 std::string traceback = script_get_backtrace(m_luastack);
144 throw LuaError("Stack is over 30 (reality check)\n" + traceback);
148 void ScriptApiBase::scriptError()
150 throw LuaError(lua_tostring(m_luastack, -1));
153 void ScriptApiBase::stackDump(std::ostream &o)
156 int top = lua_gettop(m_luastack);
157 for (i = 1; i <= top; i++) { /* repeat for each level */
158 int t = lua_type(m_luastack, i);
161 case LUA_TSTRING: /* strings */
162 o<<"\""<<lua_tostring(m_luastack, i)<<"\"";
165 case LUA_TBOOLEAN: /* booleans */
166 o<<(lua_toboolean(m_luastack, i) ? "true" : "false");
169 case LUA_TNUMBER: /* numbers */ {
171 snprintf(buf, 10, "%g", lua_tonumber(m_luastack, i));
175 default: /* other values */
176 o<<lua_typename(m_luastack, t);
185 void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
187 SCRIPTAPI_PRECHECKHEADER
188 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
190 // Create object on stack
191 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
192 int object = lua_gettop(L);
194 // Get minetest.object_refs table
195 lua_getglobal(L, "minetest");
196 lua_getfield(L, -1, "object_refs");
197 luaL_checktype(L, -1, LUA_TTABLE);
198 int objectstable = lua_gettop(L);
200 // object_refs[id] = object
201 lua_pushnumber(L, cobj->getId()); // Push id
202 lua_pushvalue(L, object); // Copy object to top of stack
203 lua_settable(L, objectstable);
206 void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
208 SCRIPTAPI_PRECHECKHEADER
209 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
211 // Get minetest.object_refs table
212 lua_getglobal(L, "minetest");
213 lua_getfield(L, -1, "object_refs");
214 luaL_checktype(L, -1, LUA_TTABLE);
215 int objectstable = lua_gettop(L);
217 // Get object_refs[id]
218 lua_pushnumber(L, cobj->getId()); // Push id
219 lua_gettable(L, objectstable);
220 // Set object reference to NULL
221 ObjectRef::set_null(L);
222 lua_pop(L, 1); // pop object
224 // Set object_refs[id] = nil
225 lua_pushnumber(L, cobj->getId()); // Push id
227 lua_settable(L, objectstable);
230 // Creates a new anonymous reference if cobj=NULL or id=0
231 void ScriptApiBase::objectrefGetOrCreate(
232 ServerActiveObject *cobj)
234 lua_State *L = getStack();
236 if(cobj == NULL || cobj->getId() == 0){
237 ObjectRef::create(L, cobj);
239 objectrefGet(cobj->getId());
243 void ScriptApiBase::objectrefGet(u16 id)
245 lua_State *L = getStack();
247 // Get minetest.object_refs[i]
248 lua_getglobal(L, "minetest");
249 lua_getfield(L, -1, "object_refs");
250 luaL_checktype(L, -1, LUA_TTABLE);
251 lua_pushnumber(L, id);
253 lua_remove(L, -2); // object_refs
254 lua_remove(L, -2); // minetest