# saveload.h File Reference

Functions/types related to saving and loading games. More...

`#include "../fileio_type.h"`

Go to the source code of this file.

## Data Structures
struct | ChunkHandler
struct | NullStruct
struct | SaveLoad
| SaveLoad type struct. More...
## Defines
#define | SIZE_MAX ((size_t)-1)
#define | SL_MAX_VERSION 255
#define | SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
#define | SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
#define | SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
#define | SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
#define | SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
#define | SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
#define | SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
#define | SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
#define | SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
#define | SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
#define | SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
#define | SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to)
#define | SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
#define | SLE_VEH_INCLUDE() {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
#define | SLE_ST_INCLUDE() {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL}
#define | SLE_END() {false, SL_END, 0, 0, 0, 0, NULL}
#define | SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
#define | SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
#define | SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
#define | SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
#define | SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
#define | SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
#define | SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
#define | SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
#define | SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
#define | SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
#define | SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
#define | SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_CONFIG_NO, length, from, to, (void*)NULL}
#define | SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
## Typedefs
typedef void | ChunkSaveLoadProc ()
typedef void | AutolengthProc (void *arg)
typedef uint32 | VarType
typedef byte | SaveLoadType
typedef SaveLoad | SaveLoadGlobVarList
## Enumerations
enum | SaveOrLoadResult { SL_OK = 0, SL_ERROR = 1, SL_REINIT = 2 }
enum | SaveOrLoadMode { SL_INVALID = -1, SL_LOAD = 0, SL_SAVE = 1, SL_OLD_LOAD = 2, SL_PNG = 3, SL_BMP = 4 }
enum | SavegameType { SGT_TTD, SGT_TTDP1, SGT_TTDP2, SGT_OTTD, SGT_TTO, SGT_INVALID = 0xFF }
enum | SLRefType { REF_ORDER = 0, REF_VEHICLE = 1, REF_STATION = 2, REF_TOWN = 3, REF_VEHICLE_OLD = 4, REF_ROADSTOPS = 5, REF_ENGINE_RENEWS = 6, REF_CARGO_PACKET = 7, REF_ORDERLIST = 8 }
enum | { INC_VEHICLE_COMMON = 0 }
enum | { CH_RIFF = 0, CH_ARRAY = 1, CH_SPARSE_ARRAY = 2, CH_TYPE_MASK = 3, CH_LAST = 8, CH_AUTO_LENGTH = 16 }
enum | VarTypes { SLE_FILE_I8 = 0, SLE_FILE_U8 = 1, SLE_FILE_I16 = 2, SLE_FILE_U16 = 3, SLE_FILE_I32 = 4, SLE_FILE_U32 = 5, SLE_FILE_I64 = 6, SLE_FILE_U64 = 7, SLE_FILE_STRINGID = 8, SLE_FILE_STRING = 9, SLE_VAR_BL = 0 << 4, SLE_VAR_I8 = 1 << 4, SLE_VAR_U8 = 2 << 4, SLE_VAR_I16 = 3 << 4, SLE_VAR_U16 = 4 << 4, SLE_VAR_I32 = 5 << 4, SLE_VAR_U32 = 6 << 4, SLE_VAR_I64 = 7 << 4, SLE_VAR_U64 = 8 << 4, SLE_VAR_NULL = 9 << 4, SLE_VAR_STRB = 10 << 4, SLE_VAR_STRBQ = 11 << 4, SLE_VAR_STR = 12 << 4, SLE_VAR_STRQ = 13 << 4, SLE_VAR_NAME = 14 << 4, SLE_VAR_CHAR = SLE_VAR_I8, SLE_BOOL = SLE_FILE_I8 | SLE_VAR_BL, SLE_INT8 = SLE_FILE_I8 | SLE_VAR_I8, SLE_UINT8 = SLE_FILE_U8 | SLE_VAR_U8, SLE_INT16 = SLE_FILE_I16 | SLE_VAR_I16, SLE_UINT16 = SLE_FILE_U16 | SLE_VAR_U16, SLE_INT32 = SLE_FILE_I32 | SLE_VAR_I32, SLE_UINT32 = SLE_FILE_U32 | SLE_VAR_U32, SLE_INT64 = SLE_FILE_I64 | SLE_VAR_I64, SLE_UINT64 = SLE_FILE_U64 | SLE_VAR_U64, SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR, SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U16, SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB, SLE_STRINGBQUOTE = SLE_FILE_STRING | SLE_VAR_STRBQ, SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR, SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ, SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME, SLE_UINT = SLE_UINT32, SLE_INT = SLE_INT32, SLE_STRB = SLE_STRINGBUF, SLE_STRBQ = SLE_STRINGBQUOTE, SLE_STR = SLE_STRING, SLE_STRQ = SLE_STRINGQUOTE, SLF_SAVE_NO = 1 << 8, SLF_CONFIG_NO = 1 << 9, SLF_NETWORK_NO = 1 << 10 }
| VarTypes is the general bitmasked magic type that tells us certain characteristics about the variable it refers to. More...
enum | SaveLoadTypes { SL_VAR = 0, SL_REF = 1, SL_ARR = 2, SL_STR = 3, SL_LST = 4, SL_WRITEBYTE = 8, SL_VEH_INCLUDE = 9, SL_ST_INCLUDE = 10, SL_END = 15 }
## Functions
void | GenerateDefaultSaveName (char *buf, const char *last)
| Fill the buffer with the default name for a savegame *or* screenshot.
void | SetSaveLoadError (uint16 str)
const char * | GetSaveLoadErrorString ()
| Get the string representation of the error message.
SaveOrLoadResult | SaveOrLoad (const char *filename, int mode, Subdirectory sb, bool threaded=true)
| Main Save or Load function where the high-level saveload functions are handled.
void | WaitTillSaved ()
void | DoExitSave ()
| Do a save when exiting the game (_settings_client.gui.autosave_on_exit).
static bool | CheckSavegameVersionOldStyle (uint16 major, byte minor)
| Checks if the savegame is below major.minor.
static bool | CheckSavegameVersion (uint16 version)
| Checks if the savegame is below version.
static bool | SlIsObjectCurrentlyValid (uint16 version_from, uint16 version_to)
| Checks if some version from/to combination falls within the range of the active savegame version.
static VarType | GetVarMemType (VarType type)
static VarType | GetVarFileType (VarType type)
static bool | IsNumericType (VarType conv)
| Check if the given saveload type is a numeric type.
static void * | GetVariableAddress (const void *object, const SaveLoad *sld)
| Get the address of the variable.
int64 | ReadValue (const void *ptr, VarType conv)
| Return a signed-long version of the value of a setting.
void | WriteValue (void *ptr, VarType conv, int64 val)
| Write the value of a setting.
void | SlSetArrayIndex (uint index)
int | SlIterateArray ()
| Iterate through the elements of an array and read the whole thing.
void | SlAutolength (AutolengthProc *proc, void *arg)
| Do something of which I have no idea what it is :P.
size_t | SlGetFieldLength ()
void | SlSetLength (size_t length)
| Sets the length of either a RIFF object or the number of items in an array.
size_t | SlCalcObjMemberLength (const void *object, const SaveLoad *sld)
size_t | SlCalcObjLength (const void *object, const SaveLoad *sld)
| Calculate the size of an object.
byte | SlReadByte ()
| Wrapper for SlReadByteInternal.
void | SlWriteByte (byte b)
| Wrapper for SlWriteByteInternal.
void | SlGlobList (const SaveLoadGlobVarList *sldg)
| Save or Load (a list of) global variables.
void | SlArray (void *array, size_t length, VarType conv)
| Save/Load an array.
void | SlObject (void *object, const SaveLoad *sld)
| Main SaveLoad function.
bool | SlObjectMember (void *object, const SaveLoad *sld)
bool | SaveloadCrashWithMissingNewGRFs ()
| Did loading the savegame cause a crash? If so, were NewGRFs missing?
## Variables
char | _savegame_format [8]
| how to compress savegames

