diff options
Diffstat (limited to 'engines/teenagent/pack.cpp')
-rw-r--r-- | engines/teenagent/pack.cpp | 83 |
1 files changed, 67 insertions, 16 deletions
diff --git a/engines/teenagent/pack.cpp b/engines/teenagent/pack.cpp index 1176bbc301..e0e560bbf4 100644 --- a/engines/teenagent/pack.cpp +++ b/engines/teenagent/pack.cpp @@ -28,28 +28,26 @@ namespace TeenAgent { -Pack::Pack() : count(0), offsets(0) {} +FilePack::FilePack() : offsets(0) {} -Pack::~Pack() { +FilePack::~FilePack() { close(); } - -void Pack::close() { +void FilePack::close() { delete[] offsets; offsets = NULL; file.close(); } - -bool Pack::open(const Common::String &filename) { +bool FilePack::open(const Common::String &filename) { if (!file.open(filename)) return false; - count = file.readUint32LE(); - debug(0, "opened %s, found %u entries", filename.c_str(), count); - offsets = new uint32[count + 1]; - for (uint32 i = 0; i <= count; ++i) { + _files_count = file.readUint32LE(); + debug(0, "opened %s, found %u entries", filename.c_str(), _files_count); + offsets = new uint32[_files_count + 1]; + for (uint32 i = 0; i <= _files_count; ++i) { offsets[i] = file.readUint32LE(); //debug(0, "%d: %06x", i, offsets[i]); } @@ -60,14 +58,14 @@ bool Pack::open(const Common::String &filename) { return true; } -uint32 Pack::get_size(uint32 id) const { - if (id < 1 || id > count) +uint32 FilePack::get_size(uint32 id) const { + if (id < 1 || id > _files_count) return 0; return offsets[id] - offsets[id - 1]; } -uint32 Pack::read(uint32 id, byte *dst, uint32 size) const { - if (id < 1 || id > count) +uint32 FilePack::read(uint32 id, byte *dst, uint32 size) const { + if (id < 1 || id > _files_count) return 0; file.seek(offsets[id - 1]); @@ -77,11 +75,64 @@ uint32 Pack::read(uint32 id, byte *dst, uint32 size) const { return r; } -Common::SeekableReadStream *Pack::getStream(uint32 id) const { - if (id < 1 || id > count) +Common::SeekableReadStream *FilePack::getStream(uint32 id) const { + if (id < 1 || id > _files_count) return 0; //debug(0, "stream: %04x-%04x", offsets[id - 1], offsets[id]); return new Common::SeekableSubReadStream(&file, offsets[id - 1], offsets[id], DisposeAfterUse::NO); } +void MemoryPack::close() { + chunks.clear(); +} + +bool MemoryPack::open(const Common::String &filename) { + Common::File file; + if (!file.open(filename)) + return false; + + uint32 count = file.readUint32LE(); + debug(0, "opened %s, found %u entries [memory]", filename.c_str(), count); + for (uint32 i = 0; i <= count; ++i) { + uint32 offset = file.readUint32LE(); + int32 pos = file.pos(); + uint32 next_offset = file.readUint32LE(); + uint32 size = next_offset - offset; + file.seek(offset); + Chunk chunk; + if (size != 0) { + chunk.data = new byte[size]; + chunk.size = size; + file.read(chunk.data, size); + } + file.seek(pos); + chunks.push_back(chunk); + } + file.close(); + return true; +} + +uint32 MemoryPack::get_size(uint32 id) const { + --id; + return id < chunks.size()? chunks[id].size: 0; +} + +uint32 MemoryPack::read(uint32 id, byte *dst, uint32 size) const { + --id; + if (id >= chunks.size()) + return 0; + const Chunk &c = chunks[id]; + memcpy(dst, c.data, c.size); + return c.size; +} + +Common::SeekableReadStream *MemoryPack::getStream(uint32 id) const { + --id; + if (id >= chunks.size()) + return 0; + const Chunk &c = chunks[id]; + return new Common::MemoryReadStream(c.data, c.size, DisposeAfterUse::NO); +} + + } // End of namespace TeenAgent |