SUPERNOVA: Fixes de-/serialization of game state

This commit is contained in:
Joseph-Eugene Winzer 2017-08-31 20:52:04 +02:00 committed by Thierry Crozat
parent 68c52362e5
commit 23f57ca83f
2 changed files with 42 additions and 32 deletions

View File

@ -33,27 +33,28 @@ bool Room::serialize(Common::WriteStream *out) {
if (out->err())
return false;
out->writeByte(_id);
out->writeSint32LE(_id);
for (int i = 0; i < kMaxSection; ++i)
out->writeByte(_shown[i]);
int numObjects = 0;
while ((_objectState[numObjects]._id != INVALIDOBJECT) && (numObjects < kMaxObject))
++numObjects;
out->writeByte(numObjects);
out->writeSint32LE(numObjects);
for (int i = 0; i < numObjects; ++i) {
out->writeSint16LE(_objectState[i]._name.size() + 1);
out->writeString(_objectState[i]._name.c_str());
out->writeSint16LE(_objectState[i]._description.size() + 1);
out->writeString(_objectState[i]._description.c_str());
out->writeUint32LE(_objectState[i]._name.size());
out->writeString(_objectState[i]._name);
out->writeUint32LE(_objectState[i]._description.size());
out->writeString(_objectState[i]._description);
out->writeByte(_objectState[i]._roomId);
out->writeByte(_objectState[i]._id);
out->writeSint16LE(_objectState[i]._type);
out->writeSint32LE(_objectState[i]._id);
out->writeSint32LE(_objectState[i]._type);
out->writeByte(_objectState[i]._click);
out->writeByte(_objectState[i]._click2);
out->writeByte(_objectState[i]._section);
out->writeByte(_objectState[i]._exitRoom);
out->writeSint32LE(_objectState[i]._exitRoom);
out->writeByte(_objectState[i]._direction);
}
@ -66,27 +67,37 @@ bool Room::deserialize(Common::ReadStream *in) {
if (in->err())
return false;
in->readByte();
in->readSint32LE();
for (int i = 0; i < kMaxSection; ++i)
_shown[i] = in->readByte();
int numObjects = in->readByte();
int bufferSize = 0;
char stringBuffer[256];
int numObjects = in->readSint32LE();
int stringLength = 0;
Common::SeekableReadStream *stream = NULL;
for (int i = 0; i < numObjects; ++i) {
bufferSize = in->readSint16LE();
in->read(stringBuffer, bufferSize > 256 ? 256 : bufferSize);
_objectState[i]._name = stringBuffer;
bufferSize = in->readSint16LE();
in->read(stringBuffer, bufferSize > 256 ? 256 : bufferSize);
_objectState[i]._description = stringBuffer;
stringLength = in->readUint32LE();
if (stringLength) {
stream = in->readStream(stringLength);
_objectState[i]._name = stream->readLine();
} else {
_objectState[i]._name = "";
}
stringLength = in->readUint32LE();
if (stringLength) {
stream = in->readStream(stringLength);
_objectState[i]._description = stream->readLine();
} else {
_objectState[i]._description = "";
}
_objectState[i]._roomId = in->readByte();
_objectState[i]._id = static_cast<ObjectID>(in->readByte());
_objectState[i]._type = static_cast<ObjectType>(in->readSint16LE());
_objectState[i]._id = static_cast<ObjectID>(in->readSint32LE());
_objectState[i]._type = static_cast<ObjectType>(in->readSint32LE());
_objectState[i]._click = in->readByte();
_objectState[i]._click2 = in->readByte();
_objectState[i]._section = in->readByte();
_objectState[i]._exitRoom = static_cast<RoomID>(in->readByte());
_objectState[i]._exitRoom = static_cast<RoomID>(in->readSint32LE());
_objectState[i]._direction = in->readByte();
}

View File

@ -56,13 +56,13 @@ bool GameManager::serialize(Common::WriteStream *out) {
out->writeByte(_state._dream);
// Inventory
out->writeSByte(_inventory.getSize());
out->writeSByte(_inventoryScroll);
out->writeSint32LE(_inventory.getSize());
out->writeSint32LE(_inventoryScroll);
for (int i = 0; i < _inventory.getSize(); ++i) {
Object *objectStateBegin = _rooms[_inventory.get(i)->_roomId]->getObject(0);
byte objectIndex = _inventory.get(i) - objectStateBegin;
out->writeByte(_inventory.get(i)->_roomId);
out->writeByte(objectIndex);
out->writeSint32LE(_inventory.get(i)->_roomId);
out->writeSint32LE(objectIndex);
}
// Rooms
@ -75,9 +75,8 @@ bool GameManager::serialize(Common::WriteStream *out) {
bool GameManager::deserialize(Common::ReadStream *in) {
if (in->err()) {
if (in->err())
return false;
}
// GameState
_state._time = in->readSint32LE();
@ -103,12 +102,12 @@ bool GameManager::deserialize(Common::ReadStream *in) {
_state._dream = in->readByte();
// Inventory
int inventorySize = in->readSByte();
_inventoryScroll = in->readSByte();
int inventorySize = in->readSint32LE();
_inventoryScroll = in->readSint32LE();
_inventory.clear();
for (int i = 0; i < inventorySize; ++i) {
RoomID objectRoom = static_cast<RoomID>(in->readByte());
int objectIndex = in->readByte();
RoomID objectRoom = static_cast<RoomID>(in->readSint32LE());
int objectIndex = in->readSint32LE();
_inventory.add(*_rooms[objectRoom]->getObject(objectIndex));
}