aboutsummaryrefslogtreecommitdiff
path: root/engines/tinsel/strres.cpp
blob: 8f9f72f446cab7eadc8a2ff38b22c3ae4f09290f (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* 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$
 *
 * String resource managment routines
 */

#include "tinsel/dw.h"
#include "tinsel/drives.h"
#include "tinsel/sound.h"
#include "tinsel/strres.h"
#include "tinsel/scn.h"
#include "common/file.h"
#include "common/endian.h"

#include "gui/message.h"

namespace Tinsel {

#ifdef DEBUG
// Diagnostic number
int newestString;
#endif

// buffer for resource strings
static uint8 *textBuffer = 0;

static struct {
	bool		bPresent;
	const char	*szStem;
	SCNHANDLE	hDescription;
	SCNHANDLE	hFlagFilm;

} languages[NUM_LANGUAGES] = {

	{ false, "English",	0, 0 },
	{ false, "French",	0, 0 },
	{ false, "German",	0, 0 },
	{ false, "Italian",	0, 0 },
	{ false, "Spanish",	0, 0 },
	{ false, "Hebrew",	0, 0 },
	{ false, "Magyar",	0, 0 },
	{ false, "Japanese",0, 0 },
	{ false, "US",		0, 0 }
};


// Set if we're handling 2-byte characters.
bool bMultiByte = false;

LANGUAGE textLanguage, sampleLanguage = TXT_ENGLISH;

//----------------- LOCAL DEFINES ----------------------------

#define languageExtension	".txt"
#define indexExtension		".idx"
#define sampleExtension		".smp"

//----------------- FUNCTIONS --------------------------------

/**
 * Called to load a resource file for a different language
 * @param newLang			The new language
 */
void ChangeLanguage(LANGUAGE newLang) {
	TinselFile f;
	uint32 textLen = 0;	// length of buffer

	textLanguage = newLang;
	sampleLanguage = newLang;

	if (textBuffer) {
		// free the previous buffer
		free(textBuffer);
		textBuffer = NULL;
	}

	// Try and open the specified language file. If it fails, and the language
	// isn't English, try falling back on opening 'english.txt' - some foreign
	// language versions reused it rather than their proper filename
	if (!f.open(_vm->getTextFile(newLang))) {
		if ((newLang == TXT_ENGLISH) || !f.open(_vm->getTextFile(TXT_ENGLISH))) {
			char buf[50];
			sprintf(buf, CANNOT_FIND_FILE, _vm->getTextFile(newLang));
			GUI::MessageDialog dialog(buf, "OK");
			dialog.runModal();

			error(CANNOT_FIND_FILE, _vm->getTextFile(newLang));
		}
	}

	// Check whether the file is compressed or not -  for compressed files the
	// first long is the filelength and for uncompressed files it is the chunk
	// identifier
	textLen = f.readUint32LE();
	if (f.eos() || f.err())
		error(FILE_IS_CORRUPT, _vm->getTextFile(newLang));

	if (textLen == CHUNK_STRING || textLen == CHUNK_MBSTRING) {
		// the file is uncompressed

		bMultiByte = (textLen == CHUNK_MBSTRING);

		// get length of uncompressed file
		textLen = f.size();
		f.seek(0, SEEK_SET);	// Set to beginning of file

		if (textBuffer == NULL) {
			// allocate a text buffer for the strings
			textBuffer = (uint8 *)malloc(textLen);

			// make sure memory allocated
			assert(textBuffer);
		}

		// load data
		if (f.read(textBuffer, textLen) != textLen)
			// file must be corrupt if we get to here
			error(FILE_IS_CORRUPT, _vm->getTextFile(newLang));

		// close the file
		f.close();
	} else {	// the file must be compressed
		error("Compression handling has been removed");
	}
}

/**
 * FindStringBase
 */
static byte *FindStringBase(int id) {
	// base of string resource table
	byte *pText = textBuffer;

	// For Tinsel 0, Ids are decremented by 1
	if (TinselV0)
		--id;

	// index into text resource file
	uint32 index = 0;

	// number of chunks to skip
	int chunkSkip = id / STRINGS_PER_CHUNK;

	// number of strings to skip when in the correct chunk
	int strSkip = id % STRINGS_PER_CHUNK;

	// skip to the correct chunk
	while (chunkSkip-- != 0) {
		// make sure chunk id is correct
		assert(READ_LE_UINT32(pText + index) == CHUNK_STRING || READ_LE_UINT32(pText + index) == CHUNK_MBSTRING);

		if (READ_LE_UINT32(pText + index + sizeof(uint32)) == 0) {
			// string does not exist
			return NULL;
		}

		// get index to next chunk
		index = READ_LE_UINT32(pText + index + sizeof(uint32));
	}

	// skip over chunk id and offset
	index += (2 * sizeof(uint32));

	// pointer to strings
	pText = pText + index;

	// skip to the correct string
	while (strSkip-- != 0) {
		// skip to next string

		if (!TinselV2 || ((*pText & 0x80) == 0)) {
			// Tinsel 1, or string of length < 128
			pText += *pText + 1;
		} else if (*pText == 0x80) {
			// string of length 128 - 255
			pText++;		// skip control byte
			pText += *pText + 1;
		} else if (*pText == 0x90) {
			// string of length 256 - 511
			pText++;		// skip control byte
			pText += *pText + 1 + 256;
		} else {	// multiple string
			int subCount;

			subCount = *pText & ~0x80;
			pText++;		// skip control byte

			// skip prior sub-strings
			while (subCount--) {
				// skip control byte, if there is one
				if (*pText == 0x80) {
					pText++;
					pText += *pText + 1;
				} else if (*pText == 0x90) {
					pText++;
					pText += *pText + 1 + 256;
				} else
					pText += *pText + 1;
			}
		}
	}

	return pText;
}


/**
 * Loads a string resource identified by id.
 * @param id			identifier of string to be loaded
 * @param pBuffer		points to buffer that receives the string
 * @param bufferMax		maximum number of chars to be copied to the buffer
 */
int LoadStringResource(int id, int sub, char *pBuffer, int bufferMax) {
	int len;	// length of string

	byte *pText = FindStringBase(id);

	if (pText == NULL) {
		strcpy(pBuffer, "!! HIGH STRING !!");
		return 0;
	}

	if (!TinselV2 || ((*pText & 0x80) == 0)) {
		// get length of string
		len = *pText;
	} else if (*pText == 0x80) {
		// string of length 128 - 255
		pText++;		// skip control byte

		// get length of string
		len = *pText;
	} else if (*pText == 0x90) {
		// string of length 128 - 255
		pText++;		// skip control byte

		// get length of string
		len = *pText + 256;
	} else {
		// multiple string
		pText++;		// skip control byte

		// skip prior sub-strings
		while (sub--) {
			// skip control byte, if there is one
			if (*pText == 0x80) {
				pText++;
				pText += *pText + 1;
			} else if (*pText == 0x90) {
				pText++;
				pText += *pText + 1 + 256;
			} else
				pText += *pText + 1;
		}
		// skip control byte, if there is one
		if (*pText == 0x80) {
			pText++;

			// get length of string
			len = *pText;
		} else if (*pText == 0x90) {
			pText++;

			// get length of string
			len = *pText + 256;
		} else {
			// get length of string
			len = *pText;
		}
	}

	if (len) {
		// the string exists

		// copy the string to the buffer
		if (len < bufferMax) {
			memcpy(pBuffer, pText + 1, len);

			// null terminate
			pBuffer[len] = 0;

			// number of chars copied
			return len + 1;
		} else {
			memcpy(pBuffer, pText + 1, bufferMax - 1);

			// null terminate
			pBuffer[bufferMax - 1] = 0;

			// number of chars copied
			return bufferMax;
		}
	}

	// TEMPORARY DIRTY BODGE
	strcpy(pBuffer, "!! NULL STRING !!");

	// string does not exist
	return 0;
}

int LoadStringRes(int id, char *pBuffer, int bufferMax) {
	return LoadStringResource(id, 0, pBuffer, bufferMax);
}

/**
 * Loads a string resource identified by id
 * @param id			identifier of string to be loaded
 * @param sub			sub-string number
 * @param pBuffer		points to buffer that receives the string
 * @param bufferMax		maximum number of chars to be copied to the buffer
 */
int LoadSubString(int id, int sub, char *pBuffer, int bufferMax) {
	return LoadStringResource(id, sub, pBuffer, bufferMax);
}

/**
 * SubStringCount
 * @param id			Identifier of string to be tested
 */
int SubStringCount(int id) {
	byte *pText;

	pText = FindStringBase(id);

	if (pText == NULL)
		return 0;

	if ((*pText & 0x80) == 0 || *pText == 0x80 || *pText == 0x90) {
		// string of length < 128 or string of length 128 - 255
		// or of length 256 - 511
		return 1;
	} else
		return (*pText & ~0x80);
}


void FreeTextBuffer() {
	if (textBuffer) {
		free(textBuffer);
		textBuffer = NULL;
	}
}

/**
 * Called from TINLIB.C from DeclareLanguage().
 */

void LanguageFacts(int language, SCNHANDLE hDescription, SCNHANDLE hFlagFilm) {
	assert(language >= 0 && language < NUM_LANGUAGES);

	languages[language].hDescription = hDescription;
	languages[language].hFlagFilm	 = hFlagFilm;
}

/**
 * Gets the current subtitles language
 */
LANGUAGE TextLanguage() {
	return textLanguage;
}

/**
 * Gets the current voice language
 */
LANGUAGE SampleLanguage() {
	return sampleLanguage;
}

int NumberOfLanguages() {
	int i, count;

	for (i = 0, count = 0; i < NUM_LANGUAGES; i++) {
		if (languages[i].bPresent)
			count++;
	}
	return count;
}

LANGUAGE NextLanguage(LANGUAGE thisOne) {
	int i;

	for (i = thisOne+1; i < NUM_LANGUAGES; i++) {
		if (languages[i].bPresent)
			return (LANGUAGE)i;
	}

	for (i = 0; i < thisOne; i++) {
		if (languages[i].bPresent)
			return (LANGUAGE)i;
	}

	// No others!
	return thisOne;
}

LANGUAGE PrevLanguage(LANGUAGE thisOne) {
	int i;

	for (i = thisOne-1; i >= 0; i--) {
		if (languages[i].bPresent)
			return (LANGUAGE)i;
	}

	for (i = NUM_LANGUAGES-1; i > thisOne; i--) {
		if (languages[i].bPresent)
			return (LANGUAGE)i;
	}

	// No others!
	return thisOne;
}

SCNHANDLE LanguageDesc(LANGUAGE thisOne) {
	return languages[thisOne].hDescription;
}

SCNHANDLE LanguageFlag(LANGUAGE thisOne) {
	return languages[thisOne].hFlagFilm;
}

} // End of namespace Tinsel