(svn r27650) -Codechange: Replace SaveOrLoadMode by FileOperation and DetailedFileType.

This commit is contained in:
alberth 2016-09-04 12:57:43 +00:00
parent df9a9f074a
commit 597380e099
16 changed files with 114 additions and 101 deletions

View File

@ -318,7 +318,7 @@ DEF_CONSOLE_CMD(ConSave)
char *filename = str_fmt("%s.sav", argv[1]); char *filename = str_fmt("%s.sav", argv[1]);
IConsolePrint(CC_DEFAULT, "Saving map..."); IConsolePrint(CC_DEFAULT, "Saving map...");
if (SaveOrLoad(filename, SL_SAVE, SAVE_DIR) != SL_OK) { if (SaveOrLoad(filename, FOP_SAVE, DFT_GAME_FILE, SAVE_DIR) != SL_OK) {
IConsolePrint(CC_ERROR, "Saving map failed"); IConsolePrint(CC_ERROR, "Saving map failed");
} else { } else {
IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename); IConsolePrintF(CC_DEFAULT, "Map successfully saved to %s", filename);

View File

@ -388,7 +388,7 @@ bool CrashLog::WriteSavegame(char *filename, const char *filename_last) const
seprintf(filename, filename_last, "%scrash.sav", _personal_dir); seprintf(filename, filename_last, "%scrash.sav", _personal_dir);
/* Don't do a threaded saveload. */ /* Don't do a threaded saveload. */
return SaveOrLoad(filename, SL_SAVE, NO_DIRECTORY, false) == SL_OK; return SaveOrLoad(filename, FOP_SAVE, DFT_GAME_FILE, NO_DIRECTORY, false) == SL_OK;
} catch (...) { } catch (...) {
return false; return false;
} }

View File

@ -47,8 +47,11 @@ enum DetailedFileType {
/** Operation performed on the file. */ /** Operation performed on the file. */
enum FileOperation { enum FileOperation {
FOP_LOAD, ///< File is being loaded. FOP_CHECK, ///< Load file for checking and/or preview.
FOP_SAVE, ///< File is being saved. FOP_LOAD, ///< File is being loaded.
FOP_SAVE, ///< File is being saved.
FOP_INVALID, ///< Unknown file operation.
}; };
/** /**

View File

@ -584,7 +584,7 @@ public:
if (GetDetailedFileType(file->type) == DFT_GAME_FILE) { if (GetDetailedFileType(file->type) == DFT_GAME_FILE) {
/* Other detailed file types cannot be checked before. */ /* Other detailed file types cannot be checked before. */
SaveOrLoad(name, SL_LOAD_CHECK, NO_DIRECTORY, false); SaveOrLoad(name, FOP_CHECK, DFT_GAME_FILE, NO_DIRECTORY, false);
} }
this->InvalidateData(1); this->InvalidateData(1);
@ -777,7 +777,7 @@ void ShowSaveLoadDialog(AbstractFileType abstract_filetype, FileOperation fop)
sld = (abstract_filetype == FT_HEIGHTMAP) ? &_load_heightmap_dialog_desc : &_load_dialog_desc; sld = (abstract_filetype == FT_HEIGHTMAP) ? &_load_heightmap_dialog_desc : &_load_dialog_desc;
} }
_file_to_saveload.filetype = abstract_filetype; _file_to_saveload.abstract_ftype = abstract_filetype;
new SaveLoadWindow(sld, abstract_filetype, fop); new SaveLoadWindow(sld, abstract_filetype, fop);
} }

View File

@ -204,7 +204,7 @@ static void _GenerateWorld(void *)
if (_debug_desync_level > 0) { if (_debug_desync_level > 0) {
char name[MAX_PATH]; char name[MAX_PATH];
seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date); seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false); SaveOrLoad(name, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false);
} }
} catch (...) { } catch (...) {
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true); BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true);

View File

