-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.cpp
344 lines (294 loc) · 9.97 KB
/
main.cpp
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
/*
* Copyright (c) 2020 LcfTrans authors
* This file is released under the MIT License
* http://opensource.org/licenses/MIT
*/
#include <cstring>
#include <fstream>
#include <iostream>
#include <lcf/encoder.h>
#include <lcf/reader_util.h>
#include <lcf/ldb/reader.h>
#include <argparse.hpp>
#include "translation.h"
#include "utils.h"
#ifdef _WIN32
# include <dirent_win.h>
#else
# include <dirent.h>
#endif
#define DATABASE_FILE "rpg_rt.ldb"
#define MAPTREE_FILE "rpg_rt.lmt"
#define INI_FILE "rpg_rt.ini"
void DumpLdb(const std::string& filename);
void DumpLmu(const std::string& filename);
void DumpLmt(const std::string& filename);
int MatchMode();
namespace {
/* config */
std::string encoding, outdir = ".", merge_indir, indir;
std::vector<std::pair<std::string, std::string>> source_files;
std::vector<std::pair<std::string, std::string>> outdir_files;
std::string ini_file, database_file;
bool create, update, match = false;
}
int main(int argc, char** argv) {
/* add usage and help messages */
argparse::ArgumentParser cli("lcftrans", PACKAGE_VERSION);
cli.set_usage_max_line_width(100);
cli.add_description("Translate RPG Maker 2000/2003 projects");
cli.add_epilog("Homepage " PACKAGE_URL " - Report bugs at: " PACKAGE_BUGREPORT);
/* parse command line arguments */
cli.add_argument("DIRECTORY").store_into(indir).metavar("DIRECTORY")
.help("Game directory");
auto &group = cli.add_mutually_exclusive_group(true);
group.add_argument("-c", "--create").store_into(create)
.help("Create a new translation");
group.add_argument("-u", "--update").store_into(update)
.help("Update an existing translation");
group.add_argument("-m", "--match").metavar("MDIR")
.action([&](const auto &arg) { merge_indir = arg; match = true; })
.help("Match the translations in MDIR and DIRECTORY. When matched\n"
"the original in MDIR becomes the translation of DIRECTORY.\n"
"Used to generate translations from games where the trans-\n"
"lation is hardcoded in the game files.");
cli.add_argument("-e", "--encoding").store_into(encoding).metavar("ENC")
.help("When not specified, is read from RPG_RT.ini or auto-detected");
cli.add_argument("-o", "--output").store_into(outdir).metavar("OUTDIR")
.help("Output directory (default: working directory)");
// for old encoding argument
cli.add_argument("additional").remaining().hidden();
try {
cli.parse_args(argc, argv);
} catch (const std::exception& err) {
std::cerr << err.what() << "\n";
// print usage message
std::cerr << cli.usage() << "\n";
std::exit(EXIT_FAILURE);
}
// for old encoding argument
if (auto additional_args = cli.present<std::vector<std::string>>("additional")) {
if(additional_args.value().size() > 1) {
std::cerr << "Found additional, unrecognized arguments.\n";
std::cerr << cli.usage() << "\n";
std::exit(EXIT_FAILURE);
} else {
std::cerr << "Specifying ENCODING as last argument is deprecated, `-e ENC` is the replacement.\n";
encoding = additional_args.value()[0];
}
}
if (outdir == merge_indir) {
std::cerr << "You need to specify a different output directory (-o).\n";
std::cerr << cli;
std::exit(EXIT_FAILURE);
}
auto full_path = [&](const auto& name) {
return indir + "/" + name;
};
DIR *dirHandle;
struct dirent* dirEntry;
dirHandle = opendir(outdir.c_str());
if (!dirHandle) {
std::cerr << "Cannot access output directory " << outdir << "\n";
return 1;
}
if (update) {
while (nullptr != (dirEntry = readdir(dirHandle))) {
outdir_files.emplace_back(dirEntry->d_name, Utils::LowerCase(dirEntry->d_name));
}
} else if (match) {
// Also read the source translations
closedir(dirHandle);
dirHandle = opendir(indir.c_str());
if (!dirHandle) {
std::cerr << "Cannot access input directory " << indir << "\n";
return 1;
}
while (nullptr != (dirEntry = readdir(dirHandle))) {
outdir_files.emplace_back(dirEntry->d_name, Utils::LowerCase(dirEntry->d_name));
}
closedir(dirHandle);
dirHandle = opendir(merge_indir.c_str());
if (!dirHandle) {
std::cerr << "Cannot access merge input directory " << merge_indir << "\n";
return 1;
}
while (nullptr != (dirEntry = readdir(dirHandle))) {
source_files.emplace_back(dirEntry->d_name, Utils::LowerCase(dirEntry->d_name));
}
}
closedir(dirHandle);
if (match) {
return MatchMode();
}
dirHandle = opendir(indir.c_str());
if (dirHandle) {
while (nullptr != (dirEntry = readdir(dirHandle))) {
const auto& lname = Utils::LowerCase(dirEntry->d_name);
source_files.emplace_back(dirEntry->d_name, lname);
if (lname == INI_FILE) {
ini_file = full_path(dirEntry->d_name);
} else if (lname == DATABASE_FILE) {
database_file = full_path(dirEntry->d_name);
}
}
closedir(dirHandle);
} else {
std::cerr << "Failed reading dir " << indir << "\n";
return 1;
}
if (encoding.empty()) {
if (!ini_file.empty()) {
encoding = lcf::ReaderUtil::GetEncoding(ini_file);
}
if (encoding.empty() && !database_file.empty()) {
auto db = lcf::LDB_Reader::Load(database_file, encoding);
if (db) {
encoding = lcf::ReaderUtil::DetectEncoding(*db);
}
}
}
lcf::Encoder enc(encoding);
if (!enc.IsOk()) {
std::cerr << "Bad encoding " << encoding << "\n";
return 3;
}
std::cout << "LcfTrans\n";
std::cout << "Using encoding " << encoding << "\n";
std::sort(source_files.begin(), source_files.end(), [](const auto& a, const auto& b) {
return a.first < b.first;
});
for (const auto& s : source_files) {
const auto& name = s.first;
const auto& lname = s.second;
if (lname == DATABASE_FILE) {
std::cout << "Parsing Database " << name << "\n";
DumpLdb(full_path(name));
} else if (lname == MAPTREE_FILE) {
std::cout << "Parsing Maptree " << name << "\n";
DumpLmt(full_path(name));
} else if (Utils::HasExt(lname, ".lmu")) {
std::cout << "Parsing Map " << name << "\n";
DumpLmu(full_path(name));
}
}
return 0;
}
static std::string get_outdir_file(const std::string& file) {
for (const auto& f : outdir_files) {
if (f.second == file) {
return f.first;
}
}
return "";
}
void DumpLdb(const std::string& filename) {
TranslationLdb t = Translation::fromLDB(filename, encoding);
auto dump = [](Translation& ti, const std::string& poname) {
if (update) {
std::string po = get_outdir_file(Utils::LowerCase(poname + ".po"));
if (!po.empty()) {
Translation pot;
pot = Translation::fromPO(outdir + "/" + po);
auto stale = ti.Merge(pot);
if (!stale.getEntries().empty()) {
std::string term = stale.getEntries().size() == 1 ? " term is " : " terms are ";
std::cout << " " << stale.getEntries().size() << term << "stale\n";
std::ofstream outfile(outdir + "/" + poname + ".stale.po");
stale.write(outfile);
}
}
}
std::ofstream outfile(outdir + "/" + poname + ".po");
ti.write(outfile);
};
auto term = [](const Translation& t) {
return std::to_string(t.getEntries().size()) + " " + (t.getEntries().size() == 1 ? "term " : "terms ");
};
std::cout << " " << term(t.terms) << "in the database\n";
dump(t.terms, "RPG_RT.ldb");
std::cout << " " << term(t.common_events) << "in Common Events\n";
dump(t.common_events, "RPG_RT.ldb.common");
std::cout << " " << term(t.battle_events) << "in Battle Events\n";
dump(t.battle_events, "RPG_RT.ldb.battle");
}
void DumpLmuLmtInner(const std::string& filename, Translation& t, const std::string& poname) {
Translation pot;
if (t.getEntries().empty()) {
std::cout << " Skipped. No terms found.\n";
return;
}
std::cout << " " << t.getEntries().size() << " term" << (t.getEntries().size() == 1 ? "" : "s") << "\n";
if (update) {
std::string po = get_outdir_file(Utils::LowerCase(poname + ".po"));
if (!po.empty()) {
pot = Translation::fromPO(outdir + "/" + po);
auto stale = t.Merge(pot);
if (!stale.getEntries().empty()) {
std::string term = stale.getEntries().size() == 1 ? " term is " : " terms are ";
std::cout << " " << stale.getEntries().size() << term << "stale\n";
std::ofstream outfile(outdir + "/" + poname + ".stale.po");
stale.write(outfile);
}
}
}
std::ofstream outfile(outdir + "/" + poname + ".po");
t.write(outfile);
}
void DumpLmu(const std::string& filename) {
Translation t = Translation::fromLMU(filename, encoding);
DumpLmuLmtInner(filename, t, Utils::GetFilename(filename));
}
void DumpLmt(const std::string& filename) {
Translation t = Translation::fromLMT(filename, encoding);
DumpLmuLmtInner(filename, t, "RPG_RT.lmt");
}
int MatchMode() {
std::sort(source_files.begin(), source_files.end(), [](const auto& a, const auto& b) {
return a.first < b.first;
});
auto ends_with_po = [](const std::string& src) {
std::string po = ".po";
if (src.length() >= po.size()) {
std::string src_l = Utils::LowerCase(src);
return (0 == src_l.compare(src_l.length() - po.length(), po.length(), po));
} else {
return false;
}
};
for (const auto& s : source_files) {
if (!ends_with_po(s.first)) {
continue;
}
for (const auto& o : outdir_files) {
if (s.second == o.second) {
Translation src_po = Translation::fromPO(merge_indir + "/" + s.first);
Translation dst_po = Translation::fromPO( indir + "/" + o.first);
int matched;
auto stale = dst_po.Match(src_po, matched);
std::cout << "Matching " << o.first << "\n";
std::cout << " " << matched << " term" << (matched != 1 ? "s" : "") << " matched\n";
int fuzzy = 0;
for (const auto& e : dst_po.getEntries()) {
if (e.fuzzy) {
++fuzzy;
}
}
if (fuzzy > 0) {
std::string term = fuzzy == 1 ? " term is " : " terms are ";
std::cout << " " << fuzzy << term << "fuzzy matched\n";
}
if (!stale.getEntries().empty()) {
std::string term = stale.getEntries().size() == 1 ? " term is " : " terms are ";
std::cout << " " << stale.getEntries().size() << term << "unmatched\n";
std::ofstream outfile(outdir + "/" + o.first.substr(0, o.first.size() - 3) + ".unmatched.po");
stale.write(outfile);
}
std::ofstream outfile(outdir + "/" + o.first);
dst_po.write(outfile);
continue;
}
}
}
return 0;
}