1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "engines/stark/services/stateprovider.h"
#include "common/memstream.h"
#include "engines/stark/resources/level.h"
#include "engines/stark/resources/location.h"
#include "engines/stark/resources/object.h"
namespace Stark {
StateReadStream::StateReadStream(Common::SeekableReadStream *parentStream, DisposeAfterUse::Flag disposeParentStream) :
SeekableSubReadStream(parentStream, 0, parentStream->size(), disposeParentStream) {
}
StateReadStream::~StateReadStream() {
}
Common::String StateReadStream::readString() {
// Read the string length
uint32 length = readUint32LE();
// Read the string
char *data = new char[length];
read(data, length);
Common::String string(data, length);
delete[] data;
return string;
}
StateProvider::ResourceTreeState::ResourceTreeState(uint32 size, byte *data, uint32 version) :
_size(size),
_data(data),
_version(version) {
}
StateProvider::ResourceTreeState::~ResourceTreeState() {
free(_data);
}
StateProvider::~StateProvider() {
clear();
}
void StateProvider::clear() {
for (ResourceTreeStateMap::iterator it = _stateStore.begin(); it != _stateStore.end(); it++) {
delete it->_value;
}
_stateStore.clear();
}
void StateProvider::restoreLevelState(Resources::Level *level) {
Common::String storeKey = level->getName();
restoreResourceTreeState(storeKey, level, false);
}
void StateProvider::restoreCurrentLevelState(Resources::Level *level) {
restoreResourceTreeState("Current", level, true);
}
void StateProvider::restoreLocationState(Resources::Level *level, Resources::Location *location) {
Common::String storeKey = level->getName() + location->getName();
restoreResourceTreeState(storeKey, location, false);
}
void StateProvider::restoreCurrentLocationState(Resources::Level *level, Resources::Location *location) {
restoreResourceTreeState("CurrentCurrent", location, true);
}
void StateProvider::restoreGlobalState(Resources::Level *level) {
restoreResourceTreeState("CurrentGlobal", level, true);
}
void StateProvider::restoreResourceTreeState(Common::String storeKey, Resources::Object *root, bool current) {
if (_stateStore.contains(storeKey)) {
ResourceTreeState *state = _stateStore[storeKey];
Common::MemoryReadStream stream(state->getData(), state->getSize(), DisposeAfterUse::NO);
readResourceTree(root, &stream, current, state->getVersion());
}
}
void StateProvider::readResourceTree(Resources::Object *resource, Common::SeekableReadStream *stream, bool current, uint32 version) {
// Read the resource to the source stream
/* byte type = */ stream->readByte();
/* byte subType = */ stream->readByte();
uint32 size = stream->readUint32LE();
if (size > 0) {
Common::SeekableReadStream *resourceStream = stream->readStream(size);
ResourceSerializer serializer(resourceStream, nullptr, version);
// Deserialize the resource state from stream
if (current) {
resource->saveLoadCurrent(&serializer);
} else {
resource->saveLoad(&serializer);
}
delete resourceStream;
}
// Deserialize the resource children
Common::Array<Resources::Object *> children = resource->listChildren<Resources::Object>();
for (uint i = 0; i < children.size(); i++) {
readResourceTree(children[i], stream, current, version);
}
}
void StateProvider::saveLevelState(Resources::Level *level) {
Common::String storeKey = level->getName();
saveResourceTreeState(storeKey, level, false);
}
void StateProvider::saveCurrentLevelState(Resources::Level *level) {
saveResourceTreeState("Current", level, true);
}
void StateProvider::saveLocationState(Resources::Level *level, Resources::Location *location) {
Common::String storeKey = level->getName() + location->getName();
saveResourceTreeState(storeKey, location, false);
}
void StateProvider::saveCurrentLocationState(Resources::Level *level, Resources::Location *location) {
saveResourceTreeState("CurrentCurrent", location, true);
}
void StateProvider::saveGlobalState(Resources::Level *level) {
saveResourceTreeState("CurrentGlobal", level, true);
}
void StateProvider::saveResourceTreeState(Common::String storeKey, Resources::Object *root, bool current) {
// Delete any previous data
if (_stateStore.contains(storeKey)) {
delete _stateStore[storeKey];
_stateStore.erase(storeKey);
}
// Write the tree state to memory
Common::MemoryWriteStreamDynamic stream(DisposeAfterUse::NO);
writeResourceTree(root, &stream, current);
// Add the state to the store
_stateStore[storeKey] = new ResourceTreeState(stream.size(), stream.getData(), kSaveVersion);
}
void StateProvider::writeResourceTree(Resources::Object *resource, Common::WriteStream *stream, bool current) {
// Explicit scope to control the lifespan of the memory stream
{
Common::MemoryWriteStreamDynamic resourceStream(DisposeAfterUse::YES);
ResourceSerializer serializer(nullptr, &resourceStream, kSaveVersion);
// Serialize the resource to a memory stream
if (current) {
resource->saveLoadCurrent(&serializer);
} else {
resource->saveLoad(&serializer);
}
// Write the resource to the target stream
stream->writeByte(resource->getType().get());
stream->writeByte(resource->getSubType());
stream->writeUint32LE(resourceStream.size());
stream->write(resourceStream.getData(), resourceStream.size());
}
// Serialize the resource children
Common::Array<Resources::Object *> children = resource->listChildren<Resources::Object>();
for (uint i = 0; i < children.size(); i++) {
writeResourceTree(children[i], stream, current);
}
}
void StateProvider::readStateFromStream(StateReadStream *stream, uint saveVersion) {
clear();
uint32 treeCount = stream->readUint32LE();
for (uint i = 0; i < treeCount; i++) {
// Read the store key
Common::String key = stream->readString();
// Each resource tree state needs to have its own version because
// some may never be made active again and serialized in the latest version.
// In that case they stay in a previous version.
uint treeVersion = 6;
if (saveVersion > 6) {
treeVersion = stream->readUint32LE();
}
// Read the data size
uint32 dataSize = stream->readUint32LE();
// Read the data
byte *data = (byte *) malloc(dataSize);
stream->read(data, dataSize);
_stateStore[key] = new ResourceTreeState(dataSize, data, treeVersion);
}
}
void StateProvider::writeStateToStream(Common::WriteStream *stream) {
stream->writeUint32LE(_stateStore.size());
for (ResourceTreeStateMap::iterator it = _stateStore.begin(); it != _stateStore.end(); it++) {
stream->writeUint32LE(it->_key.size());
stream->writeString(it->_key);
stream->writeUint32LE(it->_value->getVersion());
stream->writeUint32LE(it->_value->getSize());
stream->write(it->_value->getData(), it->_value->getSize());
}
}
ResourceSerializer::ResourceSerializer(Common::SeekableReadStream *in, Common::WriteStream *out, uint32 version) :
Common::Serializer(in, out) {
_version = version;
}
void ResourceSerializer::syncAsFloat(float &value) {
// TODO: Change this to something more portable
syncBytes((byte *) &value, sizeof(float));
}
void ResourceSerializer::syncAsVector3d(Math::Vector3d &value) {
syncAsFloat(value.x());
syncAsFloat(value.y());
syncAsFloat(value.z());
}
void ResourceSerializer::syncAsResourceReference(ResourceReference &reference) {
if (isLoading()) {
reference.loadFromStream(_loadStream);
} else {
reference.saveToStream(_saveStream);
}
}
void ResourceSerializer::syncAsString32(Common::String &string) {
if (isLoading()) {
string.clear();
uint32 length = _loadStream->readUint32LE();
for (uint i = 0; i < length; i++) {
char c = _loadStream->readByte();
string += c;
}
_bytesSynced += 4 + length;
} else {
_saveStream->writeUint32LE(string.size());
_saveStream->writeString(string);
_bytesSynced += 4 + string.size();
}
}
} // End of namespace Stark
|