Return a reason for failure from sim.loadStamp

This commit is contained in:
Tamás Bálint Misius 2021-06-21 08:04:32 +02:00
parent cb52495472
commit dcf0764fd8
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
5 changed files with 27 additions and 3 deletions

View File

@ -46,6 +46,10 @@
#include "client/http/Request.h" #include "client/http/Request.h"
#include "client/http/RequestManager.h" #include "client/http/RequestManager.h"
#ifdef LUACONSOLE
# include "lua/LuaScriptInterface.h"
#endif
extern "C" extern "C"
{ {
@ -1477,8 +1481,11 @@ SaveFile * Client::LoadSaveFile(ByteString filename)
GameSave * tempSave = new GameSave(ReadFile(filename)); GameSave * tempSave = new GameSave(ReadFile(filename));
file->SetGameSave(tempSave); file->SetGameSave(tempSave);
} }
catch (ParseException & e) catch (const ParseException &e)
{ {
#ifdef LUACONSOLE
luacon_ci->SetLastError(ByteString(e.what()).FromUtf8());
#endif
std::cerr << "Client: Invalid save file '" << filename << "': " << e.what() << std::endl; std::cerr << "Client: Invalid save file '" << filename << "': " << e.what() << std::endl;
file->SetLoadingError(ByteString(e.what()).FromUtf8()); file->SetLoadingError(ByteString(e.what()).FromUtf8());
} }

View File

@ -30,6 +30,10 @@ public:
virtual int Command(String command); virtual int Command(String command);
virtual String FormatCommand(String command); virtual String FormatCommand(String command);
void SetLastError(String err)
{
lastError = err;
}
String GetLastError(); String GetLastError();
virtual ~CommandInterface(); virtual ~CommandInterface();
}; };

View File

@ -1741,6 +1741,7 @@ int LuaScriptInterface::simulation_saveStamp(lua_State * l)
int LuaScriptInterface::simulation_loadStamp(lua_State * l) int LuaScriptInterface::simulation_loadStamp(lua_State * l)
{ {
int i = -1; int i = -1;
int pushed = 1;
SaveFile * tempfile = NULL; SaveFile * tempfile = NULL;
int x = luaL_optint(l,2,0); int x = luaL_optint(l,2,0);
int y = luaL_optint(l,3,0); int y = luaL_optint(l,3,0);
@ -1772,12 +1773,20 @@ int LuaScriptInterface::simulation_loadStamp(lua_State * l)
} }
} }
else else
{
pushed = 2;
lua_pushnil(l); lua_pushnil(l);
lua_pushstring(l, luacon_ci->GetLastError().ToUtf8().c_str());
}
delete tempfile; delete tempfile;
} }
else else
{
pushed = 2;
lua_pushnil(l); lua_pushnil(l);
return 1; lua_pushliteral(l, "Failed to read file");
}
return pushed;
} }
int LuaScriptInterface::simulation_deleteStamp(lua_State * l) int LuaScriptInterface::simulation_deleteStamp(lua_State * l)

View File

@ -216,5 +216,6 @@ public:
virtual ~LuaScriptInterface(); virtual ~LuaScriptInterface();
}; };
extern LuaScriptInterface *luacon_ci;
#endif /* LUASCRIPTINTERFACE_H_ */ #endif /* LUASCRIPTINTERFACE_H_ */

View File

@ -55,8 +55,11 @@ int Simulation::Load(GameSave * save, bool includePressure, int fullX, int fullY
{ {
save->Expand(); save->Expand();
} }
catch (ParseException &) catch (const ParseException &e)
{ {
#ifdef LUACONSOLE
luacon_ci->SetLastError(ByteString(e.what()).FromUtf8());
#endif
return 1; return 1;
} }