@ -833,7 +833,7 @@ static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode)
if (mode == GLWM_HEIGHTMAP) { if (mode == GLWM_HEIGHTMAP) {
/* If the function returns negative, it means there was a problem loading the heightmap */ /* If the function returns negative, it means there was a problem loading the heightmap */
if (!GetHeightmapDimensions(_file_to_saveload.name, &x, &y)) return; if (!GetHeightmapDimensions(_file_to_saveload.detail_ftype, _file_to_saveload.name, &x, &y)) return;
} }
WindowDesc *desc = (mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc; WindowDesc *desc = (mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc;

View File

@ -102,7 +102,7 @@ static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop
* If map == NULL only the size of the PNG is read, otherwise a map * If map == NULL only the size of the PNG is read, otherwise a map
* with grayscale pixels is allocated and assigned to *map. * with grayscale pixels is allocated and assigned to *map.
*/ */
static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map) static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, byte **map)
{ {
FILE *fp; FILE *fp;
png_structp png_ptr = NULL; png_structp png_ptr = NULL;
@ -232,7 +232,7 @@ static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
* If map == NULL only the size of the BMP is read, otherwise a map * If map == NULL only the size of the BMP is read, otherwise a map
* with grayscale pixels is allocated and assigned to *map. * with grayscale pixels is allocated and assigned to *map.
*/ */
static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map) static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, byte **map)
{ {
FILE *f; FILE *f;
BmpInfo info; BmpInfo info;
@ -444,45 +444,56 @@ void FixSlopes()
} }
/** /**
* Reads the heightmap with the correct file reader * Reads the heightmap with the correct file reader.
* @param dft Type of image file.
* @param filename Name of the file to load.
* @param [out] x Length of the image.
* @param [out] y Height of the image.
* @param [inout] map If not \c NULL, destination to store the loaded block of image data.
* @return Whether loading was successful.
*/ */
static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map) static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map)
{ {
switch (_file_to_saveload.mode) { switch (dft) {
default: NOT_REACHED(); default:
NOT_REACHED();
#ifdef WITH_PNG #ifdef WITH_PNG
case SL_PNG: case DFT_HEIGHTMAP_PNG:
return ReadHeightmapPNG(filename, x, y, map); return ReadHeightmapPNG(filename, x, y, map);
#endif /* WITH_PNG */ #endif /* WITH_PNG */
case SL_BMP:
case DFT_HEIGHTMAP_BMP:
return ReadHeightmapBMP(filename, x, y, map); return ReadHeightmapBMP(filename, x, y, map);
} }
} }
/** /**
* Get the dimensions of a heightmap. * Get the dimensions of a heightmap.
* @param dft Type of image file.
* @param filename to query * @param filename to query
* @param x dimension x * @param x dimension x
* @param y dimension y * @param y dimension y
* @return Returns false if loading of the image failed. * @return Returns false if loading of the image failed.
*/ */
bool GetHeightmapDimensions(char *filename, uint *x, uint *y) bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
{ {
return ReadHeightMap(filename, x, y, NULL); return ReadHeightMap(dft, filename, x, y, NULL);
} }
/** /**
* Load a heightmap from file and change the map in his current dimensions * Load a heightmap from file and change the map in his current dimensions
* to a landscape representing the heightmap. * to a landscape representing the heightmap.
* It converts pixels to height. The brighter, the higher. * It converts pixels to height. The brighter, the higher.
* @param dft Type of image file.
* @param filename of the heightmap file to be imported * @param filename of the heightmap file to be imported
*/ */
void LoadHeightmap(char *filename) void LoadHeightmap(DetailedFileType dft, const char *filename)
{ {
uint x, y; uint x, y;
byte *map = NULL; byte *map = NULL;
if (!ReadHeightMap(filename, &x, &y, &map)) { if (!ReadHeightMap(dft, filename, &x, &y, &map)) {
free(map); free(map);
return; return;
} }

View File

@ -12,6 +12,8 @@
#ifndef HEIGHTMAP_H #ifndef HEIGHTMAP_H
#define HEIGHTMAP_H #define HEIGHTMAP_H
#include "fileio_type.h"
/** /**
* Order of these enums has to be the same as in lang/english.txt * Order of these enums has to be the same as in lang/english.txt
* Otherwise you will get inconsistent behaviour. * Otherwise you will get inconsistent behaviour.
@ -21,8 +23,8 @@ enum HeightmapRotation {
HM_CLOCKWISE, ///< Rotate the map clockwise 45 degrees HM_CLOCKWISE, ///< Rotate the map clockwise 45 degrees
}; };
bool GetHeightmapDimensions(char *filename, uint *x, uint *y); bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y);
void LoadHeightmap(char *filename); void LoadHeightmap(DetailedFileType dft, const char *filename);
void FlatEmptyWorld(byte tile_height); void FlatEmptyWorld(byte tile_height);
void FixSlopes(); void FixSlopes();

View File

@ -1222,7 +1222,7 @@ void GenerateLandscape(byte mode)
if (mode == GWM_HEIGHTMAP) { if (mode == GWM_HEIGHTMAP) {
SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_HEIGHTMAP); SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_HEIGHTMAP);
LoadHeightmap(_file_to_saveload.name); LoadHeightmap(_file_to_saveload.detail_ftype, _file_to_saveload.name);
IncreaseGeneratingWorldProgress(GWP_LANDSCAPE); IncreaseGeneratingWorldProgress(GWP_LANDSCAPE);
} else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) { } else if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS) {
SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_TERRAGENESIS); SetGeneratingWorldProgress(GWP_LANDSCAPE, steps + GLS_TERRAGENESIS);

View File

@ -520,7 +520,7 @@ bool ClientNetworkGameSocketHandler::IsConnected()
* DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p * DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
************/ ************/
extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); extern bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p) NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p)
{ {
@ -836,7 +836,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
/* The map is done downloading, load it */ /* The map is done downloading, load it */
ClearErrorMessages(); ClearErrorMessages();
bool load_success = SafeLoad(NULL, SL_LOAD, GM_NORMAL, NO_DIRECTORY, lf); bool load_success = SafeLoad(NULL, FOP_LOAD, DFT_GAME_FILE, GM_NORMAL, NO_DIRECTORY, lf);
/* Long savegame loads shouldn't affect the lag calculation! */ /* Long savegame loads shouldn't affect the lag calculation! */
this->last_packet = _realtime_tick; this->last_packet = _realtime_tick;

View File

@ -328,7 +328,7 @@ static void LoadIntroGame(bool load_newgrfs = true)
SetupColoursAndInitialWindow(); SetupColoursAndInitialWindow();
/* Load the default opening screen savegame */ /* Load the default opening screen savegame */
if (SaveOrLoad("opntitle.dat", SL_LOAD, BASESET_DIR) != SL_OK) { if (SaveOrLoad("opntitle.dat", FOP_LOAD, DFT_GAME_FILE, BASESET_DIR) != SL_OK) {
GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world. GenerateWorld(GWM_EMPTY, 64, 64); // if failed loading, make empty world.
WaitTillGeneratedWorld(); WaitTillGeneratedWorld();
SetLocalCompany(COMPANY_SPECTATOR); SetLocalCompany(COMPANY_SPECTATOR);
@ -619,8 +619,9 @@ int openttd_main(int argc, char *argv[])
case 'g': case 'g':
if (mgo.opt != NULL) { if (mgo.opt != NULL) {
strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name)); strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name));
_switch_mode = (_switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_LOAD_GAME); bool is_scenario = _switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO;
_file_to_saveload.mode = SL_LOAD; _switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
_file_to_saveload.SetMode(FOP_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE);
/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */ /* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
const char *t = strrchr(_file_to_saveload.name, '.'); const char *t = strrchr(_file_to_saveload.name, '.');
@ -650,7 +651,7 @@ int openttd_main(int argc, char *argv[])
FiosGetSavegameListCallback(FOP_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title)); FiosGetSavegameListCallback(FOP_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title));
_load_check_data.Clear(); _load_check_data.Clear();
SaveOrLoadResult res = SaveOrLoad(mgo.opt, SL_LOAD_CHECK, SAVE_DIR, false); SaveOrLoadResult res = SaveOrLoad(mgo.opt, FOP_CHECK, DFT_GAME_FILE, SAVE_DIR, false);
if (res != SL_OK || _load_check_data.HasErrors()) { if (res != SL_OK || _load_check_data.HasErrors()) {
fprintf(stderr, "Failed to open savegame\n"); fprintf(stderr, "Failed to open savegame\n");
if (_load_check_data.HasErrors()) { if (_load_check_data.HasErrors()) {
@ -997,14 +998,15 @@ static void MakeNewEditorWorld()
* @param subdir default directory to look for filename, set to 0 if not needed * @param subdir default directory to look for filename, set to 0 if not needed
* @param lf Load filter to use, if NULL: use filename + subdir. * @param lf Load filter to use, if NULL: use filename + subdir.
*/ */
bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL) bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL)
{ {
assert(mode == SL_LOAD || (lf == NULL && mode == SL_OLD_LOAD)); assert(fop == FOP_LOAD);
assert(dft == DFT_GAME_FILE || (lf == NULL && dft == DFT_OLD_GAME_FILE));
GameMode ogm = _game_mode; GameMode ogm = _game_mode;
_game_mode = newgm; _game_mode = newgm;
switch (lf == NULL ? SaveOrLoad(filename, mode, subdir) : LoadWithFilter(lf)) { switch (lf == NULL ? SaveOrLoad(filename, fop, dft, subdir) : LoadWithFilter(lf)) {
case SL_OK: return true; case SL_OK: return true;
case SL_REINIT: case SL_REINIT:
@ -1093,11 +1095,11 @@ void SwitchToMode(SwitchMode new_mode)
ResetGRFConfig(true); ResetGRFConfig(true);
ResetWindowSystem(); ResetWindowSystem();
if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, NO_DIRECTORY)) { if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, NO_DIRECTORY)) {
SetDParamStr(0, GetSaveLoadErrorString()); SetDParamStr(0, GetSaveLoadErrorString());
ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
} else { } else {
if (_file_to_saveload.filetype == FT_SCENARIO) { if (_file_to_saveload.abstract_ftype == FT_SCENARIO) {
/* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */ /* Reset engine pool to simplify changing engine NewGRFs in scenario editor. */
EngineOverrideManager::ResetToCurrentNewGRFConfig(); EngineOverrideManager::ResetToCurrentNewGRFConfig();
} }
@ -1134,7 +1136,7 @@ void SwitchToMode(SwitchMode new_mode)
break; break;
case SM_LOAD_SCENARIO: { // Load scenario from scenario editor case SM_LOAD_SCENARIO: { // Load scenario from scenario editor
if (SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR, NO_DIRECTORY)) { if (SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_EDITOR, NO_DIRECTORY)) {
SetLocalCompany(OWNER_NONE); SetLocalCompany(OWNER_NONE);
_settings_newgame.game_creation.starting_year = _cur_year; _settings_newgame.game_creation.starting_year = _cur_year;
/* Cancel the saveload pausing */ /* Cancel the saveload pausing */
@ -1156,7 +1158,7 @@ void SwitchToMode(SwitchMode new_mode)
case SM_SAVE_GAME: // Save game. case SM_SAVE_GAME: // Save game.
/* Make network saved games on pause compatible to singleplayer */ /* Make network saved games on pause compatible to singleplayer */
if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, NO_DIRECTORY) != SL_OK) { if (SaveOrLoad(_file_to_saveload.name, FOP_SAVE, DFT_GAME_FILE, NO_DIRECTORY) != SL_OK) {
SetDParamStr(0, GetSaveLoadErrorString()); SetDParamStr(0, GetSaveLoadErrorString());
ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR); ShowErrorMessage(STR_JUST_RAW_STRING, INVALID_STRING_ID, WL_ERROR);
} else { } else {
@ -1367,7 +1369,7 @@ void StateGameLoop()
/* Save the desync savegame if needed. */ /* Save the desync savegame if needed. */
char name[MAX_PATH]; char name[MAX_PATH];
seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date); seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
SaveOrLoad(name, SL_SAVE, AUTOSAVE_DIR, false); SaveOrLoad(name, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false);
} }
CheckCaches(); CheckCaches();
@ -1424,7 +1426,7 @@ static void DoAutosave()
} }
DEBUG(sl, 2, "Autosaving to '%s'", buf); DEBUG(sl, 2, "Autosaving to '%s'", buf);
if (SaveOrLoad(buf, SL_SAVE, AUTOSAVE_DIR) != SL_OK) { if (SaveOrLoad(buf, FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) {
ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR); ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR);
} }
} }

