2012-01-06 23:29:45 +01:00
|
|
|
/* ResidualVM - A 3D game interpreter
|
2008-06-13 14:57:47 +00:00
|
|
|
*
|
2012-01-06 23:29:45 +01:00
|
|
|
* ResidualVM is the legal property of its developers, whose names
|
2011-04-16 14:12:44 +02:00
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
2008-06-13 14:57:47 +00:00
|
|
|
* file distributed with this source distribution.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* 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.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2006-04-02 14:20:45 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-12-19 23:15:43 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-04-05 18:18:42 +02:00
|
|
|
*
|
2012-12-19 23:15:43 +01:00
|
|
|
* 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.
|
2006-04-02 14:20:45 +00:00
|
|
|
*
|
|
|
|
*/
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2009-05-10 07:31:02 +00:00
|
|
|
#include "common/file.h"
|
2009-05-10 09:27:35 +00:00
|
|
|
#include "common/str.h"
|
|
|
|
#include "common/endian.h"
|
2015-01-28 20:12:58 +01:00
|
|
|
#include "common/tokenizer.h"
|
2009-05-10 07:31:02 +00:00
|
|
|
|
2009-05-24 19:13:58 +00:00
|
|
|
#include "engines/grim/localize.h"
|
|
|
|
#include "engines/grim/grim.h"
|
2011-12-21 22:53:58 +01:00
|
|
|
#include "engines/grim/resource.h"
|
2005-01-01 12:27:57 +00:00
|
|
|
|
2009-05-25 06:49:57 +00:00
|
|
|
namespace Grim {
|
|
|
|
|
2014-05-29 15:35:46 -07:00
|
|
|
Localizer *g_localizer = nullptr;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
|
|
|
Localizer::Localizer() {
|
2015-01-18 15:08:12 +01:00
|
|
|
// 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;
|
2015-01-18 17:00:12 +01:00
|
|
|
bool isFrench = g_grim->getGameLanguage() == Common::FR_FRA;
|
2015-07-08 16:12:22 +10:00
|
|
|
bool isItalian = g_grim->getGameLanguage() == Common::IT_ITA;
|
2015-07-06 13:57:22 +10:00
|
|
|
bool isSpanish = g_grim->getGameLanguage() == Common::ES_ESP;
|
2015-07-08 16:12:22 +10:00
|
|
|
bool isTranslatedGrimDemo = (isGerman || isFrench || isItalian || isSpanish) && isGrimDemo;
|
2015-01-18 15:08:12 +01:00
|
|
|
bool isPS2 = g_grim->getGamePlatform() == Common::kPlatformPS2;
|
2015-02-02 20:47:40 +01:00
|
|
|
bool isRemastered = g_grim->getGameFlags() & ADGF_REMASTERED; // TODO: Add handling of this from g_grim.
|
2015-01-18 15:08:12 +01:00
|
|
|
|
2015-01-18 17:00:12 +01:00
|
|
|
if (isGrimDemo && !isTranslatedGrimDemo)
|
2005-08-27 16:08:44 +00:00
|
|
|
return;
|
|
|
|
|
2012-04-25 21:18:18 -07:00
|
|
|
Common::String filename;
|
|
|
|
if (g_grim->getGameType() == GType_MONKEY4) {
|
|
|
|
filename = "script.tab";
|
|
|
|
} else {
|
2015-01-28 20:12:58 +01:00
|
|
|
if (isRemastered) {
|
2015-02-03 14:08:03 +01:00
|
|
|
filename = Common::String("grim.") + g_grim->getLanguagePrefix() + Common::String(".tab"); // TODO: Detect based on language.
|
2015-01-28 20:12:58 +01:00
|
|
|
} else if (isTranslatedGrimDemo) {
|
2015-01-18 15:08:12 +01:00
|
|
|
filename = "language.tab";
|
|
|
|
} else {
|
|
|
|
filename = "grim.tab";
|
|
|
|
}
|
2012-04-25 21:18:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Common::SeekableReadStream *f = g_resourceloader->openNewStreamFile(filename);
|
2011-12-21 22:53:58 +01:00
|
|
|
if (!f) {
|
2012-04-25 21:18:18 -07:00
|
|
|
error("Localizer::Localizer: Unable to find localization information (%s)", filename.c_str());
|
2004-02-24 21:09:53 +00:00
|
|
|
return;
|
2005-04-03 11:33:28 +00:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2012-04-04 21:15:31 -07:00
|
|
|
int32 filesize = f->size();
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2004-02-24 21:09:53 +00:00
|
|
|
// Read in the data
|
2012-04-04 21:15:31 -07:00
|
|
|
char *data = new char[filesize + 1];
|
|
|
|
f->read(data, filesize);
|
|
|
|
data[filesize] = '\0';
|
2011-12-21 22:53:58 +01:00
|
|
|
delete f;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2015-01-28 20:12:58 +01:00
|
|
|
if (isRemastered) {
|
|
|
|
parseRemasteredData(Common::String(data));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:08:12 +01:00
|
|
|
// Explicitly white-list german demo, as it has a .tab-file
|
2015-01-18 17:00:12 +01:00
|
|
|
if ((isTranslatedGrimDemo) || (!isAnyDemo && !isPS2)) {
|
2012-05-05 23:14:36 +02:00
|
|
|
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++) {
|
2013-07-09 21:12:55 +02:00
|
|
|
next = next * 0x343FD + 0x269EC3;
|
2012-05-05 23:14:36 +02:00
|
|
|
data[i] ^= (int)(((((next >> 16) & 0x7FFF) / 32767.f) * 254) + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 4; i < filesize; i++) {
|
|
|
|
data[i] ^= '\xdd';
|
|
|
|
}
|
2012-04-25 21:18:18 -07:00
|
|
|
}
|
2012-05-05 23:14:36 +02:00
|
|
|
case MKTAG('D', 'O', 'E', 'L'):
|
2020-10-16 18:41:42 +02:00
|
|
|
case MKTAG('a', 'r', 't', 'p'):
|
2012-05-05 23:14:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
2020-10-16 18:41:42 +02:00
|
|
|
error("Invalid magic reading %s: %08x (%s)", filename.c_str(), READ_BE_UINT32(data), tag2str(READ_BE_UINT32(data)));
|
2012-04-25 21:18:18 -07:00
|
|
|
}
|
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2013-06-30 14:27:29 +02:00
|
|
|
char *nextline = data;
|
2012-05-06 00:13:43 +02:00
|
|
|
Common::String last_entry;
|
2013-07-01 22:30:27 +02:00
|
|
|
//Read file till end
|
2013-07-09 21:12:55 +02:00
|
|
|
for (char *line = data + 4; line - data <= filesize; line = nextline + 1) {
|
2014-05-29 15:35:46 -07:00
|
|
|
if (line == nullptr || nextline == nullptr) {
|
2013-06-30 14:27:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-07-01 22:30:27 +02:00
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
nextline = strchr(line, '\n');
|
2013-07-01 22:30:27 +02:00
|
|
|
//if there is no next line we arrived the last one
|
2014-05-29 15:35:46 -07:00
|
|
|
if (nextline == nullptr) {
|
2013-07-01 22:30:27 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-09 21:12:55 +02:00
|
|
|
nextline = strchr(line + 2, '\n');
|
2013-07-01 22:30:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
2013-07-09 21:12:55 +02:00
|
|
|
|
2013-07-01 22:30:27 +02:00
|
|
|
//EMI has an garbage line which should be ignored
|
|
|
|
if (g_grim->getGameType() == GType_MONKEY4 && *line == '\x1A')
|
|
|
|
continue;
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
char *tab = strchr(line, '\t');
|
2013-07-01 22:30:27 +02:00
|
|
|
//skip line if no tab found
|
2014-05-29 15:35:46 -07:00
|
|
|
if (tab == nullptr) {
|
2013-07-01 22:30:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2012-05-06 00:13:43 +02:00
|
|
|
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;
|
2013-07-01 22:30:27 +02:00
|
|
|
} else {
|
2012-05-06 00:13:43 +02:00
|
|
|
_entries[last_entry = Common::String(line, tab - line)] = Common::String(tab + 1, (nextline - tab - 2));
|
2013-07-01 22:30:27 +02:00
|
|
|
}
|
2004-02-24 21:09:53 +00:00
|
|
|
}
|
2012-04-04 21:15:31 -07:00
|
|
|
delete[] data;
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 20:12:58 +01:00
|
|
|
void Localizer::parseRemasteredData(const Common::String &data) {
|
|
|
|
// This is probably cleaner implemented using a read line-by-line, but for now this works.
|
|
|
|
Common::StringTokenizer tokens(data, "\t\n");
|
|
|
|
while (!tokens.empty()) {
|
|
|
|
Common::String key = tokens.nextToken();
|
|
|
|
// Not sure if this is right, but it is necessary to get by the second line
|
|
|
|
key.trim();
|
|
|
|
// Handle comments
|
|
|
|
if (!(key.size() > 0 && !(key[0] == '#'))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Common::String string = tokens.nextToken();
|
|
|
|
_entries[key] = string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-10 09:27:35 +00:00
|
|
|
Common::String Localizer::localize(const char *str) const {
|
2005-01-02 13:34:50 +00:00
|
|
|
assert(str);
|
|
|
|
|
2012-04-04 21:15:31 -07:00
|
|
|
const char *slash2;
|
2004-12-09 23:55:43 +00:00
|
|
|
|
2012-04-04 21:15:31 -07:00
|
|
|
if (str[0] != '/' || str[0] == 0 || !(slash2 = strchr(str + 1, '/')))
|
2004-02-24 21:09:53 +00:00
|
|
|
return str;
|
2003-08-15 18:00:22 +00:00
|
|
|
|
2012-04-04 21:15:31 -07:00
|
|
|
Common::String key(str + 1, slash2 - str - 1);
|
|
|
|
Common::StringMap::iterator it = _entries.find(key);
|
|
|
|
if (it != _entries.end()) {
|
|
|
|
return it->_value;
|
|
|
|
} else {
|
2005-01-02 13:34:50 +00:00
|
|
|
return slash2 + 1;
|
2012-04-04 21:15:31 -07:00
|
|
|
}
|
2003-08-15 18:00:22 +00:00
|
|
|
}
|
2009-05-25 06:49:57 +00:00
|
|
|
|
|
|
|
} // end of namespace Grim
|