1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-09-16 09:26:28 +02:00

#994 should not crash anymore.

This commit is contained in:
Michał W. Urbańczyk
2012-06-09 19:58:17 +00:00
parent cdb55500f3
commit e6ebf42308
4 changed files with 49 additions and 23 deletions

View File

@@ -761,8 +761,15 @@ void YourTurn::applyCl( CClient *cl )
void SaveGame::applyCl(CClient *cl) void SaveGame::applyCl(CClient *cl)
{ {
CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vcgm1"); try
save << *cl; {
CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vcgm1");
save << *cl;
}
catch(std::exception &e)
{
tlog1 << "Failed to save game:" << e.what() << std::endl;
}
} }
void PlayerMessage::applyCl(CClient *cl) void PlayerMessage::applyCl(CClient *cl)

View File

@@ -268,17 +268,23 @@ int CSaveFile::write( const void * data, unsigned size )
void CSaveFile::openNextFile(const std::string &fname) void CSaveFile::openNextFile(const std::string &fname)
{ {
fName = fname; fName = fname;
sfile = make_unique<std::ofstream>(fname.c_str(), std::ios::binary); try
if(!(*sfile))
{
tlog1 << "Error: cannot open to write " << fname << std::endl;
sfile = NULL;
}
else
{ {
sfile = make_unique<std::ofstream>(fname.c_str(), std::ios::binary);
sfile->exceptions(std::ifstream::failbit | std::ifstream::badbit); //we throw a lot anyway
if(!(*sfile))
THROW_FORMAT("Error: cannot open to write %s!", fname);
sfile->write("VCMI",4); //write magic identifier sfile->write("VCMI",4); //write magic identifier
*this << version; //write format version *this << version; //write format version
} }
catch(...)
{
tlog1 << "Failed to save to " << fname << std::endl;
clear();
throw;
}
} }
void CSaveFile::reportState(CLogger &out) void CSaveFile::reportState(CLogger &out)
@@ -290,6 +296,12 @@ void CSaveFile::reportState(CLogger &out)
} }
} }
void CSaveFile::clear()
{
fName.clear();
sfile = nullptr;
}
CLoadFile::CLoadFile(const std::string &fname, int minimalVersion /*= version*/) CLoadFile::CLoadFile(const std::string &fname, int minimalVersion /*= version*/)
{ {
registerTypes(*this); registerTypes(*this);

View File

@@ -1004,11 +1004,12 @@ public:
std::string fName; std::string fName;
unique_ptr<std::ofstream> sfile; unique_ptr<std::ofstream> sfile;
CSaveFile(const std::string &fname); CSaveFile(const std::string &fname); //throws!
~CSaveFile(); ~CSaveFile();
int write(const void * data, unsigned size); int write(const void * data, unsigned size);
void openNextFile(const std::string &fname); void openNextFile(const std::string &fname); //throws!
void clear();
void reportState(CLogger &out); void reportState(CLogger &out);
}; };
@@ -1026,10 +1027,9 @@ public:
CLoadFile(const std::string &fname, int minimalVersion = version); //throws! CLoadFile(const std::string &fname, int minimalVersion = version); //throws!
~CLoadFile(); ~CLoadFile();
int read(const void * data, unsigned size); int read(const void * data, unsigned size); //throws!
void openNextFile(const std::string &fname, int minimalVersion); //throws! void openNextFile(const std::string &fname, int minimalVersion); //throws!
void clear(); void clear();
void reportState(CLogger &out); void reportState(CLogger &out);
}; };

View File

@@ -2250,19 +2250,26 @@ void CGameHandler::save( const std::string &fname )
sendToAllClients(&sg); sendToAllClients(&sg);
} }
try
{ {
tlog0 << "Serializing game info...\n"; {
CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vlgm1"); tlog0 << "Serializing game info...\n";
char hlp[8] = "VCMISVG"; CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vlgm1");
save << hlp << static_cast<CMapHeader&>(*gs->map) << gs->scenarioOps << *VLC << gs; char hlp[8] = "VCMISVG";
} save << hlp << static_cast<CMapHeader&>(*gs->map) << gs->scenarioOps << *VLC << gs;
}
{ {
tlog0 << "Serializing server info...\n"; tlog0 << "Serializing server info...\n";
CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vsgm1"); CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vsgm1");
save << *this; save << *this;
}
tlog0 << "Game has been successfully saved!\n";
}
catch(std::exception &e)
{
tlog1 << "Failed to save game: " << e.what() << std::endl;
} }
tlog0 << "Game has been successfully saved!\n";
} }
void CGameHandler::close() void CGameHandler::close()