View File

@ -248,7 +248,7 @@ static void InitializeWindowsAndCaches()
/* For each company, verify (while loading a scenario) that the inauguration date is the current year and set it /* For each company, verify (while loading a scenario) that the inauguration date is the current year and set it
* accordingly if it is not the case. No need to set it on companies that are not been used already, * accordingly if it is not the case. No need to set it on companies that are not been used already,
* thus the MIN_YEAR (which is really nothing more than Zero, initialized value) test */ * thus the MIN_YEAR (which is really nothing more than Zero, initialized value) test */
if (_file_to_saveload.filetype == FT_SCENARIO && c->inaugurated_year != MIN_YEAR) { if (_file_to_saveload.abstract_ftype == FT_SCENARIO && c->inaugurated_year != MIN_YEAR) {
c->inaugurated_year = _cur_year; c->inaugurated_year = _cur_year;
} }
} }

View File

@ -2778,10 +2778,10 @@ SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
* @param threaded True when threaded saving is allowed * @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) * @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) SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded)
{ {
/* An instance of saving is already active, so don't go saving again */ /* An instance of saving is already active, so don't go saving again */
if (_sl.saveinprogress && mode == SL_SAVE && threaded) { if (_sl.saveinprogress && fop == FOP_SAVE && dft == DFT_GAME_FILE && threaded) {
/* if not an autosave, but a user action, show error message */ /* 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); if (!_do_autosave) ShowErrorMessage(STR_ERROR_SAVE_STILL_IN_PROGRESS, INVALID_STRING_ID, WL_ERROR);
return SL_OK; return SL_OK;
@ -2790,7 +2790,7 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
try { try {
/* Load a TTDLX or TTDPatch game */ /* Load a TTDLX or TTDPatch game */
if (mode == SL_OLD_LOAD) { if (fop == FOP_LOAD && dft == DFT_OLD_GAME_FILE) {
InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused 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 /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them
@ -2811,25 +2811,35 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
return SL_OK; return SL_OK;
} }
switch (mode) { assert(dft == DFT_GAME_FILE);
case SL_LOAD_CHECK: _sl.action = SLA_LOAD_CHECK; break; switch (dft) {
case SL_LOAD: _sl.action = SLA_LOAD; break; case FOP_CHECK:
case SL_SAVE: _sl.action = SLA_SAVE; break; _sl.action = SLA_LOAD_CHECK;
break;
case FOP_LOAD:
_sl.action = SLA_LOAD;
break;
case FOP_SAVE:
_sl.action = SLA_SAVE;
break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
FILE *fh = (mode == SL_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); FILE *fh = (fop == FOP_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb);
/* Make it a little easier to load savegames from the console */ /* 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 && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR);
if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR); if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR);
if (fh == NULL && mode != SL_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR); if (fh == NULL && fop != FOP_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR);
if (fh == NULL) { if (fh == NULL) {
SlError(mode == SL_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); SlError(fop == FOP_SAVE ? STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE : STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE);
} }
if (mode == SL_SAVE) { // SAVE game if (fop == FOP_SAVE) { // SAVE game
DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename); DEBUG(desync, 1, "save: %08x; %02x; %s", _date, _date_fract, filename);
if (_network_server || !_settings_client.gui.threaded_saves) threaded = false; if (_network_server || !_settings_client.gui.threaded_saves) threaded = false;
@ -2837,24 +2847,25 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
} }
/* LOAD game */ /* LOAD game */
assert(mode == SL_LOAD || mode == SL_LOAD_CHECK); assert(fop == FOP_LOAD || fop == FOP_CHECK);
DEBUG(desync, 1, "load: %s", filename); DEBUG(desync, 1, "load: %s", filename);
return DoLoad(new FileReader(fh), mode == SL_LOAD_CHECK); return DoLoad(new FileReader(fh), fop == FOP_CHECK);
} catch (...) { } catch (...) {
/* This code may be executed both for old and new save games. */
ClearSaveLoadState(); ClearSaveLoadState();
/* Skip the "colour" character */ /* Skip the "colour" character */
if (mode != SL_LOAD_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3); if (fop != FOP_CHECK) DEBUG(sl, 0, "%s", GetSaveLoadErrorString() + 3);
/* A saver/loader exception!! reinitialize all variables to prevent crash! */ /* A saver/loader exception!! reinitialize all variables to prevent crash! */
return (mode == SL_LOAD || mode == SL_OLD_LOAD) ? SL_REINIT : SL_ERROR; return (fop == FOP_LOAD) ? SL_REINIT : SL_ERROR;
} }
} }
/** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */ /** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */
void DoExitSave() void DoExitSave()
{ {
SaveOrLoad("exit.sav", SL_SAVE, AUTOSAVE_DIR); SaveOrLoad("exit.sav", FOP_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR);
} }
/** /**
@ -2898,34 +2909,27 @@ void GenerateDefaultSaveName(char *buf, const char *last)
*/ */
void FileToSaveLoad::SetMode(FiosType ft) void FileToSaveLoad::SetMode(FiosType ft)
{ {
switch (ft) { this->SetMode(FOP_LOAD, GetAbstractFileType(ft), GetDetailedFileType(ft));
case FIOS_TYPE_FILE: }
case FIOS_TYPE_SCENARIO:
this->mode = SL_LOAD;
break;
case FIOS_TYPE_OLDFILE: /**
case FIOS_TYPE_OLD_SCENARIO: * Set the mode and file type of the file to save or load.
this->mode = SL_OLD_LOAD; * @param fop File operation being performed.
break; * @param aft Abstract file type.
* @param dft Detailed file type.
#ifdef WITH_PNG */
case FIOS_TYPE_PNG: void FileToSaveLoad::SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft)
this->mode = SL_PNG; {
break; if (aft == FT_INVALID || aft == FT_NONE) {
#endif /* WITH_PNG */ this->file_op = FOP_INVALID;
this->detail_ftype = DFT_INVALID;
case FIOS_TYPE_BMP: this->abstract_ftype = FT_INVALID;
this->mode = SL_BMP; return;
break;
default:
this->mode = SL_INVALID;
break;
} }
this->filetype = GetAbstractFileType(ft); this->file_op = fop;
if (this->filetype == FT_NONE) this->filetype = FT_INVALID; this->detail_ftype = dft;
this->abstract_ftype = aft;
} }
#if 0 #if 0

View File

@ -22,25 +22,16 @@ enum SaveOrLoadResult {
SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load) SL_REINIT = 2, ///< error that was caught in the middle of updating game state, need to clear it. (can only happen during load)
}; };
/** Save or load mode. @see SaveOrLoad */
enum SaveOrLoadMode {
SL_INVALID = -1, ///< Invalid mode.
SL_LOAD = 0, ///< Load game.
SL_SAVE = 1, ///< Save game.
SL_OLD_LOAD = 2, ///< Load old game.
SL_PNG = 3, ///< Load PNG file (height map).
SL_BMP = 4, ///< Load BMP file (height map).
SL_LOAD_CHECK = 5, ///< Load for game preview.
};
/** Deals with the type of the savegame, independent of extension */ /** Deals with the type of the savegame, independent of extension */
struct FileToSaveLoad { struct FileToSaveLoad {
SaveOrLoadMode mode; ///< savegame/scenario type (old, new) FileOperation file_op; ///< File operation to perform.
AbstractFileType filetype; ///< what type of file are we dealing with DetailedFileType detail_ftype; ///< Concrete file type (PNG, BMP, old save, etc).
char name[MAX_PATH]; ///< name AbstractFileType abstract_ftype; ///< Abstract type of file (scenario, heightmap, etc).
char title[255]; ///< internal name of the game char name[MAX_PATH]; ///< Name of the file.
char title[255]; ///< Internal name of the game.
void SetMode(FiosType ft); void SetMode(FiosType ft);
void SetMode(FileOperation fop, AbstractFileType aft, DetailedFileType dft);
}; };
/** Types of save games. */ /** Types of save games. */
@ -58,7 +49,7 @@ extern FileToSaveLoad _file_to_saveload;
void GenerateDefaultSaveName(char *buf, const char *last); void GenerateDefaultSaveName(char *buf, const char *last);
void SetSaveLoadError(uint16 str); void SetSaveLoadError(uint16 str);
const char *GetSaveLoadErrorString(); const char *GetSaveLoadErrorString();
SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, bool threaded = true); SaveOrLoadResult SaveOrLoad(const char *filename, FileOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded = true);
void WaitTillSaved(); void WaitTillSaved();
void ProcessAsyncSaveFinish(); void ProcessAsyncSaveFinish();
void DoExitSave(); void DoExitSave();

