Data Structures | Defines | Typedefs | Enumerations | Functions | Variables

# saveload.h File Reference

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

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

Go to the source code of this file.

## Data Structures
struct | ChunkHandler
| Handlers and description of chunk. More...
struct | NullStruct
struct | SaveLoad
| SaveLoad type struct. More...
## Defines
#define | SL_MAX_VERSION UINT16_MAX
| Highest possible savegame version.
#define | SLE_GENERAL(cmd, base, variable, type, length, from, to) {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}
| Storage of simple variables, references (pointers), and arrays.
#define | SLE_CONDVAR(base, variable, type, from, to) SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)
| Storage of a variable in some savegame versions.
#define | SLE_CONDREF(base, variable, type, from, to) SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)
| Storage of a reference in some savegame versions.
#define | SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)
| Storage of an array in some savegame versions.
#define | SLE_CONDSTR(base, variable, type, length, from, to) SLE_GENERAL(SL_STR, base, variable, type, length, from, to)
| Storage of a string in some savegame versions.
#define | SLE_CONDLST(base, variable, type, from, to) SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)
| Storage of a list in some savegame versions.
#define | SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)
| Storage of a variable in every version of a savegame.
#define | SLE_REF(base, variable, type) SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)
| Storage of a reference in every version of a savegame.
#define | SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)
| Storage of an array in every version of a savegame.
#define | SLE_STR(base, variable, type, length) SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)
| Storage of a string in every savegame version.
#define | SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
| Storage of a list in every savegame version.
#define | SLE_NULL(length) SLE_CONDNULL(length, 0, SL_MAX_VERSION)
| Empty space in every savegame version.
#define | SLE_CONDNULL(length, from, to) SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)
| Empty space in some savegame versions.
#define | SLE_WRITEBYTE(base, variable, value) SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)
| Translate values ingame to different values in the savegame and vv.
#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}
| End marker of a struct/class save or load.
#define | SLEG_GENERAL(cmd, variable, type, length, from, to) {true, cmd, type, length, from, to, (void*)&variable}
| Storage of global simple variables, references (pointers), and arrays.
#define | SLEG_CONDVAR(variable, type, from, to) SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)
| Storage of a global variable in some savegame versions.
#define | SLEG_CONDREF(variable, type, from, to) SLEG_GENERAL(SL_REF, variable, type, 0, from, to)
| Storage of a global reference in some savegame versions.
#define | SLEG_CONDARR(variable, type, length, from, to) SLEG_GENERAL(SL_ARR, variable, type, length, from, to)
| Storage of a global array in some savegame versions.
#define | SLEG_CONDSTR(variable, type, length, from, to) SLEG_GENERAL(SL_STR, variable, type, length, from, to)
| Storage of a global string in some savegame versions.
#define | SLEG_CONDLST(variable, type, from, to) SLEG_GENERAL(SL_LST, variable, type, 0, from, to)
| Storage of a global list in some savegame versions.
#define | SLEG_VAR(variable, type) SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
| Storage of a global variable in every savegame version.
#define | SLEG_REF(variable, type) SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
| Storage of a global reference in every savegame version.
#define | SLEG_ARR(variable, type) SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
| Storage of a global array in every savegame version.
#define | SLEG_STR(variable, type) SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
| Storage of a global string in every savegame version.
#define | SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
| Storage of a global list in every savegame version.
#define | SLEG_CONDNULL(length, from, to) {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}
| Empty global space in some savegame versions.
#define | SLEG_END() {true, SL_END, 0, 0, 0, 0, NULL}
| End marker of global variables save or load.
## Typedefs
typedef void | ChunkSaveLoadProc ()
typedef void | AutolengthProc (void *arg)
typedef uint32 | VarType
typedef byte | SaveLoadType
| Save/load type.
typedef SaveLoad | SaveLoadGlobVarList
| Same as SaveLoad but global variables are used (for better readability);.
## Enumerations
enum | SaveOrLoadResult { SL_OK = 0, SL_ERROR = 1, SL_REINIT = 2 }
| Save or load result codes. More...
enum | SaveOrLoadMode { SL_INVALID = -1, SL_LOAD = 0, SL_SAVE = 1, SL_OLD_LOAD = 2, SL_PNG = 3, SL_BMP = 4, SL_LOAD_CHECK = 5 }
| Save or load mode. More...
enum | SavegameType { SGT_TTD, SGT_TTDP1, SGT_TTDP2, SGT_OTTD, SGT_TTO, SGT_INVALID = 0xFF }
| Types of save games. More...
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, REF_STORAGE = 9, REF_LINK_GRAPH = 10, REF_LINK_GRAPH_JOB = 11 }
| Type of reference (SLE_REF, SLE_CONDREF). More...
enum | ChunkType { CH_RIFF = 0, CH_ARRAY = 1, CH_SPARSE_ARRAY = 2, CH_TYPE_MASK = 3, CH_LAST = 8, CH_AUTO_LENGTH = 16 }
| Flags of a chunk. More...
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_NOT_IN_SAVE = 1 << 8, SLF_NOT_IN_CONFIG = 1 << 9, SLF_NO_NETWORK_SYNC = 1 << 10, SLF_ALLOW_CONTROL = 1 << 11, SLF_ALLOW_NEWLINE = 1 << 12 }
| 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 }
| Type of data saved. More...
## 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 | ProcessAsyncSaveFinish ()
| Handle async save finishes.
void | DoExitSave ()
| Do a save when exiting the game (_settings_client.gui.autosave_on_exit).
SaveOrLoadResult | SaveWithFilter (struct SaveFilter *writer, bool threaded)
| Save the game using a (writer) filter.
SaveOrLoadResult | LoadWithFilter (struct LoadFilter *reader)
| Load the game using a (reader) filter.
static bool | IsSavegameVersionBefore (uint16 major, byte minor=0)
| Checks whether the savegame is below major.
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)
| Get the NumberType of a setting.
static VarType | GetVarFileType (VarType type)
| Get the FileType of a setting.
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 ()
| Get the length of the current object.
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 reading a byte from the buffer.
void | SlWriteByte (byte b)
| Wrapper for writing a byte to the dumper.
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)
void NORETURN | SlError (StringID string, const char *extra_msg=NULL)
| Error handler.
void NORETURN | SlErrorCorrupt (const char *msg)
| Error handler for corrupt savegames.
bool | SaveloadCrashWithMissingNewGRFs ()
| Did loading the savegame cause a crash? If so, were NewGRFs missing?
## Variables
char | _savegame_format [8]
| how to compress savegames
bool | _do_autosave
| are we doing an autosave at the moment?

