OpenTTD
Data Structures | Macros | 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  FileToSaveLoad
 Deals with the type of the savegame, independent of extension. More...
 
struct  ChunkHandler
 Handlers and description of chunk. More...
 
struct  NullStruct
 
struct  SaveLoad
 SaveLoad type struct. More...
 

Macros

#define SL_MAX_VERSION   UINT16_MAX
 Highest possible savegame version. More...
 
#define SLE_GENERAL(cmd, base, variable, type, length, from, to)   {false, cmd, type, length, from, to, (void*)cpp_offsetof(base, variable), cpp_sizeof(base, variable)}
 Storage of simple variables, references (pointers), and arrays. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#define SLE_LST(base, variable, type)   SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
 Storage of a list in every savegame version. More...
 
#define SLE_NULL(length)   SLE_CONDNULL(length, 0, SL_MAX_VERSION)
 Empty space in every savegame version. More...
 
#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. More...
 
#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. More...
 
#define SLE_VEH_INCLUDE()   {false, SL_VEH_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
 
#define SLE_ST_INCLUDE()   {false, SL_ST_INCLUDE, 0, 0, 0, SL_MAX_VERSION, NULL, 0}
 
#define SLE_END()   {false, SL_END, 0, 0, 0, 0, NULL, 0}
 End marker of a struct/class save or load. More...
 
#define SLEG_GENERAL(cmd, variable, type, length, from, to)   {true, cmd, type, length, from, to, (void*)&variable, sizeof(variable)}
 Storage of global simple variables, references (pointers), and arrays. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#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. More...
 
#define SLEG_VAR(variable, type)   SLEG_CONDVAR(variable, type, 0, SL_MAX_VERSION)
 Storage of a global variable in every savegame version. More...
 
#define SLEG_REF(variable, type)   SLEG_CONDREF(variable, type, 0, SL_MAX_VERSION)
 Storage of a global reference in every savegame version. More...
 
#define SLEG_ARR(variable, type)   SLEG_CONDARR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
 Storage of a global array in every savegame version. More...
 
#define SLEG_STR(variable, type)   SLEG_CONDSTR(variable, type, lengthof(variable), 0, SL_MAX_VERSION)
 Storage of a global string in every savegame version. More...
 
#define SLEG_LST(variable, type)   SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
 Storage of a global list in every savegame version. More...
 
#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. More...
 
#define SLEG_END()   {true, SL_END, 0, 0, 0, 0, NULL, 0}
 End marker of global variables save or load. More...
 

Typedefs

typedef void ChunkSaveLoadProc()
 
typedef void AutolengthProc(void *arg)
 
typedef uint32 VarType
 
typedef byte SaveLoadType
 Save/load type. More...
 
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  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_U32, 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. More...
 
void SetSaveLoadError (StringID str)
 Set the error message from outside of the actual loading/saving of the game (AfterLoadGame and friends)
 
const char * GetSaveLoadErrorString ()
 Get the string representation of the error message.
 
SaveOrLoadResult SaveOrLoad (const char *filename, SaveLoadOperation fop, DetailedFileType dft, Subdirectory sb, bool threaded=true)
 Main Save or Load function where the high-level saveload functions are handled. More...
 
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. More...
 
SaveOrLoadResult LoadWithFilter (struct LoadFilter *reader)
 Load the game using a (reader) filter. More...
 
static bool IsSavegameVersionBefore (uint16 major, byte minor=0)
 Checks whether the savegame is below major. More...
 
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. More...
 
static VarType GetVarMemType (VarType type)
 Get the NumberType of a setting. More...
 
static VarType GetVarFileType (VarType type)
 Get the #FileType of a setting. More...
 
static bool IsNumericType (VarType conv)
 Check if the given saveload type is a numeric type. More...
 
static void * GetVariableAddress (const void *object, const SaveLoad *sld)
 Get the address of the variable. More...
 
