(svn r22567) -Codechange: Store persistent storages inside a pool.

This commit is contained in:
terkhen
2011-06-12 20:47:45 +00:00
parent db879d5bd1
commit 7e0daae1ec
23 changed files with 257 additions and 27 deletions

View File

@@ -2595,6 +2595,58 @@ bool AfterLoadGame()
}
}
if (IsSavegameVersionBefore(161)) {
/* Before savegame version 161, persistent storages were not stored in a pool. */
if (!IsSavegameVersionBefore(76)) {
Industry *ind;
FOR_ALL_INDUSTRIES(ind) {
assert(ind->psa != NULL);
/* Check if the old storage was empty. */
bool is_empty = true;
for (uint i = 0; i < sizeof(ind->psa->storage); i++) {
if (ind->psa->GetValue(i) != 0) {
is_empty = false;
break;
}
}
if (!is_empty) {
ind->psa->grfid = _industry_mngr.GetGRFID(ind->type);
} else {
delete ind->psa;
ind->psa = NULL;
}
}
}
if (!IsSavegameVersionBefore(145)) {
Station *st;
FOR_ALL_STATIONS(st) {
if (!st->facilities & FACIL_AIRPORT) continue;
assert(st->airport.psa != NULL);
/* Check if the old storage was empty. */
bool is_empty = true;
for (uint i = 0; i < sizeof(st->airport.psa->storage); i++) {
if (st->airport.psa->GetValue(i) != 0) {
is_empty = false;
break;
}
}
if (!is_empty) {
st->airport.psa->grfid = _airport_mngr.GetGRFID(st->airport.type);
} else {
delete st->airport.psa;
st->airport.psa = NULL;
}
}
}
}
/* Road stops is 'only' updating some caches */
AfterLoadRoadStops();
AfterLoadLabelMaps();

View File

@@ -11,10 +11,13 @@
#include "../stdafx.h"
#include "../industry.h"
#include "../newgrf.h"
#include "saveload.h"
#include "newgrf_sl.h"
static OldPersistentStorage _old_ind_persistent_storage;
static const SaveLoad _industry_desc[] = {
SLE_CONDVAR(Industry, location.tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
SLE_CONDVAR(Industry, location.tile, SLE_UINT32, 6, SL_MAX_VERSION),
@@ -50,7 +53,8 @@ static const SaveLoad _industry_desc[] = {
SLE_CONDVAR(Industry, last_cargo_accepted_at, SLE_INT32, 70, SL_MAX_VERSION),
SLE_CONDVAR(Industry, selected_layout, SLE_UINT8, 73, SL_MAX_VERSION),
SLE_CONDARR(Industry, psa.storage, SLE_UINT32, 16, 76, SL_MAX_VERSION),
SLEG_CONDARR(_old_ind_persistent_storage.storage, SLE_UINT32, 16, 76, 160),
SLE_CONDREF(Industry, psa, REF_STORAGE, 161, SL_MAX_VERSION),
SLE_CONDVAR(Industry, random_triggers, SLE_UINT8, 82, SL_MAX_VERSION),
SLE_CONDVAR(Industry, random, SLE_UINT16, 82, SL_MAX_VERSION),
@@ -90,6 +94,14 @@ static void Load_INDY()
while ((index = SlIterateArray()) != -1) {
Industry *i = new (index) Industry();
SlObject(i, _industry_desc);
/* Before savegame version 161, persistent storages were not stored in a pool. */
if (IsSavegameVersionBefore(161) && !IsSavegameVersionBefore(76)) {
/* Store the old persistent storage. The GRFID will be added later. */
assert(PersistentStorage::CanAllocateItem());
i->psa = new PersistentStorage(0);
memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(i->psa->storage));
}
Industry::IncIndustryTypeCount(i->type);
}
}

View File

@@ -225,8 +225,9 @@
* 158 21933
* 159 21962
* 160 21974
* 161 22567
*/
extern const uint16 SAVEGAME_VERSION = 160; ///< Current savegame version of OpenTTD.
extern const uint16 SAVEGAME_VERSION = 161; ///< Current savegame version of OpenTTD.
SavegameType _savegame_type; ///< type of savegame we are loading
@@ -404,6 +405,7 @@ extern const ChunkHandler _autoreplace_chunk_handlers[];
extern const ChunkHandler _labelmaps_chunk_handlers[];
extern const ChunkHandler _airport_chunk_handlers[];
extern const ChunkHandler _object_chunk_handlers[];
extern const ChunkHandler _persistent_storage_chunk_handlers[];
/** Array of all chunks in a savegame, \c NULL terminated. */
static const ChunkHandler * const _chunk_handlers[] = {
@@ -434,6 +436,7 @@ static const ChunkHandler * const _chunk_handlers[] = {
_labelmaps_chunk_handlers,
_airport_chunk_handlers,
_object_chunk_handlers,
_persistent_storage_chunk_handlers,
NULL,
};
@@ -1173,9 +1176,10 @@ static size_t ReferenceToInt(const void *obj, SLRefType rt)
case REF_TOWN: return ((const Town*)obj)->index + 1;
case REF_ORDER: return ((const Order*)obj)->index + 1;
case REF_ROADSTOPS: return ((const RoadStop*)obj)->index + 1;
case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
case REF_ENGINE_RENEWS: return ((const EngineRenew*)obj)->index + 1;
case REF_CARGO_PACKET: return ((const CargoPacket*)obj)->index + 1;
case REF_ORDERLIST: return ((const OrderList*)obj)->index + 1;
case REF_STORAGE: return ((const PersistentStorage*)obj)->index + 1;
default: NOT_REACHED();
}
}
@@ -1245,6 +1249,10 @@ static void *IntToReference(size_t index, SLRefType rt)
if (CargoPacket::IsValidID(index)) return CargoPacket::Get(index);
SlErrorCorrupt("Referencing invalid CargoPacket");
case REF_STORAGE:
if (PersistentStorage::IsValidID(index)) return PersistentStorage::Get(index);
SlErrorCorrupt("Referencing invalid PersistentStorage");
default: NOT_REACHED();
}
}