## Detailed Description

Functions/types related to saving and loading games.

Definition in file saveload.h.

## Define Documentation

#define SL_MAX_VERSION UINT16_MAX

Highest possible savegame version.

Definition at line 91 of file saveload.h.

Referenced by GetCargoPacketDesc(), GetGoodsDesc(), and GetVehicleDescription().

#define SLE_ARR | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | length |
| ) |  |  | SLE_CONDARR(base, variable, type, length, 0, SL_MAX_VERSION)

Storage of an array in every version of a savegame.

Parameters:

  | base | Name of the class or struct containing the array.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | length | Number of elements in the array.

Definition at line 307 of file saveload.h.

#define SLE_CONDARR | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | SLE_GENERAL(SL_ARR, base, variable, type, length, from, to)

Storage of an array in some savegame versions.

Parameters:

  | base | Name of the class or struct containing the array.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | length | Number of elements in the array.
  | from | First savegame version that has the array.
  | to | Last savegame version that has the array.

Definition at line 261 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLE_CONDLST | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | from,
|  |  | to |
| ) |  |  | SLE_GENERAL(SL_LST, base, variable, type, 0, from, to)

Storage of a list in some savegame versions.

Parameters:

  | base | Name of the class or struct containing the list.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the list.
  | to | Last savegame version that has the list.

