Merge branch 'save_ext' into signal_tunnels_bridges

This commit is contained in:
Jonathan G Rennison
2016-09-18 13:53:01 +01:00
11 changed files with 469 additions and 101 deletions

View File

@@ -16,6 +16,7 @@
#include "../tunnelbridge_map.h"
#include "../tunnelbridge.h"
#include "../station_base.h"
#include "../settings_func.h"
#include "saveload.h"
@@ -486,6 +487,7 @@ static void Load_PLYR()
int index;
while ((index = SlIterateArray()) != -1) {
Company *c = new (index) Company();
SetDefaultCompanySettings(c->index);
SaveLoad_PLYR(c);
_company_colours[index] = (Colours)c->colour;
}
@@ -529,7 +531,25 @@ static void Ptrs_PLYR()
}
}
extern void LoadSettingsPlyx(bool skip);
extern void SaveSettingsPlyx();
static void Load_PLYX()
{
LoadSettingsPlyx(false);
}
static void Check_PLYX()
{
LoadSettingsPlyx(true);
}
static void Save_PLYX()
{
SaveSettingsPlyx();
}
extern const ChunkHandler _company_chunk_handlers[] = {
{ 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY | CH_LAST},
{ 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY },
{ 'PLYX', Save_PLYX, Load_PLYX, NULL, Check_PLYX, CH_RIFF | CH_LAST},
};

View File