## Detailed Description

Functions/types related to saving and loading games.

Definition in file saveload.h.

## Enumeration Type Documentation

enum SavegameType

Enumerator:

  SGT_TTD | TTD savegame (can be detected incorrectly).
  SGT_TTDP1 | TTDP savegame ( -//- ) (data at NW border).
  SGT_TTDP2 | TTDP savegame in new format (data at SE border).
  SGT_OTTD | OTTD savegame.
  SGT_TTO | TTO savegame.
  SGT_INVALID | broken savegame (used internally)

Definition at line 38 of file saveload.h.

enum SaveOrLoadResult

Enumerator:

  SL_OK | completed successfully
  SL_ERROR | error that was caught before internal structures were modified
  SL_REINIT | error that was caught in the middle of updating game state, need to clear it. (can only happen during load)

Definition at line 23 of file saveload.h.

enum VarTypes

VarTypes is the general bitmasked magic type that tells us certain characteristics about the variable it refers to.

For example SLE_FILE_* gives the size(type) as it would be in the savegame and SLE_VAR_* the size(type) as it is in memory during runtime. These are the first 8 bits (0-3 SLE_FILE, 4-7 SLE_VAR). Bits 8-15 are reserved for various flags as explained below

Enumerator:

  SLE_FILE_STRINGID | StringID offset into strings-array.
  SLE_VAR_NULL | useful to write zeros in savegame.
  SLE_VAR_STRB | string (with pre-allocated buffer)
  SLE_VAR_STRBQ | string enclosed in quotes (with pre-allocated buffer)
  SLE_VAR_STR | string pointer
  SLE_VAR_STRQ | string pointer enclosed in quotes
  SLE_VAR_NAME | old custom name to be converted to a char pointer
  SLF_SAVE_NO | do not save with savegame, basically client-based
  SLF_CONFIG_NO | do not save to config file
  SLF_NETWORK_NO | do not synchronize over network (but it is saved if SSF_SAVE_NO is not set)

Definition at line 103 of file saveload.h.

## Function Documentation

void GenerateDefaultSaveName | ( | char * | buf,
|  | const char * | last |
| ) |  |  |

Fill the buffer with the default name for a savegame *or* screenshot.

Parameters:

  | buf | the buffer to write to.
  | last | the last element in the buffer.

Definition at line 2054 of file saveload.cpp.

References _date, _settings_client, GUISettings::date_format_in_default_names, ClientSettings::gui, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), SanitizeFilename(), and SetDParam().