Definition at line 282 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLE_CONDNULL | ( |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | SLE_CONDARR(NullStruct, null, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to)

Empty space in some savegame versions.

Parameters:

  | length | Length of the empty space.
  | from | First savegame version that has the empty space.
  | to | Last savegame version that has the empty space.

Definition at line 338 of file saveload.h.

Referenced by GetCargoPacketDesc(), GetGoodsDesc(), and GetVehicleDescription().

#define SLE_CONDREF | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | from,
|  |  | to |
| ) |  |  | SLE_GENERAL(SL_REF, base, variable, type, 0, from, to)

Storage of a reference in some savegame versions.

Parameters:

  | base | Name of the class or struct containing the variable.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Type of the reference, a value from SLRefType.
  | from | First savegame version that has the field.
  | to | Last savegame version that has the field.

Definition at line 250 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLE_CONDSTR | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | SLE_GENERAL(SL_STR, base, variable, type, length, from, to)

Storage of a string in some savegame versions.

Parameters:

  | base | Name of the class or struct containing the string.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | length | Number of elements in the string (only used for fixed size buffers).
  | from | First savegame version that has the string.
  | to | Last savegame version that has the string.

Definition at line 272 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLE_CONDVAR | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | from,
|  |  | to |
| ) |  |  | SLE_GENERAL(SL_VAR, base, variable, type, 0, from, to)

Storage of a variable in some savegame versions.

Parameters:

  | base | Name of the class or struct containing the variable.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the field.
  | to | Last savegame version that has the field.

Definition at line 240 of file saveload.h.

Referenced by GetCargoPacketDesc(), GetGoodsDesc(), and GetVehicleDescription().

#define SLE_END | ( |  | ) | {false, SL_END, 0, 0, 0, 0, NULL}

End marker of a struct/class save or load.

Definition at line 347 of file saveload.h.

Referenced by GetCargoPacketDesc(), GetGoodsDesc(), GetLinkGraphDesc(), GetLinkGraphJobDesc(), GetLinkGraphScheduleDesc(), and GetVehicleDescription().

#define SLE_GENERAL | ( |  | cmd,
|  |  | base,
|  |  | variable,
|  |  | type,
|  |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable)}

Storage of simple variables, references (pointers), and arrays.

Parameters:

  | cmd | Load/save type.

See also:
  SaveLoadType

Parameters:

  | base | Name of the class or struct containing the variable.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the field.
  | to | Last savegame version that has the field.

Note:
  In general, it is better to use one of the SLE_* macros below.

Definition at line 230 of file saveload.h.

#define SLE_LST | ( |  | base,
|  |  | variable,
|  |  | type |
| ) |  |  | SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)

Storage of a list in every savegame version.

Parameters:

  | base | Name of the class or struct containing the list.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.

Definition at line 324 of file saveload.h.

Referenced by GetLinkGraphScheduleDesc().

#define SLE_NULL | ( |  | length | ) | SLE_CONDNULL(length, 0, SL_MAX_VERSION)

Empty space in every savegame version.

Parameters:

  | length | Length of the empty space.

Definition at line 330 of file saveload.h.

#define SLE_REF | ( |  | base,
|  |  | variable,
|  |  | type |
| ) |  |  | SLE_CONDREF(base, variable, type, 0, SL_MAX_VERSION)

Storage of a reference in every version of a savegame.

Parameters:

  | base | Name of the class or struct containing the variable.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Type of the reference, a value from SLRefType.

Definition at line 298 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLE_STR | ( |  | base,
|  |  | variable,
|  |  | type,
|  |  | length |
| ) |  |  | SLE_CONDSTR(base, variable, type, length, 0, SL_MAX_VERSION)

Storage of a string in every savegame version.

Parameters:

  | base | Name of the class or struct containing the string.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.
  | length | Number of elements in the string (only used for fixed size buffers).