View File

@ -60,7 +60,7 @@ static void Load_SIGN()
} }
/* Signs placed in scenario editor shall now be OWNER_DEITY */ /* Signs placed in scenario editor shall now be OWNER_DEITY */
if (IsSavegameVersionBefore(171) && si->owner == OWNER_NONE && _file_to_saveload.filetype == FT_SCENARIO) { if (IsSavegameVersionBefore(171) && si->owner == OWNER_NONE && _file_to_saveload.abstract_ftype == FT_SCENARIO) {
si->owner = OWNER_DEITY; si->owner = OWNER_DEITY;
} }
} }

View File

@ -142,7 +142,7 @@ static void *_dedicated_video_mem;
/* Whether a fork has been done. */ /* Whether a fork has been done. */
bool _dedicated_forks; bool _dedicated_forks;
extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); extern bool SafeLoad(const char *filename, FileOperation fop, DetailedFileType dft, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL);
static FVideoDriver_Dedicated iFVideoDriver_Dedicated; static FVideoDriver_Dedicated iFVideoDriver_Dedicated;
@ -286,7 +286,7 @@ void VideoDriver_Dedicated::MainLoop()
_switch_mode = SM_NONE; _switch_mode = SM_NONE;
/* First we need to test if the savegame can be loaded, else we will end up playing the /* First we need to test if the savegame can be loaded, else we will end up playing the
* intro game... */ * intro game... */
if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) { if (!SafeLoad(_file_to_saveload.name, _file_to_saveload.file_op, _file_to_saveload.detail_ftype, GM_NORMAL, BASE_DIR)) {
/* Loading failed, pop out.. */ /* Loading failed, pop out.. */
DEBUG(net, 0, "Loading requested map failed, aborting"); DEBUG(net, 0, "Loading requested map failed, aborting");
_networking = false; _networking = false;