int64 ReadValue (const void *ptr, VarType conv)
 Return a signed-long version of the value of a setting. More...
 
void WriteValue (void *ptr, VarType conv, int64 val)
 Write the value of a setting. More...
 
void SlSetArrayIndex (uint index)
 
int SlIterateArray ()
 Iterate through the elements of an array and read the whole thing. More...
 
void SlAutolength (AutolengthProc *proc, void *arg)
 Do something of which I have no idea what it is :P. More...
 
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. More...
 
size_t SlCalcObjMemberLength (const void *object, const SaveLoad *sld)
 
size_t SlCalcObjLength (const void *object, const SaveLoad *sld)
 Calculate the size of an object. More...
 
byte SlReadByte ()
 Wrapper for reading a byte from the buffer. More...
 
void SlWriteByte (byte b)
 Wrapper for writing a byte to the dumper. More...
 
void SlGlobList (const SaveLoadGlobVarList *sldg)
 Save or Load (a list of) global variables. More...
 
void SlArray (void *array, size_t length, VarType conv)
 Save/Load an array. More...
 
void SlObject (void *object, const SaveLoad *sld)
 Main SaveLoad function. More...
 
bool SlObjectMember (void *object, const SaveLoad *sld)
 
void NORETURN SlError (StringID string, const char *extra_msg=NULL)
 Error handler. More...
 
void NORETURN SlErrorCorrupt (const char *msg)
 Error handler for corrupt savegames. More...
 
bool SaveloadCrashWithMissingNewGRFs ()
 Did loading the savegame cause a crash? If so, were NewGRFs missing? More...
 

Variables

FileToSaveLoad _file_to_saveload
 File to save or load in the openttd loop.
 
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.

Macro Definition Documentation

◆ SL_MAX_VERSION

#define SL_MAX_VERSION   UINT16_MAX

Highest possible savegame version.

Definition at line 96 of file saveload.h.

◆ SLE_ARR

#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
baseName of the class or struct containing the array.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
lengthNumber of elements in the array.

Definition at line 313 of file saveload.h.

◆ SLE_CONDARR

#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
baseName of the class or struct containing the array.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
lengthNumber of elements in the array.
fromFirst savegame version that has the array.
toLast savegame version that has the array.

Definition at line 267 of file saveload.h.

◆ SLE_CONDLST

#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
baseName of the class or struct containing the list.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the list.
toLast savegame version that has the list.

Definition at line 288 of file saveload.h.

◆ SLE_CONDNULL

#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
lengthLength of the empty space.
fromFirst savegame version that has the empty space.
toLast savegame version that has the empty space.

Definition at line 344 of file saveload.h.

◆ SLE_CONDREF

#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
baseName of the class or struct containing the variable.
variableName of the variable in the class or struct referenced by base.
typeType of the reference, a value from SLRefType.
fromFirst savegame version that has the field.
toLast savegame version that has the field.

Definition at line 256 of file saveload.h.

◆ SLE_CONDSTR

#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
baseName of the class or struct containing the string.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
lengthNumber of elements in the string (only used for fixed size buffers).
fromFirst savegame version that has the string.
toLast savegame version that has the string.

Definition at line 278 of file saveload.h.

◆ SLE_CONDVAR

#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
baseName of the class or struct containing the variable.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the field.
toLast savegame version that has the field.

Definition at line 246 of file saveload.h.

◆ SLE_END

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

End marker of a struct/class save or load.

Definition at line 353 of file saveload.h.

◆ SLE_GENERAL

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

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

Parameters
cmdLoad/save type.
See also
SaveLoadType
Parameters
baseName of the class or struct containing the variable.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the field.
toLast savegame version that has the field.
Note
In general, it is better to use one of the SLE_* macros below.

Definition at line 236 of file saveload.h.

◆ SLE_LST

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

Storage of a list in every savegame version.

Parameters
baseName of the class or struct containing the list.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.