Referenced by DoAutosave().

static void* GetVariableAddress | ( | const void * | object,
|  | const SaveLoad * | sld |
| ) |  |  | `[inline, static]`

Get the address of the variable.

Which one to pick depends on the object pointer. If it is NULL we are dealing with global variables so the address is taken. If non-null only the offset is stored in the union and we need to add this to the address of the object

Definition at line 312 of file saveload.h.

References SaveLoad::address, and SaveLoad::global.

Referenced by CmdChangeCompanySetting(), CmdChangeSetting(), AIGameSettings::GetValue(), HandleOldDiffCustom(), IConsoleGetSetting(), IConsoleListSettings(), ini_load_settings(), ini_save_settings(), LoadSettings(), GameDifficultyWindow::OnClick(), GameDifficultyWindow::OnInvalidateData(), SaveSettings(), SetCompanySetting(), SetDefaultCompanySettings(), SetSettingValue(), GameDifficultyWindow::SetStringParameters(), SlObject(), and SyncCompanySettings().

static bool IsNumericType | ( | VarType | conv | ) | `[inline, static]`

Check if the given saveload type is a numeric type.

Parameters:

  | conv | the type to check

Returns:
  True if it's a numeric type.

Definition at line 303 of file saveload.h.

Referenced by LoadSettings().

int64 ReadValue | ( | const void * | ptr,
|  | VarType | conv |
| ) |  |  |

Return a signed-long version of the value of a setting.

