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
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "ags/shared/game/tra_file.h"
#include "ags/shared/ac/words_dictionary.h"
#include "ags/shared/debugging/out.h"
#include "ags/shared/util/data_ext.h"
#include "ags/shared/util/string_compat.h"
#include "ags/shared/util/string_utils.h"
namespace AGS3 {
namespace AGS {
namespace Shared {
const char *TRASignature = "AGSTranslation";
String GetTraFileErrorText(TraFileErrorType err) {
switch (err) {
case kTraFileErr_NoError:
return "No error.";
case kTraFileErr_SignatureFailed:
return "Not an AGS translation file or an unsupported format.";
case kTraFileErr_FormatNotSupported:
return "Format version not supported.";
case kTraFileErr_GameIDMismatch:
return "Game ID does not match, translation is meant for a different game.";
case kTraFileErr_UnexpectedEOF:
return "Unexpected end of file.";
case kTraFileErr_UnknownBlockType:
return "Unknown block type.";
case kTraFileErr_BlockDataOverlapping:
return "Block data overlapping.";
default:
return "Unknown error.";
}
}
String GetTraBlockName(TraFileBlock id) {
switch (id) {
case kTraFblk_Dict: return "Dictionary";
case kTraFblk_GameID: return "GameID";
case kTraFblk_TextOpts: return "TextOpts";
default: return "unknown";
}
}
HError OpenTraFile(Stream *in) {
// Test the file signature
char sigbuf[16] = { 0 };
in->Read(sigbuf, 15);
if (ags_stricmp(TRASignature, sigbuf) != 0)
return new TraFileError(kTraFileErr_SignatureFailed);
return HError::None();
}
HError ReadTraBlock(Translation &tra, Stream *in, TraFileBlock block, const String &ext_id, soff_t /*block_len*/) {
switch (block) {
case kTraFblk_Dict:
{
char original[1024];
char translation[1024];
// Read lines until we find zero-length key & value
while (true) {
read_string_decrypt(in, original, sizeof(original));
read_string_decrypt(in, translation, sizeof(translation));
if (!original[0] && !translation[0])
break;
tra.Dict.insert(std::make_pair(String(original), String(translation)));
}
return HError::None();
}
case kTraFblk_GameID:
{
char gamename[256];
tra.GameUid = in->ReadInt32();
read_string_decrypt(in, gamename, sizeof(gamename));
tra.GameName = gamename;
return HError::None();
}
case kTraFblk_TextOpts:
tra.NormalFont = in->ReadInt32();
tra.SpeechFont = in->ReadInt32();
tra.RightToLeft = in->ReadInt32();
return HError::None();
case kTraFblk_None:
// continue reading extensions with string ID
break;
default:
return new TraFileError(kTraFileErr_UnknownBlockType,
String::FromFormat("Type: %d, known range: %d - %d.", block, kTraFblk_Dict, kTraFblk_TextOpts));
}
if (ext_id.CompareNoCase("ext_sopts") == 0) {
StrUtil::ReadStringMap(tra.StrOptions, in);
return HError::None();
}
return new TraFileError(kTraFileErr_UnknownBlockType,
String::FromFormat("Type: %s", ext_id.GetCStr()));
}
// TRABlockReader reads whole TRA data, block by block
class TRABlockReader : public DataExtReader {
public:
TRABlockReader(Translation &tra, Stream *in)
: DataExtReader(in, kDataExt_NumID32 | kDataExt_File32)
, _tra(tra) {
}
// Reads only the Game ID block and stops
HError ReadGameID() {
HError err = FindOne(kTraFblk_GameID);
if (!err)
return err;
return ReadTraBlock(_tra, _in, kTraFblk_GameID, "", _blockLen);
}
private:
String GetOldBlockName(int block_id) const override {
return GetTraBlockName((TraFileBlock)block_id);
}
soff_t GetOverLeeway(int block_id) const override {
// TRA files made by pre-3.0 editors have a block length miscount by 1 byte
if (block_id == kTraFblk_GameID) return 1;
return 0;
}
HError ReadBlock(int block_id, const String &ext_id,
soff_t block_len, bool &read_next) override {
read_next = true;
return ReadTraBlock(_tra, _in, (TraFileBlock)block_id, ext_id, block_len);
}
Translation &_tra;
};
HError TestTraGameID(int game_uid, const String &game_name, Stream *in) {
HError err = OpenTraFile(in);
if (!err)
return err;
Translation tra;
TRABlockReader reader(tra, in);
err = reader.ReadGameID();
if (!err)
return err;
// Test the identifiers, if they are not present then skip the test
if ((tra.GameUid != 0 && (game_uid != tra.GameUid)) ||
(!tra.GameName.IsEmpty() && (game_name != tra.GameName)))
return new TraFileError(kTraFileErr_GameIDMismatch,
String::FromFormat("The translation is designed for '%s'", tra.GameName.GetCStr()));
return HError::None();
}
HError ReadTraData(Translation &tra, Stream *in) {
HError err = OpenTraFile(in);
if (!err)
return err;
TRABlockReader reader(tra, in);
return reader.Read();
}
// TODO: perhaps merge with encrypt/decrypt utilities
static const char *EncryptText(std::vector<char> &en_buf, const String &s) {
if (en_buf.size() < s.GetLength() + 1)
en_buf.resize(s.GetLength() + 1);
strncpy(&en_buf.front(), s.GetCStr(), s.GetLength() + 1);
encrypt_text(&en_buf.front());
return &en_buf.front();
}
// TODO: perhaps merge with encrypt/decrypt utilities
static const char *EncryptEmptyString(std::vector<char> &en_buf) {
en_buf[0] = 0;
encrypt_text(&en_buf.front());
return &en_buf.front();
}
void WriteGameID(const Translation &tra, Stream *out) {
std::vector<char> en_buf;
out->WriteInt32(tra.GameUid);
StrUtil::WriteString(EncryptText(en_buf, tra.GameName), tra.GameName.GetLength() + 1, out);
}
void WriteDict(const Translation &tra, Stream *out) {
std::vector<char> en_buf;
for (const auto &kv : tra.Dict) {
const String &src = kv._key;
const String &dst = kv._value;
if (!dst.IsNullOrSpace()) {
String unsrc = StrUtil::Unescape(src);
String undst = StrUtil::Unescape(dst);
StrUtil::WriteString(EncryptText(en_buf, unsrc), unsrc.GetLength() + 1, out);
StrUtil::WriteString(EncryptText(en_buf, undst), undst.GetLength() + 1, out);
}
}
// Write a pair of empty key/values
StrUtil::WriteString(EncryptEmptyString(en_buf), 1, out);
StrUtil::WriteString(EncryptEmptyString(en_buf), 1, out);
}
void WriteTextOpts(const Translation &tra, Stream *out) {
out->WriteInt32(tra.NormalFont);
out->WriteInt32(tra.SpeechFont);
out->WriteInt32(tra.RightToLeft);
}
static const Translation *writer_tra;
static void(*writer_writer)(const Translation &tra, Stream *out);
void WriteStrOptions(const Translation &tra, Stream *out) {
StrUtil::WriteStringMap(tra.StrOptions, out);
}
static void WriteTraBlockWriter(Stream *out) {
writer_writer(*writer_tra, out);
}
inline void WriteTraBlock(const Translation &tra, TraFileBlock block,
void(*writer)(const Translation &tra, Stream *out), Stream *out) {
writer_tra = &tra;
writer_writer = writer;
WriteExtBlock(block, WriteTraBlockWriter,
kDataExt_NumID32 | kDataExt_File32, out);
}
inline void WriteTraBlock(const Translation &tra, const String &ext_id,
void(*writer)(const Translation &tra, Stream *out), Stream *out) {
writer_tra = &tra;
writer_writer = writer;
WriteExtBlock(ext_id, WriteTraBlockWriter,
kDataExt_NumID32 | kDataExt_File32, out);
}
void WriteTraData(const Translation &tra, Stream *out) {
// Write header
out->Write(TRASignature, strlen(TRASignature) + 1);
// Write all blocks
WriteTraBlock(tra, kTraFblk_GameID, WriteGameID, out);
WriteTraBlock(tra, kTraFblk_Dict, WriteDict, out);
WriteTraBlock(tra, kTraFblk_TextOpts, WriteTextOpts, out);
WriteTraBlock(tra, "ext_sopts", WriteStrOptions, out);
// Write ending
out->WriteInt32(kTraFile_EOF);
}
} // namespace Shared
} // namespace AGS
} // namespace AGS3
|