give an error when a local save isn't saved properly, instead of exiting and pretending it worked

This commit is contained in:
jacob1 2013-07-15 14:59:15 -04:00
parent c2a9b5c6f6
commit f4432b4759
4 changed files with 25 additions and 13 deletions

View File

@ -515,8 +515,9 @@ int Client::MakeDirectory(const char * dirName)
#endif
}
void Client::WriteFile(std::vector<unsigned char> fileData, std::string filename)
bool Client::WriteFile(std::vector<unsigned char> fileData, std::string filename)
{
bool saveError = false;
try
{
std::ofstream fileStream;
@ -526,12 +527,15 @@ void Client::WriteFile(std::vector<unsigned char> fileData, std::string filename
fileStream.write((char*)&fileData[0], fileData.size());
fileStream.close();
}
else
saveError = true;
}
catch (std::exception & e)
{
std::cerr << "WriteFile:" << e.what() << std::endl;
throw;
saveError = true;
}
return saveError;
}
bool Client::FileExists(std::string filename)
@ -554,8 +558,9 @@ bool Client::FileExists(std::string filename)
return exists;
}
void Client::WriteFile(std::vector<char> fileData, std::string filename)
bool Client::WriteFile(std::vector<char> fileData, std::string filename)
{
bool saveError = false;
try
{
std::ofstream fileStream;
@ -565,12 +570,15 @@ void Client::WriteFile(std::vector<char> fileData, std::string filename)
fileStream.write(&fileData[0], fileData.size());
fileStream.close();
}
else
saveError = true;
}
catch (std::exception & e)
{
std::cerr << "WriteFile:" << e.what() << std::endl;
throw;
saveError = true;
}
return saveError;
}
std::vector<unsigned char> Client::ReadFile(std::string filename)

View File

@ -111,8 +111,8 @@ public:
void SetProxy(std::string proxy);
int MakeDirectory(const char * dirname);
void WriteFile(std::vector<unsigned char> fileData, std::string filename);
void WriteFile(std::vector<char> fileData, std::string filename);
bool WriteFile(std::vector<unsigned char> fileData, std::string filename);
bool WriteFile(std::vector<char> fileData, std::string filename);
bool FileExists(std::string filename);
void AddListener(ClientListener * listener);

View File

@ -1059,7 +1059,8 @@ void GameController::OpenLocalSaveWindow(bool asCurrent)
else if (gameModel->GetSaveFile())
{
Client::Ref().MakeDirectory(LOCAL_SAVE_DIR);
Client::Ref().WriteFile(gameSave->Serialise(), gameModel->GetSaveFile()->GetName());
if (Client::Ref().WriteFile(gameSave->Serialise(), gameModel->GetSaveFile()->GetName()))
new ErrorMessage("Error", "Unable to write save file.");
}
}
}

View File

@ -81,7 +81,6 @@ void LocalSaveActivity::Save()
if (result == ConfirmPrompt::ResultOkay)
{
a->saveWrite(filename);
a->Exit();
}
}
virtual ~FileOverwriteConfirmation() { }
@ -99,7 +98,6 @@ void LocalSaveActivity::Save()
else
{
saveWrite(finalFilename);
Exit();
}
}
else
@ -111,8 +109,13 @@ void LocalSaveActivity::Save()
void LocalSaveActivity::saveWrite(std::string finalFilename)
{
Client::Ref().MakeDirectory(LOCAL_SAVE_DIR);
Client::Ref().WriteFile(save.GetGameSave()->Serialise(), finalFilename);
if (Client::Ref().WriteFile(save.GetGameSave()->Serialise(), finalFilename))
new ErrorMessage("Error", "Unable to write save file.");
else
{
callback->FileSaved(&save);
Exit();
}
}
void LocalSaveActivity::OnDraw()