ULTIMA8: Switch U8SaveFile to inherit from Common::Archive

This commit is contained in:
Matthew Jimenez 2023-12-11 18:03:41 -06:00 committed by Matthew Duggan
parent ade161c7c9
commit cde990a2f5
9 changed files with 68 additions and 166 deletions

@ -19,6 +19,7 @@
*
*/
#include "ultima/shared/std/string.h"
#include "ultima/ultima8/misc/debugger.h"
#include "ultima/ultima8/audio/music_flex.h"

@ -22,6 +22,7 @@
#ifndef ULTIMA8_AUDIO_SOUNDFLEX_H
#define ULTIMA8_AUDIO_SOUNDFLEX_H
#include "ultima/shared/std/string.h"
#include "ultima/ultima8/filesys/archive.h"
namespace Ultima {

@ -58,15 +58,15 @@ bool Archive::addSource(ArchiveFile *af) {
bool Archive::addSource(Common::SeekableReadStream *rs) {
ArchiveFile *s = nullptr;
if (!rs) return false;
if (!rs)
return false;
if (FlexFile::isFlexFile(rs)) {
s = new FlexFile(rs);
} else if (U8SaveFile::isU8SaveFile(rs)) {
s = new U8SaveFile(rs);
}
if (!s) return false;
if (!s)
return false;
if (!s->isValid()) {
delete s;
return false;

@ -22,8 +22,6 @@
#ifndef ULTIMA8_FILESYS_ARCHIVE_H
#define ULTIMA8_FILESYS_ARCHIVE_H
#include "ultima/shared/std/string.h"
namespace Ultima {
namespace Ultima8 {
@ -38,7 +36,7 @@ public:
explicit Archive(ArchiveFile *af);
//! create Archive with a single input source, autodetecting the type
//! Will create FlexFile, U8SaveFile or ZipFile; ids will be deleted.
//! Will create FlexFile; ids will be deleted.
explicit Archive(Common::SeekableReadStream *rs);
virtual ~Archive();

@ -1,96 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ULTIMA8_FILESYS_NAMEDARCHIVEFILE_H
#define ULTIMA8_FILESYS_NAMEDARCHIVEFILE_H
#include "ultima/ultima8/filesys/archive_file.h"
#include "ultima/ultima8/misc/classtype.h"
namespace Ultima {
namespace Ultima8 {
class NamedArchiveFile : public ArchiveFile {
public:
NamedArchiveFile() : _indexCount(0) { }
~NamedArchiveFile() override { }
bool exists(uint32 index) override {
Std::string name;
return (indexToName(index, name));
}
bool exists(const Std::string &name) override = 0;
uint8 *getObject(uint32 index, uint32 *size = 0) override {
Std::string name;
if (!indexToName(index, name))
return nullptr;
return getObject(name, size);
}
uint8 *getObject(const Std::string &name, uint32 *size = 0) override = 0;
uint32 getSize(uint32 index) const override {
Std::string name;
if (!indexToName(index, name))
return 0;
return getSize(name);
}
uint32 getSize(const Std::string &name) const override = 0;
uint32 getCount() const override = 0;
uint32 getIndexCount() const override {
return _indexCount;
}
bool isIndexed() const override {
return false;
}
bool isNamed() const override {
return true;
}
protected:
bool indexToName(uint32 index, Std::string &name) const {
Common::HashMap<uint32, Std::string>::const_iterator iter;
iter = _indexedNames.find(index);
if (iter == _indexedNames.end()) return false;
name = iter->_value;
return true;
}
void storeIndexedName(const Std::string &name) {
uint32 index;
bool hasIndex = extractIndexFromName(name, index);
if (hasIndex) {
_indexedNames[index] = name;
if (index >= _indexCount) _indexCount = index + 1;
}
}
Common::HashMap<uint32, Std::string> _indexedNames;
uint32 _indexCount;
};
} // End of namespace Ultima8
} // End of namespace Ultima
#endif

@ -19,6 +19,7 @@
*
*/
#include "common/memstream.h"
#include "ultima/ultima8/misc/debugger.h"
#include "ultima/ultima8/filesys/u8_save_file.h"
@ -26,7 +27,7 @@
namespace Ultima {
namespace Ultima8 {
U8SaveFile::U8SaveFile(Common::SeekableReadStream *rs) : _rs(rs), _count(0) {
U8SaveFile::U8SaveFile(Common::SeekableReadStream *rs) : _rs(rs) {
_valid = isU8SaveFile(_rs);
if (_valid)
@ -49,67 +50,56 @@ bool U8SaveFile::isU8SaveFile(Common::SeekableReadStream *_rs) {
bool U8SaveFile::readMetadata() {
_rs->seek(0x18);
_count = _rs->readUint16LE();
uint16 count = _rs->readUint16LE();
_offsets.resize(_count);
_sizes.resize(_count);
for (unsigned int i = 0; i < _count; ++i) {
for (unsigned int i = 0; i < count; ++i) {
uint32 namelen = _rs->readUint32LE();
char *buf = new char[namelen];
_rs->read(buf, static_cast<int32>(namelen));
Std::string filename = buf;
_indices[filename] = i;
storeIndexedName(filename);
delete[] buf;
_sizes[i] = _rs->readUint32LE();
_offsets[i] = _rs->pos();
_rs->skip(_sizes[i]); // skip data
char *name = new char[namelen];
_rs->read(name, static_cast<int32>(namelen));
FileEntry fe;
fe._size = _rs->readUint32LE();
fe._offset = _rs->pos();
_map[Common::String(name)] = fe;
delete[] name;
_rs->skip(fe._size); // skip data
}
return true;
}
bool U8SaveFile::findIndex(const Std::string &name, uint32 &index) const {
Common::HashMap<Common::String, uint32>::const_iterator iter;
iter = _indices.find(name);
if (iter == _indices.end()) return false;
index = iter->_value;
return true;
bool U8SaveFile::hasFile(const Common::Path &path) const {
return _map.contains(path.toString());
}
bool U8SaveFile::exists(const Std::string &name) {
uint32 index;
return findIndex(name, index);
int U8SaveFile::listMembers(Common::ArchiveMemberList& list) const {
list.clear();
for (U8SaveFileMap::const_iterator it = _map.begin(); it != _map.end(); ++it) {
list.push_back(Common::ArchiveMemberPtr(new Common::GenericArchiveMember(it->_key, *this)));
}
return list.size();
}
uint8 *U8SaveFile::getObject(const Std::string &name, uint32 *sizep) {
uint32 index;
if (!findIndex(name, index))
const Common::ArchiveMemberPtr U8SaveFile::getMember(const Common::Path& path) const {
if (!hasFile(path))
return nullptr;
uint32 size = _sizes[index];
if (size == 0)
return nullptr;
uint8 *object = new uint8[size];
uint32 offset = _offsets[index];
_rs->seek(offset);
_rs->read(object, size);
if (sizep) *sizep = size;
return object;
Common::String name = path.toString();
return Common::ArchiveMemberPtr(new Common::GenericArchiveMember(name, *this));
}
Common::SeekableReadStream* U8SaveFile::createReadStreamForMember(const Common::Path& path) const {
if (!hasFile(path))
return nullptr;
uint32 U8SaveFile::getSize(const Std::string &name) const {
uint32 index;
if (!findIndex(name, index))
return 0;
const FileEntry &fe = _map[path.toString()];
uint8 *data = (uint8 *)malloc(fe._size);
_rs->seek(fe._offset);
_rs->read(data, fe._size);
return _sizes[index];
return new Common::MemoryReadStream(data, fe._size, DisposeAfterUse::YES);
}
} // End of namespace Ultima8

@ -22,42 +22,49 @@
#ifndef ULTIMA8_FILESYS_U8SAVEFILE_H
#define ULTIMA8_FILESYS_U8SAVEFILE_H
#include "ultima/ultima8/filesys/named_archive_file.h"
#include "common/archive.h"
#include "ultima/shared/std/containers.h"
#include "ultima/shared/std/string.h"
namespace Ultima {
namespace Ultima8 {
class U8SaveFile : public NamedArchiveFile {
class U8SaveFile : public Common::Archive {
public:
//! create U8SaveFile from datasource; U8SaveFile takes ownership of ds
//! and deletes it when destructed
explicit U8SaveFile(Common::SeekableReadStream *rs);
~U8SaveFile() override;
bool exists(const Std::string &name) override;
uint8 *getObject(const Std::string &name, uint32 *size = 0) override;
uint32 getSize(const Std::string &name) const override;
uint32 getCount() const override {
return _count;
//! Check if constructed object is indeed a valid archive
bool isValid() const {
return _valid;
}
// Common::Archive API implementation
bool hasFile(const Common::Path &path) const override;
int listMembers(Common::ArchiveMemberList &list) const override;
const Common::ArchiveMemberPtr getMember(const Common::Path &path) const override;
Common::SeekableReadStream *createReadStreamForMember(const Common::Path &path) const override;
static bool isU8SaveFile(Common::SeekableReadStream *rs);
protected:
Common::SeekableReadStream *_rs;
uint32 _count;
bool _valid;
Common::HashMap<Common::String, uint32> _indices;
Std::vector<uint32> _offsets;
Std::vector<uint32> _sizes;
struct FileEntry {
uint32 _offset;
uint32 _size;
FileEntry() : _offset(0), _size(0) {}
};
typedef Common::HashMap<Common::String, FileEntry, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> U8SaveFileMap;
U8SaveFileMap _map;
private:
bool readMetadata();
bool findIndex(const Std::string &name, uint32 &index) const;
};
} // End of namespace Ultima8

@ -107,19 +107,19 @@ bool U8Game::startGame() {
}
U8SaveFile *u8save = new U8SaveFile(savers);
Common::SeekableReadStream *nfd = u8save->getDataSource("NONFIXED.DAT");
Common::SeekableReadStream *nfd = u8save->createReadStreamForMember("NONFIXED.DAT");
if (!nfd) {
warning("Unable to load savegame/u8save.000/NONFIXED.DAT.");
return false;
}
World::get_instance()->loadNonFixed(nfd); // deletes nfd
Common::SeekableReadStream *icd = u8save->getDataSource("ITEMCACH.DAT");
Common::SeekableReadStream *icd = u8save->createReadStreamForMember("ITEMCACH.DAT");
if (!icd) {
warning("Unable to load savegame/u8save.000/ITEMCACH.DAT.");
return false;
}
Common::SeekableReadStream *npcd = u8save->getDataSource("NPCDATA.DAT");
Common::SeekableReadStream *npcd = u8save->createReadStreamForMember("NPCDATA.DAT");
if (!npcd) {
warning("Unable to load savegame/u8save.000/NPCDATA.DAT.");
delete icd;

@ -22,6 +22,7 @@
#ifndef WORLD_ACTORS_NPC_DAT_H
#define WORLD_ACTORS_NPC_DAT_H
#include "ultima/shared/std/string.h"
#include "ultima/ultima8/filesys/raw_archive.h"
namespace Ultima {