{
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)
#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);
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
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);
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);
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);
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);
}
}