@@ -87,6 +87,20 @@ bool SlXvIsFeaturePresent(SlXvFeatureIndex feature, uint16 min_version, uint16 m
return _sl_xv_feature_versions[feature] >= min_version && _sl_xv_feature_versions[feature] <= max_version;
}
/**
* Returns true if @p feature is present and has a version inclusively bounded by @p min_version and @p max_version
*/
const char *SlXvGetFeatureName(SlXvFeatureIndex feature)
{
const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos;
for (; info->index != XSLFI_NULL; ++info) {
if (info->index == feature) {
return info->name;
}
}
return "(unknown feature)";
}
/**
* Resets all extended feature versions to 0
*/
@@ -116,9 +130,7 @@ void SlXvSetCurrentState()
*/
void SlXvCheckSpecialSavegameVersions()
{
extern uint16 _sl_version;
// TODO: check for savegame versions
// Checks for special savegame versions go here
}
/**
@@ -165,7 +177,6 @@ static void Save_SLXI()
SlXvSetCurrentState();
static const SaveLoad _xlsi_sub_chunk_desc[] = {
SLE_VAR(SlxiSubChunkInfo, save_version, SLE_UINT16),
SLE_STR(SlxiSubChunkInfo, name, SLE_STR, 0),
SLE_END()
};
@@ -179,9 +190,9 @@ static void Save_SLXI()
chunk_counts.resize(XSLFI_SIZE);
const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos;
for (; info->index != XSLFI_NULL; ++info) {
if (info->save_version > 0) {
if (_sl_xv_feature_versions[info->index] > 0) {
item_count++;
length += 4;
length += 6;
length += SlCalcObjLength(info, _xlsi_sub_chunk_desc);
if (info->save_proc) {
uint32 extra_data_length = info->save_proc(info, true);
@@ -209,7 +220,8 @@ static void Save_SLXI()
// write data
info = _sl_xv_sub_chunk_infos;
for (; info->index != XSLFI_NULL; ++info) {
if (info->save_version > 0) {
uint16 save_version = _sl_xv_feature_versions[info->index];
if (save_version > 0) {
SlxiSubChunkFlags flags = info->flags;
assert(!(flags & (XSCF_EXTRA_DATA_PRESENT | XSCF_CHUNK_ID_LIST_PRESENT)));
uint32 extra_data_length = extra_data_lengths[info->index];
@@ -217,6 +229,7 @@ static void Save_SLXI()
if (extra_data_length > 0) flags |= XSCF_EXTRA_DATA_PRESENT;
if (chunk_count > 0) flags |= XSCF_CHUNK_ID_LIST_PRESENT;
SlWriteUint32(flags);
SlWriteUint16(save_version);
SlObject(const_cast<SlxiSubChunkInfo *>(info), _xlsi_sub_chunk_desc);
if (extra_data_length > 0) {

View File

@@ -66,6 +66,8 @@ inline bool SlXvIsFeatureMissing(SlXvFeatureIndex feature)
return !SlXvIsFeaturePresent(feature);
}
const char *SlXvGetFeatureName(SlXvFeatureIndex feature);
/**
* sub chunk flags, this is saved as-is
* (XSCF_EXTRA_DATA_PRESENT and XSCF_CHUNK_ID_LIST_PRESENT must only be set by the save code, and read by the load code)

View File

@@ -52,6 +52,7 @@
#include "../safeguards.h"
#include <deque>
#include <vector>
/*
@@ -566,7 +567,7 @@ void NORETURN SlError(StringID string, const char *extra_msg, bool already_mallo
/**
* As SlError, except that it takes a format string and additional parameters
*/
void CDECL NORETURN SlErrorFmt(StringID string, const char *msg, ...)
void NORETURN CDECL SlErrorFmt(StringID string, const char *msg, ...)
{
va_list va;
va_start(va, msg);
@@ -590,7 +591,7 @@ void NORETURN SlErrorCorrupt(const char *msg, bool already_malloced)
/**
* As SlErrorCorruptFmt, except that it takes a format string and additional parameters
*/
void CDECL NORETURN SlErrorCorruptFmt(const char *msg, ...)
void NORETURN CDECL SlErrorCorruptFmt(const char *msg, ...)
{
va_list va;
va_start(va, msg);
@@ -890,9 +891,20 @@ void SlSetLength(size_t length)
case CH_RIFF:
/* Ugly encoding of >16M RIFF chunks
* The lower 24 bits are normal
* The uppermost 4 bits are bits 24:27 */
assert(length < (1 << 28));
* The uppermost 4 bits are bits 24:27
*
* If we have more than 28 bits, use an extra uint32 and
* signal this using the extended chunk header */
assert(length < (1LL << 32));
if (length >= (1 << 28)) {
/* write out extended chunk header */
SlWriteByte(CH_EXT_HDR);
SlWriteUint32(static_cast<uint32>(SLCEHF_BIG_RIFF));
}
SlWriteUint32((uint32)((length & 0xFFFFFF) | ((length >> 24) << 28)));
if (length >= (1 << 28)) {
SlWriteUint32(length >> 28);
}
break;
case CH_ARRAY:
assert(_sl.last_array_index <= _sl.array_index);
@@ -1355,9 +1367,10 @@ static void *IntToReference(size_t index, SLRefType rt)
* Return the size in bytes of a list
* @param list The std::list to find the size of
*/
template<typename PtrList>
static inline size_t SlCalcListLen(const void *list)
{
const std::list<void *> *l = (const std::list<void *> *) list;
const PtrList *l = (const PtrList *) list;
int type_size = IsSavegameVersionBefore(69) ? 2 : 4;
/* Each entry is saved as type_size bytes, plus type_size bytes are used for the length
@@ -1371,23 +1384,23 @@ static inline size_t SlCalcListLen(const void *list)
* @param list The list being manipulated
* @param conv SLRefType type of the list (Vehicle *, Station *, etc)
*/
template<typename PtrList>
static void SlList(void *list, SLRefType conv)
{
/* Automatically calculate the length? */
if (_sl.need_length != NL_NONE) {
SlSetLength(SlCalcListLen(list));
SlSetLength(SlCalcListLen<PtrList>(list));
/* Determine length only? */
if (_sl.need_length == NL_CALCLENGTH) return;
}
typedef std::list<void *> PtrList;
PtrList *l = (PtrList *)list;
switch (_sl.action) {
case SLA_SAVE: {
SlWriteUint32((uint32)l->size());
PtrList::iterator iter;
typename PtrList::iterator iter;
for (iter = l->begin(); iter != l->end(); ++iter) {
void *ptr = *iter;
SlWriteUint32((uint32)ReferenceToInt(ptr, conv));
@@ -1409,7 +1422,7 @@ static void SlList(void *list, SLRefType conv)
PtrList temp = *l;
l->clear();
PtrList::iterator iter;
typename PtrList::iterator iter;
for (iter = temp.begin(); iter != temp.end(); ++iter) {
void *ptr = IntToReference((size_t)*iter, conv);
l->push_back(ptr);
@@ -1475,6 +1488,8 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
case SL_ARR:
case SL_STR:
case SL_LST:
case SL_DEQ:
case SL_VEC:
/* CONDITIONAL saveload types depend on the savegame version */
if (!SlIsObjectValidInSavegame(sld)) break;
@@ -1483,7 +1498,9 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld)
case SL_REF: return SlCalcRefLen();
case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv);
case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv);
case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld));
case SL_LST: return SlCalcListLen<std::list<void *>>(GetVariableAddress(object, sld));
case SL_DEQ: return SlCalcListLen<std::deque<void *>>(GetVariableAddress(object, sld));
case SL_VEC: return SlCalcListLen<std::vector<void *>>(GetVariableAddress(object, sld));
default: NOT_REACHED();
}
break;
@@ -1546,6 +1563,8 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
case SL_ARR:
case SL_STR:
case SL_LST:
case SL_DEQ:
case SL_VEC:
/* CONDITIONAL saveload types depend on the savegame version */
if (!SlIsObjectValidInSavegame(sld)) return false;
if (SlSkipVariableOnLoad(sld)) return false;
@@ -1572,7 +1591,9 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld)
break;
case SL_ARR: SlArray(ptr, sld->length, conv); break;
case SL_STR: SlString(ptr, sld->length, sld->conv); break;
case SL_LST: SlList(ptr, (SLRefType)conv); break;
case SL_LST: SlList<std::list<void *>>(ptr, (SLRefType)conv); break;
case SL_DEQ: SlList<std::deque<void *>>(ptr, (SLRefType)conv); break;
case SL_VEC: SlList<std::vector<void *>>(ptr, (SLRefType)conv); break;
default: NOT_REACHED();
}
break;
@@ -1663,6 +1684,16 @@ void SlAutolength(AutolengthProc *proc, void *arg)
if (offs != _sl.dumper->GetSize()) SlErrorCorrupt("Invalid chunk size");
}
/*
* Notes on extended chunk header:
*
* If the chunk type is CH_EXT_HDR (15), then a u32 flags field follows.
* This flag field may define additional fields which follow the flags field in future.
* The standard chunk header follows, though it my be modified by the flags field.
* At present SLCEHF_BIG_RIFF increases the RIFF size limit to a theoretical 60 bits,
* by adding a further u32 field for the high bits after the existing RIFF size field.
*/
/**
* Load a chunk of data (eg vehicles, stations, etc.)
* @param ch The chunkhandler that will be used for the operation
@@ -1676,6 +1707,15 @@ static void SlLoadChunk(const ChunkHandler *ch)
_sl.block_mode = m;
_sl.obj_len = 0;
SaveLoadChunkExtHeaderFlags ext_flags = static_cast<SaveLoadChunkExtHeaderFlags>(0);
if ((m & 0xF) == CH_EXT_HDR) {
ext_flags = static_cast<SaveLoadChunkExtHeaderFlags>(SlReadUint32());
/* read in real header */
m = SlReadByte();
_sl.block_mode = m;
}
switch (m) {
case CH_ARRAY:
_sl.array_index = 0;
@@ -1691,6 +1731,10 @@ static void SlLoadChunk(const ChunkHandler *ch)
/* Read length */
len = (SlReadByte() << 16) | ((m >> 4) << 24);
len += SlReadUint16();
if (ext_flags & SLCEHF_BIG_RIFF) {
len |= SlReadUint32() << 28;
}
_sl.obj_len = len;
endoffs = _sl.reader->GetSize() + len;
ch->load_proc();
@@ -1716,9 +1760,21 @@ static void SlLoadCheckChunk(const ChunkHandler *ch)
_sl.block_mode = m;
_sl.obj_len = 0;
SaveLoadChunkExtHeaderFlags ext_flags = static_cast<SaveLoadChunkExtHeaderFlags>(0);
if ((m & 0xF) == CH_EXT_HDR) {
ext_flags = static_cast<SaveLoadChunkExtHeaderFlags>(SlReadUint32());
/* read in real header */
m = SlReadByte();
_sl.block_mode = m;
}
switch (m) {
case CH_ARRAY:
_sl.array_index = 0;
if (ext_flags) {
SlErrorCorruptFmt("CH_ARRAY does not take chunk header extension flags: 0x%X", ext_flags);
}
if (ch && ch->load_check_proc) {
ch->load_check_proc();
} else {
@@ -1726,6 +1782,9 @@ static void SlLoadCheckChunk(const ChunkHandler *ch)
}
break;
case CH_SPARSE_ARRAY:
if (ext_flags) {
SlErrorCorruptFmt("CH_SPARSE_ARRAY does not take chunk header extension flags: 0x%X", ext_flags);
}
if (ch && ch->load_check_proc) {
ch->load_check_proc();
} else {
@@ -1734,9 +1793,19 @@ static void SlLoadCheckChunk(const ChunkHandler *ch)
break;
default:
if ((m & 0xF) == CH_RIFF) {
if (ext_flags != (ext_flags & SLCEHF_BIG_RIFF)) {
SlErrorCorruptFmt("Unknown chunk header extension flags for CH_RIFF: 0x%X", ext_flags);
}
/* Read length */
len = (SlReadByte() << 16) | ((m >> 4) << 24);
len += SlReadUint16();
if (ext_flags & SLCEHF_BIG_RIFF) {
uint64 full_len = len | (static_cast<uint64>(SlReadUint32()) << 28);
if (full_len >= (1LL << 32)) {
SlErrorCorrupt("Chunk size too large: " OTTD_PRINTFHEX64, full_len);
}
len = static_cast<size_t>(full_len);
}
_sl.obj_len = len;
endoffs = _sl.reader->GetSize() + len;
if (ch && ch->load_check_proc) {

View File

@@ -104,10 +104,17 @@ enum ChunkType {
CH_ARRAY = 1,
CH_SPARSE_ARRAY = 2,
CH_TYPE_MASK = 3,
CH_EXT_HDR = 15, ///< Extended chunk header
CH_LAST = 8, ///< Last chunk in this array.
CH_AUTO_LENGTH = 16,
};
/** Flags for chunk extended headers */
enum SaveLoadChunkExtHeaderFlags {
SLCEHF_BIG_RIFF = 1 << 0, ///< This block uses a 60-bit RIFF chunk size
};
DECLARE_ENUM_AS_BIT_SET(SaveLoadChunkExtHeaderFlags)
/**
* VarTypes is the general bitmasked magic type that tells us
* certain characteristics about the variable it refers to. For example
@@ -198,6 +205,8 @@ enum SaveLoadTypes {
SL_ARR = 2, ///< Save/load an array.
SL_STR = 3, ///< Save/load a string.
SL_LST = 4, ///< Save/load a list.
SL_DEQ = 5, ///< Save/load a deque.
SL_VEC = 6, ///< Save/load a vector.
/* non-normal save-load types */
SL_WRITEBYTE = 8,
SL_VEH_INCLUDE = 9,
@@ -303,6 +312,30 @@ typedef SaveLoad SaveLoadGlobVarList;
#define SLE_CONDLST_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_LST, base, variable, type, 0, from, to, extver)
#define SLE_CONDLST(base, variable, type, from, to) SLE_CONDLST_X(base, variable, type, from, to, SlXvFeatureTest())
/**
* Storage of a deque in some savegame versions.
* @param base Name of the class or struct containing the list.
* @param variable Name of the variable in the class or struct referenced by \a base.
* @param type Storage of the data in memory and in the savegame.
* @param from First savegame version that has the list.
* @param to Last savegame version that has the list.
* @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field
*/
#define SLE_CONDDEQ_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_DEQ, base, variable, type, 0, from, to, extver)
#define SLE_CONDDEQ(base, variable, type, from, to) SLE_CONDDEQ_X(base, variable, type, from, to, SlXvFeatureTest())
/**
* Storage of a vector in some savegame versions.
* @param base Name of the class or struct containing the list.
* @param variable Name of the variable in the class or struct referenced by \a base.
* @param type Storage of the data in memory and in the savegame.
* @param from First savegame version that has the list.
* @param to Last savegame version that has the list.
* @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field
*/
#define SLE_CONDVEC_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_VEC, base, variable, type, 0, from, to, extver)
#define SLE_CONDVEC(base, variable, type, from, to) SLE_CONDVEC_X(base, variable, type, from, to, SlXvFeatureTest())
/**
* Storage of a variable in every version of a savegame.
* @param base Name of the class or struct containing the variable.
@@ -345,6 +378,22 @@ typedef SaveLoad SaveLoadGlobVarList;
*/
#define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION)
/**
* Storage of a deque in every savegame version.
* @param base Name of the class or struct containing the list.
* @param variable Name of the variable in the class or struct referenced by \a base.
* @param type Storage of the data in memory and in the savegame.
*/
#define SLE_DEQ(base, variable, type) SLE_CONDDEQ(base, variable, type, 0, SL_MAX_VERSION)
/**
* Storage of a vector in every savegame version.
* @param base Name of the class or struct containing the list.
* @param variable Name of the variable in the class or struct referenced by \a base.
* @param type Storage of the data in memory and in the savegame.
*/
#define SLE_VEC(base, variable, type) SLE_CONDVEC(base, variable, type, 0, SL_MAX_VERSION)
/**
* Empty space in every savegame version.
* @param length Length of the empty space.
@@ -440,6 +489,28 @@ typedef SaveLoad SaveLoadGlobVarList;
#define SLEG_CONDLST_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_LST, variable, type, 0, from, to, extver)
#define SLEG_CONDLST(variable, type, from, to) SLEG_CONDLST_X(variable, type, from, to, SlXvFeatureTest())
/**
* Storage of a global deque in some savegame versions.
* @param variable Name of the global variable.
* @param type Storage of the data in memory and in the savegame.
* @param from First savegame version that has the list.
* @param to Last savegame version that has the list.
* @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field
*/
#define SLEG_CONDDEQ_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_DEQ, variable, type, 0, from, to, extver)
#define SLEG_CONDDEQ(variable, type, from, to) SLEG_CONDDEQ_X(variable, type, from, to, SlXvFeatureTest())
/**
* Storage of a global vector in some savegame versions.
* @param variable Name of the global variable.
* @param type Storage of the data in memory and in the savegame.
* @param from First savegame version that has the list.
* @param to Last savegame version that has the list.
* @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field
*/
#define SLEG_CONDVEC_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_VEC, variable, type, 0, from, to, extver)
#define SLEG_CONDVEC(variable, type, from, to) SLEG_CONDVEC_X(variable, type, from, to, SlXvFeatureTest())
/**
* Storage of a global variable in every savegame version.
* @param variable Name of the global variable.
@@ -475,6 +546,20 @@ typedef SaveLoad SaveLoadGlobVarList;
*/
#define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION)
/**
* Storage of a global deque in every savegame version.
* @param variable Name of the global variable.
* @param type Storage of the data in memory and in the savegame.
*/
#define SLEG_DEQ(variable, type) SLEG_CONDDEQ(variable, type, 0, SL_MAX_VERSION)
/**
* Storage of a global vector in every savegame version.
* @param variable Name of the global variable.
* @param type Storage of the data in memory and in the savegame.
*/
#define SLEG_VEC(variable, type) SLEG_CONDVEC(variable, type, 0, SL_MAX_VERSION)
/**
* Empty global space in some savegame versions.
* @param length Length of the empty space.
@@ -629,8 +714,8 @@ void SlObject(void *object, const SaveLoad *sld);
bool SlObjectMember(void *object, const SaveLoad *sld);
void NORETURN SlError(StringID string, const char *extra_msg = NULL, bool already_malloced = false);
void NORETURN SlErrorCorrupt(const char *msg, bool already_malloced = false);
void CDECL NORETURN SlErrorFmt(StringID string, const char *msg, ...) WARN_FORMAT(2, 3);
void CDECL NORETURN SlErrorCorruptFmt(const char *msg, ...) WARN_FORMAT(1, 2);
void NORETURN CDECL SlErrorFmt(StringID string, const char *msg, ...) WARN_FORMAT(2, 3);
void NORETURN CDECL SlErrorCorruptFmt(const char *msg, ...) WARN_FORMAT(1, 2);
bool SaveloadCrashWithMissingNewGRFs();