1
0
Fork 0

(svn r21394) -Codechange: split the actual savegame loading from SaveOrLoad as well

release/1.1
rubidium 2010-12-05 14:37:54 +00:00
parent da76d2f46f
commit 5ff58155d1
1 changed files with 147 additions and 135 deletions

View File

@ -2490,6 +2490,7 @@ void WaitTillSaved()
* using the writer, either in threaded mode if possible, or single-threaded. * using the writer, either in threaded mode if possible, or single-threaded.
* @param writer The filter to write the savegame to. * @param writer The filter to write the savegame to.
* @param threaded Whether to try to perform the saving asynchroniously. * @param threaded Whether to try to perform the saving asynchroniously.
* @return Return the result of the action. #SL_OK or #SL_ERROR
*/ */
static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded) static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
{ {
@ -2517,85 +2518,23 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
} }
/** /**
* Main Save or Load function where the high-level saveload functions are * Actually perform the loading of a "non-old" savegame.
* handled. It opens the savegame, selects format and checks versions * @param reader The filter to read the savegame from.
* @param filename The name of the savegame being created/loaded * @param load_check Whether to perform the checking ("preview") or actually load the game.
* @param mode Save or load mode. Load can also be a TTD(Patch) game. Use #SL_LOAD, #SL_OLD_LOAD, #SL_LOAD_CHECK, or #SL_SAVE. * @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
* @param sb The sub directory to save the savegame in
* @param threaded True when threaded saving is allowed
* @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
*/ */
SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded) static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
{ {
uint32 hdr[2]; _sl.lf = reader;
/* An instance of saving is already active, so don't go saving again */
if (_sl.saveinprogress && mode == SL_SAVE) {
/* if not an autosave, but a user action, show error message */
if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
return SL_OK;
}
WaitTillSaved();
if (load_check) {
/* Clear previous check data */ /* Clear previous check data */
if (mode == SL_LOAD_CHECK) _load_check_data.Clear(); _load_check_data.Clear();
_next_offs = 0;
/* Load a TTDLX or TTDPatch game */
if (mode == SL_OLD_LOAD) {
_engine_mngr.ResetToDefaultMapping();
InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
/* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
* and if so a new NewGRF list will be made in LoadOldSaveGame.
* Note: this is done here because AfterLoadGame is also called
* for OTTD savegames which have their own NewGRF logic. */
ClearGRFConfigList(&_grfconfig);
GamelogReset();
if (!LoadOldSaveGame(filename)) return SL_REINIT;
_sl_version = 0;
_sl_minor_version = 0;
GamelogStartAction(GLAT_LOAD);
if (!AfterLoadGame()) {
GamelogStopAction();
return SL_REINIT;
}
GamelogStopAction();
return SL_OK;
}
/* Mark SL_LOAD_CHECK as supported for this savegame. */ /* Mark SL_LOAD_CHECK as supported for this savegame. */
if (mode == SL_LOAD_CHECK) _load_check_data.checkable = true; _load_check_data.checkable = true;
switch (mode) {
case SL_LOAD_CHECK: _sl.action = SLA_LOAD_CHECK; break;
case SL_LOAD: _sl.action = SLA_LOAD; break;
case SL_SAVE: _sl.action = SLA_SAVE; break;
default: NOT_REACHED();
} }
try { uint32 hdr[2];
FILE *fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
/* Make it a little easier to load savegames from the console */
if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
if (fh == NULL) {
SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
}
if (mode == SL_SAVE) { // SAVE game
DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
return DoSave(new FileWriter(fh), threaded);
} else { // LOAD game
assert(mode == SL_LOAD || mode == SL_LOAD_CHECK);
DEBUG(desync, 1, "load: %s", filename);
_sl.lf = new FileReader(fh);
if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); if (_sl.lf->Read((byte*)hdr, sizeof(hdr)) != sizeof(hdr)) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
/* see if we have any loader for this type. */ /* see if we have any loader for this type. */
@ -2649,8 +2588,9 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
_sl.lf = fmt->init_load(_sl.lf); _sl.lf = fmt->init_load(_sl.lf);
_sl.reader = new ReadBuffer(_sl.lf); _sl.reader = new ReadBuffer(_sl.lf);
_next_offs = 0;
if (mode != SL_LOAD_CHECK) { if (!load_check) {
_engine_mngr.ResetToDefaultMapping(); _engine_mngr.ResetToDefaultMapping();
/* Old maps were hardcoded to 256x256 and thus did not contain /* Old maps were hardcoded to 256x256 and thus did not contain
@ -2686,7 +2626,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
} }
} }
if (mode == SL_LOAD_CHECK) { if (load_check) {
/* Load chunks into _load_check_data. /* Load chunks into _load_check_data.
* No pools are loaded. References are not possible, and thus do not need resolving. */ * No pools are loaded. References are not possible, and thus do not need resolving. */
SlLoadCheckChunks(); SlLoadCheckChunks();
@ -2700,7 +2640,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
_savegame_type = SGT_OTTD; _savegame_type = SGT_OTTD;
if (mode == SL_LOAD_CHECK) { if (load_check) {
/* The only part from AfterLoadGame() we need */ /* The only part from AfterLoadGame() we need */
_load_check_data.grf_compatibility = IsGoodGRFConfigList(_load_check_data.grfconfig); _load_check_data.grf_compatibility = IsGoodGRFConfigList(_load_check_data.grfconfig);
} else { } else {
@ -2715,9 +2655,81 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
GamelogStopAction(); GamelogStopAction();
} }
}
return SL_OK; return SL_OK;
}
/**
* Main Save or Load function where the high-level saveload functions are
* handled. It opens the savegame, selects format and checks versions
* @param filename The name of the savegame being created/loaded
* @param mode Save or load mode. Load can also be a TTD(Patch) game. Use #SL_LOAD, #SL_OLD_LOAD, #SL_LOAD_CHECK, or #SL_SAVE.
* @param sb The sub directory to save the savegame in
* @param threaded True when threaded saving is allowed
* @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
*/
SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded)
{
/* An instance of saving is already active, so don't go saving again */
if (_sl.saveinprogress && mode == SL_SAVE) {
/* if not an autosave, but a user action, show error message */
if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
return SL_OK;
}
WaitTillSaved();
/* Load a TTDLX or TTDPatch game */
if (mode == SL_OLD_LOAD) {
_engine_mngr.ResetToDefaultMapping();
InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused
/* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
* and if so a new NewGRF list will be made in LoadOldSaveGame.
* Note: this is done here because AfterLoadGame is also called
* for OTTD savegames which have their own NewGRF logic. */
ClearGRFConfigList(&_grfconfig);
GamelogReset();
if (!LoadOldSaveGame(filename)) return SL_REINIT;
_sl_version = 0;
_sl_minor_version = 0;
GamelogStartAction(GLAT_LOAD);
if (!AfterLoadGame()) {
GamelogStopAction();
return SL_REINIT;
}
GamelogStopAction();
return SL_OK;
}
switch (mode) {
case SL_LOAD_CHECK: _sl.action = SLA_LOAD_CHECK; break;
case SL_LOAD: _sl.action = SLA_LOAD; break;
case SL_SAVE: _sl.action = SLA_SAVE; break;
default: NOT_REACHED();
}
try {
FILE *fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
/* Make it a little easier to load savegames from the console */
if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
if (fh == NULL) {
SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
}
if (mode == SL_SAVE) { // SAVE game
DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
return DoSave(new FileWriter(fh), threaded);
}
/* LOAD game */
assert(mode == SL_LOAD || mode == SL_LOAD_CHECK);
DEBUG(desync, 1, "load: %s", filename);
return DoLoad(new FileReader(fh), mode == SL_LOAD_CHECK);
} catch (...) { } catch (...) {
ClearSaveLoadState(); ClearSaveLoadState();