Definition at line 316 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLE_VAR | ( |  | base,
|  |  | variable,
|  |  | type |
| ) |  |  | SLE_CONDVAR(base, variable, type, 0, SL_MAX_VERSION)

Storage of a variable in every version of a savegame.

Parameters:

  | base | Name of the class or struct containing the variable.
  | variable | Name of the variable in the class or struct referenced by base.
  | type | Storage of the data in memory and in the savegame.

Definition at line 290 of file saveload.h.

Referenced by GetCargoPacketDesc(), GetGoodsDesc(), GetLinkGraphDesc(), GetLinkGraphJobDesc(), and GetVehicleDescription().

#define SLE_WRITEBYTE | ( |  | base,
|  |  | variable,
|  |  | value |
| ) |  |  | SLE_GENERAL(SL_WRITEBYTE, base, variable, 0, 0, value, value)

Translate values ingame to different values in the savegame and vv.

Definition at line 341 of file saveload.h.

Referenced by GetVehicleDescription().

#define SLEG_ARR | ( |  | variable,
|  |  | type |
| ) |  |  | SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)

Storage of a global array in every savegame version.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.

Definition at line 426 of file saveload.h.

#define SLEG_CONDARR | ( |  | variable,
|  |  | type,
|  |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | SLEG_GENERAL(SL_ARR, variable, type, length, from, to)

Storage of a global array in some savegame versions.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.
  | length | Number of elements in the array.
  | from | First savegame version that has the array.
  | to | Last savegame version that has the array.

Definition at line 386 of file saveload.h.

#define SLEG_CONDLST | ( |  | variable,
|  |  | type,
|  |  | from,
|  |  | to |
| ) |  |  | SLEG_GENERAL(SL_LST, variable, type, 0, from, to)

Storage of a global list in some savegame versions.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the list.
  | to | Last savegame version that has the list.

Definition at line 405 of file saveload.h.

Referenced by GetGoodsDesc().

#define SLEG_CONDNULL | ( |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | {true, SL_ARR, SLE_FILE_U8 | SLE_VAR_NULL | SLF_NOT_IN_CONFIG, length, from, to, (void*)NULL}

Empty global space in some savegame versions.

Parameters:

  | length | Length of the empty space.
  | from | First savegame version that has the empty space.
  | to | Last savegame version that has the empty space.

Definition at line 448 of file saveload.h.

#define SLEG_CONDREF | ( |  | variable,
|  |  | type,
|  |  | from,
|  |  | to |
| ) |  |  | SLEG_GENERAL(SL_REF, variable, type, 0, from, to)

Storage of a global reference in some savegame versions.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the field.
  | to | Last savegame version that has the field.

Definition at line 376 of file saveload.h.

#define SLEG_CONDSTR | ( |  | variable,
|  |  | type,
|  |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | SLEG_GENERAL(SL_STR, variable, type, length, from, to)

Storage of a global string in some savegame versions.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.
  | length | Number of elements in the string (only used for fixed size buffers).
  | from | First savegame version that has the string.
  | to | Last savegame version that has the string.

Definition at line 396 of file saveload.h.

#define SLEG_CONDVAR | ( |  | variable,
|  |  | type,
|  |  | from,
|  |  | to |
| ) |  |  | SLEG_GENERAL(SL_VAR, variable, type, 0, from, to)

Storage of a global variable in some savegame versions.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the field.
  | to | Last savegame version that has the field.

Definition at line 367 of file saveload.h.

Referenced by GetGoodsDesc(), and GetVehicleDescription().

#define SLEG_END | ( |  | ) | {true, SL_END, 0, 0, 0, 0, NULL}

End marker of global variables save or load.

Definition at line 451 of file saveload.h.

#define SLEG_GENERAL | ( |  | cmd,
|  |  | variable,
|  |  | type,
|  |  | length,
|  |  | from,
|  |  | to |
| ) |  |  | {true, cmd, type, length, from, to, (void*)&variable}

Storage of global simple variables, references (pointers), and arrays.

Parameters:

  | cmd | Load/save type.

