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 // Make the ScriptApiBase* accessible to ModApiBase
76 lua_pushlightuserdata(m_luastack, this);
77 lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
79 // If we are using LuaJIT add a C++ wrapper function to catch
80 // exceptions thrown in Lua -> C++ calls
82 lua_pushlightuserdata(m_luastack, (void*) script_exception_wrapper);
83 luaJIT_setmode(m_luastack, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
84 lua_pop(m_luastack, 1);
92 ScriptApiBase::~ScriptApiBase()
94 lua_close(m_luastack);
97 bool ScriptApiBase::loadMod(const std::string &scriptpath,
98 const std::string &modname)
100 ModNameStorer modnamestorer(getStack(), modname);
102 if(!string_allowed(modname, MODNAME_ALLOWED_CHARS)){
103 errorstream<<"Error loading mod \""<<modname
104 <<"\": modname does not follow naming conventions: "
105 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
109 bool success = false;
112 success = loadScript(scriptpath);
115 errorstream<<"Error loading mod \""<<modname
116 <<"\": "<<e.what()<<std::endl;
122 bool ScriptApiBase::loadScript(const std::string &scriptpath)
124 verbosestream<<"Loading and running script from "<<scriptpath<<std::endl;
126 lua_State *L = getStack();
128 lua_pushcfunction(L, script_error_handler);
129 int errorhandler = lua_gettop(L);
131 int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, errorhandler);
133 errorstream<<"========== ERROR FROM LUA ==========="<<std::endl;
134 errorstream<<"Failed to load and run script from "<<std::endl;
135 errorstream<<scriptpath<<":"<<std::endl;
136 errorstream<<std::endl;
137 errorstream<<lua_tostring(L, -1)<<std::endl;
138 errorstream<<std::endl;
139 errorstream<<"======= END OF ERROR FROM LUA ========"<<std::endl;
140 lua_pop(L, 1); // Pop error message from stack
141 lua_pop(L, 1); // Pop the error handler from stack
144 lua_pop(L, 1); // Pop the error handler from stack
148 void ScriptApiBase::realityCheck()
150 int top = lua_gettop(m_luastack);
152 dstream<<"Stack is over 30:"<<std::endl;
154 throw LuaError(m_luastack, "Stack is over 30 (reality check)");
158 void ScriptApiBase::scriptError()
160 throw LuaError(NULL, lua_tostring(m_luastack, -1));
163 void ScriptApiBase::stackDump(std::ostream &o)
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);
171 case LUA_TSTRING: /* strings */
172 o<<"\""<<lua_tostring(m_luastack, i)<<"\"";
175 case LUA_TBOOLEAN: /* booleans */
176 o<<(lua_toboolean(m_luastack, i) ? "true" : "false");
179 case LUA_TNUMBER: /* numbers */ {
181 snprintf(buf, 10, "%g", lua_tonumber(m_luastack, i));
185 default: /* other values */
186 o<<lua_typename(m_luastack, t);
195 void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
197 SCRIPTAPI_PRECHECKHEADER
198 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
200 // Create object on stack
201 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
202 int object = lua_gettop(L);
204 // Get minetest.object_refs table
205 lua_getglobal(L, "minetest");
206 lua_getfield(L, -1, "object_refs");
207 luaL_checktype(L, -1, LUA_TTABLE);
208 int objectstable = lua_gettop(L);
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);
216 void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
218 SCRIPTAPI_PRECHECKHEADER
219 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
221 // Get minetest.object_refs table
222 lua_getglobal(L, "minetest");
223 lua_getfield(L, -1, "object_refs");
224 luaL_checktype(L, -1, LUA_TTABLE);
225 int objectstable = lua_gettop(L);
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
234 // Set object_refs[id] = nil
235 lua_pushnumber(L, cobj->getId()); // Push id
237 lua_settable(L, objectstable);
240 // Creates a new anonymous reference if cobj=NULL or id=0
241 void ScriptApiBase::objectrefGetOrCreate(
242 ServerActiveObject *cobj)
244 lua_State *L = getStack();
246 if(cobj == NULL || cobj->getId() == 0){
247 ObjectRef::create(L, cobj);
249 objectrefGet(cobj->getId());
253 void ScriptApiBase::objectrefGet(u16 id)
255 lua_State *L = getStack();
257 // Get minetest.object_refs[i]
258 lua_getglobal(L, "minetest");
259 lua_getfield(L, -1, "object_refs");
260 luaL_checktype(L, -1, LUA_TTABLE);
261 lua_pushnumber(L, id);
263 lua_remove(L, -2); // object_refs
264 lua_remove(L, -2); // minetest