Parameters:

  | ptr | pointer to the variable
  | conv | type of variable, can be a non-clean type, eg one with other flags because it is parsed

Returns:
  returns the value of the pointer-setting

Definition at line 569 of file saveload.cpp.

References SLE_VAR_NULL.

Referenced by CmdChangeCompanySetting(), CmdChangeSetting(), SettingEntry::DrawSetting(), CheatWindow::DrawWidget(), AIGameSettings::GetValue(), IConsoleGetSetting(), IConsoleListSettings(), ini_save_settings(), LoadSettings(), GameSettingsWindow::OnClick(), GameDifficultyWindow::OnClick(), CheatWindow::OnClick(), GameDifficultyWindow::OnInvalidateData(), SetCompanySetting(), SetSettingValue(), GameDifficultyWindow::SetStringParameters(), SlSaveLoadConv(), and SyncCompanySettings().

bool SaveloadCrashWithMissingNewGRFs | ( |  | ) |

Did loading the savegame cause a crash? If so, were NewGRFs missing?

Returns:
  when the saveload crashed due to missing NewGRFs.

Definition at line 324 of file afterload.cpp.

Referenced by HandleCrash().

SaveOrLoadResult SaveOrLoad | ( | const char * | filename,
|  | int | mode,
|  | Subdirectory | sb,
|  | bool | threaded |
| ) |  |  |

Main Save or Load function where the high-level saveload functions are handled.

It opens the savegame, selects format and checks versions

Parameters:

  | filename | The name of the savegame being created/loaded
  | mode | Save or load. Load can also be a TTD(Patch) game. Use SL_LOAD, SL_OLD_LOAD or SL_SAVE
  | sb | The sub directory to save the savegame in
  | threaded | True when threaded saving is allowed

Returns:
  Return the results of the action. SL_OK, SL_ERROR or SL_REINIT ("unload" the game)

Definition at line 1829 of file saveload.cpp.

References _date, _grfconfig, _network_server, _savegame_type, _settings_client, _sl_minor_version, _sl_version, AbortSaveLoad(), SaveLoadParams::action, BASE_DIR, SaveLoadParams::bufe, CheckSavegameVersion(), endof, SaveLoadParams::excpt_uninit, SaveLoadParams::fh, FioFOpenFile(), GamelogReset(), GamelogStartAction(), GamelogStopAction(), GetSaveLoadErrorString(), GLAT_LOAD, ClientSettings::gui, SaveLoadFormat::init_read, INVALID_STRING_ID, lengthof, SaveLoadFormat::name, ThreadObject::New(), SaveLoadParams::offs_base, SaveLoadParams::read_bytes, SaveLoadFormat::reader, EngineOverrideManager::ResetToDefaultMapping(), SAVE_DIR, SaveFileDone(), SaveFileStart(), SaveFileToDisk(), SAVEGAME_VERSION, SGT_OTTD, ShowErrorMessage(), SL_ERROR, SL_OK, SL_REINIT, SLA_LOAD, SLA_SAVE, SlError(), SlFixPointers(), SlLoadChunks(), SlSaveChunks(), SlWriteFill(), SaveLoadFormat::tag, GUISettings::threaded_saves, SaveLoadFormat::uninit_read, and SaveLoadParams::write_bytes.

Referenced by _GenerateWorld(), DoAutosave(), DoExitSave(), SafeSaveOrLoad(), StartScenario(), StateGameLoop(), and CrashLog::WriteSavegame().

void SlArray | ( | void * | array,
|  | size_t | length,
|  | VarType | conv |
| ) |  |  |

Save/Load an array.

Parameters:

  | array | The array being manipulated
  | length | The length of the array in elements
  | conv | VarType type of the atomic array (int, byte, uint64, etc.)

Definition at line 789 of file saveload.cpp.

References _sl_version, SaveLoadParams::action, BSWAP32(), SaveLoadParams::need_length, NL_CALCLENGTH, NL_NONE, SLA_NULL, SLA_PTRS, SLA_SAVE, SlCalcArrayLen(), SlCalcConvFileLen(), SlCalcConvMemLen(), SlCopyBytes(), SlSaveLoadConv(), and SlSetLength().

