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

@@ -760,10 +760,17 @@ void YourTurn::applyCl( CClient *cl )
}
void SaveGame::applyCl(CClient *cl)
{
try
{
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)
{

View File

@@ -268,17 +268,23 @@ int CSaveFile::write( const void * data, unsigned size )
void CSaveFile::openNextFile(const std::string &fname)
{
fName = fname;
try
{
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))
{
tlog1 << "Error: cannot open to write " << fname << std::endl;
sfile = NULL;
}
else
{
THROW_FORMAT("Error: cannot open to write %s!", fname);
sfile->write("VCMI",4); //write magic identifier
*this << version; //write format version
}
catch(...)
{
tlog1 << "Failed to save to " << fname << std::endl;
clear();
throw;
}
}
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*/)
{
registerTypes(*this);

View File

@@ -1004,11 +1004,12 @@ public:
std::string fName;
unique_ptr<std::ofstream> sfile;
CSaveFile(const std::string &fname);
CSaveFile(const std::string &fname); //throws!
~CSaveFile();
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);
};
@@ -1026,10 +1027,9 @@ public:
CLoadFile(const std::string &fname, int minimalVersion = version); //throws!
~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 clear();
void reportState(CLogger &out);
};

View File

@@ -2250,6 +2250,8 @@ void CGameHandler::save( const std::string &fname )
sendToAllClients(&sg);
}
try
{
{
tlog0 << "Serializing game info...\n";
CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vlgm1");
@@ -2264,6 +2266,11 @@ void CGameHandler::save( const std::string &fname )
}
tlog0 << "Game has been successfully saved!\n";
}
catch(std::exception &e)
{
tlog1 << "Failed to save game: " << e.what() << std::endl;
}
}
void CGameHandler::close()
{