MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / SaveOrLoad

Function SaveOrLoad

src/saveload/saveload.cpp:3258–3342  ·  view source on GitHub ↗

* 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

Source from the content-addressed store, hash-verified

3256 * @return Return the result of the action. #SL_OK, #SL_ERROR, or #SL_REINIT ("unload" the game)
3257 */
3258SaveOrLoadResult 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);

Callers 11

OnClickMethod · 0.85
LoadIntroGameFunction · 0.85
openttd_mainFunction · 0.85
SafeLoadFunction · 0.85
SwitchToModeFunction · 0.85
StateGameLoopFunction · 0.85
ConSaveFunction · 0.85
_GenerateWorldFunction · 0.85
WriteSavegameMethod · 0.85
DoAutoOrNetsaveFunction · 0.85
DoExitSaveFunction · 0.85

Calls 15

ShowErrorMessageFunction · 0.85
WaitTillSavedFunction · 0.85
ResetSaveloadDataFunction · 0.85
InitializeGameFunction · 0.85
ClearGRFConfigListFunction · 0.85
LoadOldSaveGameFunction · 0.85
AfterLoadGameFunction · 0.85
NOT_REACHEDFunction · 0.85
FioFOpenFileFunction · 0.85
SlErrorFunction · 0.85
DoSaveFunction · 0.85
DoLoadFunction · 0.85

Tested by

no test coverage detected