mirror of
https://github.com/minetest/minetest.git
synced 2024-11-30 11:33:44 +01:00
Fix script error reporting a bit
This commit is contained in:
parent
9344816bd6
commit
581f950e10
@ -35,10 +35,11 @@ void script_error(lua_State *L, const char *fmt, ...)
|
|||||||
{
|
{
|
||||||
va_list argp;
|
va_list argp;
|
||||||
va_start(argp, fmt);
|
va_start(argp, fmt);
|
||||||
vfprintf(stderr, fmt, argp);
|
char buf[10000];
|
||||||
|
vsnprintf(buf, 10000, fmt, argp);
|
||||||
va_end(argp);
|
va_end(argp);
|
||||||
lua_close(L);
|
//errorstream<<"SCRIPT ERROR: "<<buf;
|
||||||
exit(EXIT_FAILURE);
|
throw LuaError(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool script_load(lua_State *L, const char *path)
|
bool script_load(lua_State *L, const char *path)
|
||||||
|
21
src/script.h
21
src/script.h
@ -20,8 +20,27 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#ifndef SCRIPT_HEADER
|
#ifndef SCRIPT_HEADER
|
||||||
#define 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;
|
typedef struct lua_State lua_State;
|
||||||
//#include <string>
|
|
||||||
|
|
||||||
lua_State* script_init();
|
lua_State* script_init();
|
||||||
void script_deinit(lua_State *L);
|
void script_deinit(lua_State *L);
|
||||||
|
@ -1101,7 +1101,10 @@ static int l_register_craft(lua_State *L)
|
|||||||
width = colcount;
|
width = colcount;
|
||||||
} else {
|
} else {
|
||||||
if(colcount != width){
|
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
|
// removes value, keeps key for next iteration
|
||||||
@ -2469,6 +2472,21 @@ void scriptapi_export(lua_State *L, Server *server)
|
|||||||
ObjectRef::Register(L);
|
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)
|
void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
|
||||||
{
|
{
|
||||||
realitycheck(L);
|
realitycheck(L);
|
||||||
|
@ -34,6 +34,8 @@ struct PointedThing;
|
|||||||
class ServerRemotePlayer;
|
class ServerRemotePlayer;
|
||||||
|
|
||||||
void scriptapi_export(lua_State *L, Server *server);
|
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_environment(lua_State *L, ServerEnvironment *env);
|
||||||
|
|
||||||
void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
|
void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj);
|
||||||
|
@ -982,7 +982,7 @@ Server::Server(
|
|||||||
if(!success){
|
if(!success){
|
||||||
errorstream<<"Server: Failed to load and run "
|
errorstream<<"Server: Failed to load and run "
|
||||||
<<builtinpath<<std::endl;
|
<<builtinpath<<std::endl;
|
||||||
assert(0);
|
exit(1);
|
||||||
}
|
}
|
||||||
// Load and run "mod" scripts
|
// Load and run "mod" scripts
|
||||||
core::list<ModSpec> mods = getMods(m_modspaths);
|
core::list<ModSpec> mods = getMods(m_modspaths);
|
||||||
@ -991,11 +991,11 @@ Server::Server(
|
|||||||
ModSpec mod = *i;
|
ModSpec mod = *i;
|
||||||
infostream<<"Server: Loading mod \""<<mod.name<<"\""<<std::endl;
|
infostream<<"Server: Loading mod \""<<mod.name<<"\""<<std::endl;
|
||||||
std::string scriptpath = mod.path + DIR_DELIM + "init.lua";
|
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){
|
if(!success){
|
||||||
errorstream<<"Server: Failed to load and run "
|
errorstream<<"Server: Failed to load and run "
|
||||||
<<scriptpath<<std::endl;
|
<<scriptpath<<std::endl;
|
||||||
assert(0);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user