Merge branch 'save_ext' into jgrpp

This commit is contained in:
Jonathan G Rennison 2016-09-13 22:57:08 +01:00
commit ae8e19cf9a
6 changed files with 148 additions and 30 deletions

View File

@ -15,7 +15,7 @@
#include <map>
#include <list>
template<typename Tkey, typename Tvalue, typename Tcompare>
template<typename Tkey, typename Tvalue, typename Tcontainer, typename Tcompare>
class MultiMap;
/**
@ -23,14 +23,15 @@ class MultiMap;
* @tparam Tmap_iter Iterator type for the map in the MultiMap.
* @tparam Tlist_iter Iterator type for the lists in the MultiMap.
* @tparam Tkey Key type of the MultiMap.
* @tparam Tvalue Value type of the MultMap.
* @tparam Tvalue Value type of the MultiMap.
* @tparam Tcontainer Container type for the values of the MultiMap.
* @tparam Tcompare Comparator type for keys of the MultiMap.
*/
template<class Tmap_iter, class Tlist_iter, class Tkey, class Tvalue, class Tcompare>
template<class Tmap_iter, class Tlist_iter, class Tkey, class Tvalue, class Tcontainer, class Tcompare>
class MultiMapIterator {
protected:
friend class MultiMap<Tkey, Tvalue, Tcompare>;
typedef MultiMapIterator<Tmap_iter, Tlist_iter, Tkey, Tvalue, Tcompare> Self;
friend class MultiMap<Tkey, Tvalue, Tcontainer, Tcompare>;
typedef MultiMapIterator<Tmap_iter, Tlist_iter, Tkey, Tvalue, Tcontainer, Tcompare> Self;
Tlist_iter list_iter; ///< Iterator pointing to current position in the current list of items with equal keys.
Tmap_iter map_iter; ///< Iterator pointing to the position of the current list of items with equal keys in the map.
@ -201,8 +202,8 @@ public:
* @param iter2 Second iterator to compare.
* @return If iter1 and iter2 are equal.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcompare>
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare> &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcontainer1, class Tcontainer2, class Tcompare>
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcontainer1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcontainer2, Tcompare> &iter2)
{
if (iter1.GetMapIter() != iter2.GetMapIter()) return false;
if (!iter1.ListValid()) return !iter2.ListValid();
@ -218,8 +219,8 @@ bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, T
* @param iter2 Second iterator to compare.
* @return If iter1 and iter2 are not equal.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcompare>
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcompare> &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tlist_iter2, class Tkey, class Tvalue1, class Tvalue2, class Tcontainer1, class Tcontainer2, class Tcompare>
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, Tcontainer1, Tcompare> &iter1, const MultiMapIterator<Tmap_iter2, Tlist_iter2, Tkey, Tvalue2, Tcontainer2, Tcompare> &iter2)
{
return !(iter1 == iter2);
}
@ -232,8 +233,8 @@ bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue1, T
* @param iter2 Map iterator.
* @return If iter1 points to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1, const Tmap_iter2 &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1, const Tmap_iter2 &iter2)
{
return !iter1.ListValid() && iter1.GetMapIter() == iter2;
}
@ -244,8 +245,8 @@ bool operator==(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tc
* @param iter2 Map iterator.
* @return If iter1 doesn't point to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1, const Tmap_iter2 &iter2)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1, const Tmap_iter2 &iter2)
{
return iter1.ListValid() || iter1.GetMapIter() != iter2;
}
@ -256,8 +257,8 @@ bool operator!=(const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tc
* @param iter1 MultiMap iterator.
* @return If iter1 points to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1)
{
return !iter1.ListValid() && iter1.GetMapIter() == iter2;
}
@ -268,8 +269,8 @@ bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlis
* @param iter1 MultiMap iterator.
* @return If iter1 doesn't point to the begin of the list pointed to by iter2.
*/
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcompare >
bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcompare> &iter1)
template<class Tmap_iter1, class Tlist_iter1, class Tmap_iter2, class Tkey, class Tvalue, class Tcontainer, class Tcompare >
bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlist_iter1, Tkey, Tvalue, Tcontainer, Tcompare> &iter1)
{
return iter1.ListValid() || iter1.GetMapIter() != iter2;
}
@ -282,10 +283,10 @@ bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator<Tmap_iter1, Tlis
* STL-compatible members are named in STL style, all others are named in OpenTTD
* style.
*/
template<typename Tkey, typename Tvalue, typename Tcompare = std::less<Tkey> >
class MultiMap : public std::map<Tkey, std::list<Tvalue>, Tcompare > {
template<typename Tkey, typename Tvalue, typename Tcontainer = std::list<Tvalue>, typename Tcompare = std::less<Tkey> >
class MultiMap : public std::map<Tkey, Tcontainer, Tcompare > {
public:
typedef typename std::list<Tvalue> List;
typedef Tcontainer List;
typedef typename List::iterator ListIterator;
typedef typename List::const_iterator ConstListIterator;
@ -293,8 +294,8 @@ public:
typedef typename Map::iterator MapIterator;
typedef typename Map::const_iterator ConstMapIterator;
typedef MultiMapIterator<MapIterator, ListIterator, Tkey, Tvalue, Tcompare> iterator;
typedef MultiMapIterator<ConstMapIterator, ConstListIterator, Tkey, const Tvalue, Tcompare> const_iterator;
typedef MultiMapIterator<MapIterator, ListIterator, Tkey, Tvalue, Tcontainer, Tcompare> iterator;
typedef MultiMapIterator<ConstMapIterator, ConstListIterator, Tkey, const Tvalue, Tcontainer, Tcompare> const_iterator;
/**
* Erase the value pointed to by an iterator. The iterator may be invalid afterwards.

View File

@ -255,7 +255,20 @@ static void WriteSavegameInfo(const char *name)
char buf[8192];
char *p = buf;
p += seprintf(p, lastof(buf), "Name: %s\n", name);
p += seprintf(p, lastof(buf), "Savegame ver: %d\n", _sl_version);
const char *type = "";
extern bool _sl_is_faked_ext;
extern bool _sl_is_ext_version;
if (_sl_is_faked_ext) {
type = " (fake extended)";
} else if (_sl_is_ext_version) {
type = " (extended)";
}
p += seprintf(p, lastof(buf), "Savegame ver: %d%s\n", _sl_version, type);
for (size_t i = 0; i < XSLFI_SIZE; i++) {
if (_sl_xv_feature_versions[i] > 0) {
p += seprintf(p, lastof(buf), " Feature: %s = %d\n", SlXvGetFeatureName((SlXvFeatureIndex) i), _sl_xv_feature_versions[i]);
}
}
p += seprintf(p, lastof(buf), "NewGRF ver: 0x%08X\n", last_ottd_rev);
p += seprintf(p, lastof(buf), "Modified: %d\n", ever_modified);

View File

@ -114,6 +114,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
*/

View File

@ -97,6 +97,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

@ -54,6 +54,7 @@
#include "../safeguards.h"
#include <deque>
#include <vector>
/*
@ -1383,9 +1384,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
@ -1399,23 +1401,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));
@ -1437,7 +1439,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);
@ -1503,6 +1505,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;
@ -1511,7 +1515,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;
@ -1574,6 +1580,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;
@ -1600,7 +1608,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;

View File

@ -205,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,
@ -310,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.
@ -352,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.
@ -447,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.
@ -482,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.