* 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 fop Save or load mode. Load can also be a TTD(Patch) game. * @param sb The sub directory to save the savegame in * @param threaded True when threaded saving is allowed * @return R
| 3256 | * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game) |
| 3257 | */ |
| 3258 | SaveOrLoadResult SaveOrLoad(std::string_view filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded) |
| 3259 | { |
| 3260 | /* An instance of saving is already active, so don't go saving again */ |
| 3261 | if (_sl.saveinprogress && fop == SLO_SAVE && dft == DFT_GAME_FILE && threaded) { |
| 3262 | /* if not an autosave, but a user action, show error message */ |
| 3263 | if (!_do_autosave) ShowErrorMessage(GetEncodedString(STR_ERROR_SAVE_STILL_IN_PROGRESS), {}, WL_ERROR); |
| 3264 | return SL_OK; |
| 3265 | } |
| 3266 | WaitTillSaved(); |
| 3267 | |
| 3268 | try { |
| 3269 | /* Load a TTDLX or TTDPatch game */ |
| 3270 | if (fop == SLO_LOAD && dft == DFT_OLD_GAME_FILE) { |
| 3271 | ResetSaveloadData(); |
| 3272 | |
| 3273 | InitializeGame(256, 256, true, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused |
| 3274 | |
| 3275 | /* TTD/TTO savegames have no NewGRFs, TTDP savegame have them |
| 3276 | * and if so a new NewGRF list will be made in LoadOldSaveGame. |
| 3277 | * Note: this is done here because AfterLoadGame is also called |
| 3278 | * for OTTD savegames which have their own NewGRF logic. */ |
| 3279 | ClearGRFConfigList(_grfconfig); |
| 3280 | _gamelog.Reset(); |
| 3281 | if (!LoadOldSaveGame(filename)) return SL_REINIT; |
| 3282 | _sl_version = SL_MIN_VERSION; |
| 3283 | _sl_minor_version = 0; |
| 3284 | _gamelog.StartAction(GLAT_LOAD); |
| 3285 | if (!AfterLoadGame()) { |
| 3286 | _gamelog.StopAction(); |
| 3287 | return SL_REINIT; |
| 3288 | } |
| 3289 | _gamelog.StopAction(); |
| 3290 | return SL_OK; |
| 3291 | } |
| 3292 | |
| 3293 | assert(dft == DFT_GAME_FILE); |
| 3294 | switch (fop) { |
| 3295 | case SLO_CHECK: |
| 3296 | _sl.action = SLA_LOAD_CHECK; |
| 3297 | break; |
| 3298 | |
| 3299 | case SLO_LOAD: |
| 3300 | _sl.action = SLA_LOAD; |
| 3301 | break; |
| 3302 | |
| 3303 | case SLO_SAVE: |
| 3304 | _sl.action = SLA_SAVE; |
| 3305 | break; |
| 3306 | |
| 3307 | default: NOT_REACHED(); |
| 3308 | } |
| 3309 | |
| 3310 | auto fh = (fop == SLO_SAVE) ? FioFOpenFile(filename, "wb", sb) : FioFOpenFile(filename, "rb", sb); |
| 3311 | |
| 3312 | /* Make it a little easier to load savegames from the console */ |
| 3313 | if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SAVE_DIR); |
| 3314 | if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", BASE_DIR); |
| 3315 | if (!fh.has_value() && fop != SLO_SAVE) fh = FioFOpenFile(filename, "rb", SCENARIO_DIR); |
no test coverage detected