1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "common/dcl.h"
24 #include "common/file.h"
25 #include "common/stream.h"
26 #include "common/substream.h"
27
28 #include "gnap/gnap.h"
29 #include "gnap/datarchive.h"
30
31 #include "engines/util.h"
32
33 namespace Gnap {
34
35 // DatArchive
36
DatArchive(const char * filename)37 DatArchive::DatArchive(const char *filename) {
38 _fd = new Common::File();
39 if (!_fd->open(filename))
40 error("DatArchive::DatArchive() Could not open %s", filename);
41 _fd->skip(8); // Skip signature
42 _fd->skip(2); // Skip unknown
43 _fd->skip(2); // Skip unknown
44 _entriesCount = _fd->readUint32LE();
45 debugC(kDebugBasic, "_entriesCount: %d", _entriesCount);
46 _fd->skip(4); // Skip unknown
47 _entries = new DatEntry[_entriesCount];
48 for (int i = 0; i < _entriesCount; ++i) {
49 _entries[i]._ofs = _fd->readUint32LE();
50 _entries[i]._outSize1 = _fd->readUint32LE();
51 _entries[i]._type = _fd->readUint32LE();
52 _entries[i]._outSize2 = _fd->readUint32LE();
53 }
54 }
55
~DatArchive()56 DatArchive::~DatArchive() {
57 _fd->close();
58 delete _fd;
59 delete[] _entries;
60 }
61
load(int index)62 byte *DatArchive::load(int index) {
63 _fd->seek(_entries[index]._ofs);
64 debugC(kDebugBasic, "_entries[index].outSize2: %d; _entries[index].outSize1: %d", _entries[index]._outSize2, _entries[index]._outSize1);
65 byte *buffer = new byte[_entries[index]._outSize1];
66 if (!Common::decompressDCL(_fd, buffer, _entries[index]._outSize2, _entries[index]._outSize1))
67 error("DatArchive::load() Error during decompression of entry %d", index);
68 return buffer;
69 }
70
71 // DatManager
72
DatManager()73 DatManager::DatManager() {
74 for (int i = 0; i < kMaxDatArchives; ++i)
75 _datArchives[i] = nullptr;
76 }
77
~DatManager()78 DatManager::~DatManager() {
79 for (int i = 0; i < kMaxDatArchives; ++i)
80 delete _datArchives[i];
81 }
82
open(int index,const char * filename)83 void DatManager::open(int index, const char *filename) {
84 close(index);
85 _datArchives[index] = new DatArchive(filename);
86 }
87
close(int index)88 void DatManager::close(int index) {
89 delete _datArchives[index];
90 _datArchives[index] = nullptr;
91 }
92
loadResource(int resourceId)93 byte *DatManager::loadResource(int resourceId) {
94 const int datIndex = ridToDatIndex(resourceId);
95 const int entryIndex = ridToEntryIndex(resourceId);
96 return _datArchives[datIndex] ? _datArchives[datIndex]->load(entryIndex) : 0;
97 }
98
getResourceType(int resourceId)99 uint32 DatManager::getResourceType(int resourceId) {
100 const int datIndex = ridToDatIndex(resourceId);
101 const int entryIndex = ridToEntryIndex(resourceId);
102 return _datArchives[datIndex] ? _datArchives[datIndex]->getEntryType(entryIndex) : 0;
103 }
104
getResourceSize(int resourceId)105 uint32 DatManager::getResourceSize(int resourceId) {
106 const int datIndex = ridToDatIndex(resourceId);
107 const int entryIndex = ridToEntryIndex(resourceId);
108 return _datArchives[datIndex] ? _datArchives[datIndex]->getEntrySize(entryIndex) : 0;
109 }
110
ridToDatIndex(int resourceId)111 int ridToDatIndex(int resourceId) {
112 return (resourceId & 0xFFFF0000) >> 16;
113 }
114
ridToEntryIndex(int resourceId)115 int ridToEntryIndex(int resourceId) {
116 return resourceId & 0xFFFF;
117 }
118
makeRid(int datIndex,int entryIndex)119 int makeRid(int datIndex, int entryIndex) {
120 return (datIndex << 16) | entryIndex;
121 }
122
123 } // End of namespace Gnap
124