-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.cpp
145 lines (127 loc) · 3.34 KB
/
bank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Raw - Another World Interpreter
* Copyright (C) 2004 Gregory Montoir
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "bank.h"
#include "file.h"
#include "resource.h"
Bank::Bank(const char *dataDir)
: _dataDir(dataDir) {
}
bool Bank::read(const MemEntry *me, uint8_t *buf) {
bool ret = false;
char bankName[10];
sprintf(bankName, "bank%02x", me->bankId);
File f;
if (!f.open(bankName, _dataDir))
error("Bank::read() unable to open '%s'", bankName);
f.seek(me->bankOffset);
// Depending if the resource is packed or not we
// can read directly or unpack it.
if (me->packedSize == me->size) {
f.read(buf, me->packedSize);
ret = true;
} else {
f.read(buf, me->packedSize);
_startBuf = buf;
_iBuf = buf + me->packedSize - 4;
ret = unpack();
}
return ret;
}
void Bank::decUnk1(uint8_t numChunks, uint8_t addCount) {
uint16_t count = getCode(numChunks) + addCount + 1;
debug(DBG_BANK, "Bank::decUnk1(%d, %d) count=%d", numChunks, addCount, count);
_unpCtx.datasize -= count;
while (count--) {
assert(_oBuf >= _iBuf && _oBuf >= _startBuf);
*_oBuf = (uint8_t)getCode(8);
--_oBuf;
}
}
/*
Note from fab: This look like run-length encoding.
*/
void Bank::decUnk2(uint8_t numChunks) {
uint16_t i = getCode(numChunks);
uint16_t count = _unpCtx.size + 1;
debug(DBG_BANK, "Bank::decUnk2(%d) i=%d count=%d", numChunks, i, count);
_unpCtx.datasize -= count;
while (count--) {
assert(_oBuf >= _iBuf && _oBuf >= _startBuf);
*_oBuf = *(_oBuf + i);
--_oBuf;
}
}
/*
Most resource in the banks are compacted.
*/
bool Bank::unpack() {
_unpCtx.size = 0;
_unpCtx.datasize = READ_BE_UINT32(_iBuf); _iBuf -= 4;
_oBuf = _startBuf + _unpCtx.datasize - 1;
_unpCtx.crc = READ_BE_UINT32(_iBuf); _iBuf -= 4;
_unpCtx.chk = READ_BE_UINT32(_iBuf); _iBuf -= 4;
_unpCtx.crc ^= _unpCtx.chk;
do {
if (!nextChunk()) {
_unpCtx.size = 1;
if (!nextChunk()) {
decUnk1(3, 0);
} else {
decUnk2(8);
}
} else {
uint16_t c = getCode(2);
if (c == 3) {
decUnk1(8, 8);
} else {
if (c < 2) {
_unpCtx.size = c + 2;
decUnk2(c + 9);
} else {
_unpCtx.size = getCode(8);
decUnk2(12);
}
}
}
} while (_unpCtx.datasize > 0);
return (_unpCtx.crc == 0);
}
uint16_t Bank::getCode(uint8_t numChunks) {
uint16_t c = 0;
while (numChunks--) {
c <<= 1;
if (nextChunk()) {
c |= 1;
}
}
return c;
}
bool Bank::nextChunk() {
bool CF = rcr(false);
if (_unpCtx.chk == 0) {
assert(_iBuf >= _startBuf);
_unpCtx.chk = READ_BE_UINT32(_iBuf); _iBuf -= 4;
_unpCtx.crc ^= _unpCtx.chk;
CF = rcr(true);
}
return CF;
}
bool Bank::rcr(bool CF) {
bool rCF = (_unpCtx.chk & 1);
_unpCtx.chk >>= 1;
if (CF) _unpCtx.chk |= 0x80000000;
return rCF;
}