1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 * String resource managment routines
22 */
23
24 #include "tinsel/dw.h"
25 #include "tinsel/drives.h"
26 #include "tinsel/sound.h"
27 #include "tinsel/strres.h"
28 #include "tinsel/scn.h"
29 #include "common/file.h"
30 #include "common/endian.h"
31 #include "common/textconsole.h"
32
33 #include "gui/message.h"
34
35 namespace Tinsel {
36
37 // FIXME: Avoid non-const global vars
38
39 #ifdef DEBUG
40 // Diagnostic number
41 int g_newestString;
42 #endif
43
44 // buffer for resource strings
45 static uint8 *g_textBuffer = 0;
46
47 static struct {
48 bool bPresent;
49 const char *szStem;
50 SCNHANDLE hDescription;
51 SCNHANDLE hFlagFilm;
52
53 } g_languages[NUM_LANGUAGES] = {
54
55 { false, "English", 0, 0 },
56 { false, "French", 0, 0 },
57 { false, "German", 0, 0 },
58 { false, "Italian", 0, 0 },
59 { false, "Spanish", 0, 0 },
60 { false, "Hebrew", 0, 0 },
61 { false, "Magyar", 0, 0 },
62 { false, "Japanese",0, 0 },
63 { false, "US", 0, 0 }
64 };
65
66
67 // Set if we're handling 2-byte characters.
68 bool g_bMultiByte = false;
69
70 LANGUAGE g_textLanguage, g_sampleLanguage = TXT_ENGLISH;
71
72 //----------------- LOCAL DEFINES ----------------------------
73
74 #define languageExtension ".txt"
75 #define indexExtension ".idx"
76 #define sampleExtension ".smp"
77
78 //----------------- FUNCTIONS --------------------------------
79
80 /**
81 * Called to load a resource file for a different language
82 * @param newLang The new language
83 */
ChangeLanguage(LANGUAGE newLang)84 void ChangeLanguage(LANGUAGE newLang) {
85 TinselFile f;
86 uint32 textLen = 0; // length of buffer
87
88 g_textLanguage = newLang;
89 g_sampleLanguage = newLang;
90
91 // free the previous buffer
92 free(g_textBuffer);
93 g_textBuffer = NULL;
94
95 // Try and open the specified language file. If it fails, and the language
96 // isn't English, try falling back on opening 'english.txt' - some foreign
97 // language versions reused it rather than their proper filename
98 if (!f.open(_vm->getTextFile(newLang))) {
99 if ((newLang == TXT_ENGLISH) || !f.open(_vm->getTextFile(TXT_ENGLISH))) {
100 char buf[50];
101 sprintf(buf, CANNOT_FIND_FILE, _vm->getTextFile(newLang));
102 GUI::MessageDialog dialog(buf, "OK");
103 dialog.runModal();
104
105 error(CANNOT_FIND_FILE, _vm->getTextFile(newLang));
106 }
107 }
108
109 // Check whether the file is compressed or not - for compressed files the
110 // first long is the filelength and for uncompressed files it is the chunk
111 // identifier
112 textLen = f.readUint32();
113 if (f.eos() || f.err())
114 error(FILE_IS_CORRUPT, _vm->getTextFile(newLang));
115
116 if (textLen == CHUNK_STRING || textLen == CHUNK_MBSTRING) {
117 // the file is uncompressed
118
119 g_bMultiByte = (textLen == CHUNK_MBSTRING);
120
121 // get length of uncompressed file
122 textLen = f.size();
123 f.seek(0, SEEK_SET); // Set to beginning of file
124
125 if (g_textBuffer == NULL) {
126 // allocate a text buffer for the strings
127 g_textBuffer = (uint8 *)malloc(textLen);
128
129 // make sure memory allocated
130 assert(g_textBuffer);
131 }
132
133 // load data
134 if (f.read(g_textBuffer, textLen) != textLen)
135 // file must be corrupt if we get to here
136 error(FILE_IS_CORRUPT, _vm->getTextFile(newLang));
137
138 // close the file
139 f.close();
140 } else { // the file must be compressed
141 error("Compression handling has been removed");
142 }
143 }
144
145 /**
146 * FindStringBase
147 */
FindStringBase(int id)148 static byte *FindStringBase(int id) {
149 // base of string resource table
150 byte *pText = g_textBuffer;
151
152 // For Tinsel 0, Ids are decremented by 1
153 if (TinselV0)
154 --id;
155
156 // index into text resource file
157 uint32 index = 0;
158
159 // number of chunks to skip
160 int chunkSkip = id / STRINGS_PER_CHUNK;
161
162 // number of strings to skip when in the correct chunk
163 int strSkip = id % STRINGS_PER_CHUNK;
164
165 // skip to the correct chunk
166 while (chunkSkip-- != 0) {
167 // make sure chunk id is correct
168 assert(READ_32(pText + index) == CHUNK_STRING || READ_32(pText + index) == CHUNK_MBSTRING);
169
170 if (READ_32(pText + index + sizeof(uint32)) == 0) {
171 // string does not exist
172 return NULL;
173 }
174
175 // get index to next chunk
176 index = READ_32(pText + index + sizeof(uint32));
177 }
178
179 // skip over chunk id and offset
180 index += (2 * sizeof(uint32));
181
182 // pointer to strings
183 pText = pText + index;
184
185 // skip to the correct string
186 while (strSkip-- != 0) {
187 // skip to next string
188
189 if (!TinselV2 || ((*pText & 0x80) == 0)) {
190 // Tinsel 1, or string of length < 128
191 pText += *pText + 1;
192 } else if (*pText == 0x80) {
193 // string of length 128 - 255
194 pText++; // skip control byte
195 pText += *pText + 1;
196 } else if (*pText == 0x90) {
197 // string of length 256 - 511
198 pText++; // skip control byte
199 pText += *pText + 1 + 256;
200 } else { // multiple string
201 int subCount;
202
203 subCount = *pText & ~0x80;
204 pText++; // skip control byte
205
206 // skip prior sub-strings
207 while (subCount--) {
208 // skip control byte, if there is one
209 if (*pText == 0x80) {
210 pText++;
211 pText += *pText + 1;
212 } else if (*pText == 0x90) {
213 pText++;
214 pText += *pText + 1 + 256;
215 } else
216 pText += *pText + 1;
217 }
218 }
219 }
220
221 return pText;
222 }
223
224
225 /**
226 * Loads a string resource identified by id.
227 * @param id identifier of string to be loaded
228 * @param pBuffer points to buffer that receives the string
229 * @param bufferMax maximum number of chars to be copied to the buffer
230 */
LoadStringResource(int id,int sub,char * pBuffer,int bufferMax)231 int LoadStringResource(int id, int sub, char *pBuffer, int bufferMax) {
232 int len; // length of string
233
234 byte *pText = FindStringBase(id);
235
236 if (pText == NULL) {
237 strcpy(pBuffer, "!! HIGH STRING !!");
238 return 0;
239 }
240
241 if (!TinselV2 || ((*pText & 0x80) == 0)) {
242 // get length of string
243 len = *pText;
244 } else if (*pText == 0x80) {
245 // string of length 128 - 255
246 pText++; // skip control byte
247
248 // get length of string
249 len = *pText;
250 } else if (*pText == 0x90) {
251 // string of length 128 - 255
252 pText++; // skip control byte
253
254 // get length of string
255 len = *pText + 256;
256 } else {
257 // multiple string
258 pText++; // skip control byte
259
260 // skip prior sub-strings
261 while (sub--) {
262 // skip control byte, if there is one
263 if (*pText == 0x80) {
264 pText++;
265 pText += *pText + 1;
266 } else if (*pText == 0x90) {
267 pText++;
268 pText += *pText + 1 + 256;
269 } else
270 pText += *pText + 1;
271 }
272 // skip control byte, if there is one
273 if (*pText == 0x80) {
274 pText++;
275
276 // get length of string
277 len = *pText;
278 } else if (*pText == 0x90) {
279 pText++;
280
281 // get length of string
282 len = *pText + 256;
283 } else {
284 // get length of string
285 len = *pText;
286 }
287 }
288
289 if (len) {
290 // the string exists
291
292 // copy the string to the buffer
293 if (len < bufferMax) {
294 memcpy(pBuffer, pText + 1, len);
295
296 // null terminate
297 pBuffer[len] = 0;
298
299 // number of chars copied
300 return len + 1;
301 } else {
302 memcpy(pBuffer, pText + 1, bufferMax - 1);
303
304 // null terminate
305 pBuffer[bufferMax - 1] = 0;
306
307 // number of chars copied
308 return bufferMax;
309 }
310 }
311
312 // TEMPORARY DIRTY BODGE
313 strcpy(pBuffer, "!! NULL STRING !!");
314
315 // string does not exist
316 return 0;
317 }
318
LoadStringRes(int id,char * pBuffer,int bufferMax)319 int LoadStringRes(int id, char *pBuffer, int bufferMax) {
320 return LoadStringResource(id, 0, pBuffer, bufferMax);
321 }
322
323 /**
324 * Loads a string resource identified by id
325 * @param id identifier of string to be loaded
326 * @param sub sub-string number
327 * @param pBuffer points to buffer that receives the string
328 * @param bufferMax maximum number of chars to be copied to the buffer
329 */
LoadSubString(int id,int sub,char * pBuffer,int bufferMax)330 int LoadSubString(int id, int sub, char *pBuffer, int bufferMax) {
331 return LoadStringResource(id, sub, pBuffer, bufferMax);
332 }
333
334 /**
335 * SubStringCount
336 * @param id Identifier of string to be tested
337 */
SubStringCount(int id)338 int SubStringCount(int id) {
339 byte *pText;
340
341 pText = FindStringBase(id);
342
343 if (pText == NULL)
344 return 0;
345
346 if ((*pText & 0x80) == 0 || *pText == 0x80 || *pText == 0x90) {
347 // string of length < 128 or string of length 128 - 255
348 // or of length 256 - 511
349 return 1;
350 } else
351 return (*pText & ~0x80);
352 }
353
354
FreeTextBuffer()355 void FreeTextBuffer() {
356 free(g_textBuffer);
357 g_textBuffer = NULL;
358 }
359
360 /**
361 * Called from TINLIB.C from DeclareLanguage().
362 */
363
LanguageFacts(int language,SCNHANDLE hDescription,SCNHANDLE hFlagFilm)364 void LanguageFacts(int language, SCNHANDLE hDescription, SCNHANDLE hFlagFilm) {
365 assert(language >= 0 && language < NUM_LANGUAGES);
366
367 g_languages[language].hDescription = hDescription;
368 g_languages[language].hFlagFilm = hFlagFilm;
369 }
370
371 /**
372 * Gets the current subtitles language
373 */
TextLanguage()374 LANGUAGE TextLanguage() {
375 return g_textLanguage;
376 }
377
378 /**
379 * Gets the current voice language
380 */
SampleLanguage()381 LANGUAGE SampleLanguage() {
382 return g_sampleLanguage;
383 }
384
NumberOfLanguages()385 int NumberOfLanguages() {
386 int i, count;
387
388 for (i = 0, count = 0; i < NUM_LANGUAGES; i++) {
389 if (g_languages[i].bPresent)
390 count++;
391 }
392 return count;
393 }
394
NextLanguage(LANGUAGE thisOne)395 LANGUAGE NextLanguage(LANGUAGE thisOne) {
396 int i;
397
398 for (i = thisOne+1; i < NUM_LANGUAGES; i++) {
399 if (g_languages[i].bPresent)
400 return (LANGUAGE)i;
401 }
402
403 for (i = 0; i < thisOne; i++) {
404 if (g_languages[i].bPresent)
405 return (LANGUAGE)i;
406 }
407
408 // No others!
409 return thisOne;
410 }
411
PrevLanguage(LANGUAGE thisOne)412 LANGUAGE PrevLanguage(LANGUAGE thisOne) {
413 int i;
414
415 for (i = thisOne-1; i >= 0; i--) {
416 if (g_languages[i].bPresent)
417 return (LANGUAGE)i;
418 }
419
420 for (i = NUM_LANGUAGES-1; i > thisOne; i--) {
421 if (g_languages[i].bPresent)
422 return (LANGUAGE)i;
423 }
424
425 // No others!
426 return thisOne;
427 }
428
LanguageDesc(LANGUAGE thisOne)429 SCNHANDLE LanguageDesc(LANGUAGE thisOne) {
430 return g_languages[thisOne].hDescription;
431 }
432
LanguageFlag(LANGUAGE thisOne)433 SCNHANDLE LanguageFlag(LANGUAGE thisOne) {
434 return g_languages[thisOne].hFlagFilm;
435 }
436
437 } // End of namespace Tinsel
438