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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/* 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 "sci/engine/message.h"
#include "sci/tools.h"
namespace Sci {
MessageTuple MessageState::getTuple() {
MessageTuple t;
t.noun = *(_engineCursor.index_record + 0);
t.verb = *(_engineCursor.index_record + 1);
if (_version == 2) {
t.cond = 0;
t.seq = 1;
} else {
t.cond = *(_engineCursor.index_record + 2);
t.seq = *(_engineCursor.index_record + 3);
}
return t;
}
MessageTuple MessageState::getRefTuple() {
MessageTuple t;
if (_version == 2) {
t.noun = 0;
t.verb = 0;
t.cond = 0;
} else {
t.noun = *(_engineCursor.index_record + 7);
t.verb = *(_engineCursor.index_record + 8);
t.cond = *(_engineCursor.index_record + 9);
}
t.seq = 1;
return t;
}
void MessageState::initCursor() {
_engineCursor.index_record = _indexRecords;
_engineCursor.index = 0;
_engineCursor.nextSeq = 0;
}
void MessageState::advanceCursor(bool increaseSeq) {
_engineCursor.index_record += ((_version == 2) ? 4 : 11);
_engineCursor.index++;
if (increaseSeq)
_engineCursor.nextSeq++;
}
int MessageState::findTuple(MessageTuple &t) {
if (_module == -1)
return 0;
// Reset the cursor
initCursor();
_engineCursor.nextSeq = t.seq;
// Do a linear search for the message
while (1) {
MessageTuple looking_at = getTuple();
if (t.noun == looking_at.noun &&
t.verb == looking_at.verb &&
t.cond == looking_at.cond &&
t.seq == looking_at.seq)
break;
advanceCursor(false);
// Message tuple is not present
if (_engineCursor.index == _recordCount)
return 0;
}
return 1;
}
int MessageState::getMessage() {
if (_module == -1)
return 0;
if (_engineCursor.index != _recordCount) {
MessageTuple mesg = getTuple();
MessageTuple ref = getRefTuple();
if (_engineCursor.nextSeq == mesg.seq) {
// We found the right sequence number, check for recursion
if (ref.noun != 0) {
// Recursion, advance the current cursor and load the reference
advanceCursor(true);
if (findTuple(ref))
return getMessage();
else {
// Reference not found
return 0;
}
} else {
// No recursion, we are done
return 1;
}
}
}
// We either ran out of records, or found an incorrect sequence number. Go to previous stack frame.
if (!_cursorStack.empty()) {
_engineCursor = _cursorStack.pop();
return getMessage();
}
// Stack is empty, no message available
return 0;
}
int MessageState::getTalker() {
return (_version == 2) ? -1 : *(_engineCursor.index_record + 4);
}
MessageTuple &MessageState::getLastTuple() {
return _lastReturned;
}
int MessageState::getLastModule() {
return _lastReturnedModule;
}
Common::String MessageState::getText() {
char *str = (char *)_currentResource->data + READ_LE_UINT16(_engineCursor.index_record + ((_version == 2) ? 2 : 5));
Common::String strippedStr;
Common::String skippedSubstr;
bool skipping = false;
for (uint i = 0; i < strlen(str); i++) {
if (skipping) {
// Skip stage direction
skippedSubstr += str[i];
// Hopefully these locale-dependant functions are good enough
if (islower(str[i]) || isdigit(str[i])) {
// Lowercase or digit found, this is not a stage direction
strippedStr += skippedSubstr;
skipping = false;
} else if (str[i] == ')') {
// End of stage direction, skip trailing white space
while ((i + 1 < strlen(str)) && isspace(str[i + 1]))
i++;
skipping = false;
}
} else {
if (str[i] == '(') {
// Start skipping stage direction
skippedSubstr = str[i];
skipping = true;
} else if (str[i] == '\\') {
// Escape sequence
if ((i + 2 < strlen(str)) && isdigit(str[i + 1]) && isdigit(str[i + 2])) {
// Hex escape sequence
char hexStr[3];
hexStr[0] = str[++i];
hexStr[1] = str[++i];
hexStr[2] = 0;
char *endptr;
int hexNr = strtol(hexStr, &endptr, 16);
if (*endptr == 0)
strippedStr += hexNr;
} else if (i + 1 < strlen(str)) {
// Literal escape sequence
strippedStr += str[++i];
}
} else {
strippedStr += str[i];
}
}
}
return strippedStr;
}
void MessageState::gotoNext() {
_lastReturned = getTuple();
_lastReturnedModule = _module;
advanceCursor(true);
}
int MessageState::getLength() {
int offset = READ_LE_UINT16(_engineCursor.index_record + ((_version == 2) ? 2 : 5));
char *stringptr = (char *)_currentResource->data + offset;
return strlen(stringptr);
}
int MessageState::loadRes(ResourceManager *resmgr, int module, bool lock) {
if (_locked) {
// We already have a locked resource
if (_module == module) {
// If it's the same resource, we are done
return 1;
}
// Otherwise, free the old resource
resmgr->unlockResource(_currentResource);
_locked = false;
}
_currentResource = resmgr->findResource(ResourceId(kResourceTypeMessage, module), lock);
if (_currentResource == NULL || _currentResource->data == NULL) {
warning("Message: failed to load %d.msg", module);
return 0;
}
_module = module;
_locked = lock;
_version = READ_LE_UINT16(_currentResource->data);
debug(5, "Message: reading resource %d.msg, version %d.%03d", _module, _version / 1000, _version % 1000);
// We assume for now that storing the major version is sufficient
_version /= 1000;
int offs = (_version == 2) ? 0 : 4;
_recordCount = READ_LE_UINT16(_currentResource->data + 4 + offs);
_indexRecords = _currentResource->data + 6 + offs;
_cursorStack.clear();
initCursor();
return 1;
}
} // End of namespace Sci
|