View File

@@ -88,6 +88,7 @@ enum SLRefType {
REF_ENGINE_RENEWS = 6, ///< Load/save a reference to an engine renewal (autoreplace).
REF_CARGO_PACKET = 7, ///< Load/save a reference to a cargo packet.
REF_ORDERLIST = 8, ///< Load/save a reference to an orderlist.
REF_STORAGE = 9, ///< Load/save a reference to a persistent storage.
};
/** Highest possible savegame version. */

View File

@@ -15,6 +15,7 @@
#include "../roadstop_base.h"
#include "../vehicle_base.h"
#include "../newgrf_station.h"
#include "../newgrf.h"
#include "saveload.h"
#include "table/strings.h"
@@ -344,6 +345,8 @@ static const SaveLoad _base_station_desc[] = {
SLE_END()
};
static OldPersistentStorage _old_st_persistent_storage;
static const SaveLoad _station_desc[] = {
SLE_WRITEBYTE(Station, facilities, FACIL_NONE),
SLE_ST_INCLUDE(),
@@ -362,7 +365,8 @@ static const SaveLoad _station_desc[] = {
SLE_CONDVAR(Station, airport.layout, SLE_UINT8, 145, SL_MAX_VERSION),
SLE_VAR(Station, airport.flags, SLE_UINT64),
SLE_CONDVAR(Station, airport.rotation, SLE_UINT8, 145, SL_MAX_VERSION),
SLE_CONDARR(Station, airport.psa.storage, SLE_UINT32, 16, 145, SL_MAX_VERSION),
SLEG_CONDARR(_old_st_persistent_storage.storage, SLE_UINT32, 16, 145, 160),
SLE_CONDREF(Station, airport.psa, REF_STORAGE, 161, SL_MAX_VERSION),
SLE_VAR(Station, indtype, SLE_UINT8),
@@ -437,6 +441,15 @@ static void Load_STNN()
if (!waypoint) {
Station *st = Station::From(bst);
/* Before savegame version 161, persistent storages were not stored in a pool. */
if (IsSavegameVersionBefore(161) && !IsSavegameVersionBefore(145) && st->facilities & FACIL_AIRPORT) {
/* Store the old persistent storage. The GRFID will be added later. */
assert(PersistentStorage::CanAllocateItem());
st->airport.psa = new PersistentStorage(0);
memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(st->airport.psa->storage));
}
for (CargoID i = 0; i < NUM_CARGO; i++) {
SlObject(&st->goods[i], GetGoodsDesc());
}

View File

@@ -0,0 +1,50 @@
/* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file persistent_storage_sl.cpp Code handling saving and loading of persistent storages. */
#include "../stdafx.h"
#include "../newgrf_storage.h"
#include "saveload.h"
/** Description of the data to save and load in #PersistentStorage. */
static const SaveLoad _storage_desc[] = {
SLE_CONDVAR(PersistentStorage, grfid, SLE_UINT32, 6, SL_MAX_VERSION),
SLE_CONDARR(PersistentStorage, storage, SLE_UINT32, 16, 161, SL_MAX_VERSION),
SLE_END()
};
/** Load persistent storage data. */
static void Load_PSAC()
{
int index;
while ((index = SlIterateArray()) != -1) {
assert(PersistentStorage::CanAllocateItem());
PersistentStorage *ps = new (index) PersistentStorage(0);
SlObject(ps, _storage_desc);
}
}
/** Save persistent storage data. */
static void Save_PSAC()
{
PersistentStorage *ps;
/* Write the industries */
FOR_ALL_STORAGES(ps) {
SlSetArrayIndex(ps->index);
SlObject(ps, _storage_desc);
}
}
/** Chunk handler for persistent storages. */
extern const ChunkHandler _persistent_storage_chunk_handlers[] = {
{ 'PSAC', Save_PSAC, Load_PSAC, NULL, NULL, CH_ARRAY | CH_LAST},
};