Definition at line 330 of file saveload.h.

Referenced by GetLinkGraphScheduleDesc().

◆ SLE_NULL

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

Empty space in every savegame version.

Parameters
lengthLength of the empty space.

Definition at line 336 of file saveload.h.

◆ SLE_REF

#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
baseName of the class or struct containing the variable.
variableName of the variable in the class or struct referenced by base.
typeType of the reference, a value from SLRefType.

Definition at line 304 of file saveload.h.

◆ SLE_STR

#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
baseName of the class or struct containing the string.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.
lengthNumber of elements in the string (only used for fixed size buffers).

Definition at line 322 of file saveload.h.

◆ SLE_VAR

#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
baseName of the class or struct containing the variable.
variableName of the variable in the class or struct referenced by base.
typeStorage of the data in memory and in the savegame.

Definition at line 296 of file saveload.h.

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

◆ SLE_WRITEBYTE

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

◆ SLEG_ARR

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.

Definition at line 432 of file saveload.h.

◆ SLEG_CONDARR

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.
lengthNumber of elements in the array.
fromFirst savegame version that has the array.
toLast savegame version that has the array.

Definition at line 392 of file saveload.h.

◆ SLEG_CONDLST

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the list.
toLast savegame version that has the list.

Definition at line 411 of file saveload.h.

◆ SLEG_CONDNULL

#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
lengthLength of the empty space.
fromFirst savegame version that has the empty space.
toLast savegame version that has the empty space.

Definition at line 454 of file saveload.h.

◆ SLEG_CONDREF

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the field.
toLast savegame version that has the field.

Definition at line 382 of file saveload.h.

◆ SLEG_CONDSTR

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.
lengthNumber of elements in the string (only used for fixed size buffers).
fromFirst savegame version that has the string.
toLast savegame version that has the string.

Definition at line 402 of file saveload.h.

◆ SLEG_CONDVAR

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the field.
toLast savegame version that has the field.

Definition at line 373 of file saveload.h.

Referenced by GetGoodsDesc().

◆ SLEG_END

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

End marker of global variables save or load.

Definition at line 457 of file saveload.h.

◆ SLEG_GENERAL

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

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

Parameters
cmdLoad/save type.
See also
SaveLoadType
Parameters
variableName of the global variable.
typeStorage of the data in memory and in the savegame.
fromFirst savegame version that has the field.
toLast savegame version that has the field.
Note
In general, it is better to use one of the SLEG_* macros below.

Definition at line 364 of file saveload.h.

◆ SLEG_LST

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

Storage of a global list in every savegame version.

Parameters
variableName of the global variable.
typeStorage of the data in memory and in the savegame.

Definition at line 446 of file saveload.h.

◆ SLEG_REF

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

Storage of a global reference in every savegame version.

Parameters
variableName of the global variable.
typeStorage of the data in memory and in the savegame.

Definition at line 425 of file saveload.h.

◆ SLEG_STR

#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
variableName of the global variable.
typeStorage of the data in memory and in the savegame.

Definition at line 439 of file saveload.h.

◆ SLEG_VAR

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

Storage of a global variable in every savegame version.

Parameters
variableName of the global variable.
typeStorage of the data in memory and in the savegame.

Definition at line 418 of file saveload.h.

Typedef Documentation

◆ SaveLoadType

typedef byte SaveLoadType

Save/load type.

See also
SaveLoadTypes

Definition at line 205 of file saveload.h.

Enumeration Type Documentation

◆ ChunkType

enum ChunkType

Flags of a chunk.

Enumerator
CH_LAST 

Last chunk in this array.

Definition at line 99 of file saveload.h.

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

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

◆ 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.

◆ SLRefType

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

◆ VarTypes

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

Function Documentation

◆ GenerateDefaultSaveName()

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

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

Parameters
bufthe buffer to write to.
lastthe last element in the buffer.

