| 198 | SegaSaturnObject::~SegaSaturnObject() { |
| 199 | if (_freeData && _objData) |
| 200 | delete[] _objData; |
| 201 | } |
| 202 | |
| 203 | int SegaSaturnObject::loadVersion3(Common::SeekableReadStream &source) { |
| 204 | _freeData = true; |
| 205 | source.readUint16BE(); // skip flags |
| 206 | uint16 type = source.readUint16BE(); |
| 207 | if (type == 0x7FFF) { |
| 208 | _objSize = source.readUint16BE(); |
| 209 | } else if (type == 0x7FFE) { |
| 210 | _objSize = source.readUint16BE() * 2; |
| 211 | } else if (type < 0x7FFE) { |
| 212 | byte count1 = source.readByte(); |
| 213 | byte count2 = source.readByte(); |
| 214 | _objSize = (count1 + count2) * 2; |
| 215 | } |
| 216 | source.seek(-6, SEEK_CUR); |
| 217 | _objSize += 6; |
| 218 | _objData = new byte[_objSize]; |
| 219 | source.read(_objData, _objSize); |
| 220 | return _objSize; |
| 221 | } |
| 222 | |
| 223 | uint16 SegaSaturnObject::getFlags() const { |
| 224 | return READ_BE_UINT16(_objData); |
| 225 | } |
| 226 | |
| 227 | uint16 SegaSaturnObject::getClass() const { |
| 228 | return READ_BE_UINT16(_objData + 2); |
| 229 | } |
| 230 | |
| 231 | uint16 SegaSaturnObject::getSize() const { |
| 232 | return READ_BE_UINT16(_objData + 4); |
| 233 | } |
| 234 | |
| 235 | int16 SegaSaturnObject::getVectorItem(int16 index) { |
| 236 | if (getClass() == 0x7FFF) { |
| 237 | byte *vector = (byte*)getData(); |
| 238 | return vector[index]; |
| 239 | } else if (getClass() == 0x7FFE) { |
| 240 | int16 *vector = (int16*)getData(); |
| 241 | return READ_BE_UINT16(&vector[index]); |
| 242 | } else if (getClass() < 0x7FFE) { |
| 243 | int16 *vector = (int16*)getData(); |
| 244 | return READ_BE_UINT16(&vector[index]); |
| 245 | } else { |
| 246 | // should never reach here |
| 247 | error("Unknown object class"); |
| 248 | return 0; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void SegaSaturnObject::setVectorItem(int16 index, int16 value) { |
| 253 | if (getClass() == 0x7FFF) { |
| 254 | byte *vector = (byte*)getData(); |
| 255 | vector[index] = value; |
| 256 | } else if (getClass() <= 0x7FFE) { |
| 257 | int16 *vector = (int16*)getData(); |
| 258 | WRITE_BE_UINT16(&vector[index], value); |
| 259 | } |
| 260 | } |
| 261 | |
| 399 | void GameDatabase::loadVersionSegaSaturn(Common::SeekableReadStream &sourceS) { |
| 400 | |
| 401 | // TODO: Read/verifiy header |
| 402 | |
| 403 | sourceS.seek(0x1E); |
| 404 | |
| 405 | uint32 objectIndexOffs = sourceS.readUint32BE(); |
| 406 | uint16 objectCount = sourceS.readUint16BE(); |
| 407 | uint32 gameStateOffs = sourceS.readUint32BE(); |
| 408 | _gameStateSize = sourceS.readUint32BE(); |
| 409 | uint32 objectsOffs = sourceS.readUint32BE(); |
| 410 | uint32 objectsSize = sourceS.readUint32BE(); |
| 411 | _mainCodeObjectIndex = sourceS.readUint16BE(); |
| 412 | |
| 413 | debug(2, "objectIndexOffs = %08X; objectCount = %d; gameStateOffs = %08X; gameStateSize = %d; objectsOffs = %08X; objectsSize = %d\n", objectIndexOffs, objectCount, gameStateOffs, _gameStateSize, objectsOffs, objectsSize); |
| 414 | |
| 415 | _gameState = new byte[_gameStateSize]; |
| 416 | sourceS.seek(gameStateOffs); |
| 417 | sourceS.read(_gameState, _gameStateSize); |
| 418 | |
| 419 | Common::Array<uint32> objectOffsets; |
| 420 | sourceS.seek(objectIndexOffs); |
| 421 | for (uint32 i = 0; i < objectCount; i++) |
| 422 | objectOffsets.push_back(sourceS.readUint32BE()); |
| 423 | |
| 424 | for (uint32 i = 0; i < objectCount; i++) { |
| 425 | Object *obj = new SegaSaturnObject(); |
| 426 | |
| 427 | // The LSB indicates if it's a constant or variable object. |
| 428 | // Constant objects are loaded from disk, while variable objects exist |
| 429 | // in the _gameState buffer. |
| 430 | |
| 431 | debug(3, "obj(%04X) ofs = %08X\n", i, objectOffsets[i]); |
| 432 | |
| 433 | if (objectOffsets[i] & 1) { |
| 434 | debug(3, "-> const %08X\n", objectsOffs + objectOffsets[i] - 1); |
| 435 | sourceS.seek(objectsOffs + objectOffsets[i] - 1); |
| 436 | obj->loadVersion3(sourceS); |
| 437 | } else { |
| 438 | debug(3, "-> var\n"); |
| 439 | obj->loadVersion3(_gameState + objectOffsets[i]); |
| 440 | } |
| 441 | _objects.push_back(obj); |
| 442 | } |
| 443 | |
| 444 | } |
| 445 | |