See also:
  SaveLoadType

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.
  | from | First savegame version that has the field.
  | to | Last savegame version that has the field.

Note:
  In general, it is better to use one of the SLEG_* macros below.

Definition at line 358 of file saveload.h.

#define SLEG_LST | ( |  | variable,
|  |  | type |
| ) |  |  | SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)

Storage of a global list in every savegame version.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.

Definition at line 440 of file saveload.h.

#define SLEG_REF | ( |  | variable,
|  |  | type |
| ) |  |  | SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)

Storage of a global reference in every savegame version.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.

Definition at line 419 of file saveload.h.

#define SLEG_STR | ( |  | variable,
|  |  | type |
| ) |  |  | SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)

Storage of a global string in every savegame version.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.

Definition at line 433 of file saveload.h.

#define SLEG_VAR | ( |  | variable,
|  |  | type |
| ) |  |  | SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)

Storage of a global variable in every savegame version.

Parameters:

  | variable | Name of the global variable.
  | type | Storage of the data in memory and in the savegame.

Definition at line 412 of file saveload.h.

Referenced by GetLinkGraphDesc().

## Typedef Documentation

typedef byte SaveLoadType

Save/load type.

See also:
  SaveLoadTypes

Definition at line 200 of file saveload.h.

## Enumeration Type Documentation

enum ChunkType

Flags of a chunk.

Enumerator:

  CH_LAST | Last chunk in this array.

Definition at line 94 of file saveload.h.

enum SavegameType

Types of save games.

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 37 of file saveload.h.

enum SaveLoadTypes

Type of data saved.

Enumerator:

  SL_VAR | Save/load a variable.
  SL_REF | Save/load a reference.
  SL_ARR | Save/load an array.
  SL_STR | Save/load a string.
  SL_LST | Save/load a list.

Definition at line 187 of file saveload.h.

enum SaveOrLoadMode

Save or load mode.

See also:
  SaveOrLoad

Enumerator:

  SL_INVALID | Invalid mode.
  SL_LOAD | Load game.
  SL_SAVE | Save game.
  SL_OLD_LOAD | Load old game.
  SL_PNG | Load PNG file (height map).
  SL_BMP | Load BMP file (height map).
  SL_LOAD_CHECK | Load for game preview.

Definition at line 26 of file saveload.h.

enum SaveOrLoadResult

Save or load result codes.

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 19 of file saveload.h.

enum SLRefType

Type of reference (SLE_REF, SLE_CONDREF).

Enumerator:

  REF_ORDER | Load/save a reference to an order.
  REF_VEHICLE | Load/save a reference to a vehicle.
  REF_STATION | Load/save a reference to a station.
  REF_TOWN | Load/save a reference to a town.
  REF_VEHICLE_OLD | Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
  REF_ROADSTOPS | Load/save a reference to a bus/truck stop.
  REF_ENGINE_RENEWS | Load/save a reference to an engine renewal (autoreplace).
  REF_CARGO_PACKET | Load/save a reference to a cargo packet.
  REF_ORDERLIST | Load/save a reference to an orderlist.
  REF_STORAGE | Load/save a reference to a persistent storage.
  REF_LINK_GRAPH | Load/save a reference to a link graph.
  REF_LINK_GRAPH_JOB | Load/save a reference to a link graph job.

Definition at line 75 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_NOT_IN_SAVE | do not save with savegame, basically client-based
  SLF_NOT_IN_CONFIG | do not save to config file
  SLF_NO_NETWORK_SYNC | do not synchronize over network (but it is saved if SLF_NOT_IN_SAVE is not set)
  SLF_ALLOW_CONTROL | allow control codes in the strings
  SLF_ALLOW_NEWLINE | allow new lines in the strings

Definition at line 111 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 2798 of file saveload.cpp.

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

Referenced by DoAutosave(), SaveLoadWindow::GenerateFileName(), and MakeScreenshotName().

static VarType GetVarFileType | ( | VarType | type | ) | `[inline, static]`

