Fix script error reporting a bit

master
Perttu Ahola 2011-12-02 22:49:54 +02:00
parent 9344816bd6
commit 581f950e10
5 changed files with 48 additions and 8 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);
}
}