Definition at line 2885 of file saveload.cpp.

References _local_company, and Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID().

Referenced by SaveLoadWindow::GenerateFileName().

◆ GetVarFileType()

static VarType GetVarFileType ( VarType  type)
inlinestatic

Get the #FileType of a setting.

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

Parameters
typeVarType holding information about the file-type
returnthe SLE_FILE_* part of a variable-type description

Definition at line 504 of file saveload.h.

◆ GetVariableAddress()

static void* GetVariableAddress ( const void *  object,
const SaveLoad sld 
)
inlinestatic

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

References SaveLoad::address, SaveLoad::global, ReadValue(), and WriteValue().

Referenced by CmdChangeCompanySetting(), HandleOldDiffCustom(), IConsoleGetSetting(), and SetSettingValue().

◆ GetVarMemType()

static VarType GetVarMemType ( VarType  type)
inlinestatic

Get the NumberType of a setting.

This describes the integer type as it is represented in memory

Parameters
typeVarType holding information about the variable-type
Returns
return the SLE_VAR_* part of a variable-type description

Definition at line 493 of file saveload.h.

Referenced by IsNumericType(), IsVariableSizeRight(), ReadValue(), SlCalcStringLen(), SlString(), Write_ValidateSetting(), and WriteValue().

◆ IsNumericType()

static bool IsNumericType ( VarType  conv)
inlinestatic

Check if the given saveload type is a numeric type.

Parameters
convthe type to check
Returns
True if it's a numeric type.

Definition at line 514 of file saveload.h.

References GetVarMemType().

◆ IsSavegameVersionBefore()

static bool IsSavegameVersionBefore ( uint16  major,
byte  minor = 0 
)
inlinestatic

Checks whether the savegame is below major.

minor.

Parameters
majorMajor number of the version to check against.
minorMinor 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 465 of file saveload.h.

References _sl_minor_version, and _sl_version.

Referenced by CargoPacket::AfterLoad(), AfterLoadLinkGraphs(), AfterLoadStoryBook(), Order::ConvertFromOldSavegame(), HandleOldDiffCustom(), Load_ANIT(), Load_CAPR(), Load_PRIC(), Load_SIGN(), MoveWaypointsToBaseStations(), Ptrs_TOWN(), SaveLoad_LinkGraph(), SlCalcListLen(), and SlCalcRefLen().

◆ LoadWithFilter()

SaveOrLoadResult LoadWithFilter ( LoadFilter reader)

Load the game using a (reader) filter.

Parameters
readerThe filter to read the savegame from.
Returns
Return the result of the action. SL_OK or SL_REINIT ("unload" the game)

Definition at line 2770 of file saveload.cpp.

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

◆ ReadValue()

int64 ReadValue ( const void *  ptr,
VarType  conv 
)

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

Parameters
ptrpointer to the variable
convtype 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 946 of file saveload.cpp.

References GetVarMemType().

Referenced by GetVariableAddress().

◆ SaveloadCrashWithMissingNewGRFs()

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 359 of file afterload.cpp.

References _saveload_crash_with_missing_newgrfs.

Referenced by HandleCrash().

◆ SaveOrLoad()