Get the FileType of a setting.

This describes the integer type as it is represented in a savegame/file

Parameters:

  | type | VarType holding information about the file-type
  | return | the SLE_FILE_* part of a variable-type description

Definition at line 498 of file saveload.h.

Referenced by SlSaveLoadConv().

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 519 of file saveload.h.

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

Referenced by CmdChangeCompanySetting(), CmdChangeSetting(), NewsTypeData::GetDisplay(), HandleOldDiffCustom(), IConsoleGetSetting(), IConsoleListSettings(), IniLoadSettings(), IniSaveSettings(), LoadSettings(), SaveSettings(), SetCompanySetting(), SetDefaultCompanySettings(), SetSettingValue(), SlObject(), and SyncCompanySettings().

static VarType GetVarMemType | ( | VarType | type | ) | `[inline, static]`

Get the NumberType of a setting.

This describes the integer type as it is represented in memory

Parameters:

  | type | VarType holding information about the variable-type

Returns:
  return the SLE_VAR_* part of a variable-type description

Definition at line 487 of file saveload.h.

Referenced by IConsoleGetSetting(), IConsoleListSettings(), IniLoadSettings(), IniSaveSettings(), IsNumericType(), ReadValue(), SetSettingValue(), SlCalcStringLen(), SlString(), Write_ValidateSetting(), and WriteValue().

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 508 of file saveload.h.

References GetVarMemType().

Referenced by LoadSettings().

static bool IsSavegameVersionBefore | ( | uint16 | major,
|  | byte | minor = `0` |
| ) |  |  | `[inline, static]`

Checks whether the savegame is below major.

minor.

Parameters:

  | major | Major number of the version to check against.
  | minor | Minor number of the version to check against. If minor is 0 or not specified, only the major number is checked.

Returns:
  Savegame version is earlier than the specified version.

Definition at line 459 of file saveload.h.

References _sl_minor_version, and _sl_version.

Referenced by CargoPacket::AfterLoad(), AfterLoadGame(), AfterLoadStoryBook(), AfterLoadVehicles(), Order::ConvertFromOldSavegame(), CopyFromOldName(), DoLoad(), HandleOldDiffCustom(), IntToReference(), Load_ANIT(), Load_CAPR(), Load_ECMY(), Load_PRIC(), Load_SIGN(), Load_VEHS(), MoveWaypointsToBaseStations(), Ptrs_TOWN(), SlCalcListLen(), SlCalcRefLen(), SlList(), and SlString().

SaveOrLoadResult LoadWithFilter | ( | LoadFilter * | reader | ) |

Load the game using a (reader) filter.

Parameters:

  | reader | The filter to read the savegame from.

Returns:
  Return the result of the action. SL_OK or SL_REINIT ("unload" the game)

Definition at line 2694 of file saveload.cpp.

References SaveLoadParams::action, ClearSaveLoadState(), and DoLoad().

Referenced by SafeLoad().

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 920 of file saveload.cpp.

References GetVarMemType(), and SLE_VAR_NULL.

Referenced by CmdChangeCompanySetting(), CmdChangeSetting(), SettingEntry::DrawSetting(), GameSettingsWindow::DrawWidget(), CheatWindow::DrawWidget(), NewsTypeData::GetDisplay(), IConsoleGetSetting(), IConsoleListSettings(), IniSaveSettings(), SettingEntry::IsVisibleByRestrictionMode(), LoadSettings(), GameSettingsWindow::OnClick(), CheatWindow::OnClick(), CheatWindow::OnQueryTextFinished(), SetCompanySetting(), SetSettingValue(), 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 354 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 mode. Load can also be a TTD(Patch) game. Use SL_LOAD, SL_OLD_LOAD, SL_LOAD_CHECK, or SL_SAVE.
  | sb | The sub directory to save the savegame in
  | threaded | True when threaded saving is allowed

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

Definition at line 2714 of file saveload.cpp.