Referenced by Load_ANIT(), Load_CAPR(), Load_PRIC(), AIInstance::LoadObjects(), Save_ANIT(), and AIInstance::SaveObject().

void SlAutolength | ( | AutolengthProc * | proc,
|  | void * | arg |
| ) |  |  |

Do something of which I have no idea what it is :P.

Parameters:

  | proc | The callback procedure that is called
  | arg | The variable that will be used for the callback procedure

Definition at line 1081 of file saveload.cpp.

References SaveLoadParams::action, SaveLoadParams::need_length, NL_CALCLENGTH, NL_WANTLENGTH, SaveLoadParams::obj_len, SLA_SAVE, SlError(), and SlSetLength().

size_t SlCalcObjLength | ( | const void * | object,
|  | const SaveLoad * | sld |
| ) |  |  |

Calculate the size of an object.

Parameters:

  | object | to be measured
  | sld | The SaveLoad description of the object so we know how to manipulate it

Returns:
  size of given objetc

Definition at line 938 of file saveload.cpp.

References SaveLoad::cmd.

Referenced by SlObject().

void SlGlobList | ( | const SaveLoadGlobVarList * | sldg | ) |

Save or Load (a list of) global variables.

Parameters:

  | sldg | The global variable that is being loaded or saved

Definition at line 1071 of file saveload.cpp.

References SlObject().

int SlIterateArray | ( |  | ) |

Iterate through the elements of an array and read the whole thing.

Returns:
  The index of the object, or -1 if we have reached the end of current block

Definition at line 459 of file saveload.cpp.

References SaveLoadParams::block_mode, SaveLoadParams::obj_len, and SlError().

Referenced by Load_SIGN(), and Load_VEHS().

void SlObject | ( | void * | object,
|  | const SaveLoad * | sld |
| ) |  |  |

Main SaveLoad function.

Parameters:

  | object | The object that is being saved or loaded
  | sld | The SaveLoad description of the object so we know how to manipulate it

Definition at line 1053 of file saveload.cpp.

References SaveLoad::address, SaveLoad::cmd, GetVariableAddress(), SaveLoad::global, SaveLoadParams::need_length, NL_CALCLENGTH, NL_NONE, SlCalcObjLength(), and SlSetLength().

Referenced by AIInstance::Load(), Load_ECMY(), Load_SIGN(), Load_VEHS(), AIInstance::LoadEmpty(), AIInstance::LoadObjects(), AIInstance::Save(), Save_ECMY(), Save_SIGN(), Save_VEHS(), AIInstance::SaveEmpty(), AIInstance::SaveObject(), and SlGlobList().

void SlSetLength | ( | size_t | length | ) |

Sets the length of either a RIFF object or the number of items in an array.

This lets us load an object or an array of arbitrary size

Parameters:

  | length | The length of the sought object/array

Definition at line 494 of file saveload.cpp.

References SaveLoadParams::action, SaveLoadParams::block_mode, SaveLoadParams::last_array_index, SaveLoadParams::need_length, NL_CALCLENGTH, NL_NONE, NL_WANTLENGTH, SaveLoadParams::obj_len, and SLA_SAVE.

Referenced by Save_ANIT(), SaveSettings(), SlArray(), SlAutolength(), SlList(), and SlObject().

void WriteValue | ( | void * | ptr,
|  | VarType | conv,
|  | int64 | val |
| ) |  |  |

Write the value of a setting.

Parameters:

  | ptr | pointer to the variable
  | conv | type of variable, can be a non-clean type, eg with other flags. It is parsed upon read
  | val | the new value being given to the variable

Definition at line 591 of file saveload.cpp.

References CopyFromOldName(), SLE_VAR_NAME, and SLE_VAR_NULL.

Referenced by CmdChangeCompanySetting(), CmdChangeSetting(), GameDifficultyWindow::OnClick(), CheatWindow::OnClick(), SlSaveLoadConv(), and Write_ValidateSetting().