Exception when loading invalid save

This commit is contained in:
Simon Robertshaw 2012-03-29 00:59:10 +01:00
parent e9770d8ee7
commit 1f388e4ca0
4 changed files with 46 additions and 4 deletions

View File

@ -10,6 +10,7 @@
#include "login/LoginController.h" #include "login/LoginController.h"
#include "interface/Point.h" #include "interface/Point.h"
#include "dialogues/ErrorMessage.h" #include "dialogues/ErrorMessage.h"
#include "SaveLoadException.h"
using namespace std; using namespace std;
@ -33,9 +34,16 @@ public:
virtual void ControllerExit() virtual void ControllerExit()
{ {
if(cc->search->GetLoadedSave()) if(cc->search->GetLoadedSave())
{
try
{ {
cc->gameModel->SetSave(new Save(*(cc->search->GetLoadedSave()))); cc->gameModel->SetSave(new Save(*(cc->search->GetLoadedSave())));
} }
catch(SaveLoadException & ex)
{
new ErrorMessage("Cannot open save", ex.what());
}
}
} }
}; };
@ -61,6 +69,7 @@ public:
if(cc->ssave->GetSaveUploaded()) if(cc->ssave->GetSaveUploaded())
{ {
cc->gameModel->SetSave(new Save(*(cc->ssave->GetSave()))); cc->gameModel->SetSave(new Save(*(cc->ssave->GetSave())));
} }
//cc->gameModel->SetUser(cc->loginWindow->GetUser()); //cc->gameModel->SetUser(cc->loginWindow->GetUser());
} }

View File

@ -8,6 +8,7 @@
#include "EllipseBrush.h" #include "EllipseBrush.h"
#include "client/Client.h" #include "client/Client.h"
#include "game/DecorationTool.h" #include "game/DecorationTool.h"
#include "SaveLoadException.h"
GameModel::GameModel(): GameModel::GameModel():
activeTools({NULL, NULL, NULL}), activeTools({NULL, NULL, NULL}),
@ -257,7 +258,12 @@ void GameModel::SetSave(Save * newSave)
currentSave = newSave; currentSave = newSave;
if(currentSave) if(currentSave)
{ {
sim->Load(currentSave->GetData(), currentSave->GetDataLength()); int returnVal = sim->Load(currentSave->GetData(), currentSave->GetDataLength());
if(returnVal){
delete currentSave;
currentSave = NULL;
throw SaveLoadException(returnVal==2?"Save from newer version":"Save data corrupt");
}
} }
notifySaveChanged(); notifySaveChanged();
notifyPausedChanged(); notifyPausedChanged();

View File

@ -2,6 +2,7 @@
#include "Config.h" #include "Config.h"
#include "GameView.h" #include "GameView.h"
#include "Graphics.h"
#include "interface/Window.h" #include "interface/Window.h"
#include "interface/Button.h" #include "interface/Button.h"
#include "interface/Colour.h" #include "interface/Colour.h"
@ -482,7 +483,7 @@ void GameView::OnMouseDown(int x, int y, unsigned button)
{ {
if(selectMode!=SelectNone) if(selectMode!=SelectNone)
{ {
if(button!=3) if(button==BUTTON_LEFT)
{ {
selectPoint1 = ui::Point(x, y); selectPoint1 = ui::Point(x, y);
selectPoint2 = selectPoint1; selectPoint2 = selectPoint1;
@ -517,7 +518,7 @@ void GameView::OnMouseUp(int x, int y, unsigned button)
int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y; int y2 = (selectPoint1.Y>selectPoint2.Y)?selectPoint1.Y:selectPoint2.Y;
int x1 = (selectPoint2.X<selectPoint1.X)?selectPoint2.X:selectPoint1.X; int x1 = (selectPoint2.X<selectPoint1.X)?selectPoint2.X:selectPoint1.X;
int y1 = (selectPoint2.Y<selectPoint1.Y)?selectPoint2.Y:selectPoint1.Y; int y1 = (selectPoint2.Y<selectPoint1.Y)?selectPoint2.Y:selectPoint1.Y;
if(button !=3 && x2-x1>0 && y2-y1>0) if(button==BUTTON_LEFT && x2-x1>0 && y2-y1>0)
{ {
if(selectMode==SelectCopy) if(selectMode==SelectCopy)
c->CopyRegion(ui::Point(x1, y1), ui::Point(x2, y2)); c->CopyRegion(ui::Point(x1, y1), ui::Point(x2, y2));

View File

@ -0,0 +1,26 @@
/*
* SaveLoadException.h
*
* Created on: Mar 29, 2012
* Author: Simon
*/
#ifndef SAVELOADEXCEPTION_H_
#define SAVELOADEXCEPTION_H_
#include <string>
#include <exception>
using namespace std;
struct SaveLoadException: public exception {
string message;
public:
SaveLoadException(string message_): message(message_) {}
const char * what() const throw()
{
return message.c_str();
}
~SaveLoadException() throw() {};
};
#endif /* SAVELOADEXCEPTION_H_ */