References _date, _date_fract, _do_autosave, _grfconfig, _network_server, _settings_client, _sl_minor_version, _sl_version, SaveLoadParams::action, AfterLoadGame(), BASE_DIR, ClearGRFConfigList(), ClearSaveLoadState(), DEBUG, DoLoad(), DoSave(), FioFOpenFile(), GamelogReset(), GamelogStartAction(), GamelogStopAction(), GetSaveLoadErrorString(), GLAT_LOAD, ClientSettings::gui, INVALID_STRING_ID, SAVE_DIR, SaveLoadParams::saveinprogress, SCENARIO_DIR, ShowErrorMessage(), SL_LOAD, SL_LOAD_CHECK, SL_OLD_LOAD, SL_REINIT, SL_SAVE, SlError(), GUISettings::threaded_saves, and WL_ERROR.

Referenced by _GenerateWorld(), DEF_CONSOLE_CMD(), DoAutosave(), DoExitSave(), LoadIntroGame(), SaveLoadWindow::OnClick(), openttd_main(), SafeLoad(), StateGameLoop(), and CrashLog::WriteSavegame().

SaveOrLoadResult SaveWithFilter | ( | SaveFilter * | writer,
|  | bool | threaded |
| ) |  |  |

Save the game using a (writer) filter.

Parameters:

  | writer | The filter to write the savegame to.
  | threaded | Whether to try to perform the saving asynchronously.

Returns:
  Return the result of the action. SL_OK or SL_ERROR

Definition at line 2539 of file saveload.cpp.

References SaveLoadParams::action, ClearSaveLoadState(), and DoSave().

Referenced by ServerNetworkGameSocketHandler::SendMap().

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 1160 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_NAME(), Load_PRIC(), ScriptInstance::LoadObjects(), Save_ANIT(), and ScriptInstance::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 1567 of file saveload.cpp.

References SaveLoadParams::action, SaveLoadParams::dumper, MemoryDumper::GetSize(), SaveLoadParams::need_length, SaveLoadParams::obj_len, SLA_SAVE, SlErrorCorrupt(), and SlSetLength().

Referenced by Save_LGRJ(), Save_LGRP(), and SlStubSaveProc().

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 object

Definition at line 1422 of file saveload.cpp.

References SaveLoad::cmd.

Referenced by SlObject().

void NORETURN SlError | ( | StringID | string,
|  | const char * | extra_msg |
| ) |  |  |

Error handler.

Sets everything up to show an error message and to clean up the mess of a partial savegame load.

Parameters:

  | string | The translatable error message to show.
  | extra_msg | An extra error message coming from one of the APIs.

Note:
  This function does never return as it throws an exception to break out of all the saveload code.

Definition at line 521 of file saveload.cpp.

References _load_check_data, SaveLoadParams::action, LoadCheckData::error, LoadCheckData::error_data, SaveLoadParams::error_str, SaveLoadParams::extra_msg, free(), SLA_LOAD, SLA_LOAD_CHECK, SLA_PTRS, and SlNullPointers().

Referenced by AfterLoadGame(), DoLoad(), PacketWriter::Finish(), LZMALoadFilter::LZMALoadFilter(), LZMASaveFilter::LZMASaveFilter(), LZOLoadFilter::LZOLoadFilter(), LZOSaveFilter::LZOSaveFilter(), MoveWaypointsToBaseStations(), LZMALoadFilter::Read(), ZlibLoadFilter::Read(), LZOLoadFilter::Read(), SaveOrLoad(), SlErrorCorrupt(), FileWriter::Write(), PacketWriter::Write(), LZMASaveFilter::WriteLoop(), ZlibSaveFilter::WriteLoop(), ZlibLoadFilter::ZlibLoadFilter(), and ZlibSaveFilter::ZlibSaveFilter().

void NORETURN SlErrorCorrupt | ( | const char * | msg | ) |

Error handler for corrupt savegames.

Sets everything up to show the error message and to clean up the mess of a partial savegame load.

