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"
28 #include "util/string.h"
47 ModNameStorer(lua_State *L_, const std::string &modname):
50 // Store current modname in registry
51 lua_pushstring(L, modname.c_str());
52 lua_setfield(L, LUA_REGISTRYINDEX, "current_modname");
56 // Clear current modname in registry
58 lua_setfield(L, LUA_REGISTRYINDEX, "current_modname");
67 ScriptApiBase::ScriptApiBase()
69 #ifdef SCRIPTAPI_LOCK_DEBUG
73 m_luastack = luaL_newstate();
76 luaL_openlibs(m_luastack);
78 // Add and save an error handler
79 lua_pushcfunction(m_luastack, script_error_handler);
80 m_errorhandler = lua_gettop(m_luastack);
82 // Make the ScriptApiBase* accessible to ModApiBase
83 lua_pushlightuserdata(m_luastack, this);
84 lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
86 // If we are using LuaJIT add a C++ wrapper function to catch
87 // exceptions thrown in Lua -> C++ calls
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);
95 lua_newtable(m_luastack);
96 lua_setglobal(m_luastack, "core");
98 lua_pushstring(m_luastack, DIR_DELIM);
99 lua_setglobal(m_luastack, "DIR_DELIM");
102 m_environment = NULL;
106 ScriptApiBase::~ScriptApiBase()
108 lua_close(m_luastack);
111 bool ScriptApiBase::loadMod(const std::string &scriptpath,
112 const std::string &modname)
114 ModNameStorer modnamestorer(getStack(), modname);
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;
123 return loadScript(scriptpath);
126 bool ScriptApiBase::loadScript(const std::string &scriptpath)
128 verbosestream<<"Loading and running script from "<<scriptpath<<std::endl;
130 lua_State *L = getStack();
132 int ret = luaL_loadfile(L, scriptpath.c_str()) || lua_pcall(L, 0, 0, m_errorhandler);
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
147 void ScriptApiBase::realityCheck()
149 int top = lua_gettop(m_luastack);
151 dstream<<"Stack is over 30:"<<std::endl;
153 std::string traceback = script_get_backtrace(m_luastack);
154 throw LuaError("Stack is over 30 (reality check)\n" + traceback);
158 void ScriptApiBase::scriptError()
160 throw LuaError(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 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);
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 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);
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(lua_State *L,
242 ServerActiveObject *cobj)
244 if(cobj == NULL || cobj->getId() == 0){
245 ObjectRef::create(L, cobj);
247 objectrefGet(L, cobj->getId());
251 void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
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);
259 lua_remove(L, -2); // object_refs
260 lua_remove(L, -2); // core