SaveOrLoadResult SaveOrLoad ( const char *  filename,
SaveLoadOperation  fop,
DetailedFileType  dft,
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
filenameThe name of the savegame being created/loaded
modeSave or load mode. Load can also be a TTD(Patch) game. Use #SL_LOAD, #SL_OLD_LOAD, #SL_LOAD_CHECK, or #SL_SAVE.
sbThe sub directory to save the savegame in
threadedTrue 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 2790 of file saveload.cpp.

References _do_autosave, DFT_GAME_FILE, INVALID_STRING_ID, SaveLoadParams::saveinprogress, ShowErrorMessage(), SL_OK, SLO_SAVE, and WL_ERROR.

Referenced by DEF_CONSOLE_CMD(), DoExitSave(), and CrashLog::WriteSavegame().

◆ SaveWithFilter()

SaveOrLoadResult SaveWithFilter ( SaveFilter writer,
bool  threaded 
)

Save the game using a (writer) filter.

Parameters
writerThe filter to write the savegame to.
threadedWhether to try to perform the saving asynchronously.
Returns
Return the result of the action. SL_OK or SL_ERROR

Definition at line 2615 of file saveload.cpp.

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

◆ SlArray()

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

Save/Load an array.

Parameters
arrayThe array being manipulated
lengthThe length of the array in elements
convVarType type of the atomic array (int, byte, uint64, etc.)

Definition at line 1186 of file saveload.cpp.

References _sl_version, SaveLoadParams::action, SaveLoadParams::need_length, NL_CALCLENGTH, NL_NONE, SLA_NULL, SLA_PTRS, SLA_SAVE, SlCalcArrayLen(), and SlSetLength().

Referenced by Load_ANIT(), Load_NAME(), ScriptInstance::LoadObjects(), Save_ANIT(), and ScriptInstance::SaveObject().

◆ SlAutolength()

void SlAutolength ( AutolengthProc *  proc,
void *  arg 
)

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

Parameters
procThe callback procedure that is called
argThe variable that will be used for the callback procedure

Definition at line 1640 of file saveload.cpp.

Referenced by SlStubSaveProc().

◆ SlCalcObjLength()

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

Calculate the size of an object.

Parameters
objectto be measured
sldThe SaveLoad description of the object so we know how to manipulate it
Returns
size of given object

Definition at line 1448 of file saveload.cpp.

References SaveLoad::cmd.

Referenced by SlObject().

◆ SlError()

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
stringThe translatable error message to show.
extra_msgAn 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 533 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, SlNullPointers(), and stredup().

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

◆ SlErrorCorrupt()

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

References SlError().

Referenced by Load_CHTS(), Load_ITBL(), Load_NAME(), Load_NewGRFMapping(), ReadBuffer::ReadBuffer(), SetWaterClassDependingOnSurroundings(), and SlReadSimpleGamma().

◆ SlGlobList()

void SlGlobList ( const SaveLoadGlobVarList sldg)

Save or Load (a list of) global variables.

Parameters
sldgThe global variable that is being loaded or saved

Definition at line 1630 of file saveload.cpp.

References SlObject().

Referenced by LoadSave_IBLD().

◆ SlIsObjectCurrentlyValid()

static bool SlIsObjectCurrentlyValid ( uint16  version_from,
uint16  version_to 
)
inlinestatic

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

Parameters
version_fromLowest version number that falls within the range.
version_toHighest version number that falls within the range.
Returns
Active savegame version falls within the given range.

Definition at line 479 of file saveload.h.

References SAVEGAME_VERSION.

Referenced by HandleOldDiffCustom().

◆ SlIterateArray()

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

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

◆ SlObject()

void SlObject ( void *  object,
const SaveLoad sld 
)

◆ SlReadByte()

byte SlReadByte ( )

Wrapper for reading a byte from the buffer.

Returns
The read byte.

Definition at line 605 of file saveload.cpp.

References SaveLoadParams::reader.

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

◆ SlSetLength()

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
lengthThe length of the sought object/array

Definition at line 873 of file saveload.cpp.

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

◆ SlWriteByte()

void SlWriteByte ( byte  b)

Wrapper for writing a byte to the dumper.

Parameters
bThe byte to write.

Definition at line 614 of file saveload.cpp.

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

Referenced by Save_CHTS(), and SlCopyBytes().

◆ WriteValue()

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

Write the value of a setting.

Parameters
ptrpointer to the variable
convtype of variable, can be a non-clean type, eg with other flags. It is parsed upon read
valthe new value being given to the variable

Definition at line 970 of file saveload.cpp.

References GetVarMemType().

Referenced by GetVariableAddress().