Parameters:

  | msg | Location the corruption has been spotted.

Note:
  This function does never return as it throws an exception to break out of all the saveload code.

Definition at line 549 of file saveload.cpp.

References SlError().

Referenced by AfterLoadGame(), AfterLoadVehicles(), ConvertOldMultiheadToNew(), IntToReference(), Load_CHTS(), Load_NAME(), Load_VEHS(), LoadOldVehicle(), LZOLoadFilter::Read(), SetWaterClassDependingOnSurroundings(), SlAutolength(), SlIterateArray(), SlLoadCheckChunk(), SlLoadCheckChunks(), SlLoadChunk(), SlLoadChunks(), and SlReadSimpleGamma().

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 1557 of file saveload.cpp.

References SlObject().

Referenced by LoadSave_IBLD().

static bool SlIsObjectCurrentlyValid | ( | uint16 | version_from,
|  | uint16 | version_to |
| ) |  |  | `[inline, static]`

Checks if some version from/to combination falls within the range of the active savegame version.

Parameters:

  | version_from | Lowest version number that falls within the range.
  | version_to | Highest version number that falls within the range.

Returns:
  Active savegame version falls within the given range.

Definition at line 473 of file saveload.h.

References SAVEGAME_VERSION.

Referenced by CmdChangeSetting(), GetSettingFromName(), HandleOldDiffCustom(), IConsoleListSettings(), IniLoadSettings(), and IniSaveSettings().

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 802 of file saveload.cpp.

References SaveLoadParams::block_mode, DEBUG, ReadBuffer::GetSize(), SaveLoadParams::obj_len, SaveLoadParams::reader, and SlErrorCorrupt().

Referenced by Load_CAPA(), Load_ITBL(), Load_LGRJ(), Load_LGRP(), Load_NAME(), Load_NewGRFMapping(), Load_PSAC(), Load_SIGN(), Load_VEHS(), LoadDelivery(), LoadPickup(), and SlSkipArray().

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 1539 of file saveload.cpp.

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

Referenced by DoSave_LGRJ(), DoSave_LGRP(), ScriptInstance::Load(), Load_CAPA(), Load_ECMY(), Load_ITBL(), Load_LGRJ(), Load_LGRP(), Load_LGRS(), Load_NewGRFMapping(), Load_PSAC(), Load_SIGN(), Load_VEHS(), LoadDelivery(), ScriptInstance::LoadEmpty(), ScriptInstance::LoadObjects(), LoadPickup(), Ptrs_LGRS(), Ptrs_TOWN(), ScriptInstance::Save(), Save_CAPA(), Save_ECMY(), Save_ITBL(), Save_LGRS(), Save_NewGRFMapping(), Save_PSAC(), Save_SIGN(), Save_VEHS(), SaveDelivery(), ScriptInstance::SaveEmpty(), SaveLoad_LinkGraph(), ScriptInstance::SaveObject(), SavePickup(), and SlGlobList().

byte SlReadByte | ( |  | ) |

Wrapper for reading a byte from the buffer.

Returns:
  The read byte.

Definition at line 593 of file saveload.cpp.

References SaveLoadParams::reader.

Referenced by Load_CHTS(), Load_VEHS(), SlCopyBytes(), SlLoadCheckChunk(), SlLoadChunk(), SlReadSimpleGamma(), SlSaveLoadConv(), and SlSkipBytes().

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 847 of file saveload.cpp.

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

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

void SlWriteByte | ( | byte | b | ) |

Wrapper for writing a byte to the dumper.

Parameters:

  | b | The byte to write.

Definition at line 602 of file saveload.cpp.

References SaveLoadParams::dumper, and MemoryDumper::WriteByte().

Referenced by Save_CHTS(), SlCopyBytes(), SlSaveChunk(), SlSaveLoadConv(), and SlWriteSimpleGamma().

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 944 of file saveload.cpp.

References CopyFromOldName(), GetVarMemType(), SLE_VAR_NAME, and SLE_VAR_NULL.

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