aboutsummaryrefslogtreecommitdiff
path: root/engines/hugo/text.cpp
blob: 617fccc36d91e1a1b9c37d8fb934f785076acb0e (plain)
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
263
264
265
266
267
268
269
270
271
/* 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.
 *
 */
#include "common/system.h"
#include "common/stream.h"

#include "hugo/hugo.h"
#include "hugo/text.h"

namespace Hugo {

TextHandler::TextHandler(HugoEngine *vm) : _vm(vm) {
	_textData = nullptr;
	_stringtData = nullptr;
	_textEngine = nullptr;
	_textIntro = nullptr;
	_textMouse = nullptr;
	_textParser = nullptr;
	_textUtil = nullptr;
	_screenNames = nullptr;
	_arrayNouns = nullptr;
	_arrayVerbs = nullptr;
}

TextHandler::~TextHandler() {
}

const char *TextHandler::getNoun(int idx1, int idx2) const {
	return _arrayNouns[idx1][idx2];
}

const char *TextHandler::getScreenNames(int screenIndex) const {
	return _screenNames[screenIndex];
}

const char *TextHandler::getStringtData(int stringIndex) const {
	return _stringtData[stringIndex];
}

const char *TextHandler::getTextData(int textIndex) const {
	return _textData[textIndex];
}

const char *TextHandler::getTextEngine(int engineIndex) const {
	return _textEngine[engineIndex];
}

const char *TextHandler::getTextIntro(int introIndex) const {
	return _textIntro[introIndex];
}

const char *TextHandler::getTextMouse(int mouseIndex) const {
	return _textMouse[mouseIndex];
}

const char *TextHandler::getTextParser(int parserIndex) const {
	return _textParser[parserIndex];
}

const char *TextHandler::getTextUtil(int utilIndex) const {
	return _textUtil[utilIndex];
}

const char *TextHandler::getVerb(int idx1, int idx2) const {
	return _arrayVerbs[idx1][idx2];
}

char **TextHandler::getNounArray(int idx1) const {
	return _arrayNouns[idx1];
}

char **TextHandler::getVerbArray(int idx1) const {
	return _arrayVerbs[idx1];
}

char **TextHandler::loadTextsVariante(Common::ReadStream &in, uint16 *arraySize) {
	int  len;
	char **res = nullptr;
	char *pos = nullptr;
	char *posBck = nullptr;

	for (int varnt = 0; varnt < _vm->_numVariant; varnt++) {
		int numTexts = in.readUint16BE();
		int entryLen = in.readUint16BE();
		pos = (char *)malloc(entryLen);
		if (varnt == _vm->_gameVariant) {
			if (arraySize)
				*arraySize = numTexts;
			res = (char **)malloc(sizeof(char *) * numTexts);
			res[0] = pos;
			in.read(res[0], entryLen);
			res[0] += DATAALIGNMENT;
		} else {
			in.read(pos, entryLen);
			posBck = pos;
		}

		pos += DATAALIGNMENT;

		for (int i = 1; i < numTexts; i++) {
			pos -= 2;

			len = READ_BE_UINT16(pos);
			pos += 2 + len;

			if (varnt == _vm->_gameVariant)
				res[i] = pos;
		}

		if (varnt != _vm->_gameVariant)
			free(posBck);
	}

	return res;
}

char ***TextHandler::loadTextsArray(Common::ReadStream &in) {
	char ***resArray = nullptr;
	uint16 arraySize;

	for (int varnt = 0; varnt < _vm->_numVariant; varnt++) {
		arraySize = in.readUint16BE();
		if (varnt == _vm->_gameVariant) {
			resArray = (char ***)malloc(sizeof(char **) * (arraySize + 1));
			resArray[arraySize] = nullptr;
		}
		for (int i = 0; i < arraySize; i++) {
			int numTexts = in.readUint16BE();
			int entryLen = in.readUint16BE();
			char *pos = (char *)malloc(entryLen);
			char *posBck = nullptr;
			char **res = nullptr;
			if (varnt == _vm->_gameVariant) {
				res = (char **)malloc(sizeof(char *) * numTexts);
				res[0] = pos;
				in.read(res[0], entryLen);
				res[0] += DATAALIGNMENT;
			} else {
				in.read(pos, entryLen);
				posBck = pos;
			}

			pos += DATAALIGNMENT;

			for (int j = 0; j < numTexts; j++) {
				if (varnt == _vm->_gameVariant)
					res[j] = pos;

				pos -= 2;
				int len = READ_BE_UINT16(pos);
				pos += 2 + len;
			}

			if (varnt == _vm->_gameVariant)
				resArray[i] = res;
			else
				free(posBck);
		}
	}

	return resArray;
}

char **TextHandler::loadTexts(Common::ReadStream &in) {
	int numTexts = in.readUint16BE();
	char **res = (char **)malloc(sizeof(char *) * numTexts);
	int entryLen = in.readUint16BE();
	char *pos = (char *)malloc(entryLen);

	in.read(pos, entryLen);

	pos += DATAALIGNMENT;
	res[0] = pos;

	for (int i = 1; i < numTexts; i++) {
		pos -= 2;
		int len = READ_BE_UINT16(pos);
		pos += 2 + len;
		res[i] = pos;
	}

	return res;
}

void TextHandler::loadAllTexts(Common::ReadStream &in) {
	// Read textData
	_textData = loadTextsVariante(in, 0);

	// Read stringtData
	// Only Hugo 1 DOS should use this array
	_stringtData = loadTextsVariante(in, 0);

	// Read arrayNouns
	_arrayNouns = loadTextsArray(in);

	// Read arrayVerbs
	_arrayVerbs = loadTextsArray(in);

	// Read screenNames
	_screenNames = loadTextsVariante(in, &_vm->_numScreens);

	// Read textEngine
	_textEngine = loadTexts(in);

	// Read textIntro
	_textIntro = loadTextsVariante(in, 0);

	// Read textMouse
	_textMouse = loadTexts(in);

	// Read textParser
	_textParser = loadTexts(in);

	// Read textUtil
	_textUtil = loadTextsVariante(in, 0);
}

void TextHandler::freeTexts(char **ptr) {
	if (!ptr)
		return;

	free(*ptr - DATAALIGNMENT);
	free(ptr);
	ptr = nullptr;
}

void TextHandler::freeAllTexts() {
	freeTexts(_textData);
	freeTexts(_stringtData);

	if (_arrayNouns) {
		for (int i = 0; _arrayNouns[i]; i++)
			freeTexts(_arrayNouns[i]);
		free(_arrayNouns);
		_arrayNouns = nullptr;
	}

	if (_arrayVerbs) {
		for (int i = 0; _arrayVerbs[i]; i++)
			freeTexts(_arrayVerbs[i]);
		free(_arrayVerbs);
		_arrayVerbs = nullptr;
	}

	freeTexts(_screenNames);
	freeTexts(_textEngine);
	freeTexts(_textIntro);
	freeTexts(_textMouse);
	freeTexts(_textParser);
	freeTexts(_textUtil);
}

} // End of namespace Hugo