diff options
-rw-r--r-- | src/script.cpp | 7 | ||||
-rw-r--r-- | src/script.h | 21 | ||||
-rw-r--r-- | src/scriptapi.cpp | 20 | ||||
-rw-r--r-- | src/scriptapi.h | 2 | ||||
-rw-r--r-- | src/server.cpp | 6 |
5 files changed, 48 insertions, 8 deletions
diff --git a/src/script.cpp b/src/script.cpp index 16d8030d6..5a6c98026 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -35,10 +35,11 @@ void script_error(lua_State *L, const char *fmt, ...) { va_list argp; va_start(argp, fmt); - vfprintf(stderr, fmt, argp); + char buf[10000]; + vsnprintf(buf, 10000, fmt, argp); va_end(argp); - lua_close(L); - exit(EXIT_FAILURE); + //errorstream<<"SCRIPT ERROR: "<<buf; + throw LuaError(buf); } bool script_load(lua_State *L, const char *path) diff --git a/src/script.h b/src/script.h index ce697bc50..6da95acf7 100644 --- a/src/script.h +++ b/src/script.h @@ -20,8 +20,27 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef SCRIPT_HEADER #define SCRIPT_HEADER +#include <exception> +#include <string> + +class LuaError : public std::exception +{ +public: + LuaError(const std::string &s) + { + m_s = "LuaError: "; + m_s += s; + } + virtual ~LuaError() throw() + {} + virtual const char * what() const throw() + { + return m_s.c_str(); + } + std::string m_s; +}; + typedef struct lua_State lua_State; -//#include <string> lua_State* script_init(); void script_deinit(lua_State *L); diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index cb26fa472..db6b7e86e 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -1101,7 +1101,10 @@ static int l_register_craft(lua_State *L) width = colcount; } else { if(colcount != width){ - script_error(L, "error: %s\n", "Invalid crafting recipe"); + std::string error; + error += "Invalid crafting recipe (output=\"" + + output + "\")"; + throw LuaError(error); } } // removes value, keeps key for next iteration @@ -2469,6 +2472,21 @@ void scriptapi_export(lua_State *L, Server *server) ObjectRef::Register(L); } +bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath, + const std::string &modname) +{ + bool success = false; + + try{ + success = script_load(L, scriptpath.c_str()); + } + catch(LuaError &e){ + errorstream<<"Error loading mod: "<<e.what()<<std::endl; + } + + return success; +} + void scriptapi_add_environment(lua_State *L, ServerEnvironment *env) { realitycheck(L); diff --git a/src/scriptapi.h b/src/scriptapi.h index 33b795415..d107b15ce 100644 --- a/src/scriptapi.h +++ b/src/scriptapi.h @@ -34,6 +34,8 @@ struct PointedThing; class ServerRemotePlayer; void scriptapi_export(lua_State *L, Server *server); +bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath, + const std::string &modname); void scriptapi_add_environment(lua_State *L, ServerEnvironment *env); void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj); diff --git a/src/server.cpp b/src/server.cpp index b8bdd8830..e345d8811 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -982,7 +982,7 @@ Server::Server( if(!success){ errorstream<<"Server: Failed to load and run " <<builtinpath<<std::endl; - assert(0); + exit(1); } // Load and run "mod" scripts core::list<ModSpec> mods = getMods(m_modspaths); @@ -991,11 +991,11 @@ Server::Server( ModSpec mod = *i; infostream<<"Server: Loading mod \""<<mod.name<<"\""<<std::endl; std::string scriptpath = mod.path + DIR_DELIM + "init.lua"; - bool success = script_load(m_lua, scriptpath.c_str()); + bool success = scriptapi_loadmod(m_lua, scriptpath, mod.name); if(!success){ errorstream<<"Server: Failed to load and run " <<scriptpath<<std::endl; - assert(0); + exit(1); } } |