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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/* 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "lure/strings.h"
#include "lure/disk.h"
#include "lure/lure.h"
#include "lure/res.h"
#include "lure/room.h"
#include "common/endian.h"
namespace Lure {
StringData *int_strings = NULL;
StringData::StringData() {
int_strings = this;
Disk &disk = Disk::getReference();
for (uint8 ctr = 0; ctr < MAX_NUM_CHARS; ++ctr) _chars[ctr] = NULL;
_numChars = 0;
_names = Disk::getReference().getEntry(NAMES_RESOURCE_ID);
_strings[0] = disk.getEntry(STRINGS_RESOURCE_ID);
_strings[1] = disk.getEntry(STRINGS_2_RESOURCE_ID);
_strings[2] = disk.getEntry(STRINGS_3_RESOURCE_ID);
// Add in the list of bit sequences, and what characters they represent
MemoryBlock *decoderList = disk.getEntry(STRING_DECODER_RESOURCE_ID);
const char *p = (const char *) decoderList->data();
while ((byte)*p != 0xff) {
char ascii = *p++;
add(p, ascii);
p += strlen(p) + 1;
}
delete decoderList;
}
StringData::~StringData() {
int_strings = NULL;
for (uint8 ctr = 0; ctr < MAX_NUM_CHARS; ++ctr)
if (_chars[ctr]) delete _chars[ctr];
else break;
delete _names;
delete _strings[0];
delete _strings[1];
delete _strings[2];
}
StringData &StringData::getReference() {
return *int_strings;
}
void StringData::add(const char *sequence, char ascii) {
uint32 value = 0;
for (uint8 index = 0; index < strlen(sequence); ++index) {
if (sequence[index] == '1')
value |= (1 << index);
else if (sequence[index] != '0')
error("Invalid character in string bit-stream sequence");
}
if (_numChars == MAX_NUM_CHARS)
error("Max characters too lower in string decoder");
_chars[_numChars++] = new CharacterEntry(strlen(sequence), value, ascii);
}
byte StringData::readBit() {
byte result = ((*_srcPos & _bitMask) != 0) ? 1 : 0;
_bitMask >>= 1;
if (_bitMask == 0) {
_bitMask = 0x80;
++_srcPos;
}
return result;
}
bool StringData::initPosition(uint16 stringId) {
uint16 roomNumber = Room::getReference().roomNumber();
byte *stringTable;
if ((roomNumber >= 0x2A) && (stringId >= STRING_ID_RANGE) && (stringId < STRING_ID_UPPER))
stringId = 0x76;
if ((roomNumber < 0x2A) && (stringId >= STRING_ID_UPPER))
stringId = 0x76;
if (stringId < STRING_ID_RANGE)
stringTable = _strings[0]->data();
else if (stringId < STRING_ID_RANGE*2) {
stringId -= STRING_ID_RANGE;
stringTable = _strings[1]->data();
} else {
stringId -= STRING_ID_RANGE * 2;
stringTable = _strings[2]->data();
}
_srcPos = stringTable + 4;
uint32 total = 0;
int numLoops = stringId >> 5;
for (int ctr = 0; ctr < numLoops; ++ctr) {
total += READ_LE_UINT16(_srcPos);
_srcPos += sizeof(uint16);
}
numLoops = stringId & 0x1f;
if (numLoops!= 0) {
byte *tempPtr = stringTable + (stringId & 0xffe0) + READ_LE_UINT16(stringTable);
for (int ctr = 0; ctr < numLoops; ++ctr) {
byte v = *tempPtr++;
if ((v & 0x80) == 0) {
total += v;
} else {
total += (v & 0x7f) << 3;
}
}
}
_bitMask = 0x80;
if ((total & 3) != 0)
_bitMask >>= (total & 3) * 2;
_srcPos = stringTable + (total >> 2) + READ_LE_UINT16(stringTable + 2);
// Final positioning to start of string
for (;;) {
if (readBit() == 0) break;
_srcPos += 2;
}
return readBit() != 0;
}
// readCharatcer
// Reads the next character from the input bit stream
char StringData::readCharacter() {
uint32 searchValue = 0;
// Loop through an increasing number of bits
for (uint8 numBits = 1; numBits <= 18; ++numBits) {
searchValue |= readBit() << (numBits - 1);
// Scan through list for a match
for (int index = 0; _chars[index] != NULL; ++index) {
if ((_chars[index]->_numBits == numBits) &&
(_chars[index]->_sequence == searchValue))
return _chars[index]->_ascii;
}
}
error("Unknown bit sequence encountered when decoding string");
return 0;
}
void StringData::getString(uint16 stringId, char *dest, const char *hotspotName,
const char *characterName, int hotspotArticle, int characterArticle) {
StringList &stringList = Resources::getReference().stringList();
char ch;
strcpy(dest, "");
char *destPos = dest;
stringId &= 0x1fff; // Strip off any article identifier
if (stringId == 0) return;
bool includeArticles = initPosition(stringId);
ch = readCharacter();
while (ch != '\0') {
if (ch == '%') {
// Copy over hotspot or action
ch = readCharacter();
const char *p = (ch == '1') ? hotspotName : characterName;
int article = !includeArticles ? 3 : ((ch == 1) ? hotspotArticle : characterArticle);
if (p != NULL) {
if (article > 0)
strcpy(destPos, stringList.getString(S_ARTICLE_LIST + article - 1));
strcat(destPos, p);
destPos += strlen(destPos);
}
} else if ((uint8) ch >= 0xa0) {
const char *p = getName((uint8) ch - 0xa0);
strcpy(destPos, p);
destPos += strlen(p);
} else {
*destPos++ = ch;
}
ch = readCharacter();
}
*destPos = '\0';
}
// getName
// Returns the name or fragment of word at the specified index in the names resource
char *StringData::getName(uint8 nameIndex) {
uint16 numNames = *((uint16 *) _names->data()) / 2;
if (nameIndex >= numNames)
error("Invalid name index was passed to getCharacterName");
uint16 nameStart = *((uint16 *) (_names->data() + (nameIndex * 2)));
return (char *) (_names->data() + nameStart);
}
} // namespace Lure
|