From d96cd236f3e983ff35ef16f7c9979c8341d5ae0c Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 3 Dec 2011 02:45:55 +0200 Subject: Enforced mod global naming convention and better error reporting --- src/scriptapi.cpp | 118 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 89 insertions(+), 29 deletions(-) (limited to 'src/scriptapi.cpp') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index db6b7e86e..be1366737 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -107,6 +107,59 @@ public: } }; +class ModNameStorer +{ +private: + lua_State *L; +public: + ModNameStorer(lua_State *L_, const std::string modname): + L(L_) + { + // Store current modname in registry + lua_pushstring(L, modname.c_str()); + lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname"); + } + ~ModNameStorer() + { + // Clear current modname in registry + lua_pushnil(L); + lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname"); + } +}; + +std::string get_current_modname(lua_State *L) +{ + lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname"); + std::string modname = ""; + if(lua_type(L, -1) == LUA_TSTRING) + modname = lua_tostring(L, -1); + lua_pop(L, 1); + return modname; +} + +void check_modname_prefix(lua_State *L, std::string &name) +{ + if(name.size() == 0) + throw LuaError(L, std::string("Name is empty")); + + if(name[0] == ':'){ + name = name.substr(1); + return; + } + + std::string modname = get_current_modname(L); + assert(modname != ""); + + // For __builtin, anything goes + if(modname == "__builtin") + return; + + if(name.substr(0, modname.size()+1) != modname + "_") + throw LuaError(L, std::string("Name \"")+name + +"\" does not follow naming conventions: " + +"\"modname_\" or \":\" prefix required)"); +} + static v3f readFloatPos(lua_State *L, int index) { v3f pos; @@ -470,7 +523,7 @@ static void inventory_get_list_to_lua(Inventory *inv, const char *name, lua_pushstring(L, item->getItemString().c_str()); } if(lua_pcall(L, 2, 0, 0)) - script_error(L, "error: %s\n", lua_tostring(L, -1)); + script_error(L, "error: %s", lua_tostring(L, -1)); } } @@ -611,8 +664,9 @@ static int l_register_nodedef_defaults(lua_State *L) // register_entity(name, prototype) static int l_register_entity(lua_State *L) { - const char *name = luaL_checkstring(L, 1); - infostream<<"register_entity: "< stack top // registered_entities[name] = object - lua_setfield(L, registered_entities, name); + lua_setfield(L, registered_entities, name.c_str()); // Get registered object to top of stack lua_pushvalue(L, 2); @@ -703,14 +757,14 @@ public: lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count_wider); if(lua_pcall(L, 4, 0, 0)) - script_error(L, "error: %s\n", lua_tostring(L, -1)); + script_error(L, "error: %s", lua_tostring(L, -1)); } }; // register_abm({...}) static int l_register_abm(lua_State *L) { - infostream<<"register_abm"<registerCraftItem(name, def); lua_pushvalue(L, table); - scriptapi_add_craftitem(L, name); + scriptapi_add_craftitem(L, name.c_str()); return 0; /* number of results */ } @@ -814,8 +870,9 @@ static int l_register_craftitem(lua_State *L) // register_node(name, {lots of stuff}) static int l_register_node(lua_State *L) { - const char *name = luaL_checkstring(L, 1); - infostream<<"register_node: "<