File: localize.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (162 lines) | stat: -rw-r--r-- 4,809 bytes parent folder | download | duplicates (2)
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
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM 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/file.h"
#include "common/str.h"
#include "common/endian.h"

#include "engines/grim/localize.h"
#include "engines/grim/grim.h"
#include "engines/grim/resource.h"

namespace Grim {

Localizer *g_localizer = nullptr;

Localizer::Localizer() {
	// To avoid too wide lines further below, we just name these here.
	bool isAnyDemo = g_grim->getGameFlags() & ADGF_DEMO;
	bool isGrimDemo = g_grim->getGameType() == GType_GRIM && isAnyDemo;
	bool isGerman = g_grim->getGameLanguage() == Common::DE_DEU;
	bool isFrench = g_grim->getGameLanguage() == Common::FR_FRA;
	bool isItalian = g_grim->getGameLanguage() == Common::IT_ITA;
	bool isSpanish = g_grim->getGameLanguage() == Common::ES_ESP;
	bool isTranslatedGrimDemo = (isGerman || isFrench || isItalian || isSpanish) && isGrimDemo;
	bool isPS2 = g_grim->getGamePlatform() == Common::kPlatformPS2;

	if (isGrimDemo && !isTranslatedGrimDemo)
		return;

	Common::String filename;
	if (g_grim->getGameType() == GType_MONKEY4) {
		filename = "script.tab";
	} else {
		if (isTranslatedGrimDemo) {
			filename = "language.tab";
		} else {
			filename = "grim.tab";
		}
	}

	Common::SeekableReadStream *f = g_resourceloader->openNewStreamFile(filename);
	if (!f) {
		error("Localizer::Localizer: Unable to find localization information (%s)", filename.c_str());
		return;
	}

	int32 filesize = f->size();

	// Read in the data
	char *data = new char[filesize + 1];
	f->read(data, filesize);
	data[filesize] = '\0';
	delete f;

	// Explicitly white-list german demo, as it has a .tab-file
	if ((isTranslatedGrimDemo) || (!isAnyDemo && !isPS2)) {
		if (filesize < 4)
			error("%s to short: %i", filename.c_str(), filesize);
		switch (READ_BE_UINT32(data)) {
		case MKTAG('R','C','N','E'):
			// Decode the data
			if (g_grim->getGameType() == GType_MONKEY4) {
				uint32 next = 0x16;
				for (int i = 4; i < filesize; i++) {
					next = next * 0x343FD + 0x269EC3;
					data[i] ^= (int)(((((next >> 16) & 0x7FFF) / 32767.f) * 254) + 1);
				}
			} else {
				for (int i = 4; i < filesize; i++) {
					data[i] ^= '\xdd';
				}
			}
		case MKTAG('D', 'O', 'E', 'L'):
			break;
		default:
			error("Invalid magic reading %s: %08x", filename.c_str(), READ_BE_UINT32(data));
		}
	}

	char *nextline = data;
	Common::String last_entry;
	//Read file till end
	for (char *line = data + 4; line - data <= filesize; line = nextline + 1) {
		if (line == nullptr || nextline == nullptr) {
			break;
		}

		nextline = strchr(line, '\n');
		//if there is no next line we arrived the last one
		if (nextline == nullptr) {
			nextline = strchr(line, '\0');
		}

		//in grim we have to exit on first empty line else skip line
		if (*line == '\r') {
			if (g_grim->getGameType() == GType_GRIM) {
				break;
			}

			nextline = strchr(line + 2, '\n');
			continue;
		}

		//EMI has an garbage line which should be ignored
		if (g_grim->getGameType() == GType_MONKEY4 && *line == '\x1A')
			continue;

		char *tab = strchr(line, '\t');
		//skip line if no tab found
		if (tab == nullptr) {
			continue;
		}

		if (tab > nextline) {
			Common::String cont = Common::String(line, nextline - line - 1);
			assert(last_entry != "");
			warning("Continuation line: \"%s\" = \"%s\" + \"%s\"", last_entry.c_str(), _entries[last_entry].c_str(), cont.c_str());
			_entries[last_entry] += cont;
		} else {
			_entries[last_entry = Common::String(line, tab - line)] = Common::String(tab + 1, (nextline - tab - 2));
		}
	}
	delete[] data;
}

Common::String Localizer::localize(const char *str) const {
	assert(str);

	const char *slash2;

	if (str[0] != '/' || str[0] == 0 || !(slash2 = strchr(str + 1, '/')))
		return str;

	Common::String key(str + 1, slash2 - str - 1);
	Common::StringMap::iterator it = _entries.find(key);
	if (it != _entries.end()) {
		return it->_value;
	} else {
		return slash2 + 1;
	}
}

} // end of namespace Grim