-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-source.js
More file actions
108 lines (86 loc) · 2.86 KB
/
Copy pathextract-source.js
File metadata and controls
108 lines (86 loc) · 2.86 KB
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
const { argv, argc, exit } = require("node:process");
const { readFileSync, writeFileSync, globSync } = require("node:fs");
const { basename } = require("node:path");
if (argc <= 4) {
console.error("Syntax: node extract-source.js Japanese fr");
exit(-1);
}
const sourceLanguageName = argv[2];
const targetLanguageCode = argv[3];
const placeholder = argv[4];
const translations = JSON.parse(String(readFileSync("isat-orig/data/Translations.json")));
const flat = {
msg: {},
cmd: {},
terms: {},
custom: {},
objects: {},
hardcoded: {},
};
for (const msg in translations.msg) {
if (placeholder) {
flat.msg[msg] = msg;
} else {
flat.msg[msg] = "";
}
}
for (const cmd in translations.cmd) {
if (placeholder) {
flat.cmd[cmd] = cmd;
} else {
flat.cmd[cmd] = "";
}
}
for (const term in translations.terms) {
if (placeholder) {
flat.terms[term] = term;
} else {
flat.terms[term] = "";
}
}
for (const msg in translations.custom[sourceLanguageName]) {
if (placeholder) {
flat.custom[msg] = msg;
} else {
flat.custom[msg] = "";
}
}
// EXTRACT TRANSLATIONS IN SCRIPTS
const extractScriptTranslations = (ops) => {
for (let i = 0; i < ops.length; i++) {
let op = ops[i];
if (op.code !== 111 || op.parameters[0] !== 12 || op.parameters[1] !== "ConfigManager.getLanguage() === \"English\"") continue;
for (; i < ops.length && (op.code !== 111 || op.parameters[0] !== 12 || op.parameters[1] !== "ConfigManager.getLanguage() === \"Japanese\""); i++) {
op = ops[i];
if (op.code === 122 && op.parameters[2] === 0 && op.parameters[3] === 4) {
try {
const str = eval(op.parameters[4]);
flat.hardcoded[str] = str;
} catch (e) {
console.warn("Failed to eval: ", op.parameters[4], e);
}
}
}
}
};
JSON.parse(String(readFileSync("isat-orig/data/CommonEvents.json")))
.filter(Boolean)
.forEach(e => extractScriptTranslations(e.list));
const handleEvents = (events) => {
for (let eventIdx = 0; events && eventIdx < events.length; eventIdx++) {
const event = events[eventIdx];
if (!event) continue;
for (let pageIdx = 0; pageIdx < event.pages.length; pageIdx++) {
const page = event.pages[pageIdx];
if (!page) continue;
extractScriptTranslations(page.list);
}
}
};
for (const mapPath of globSync("isat-orig/data/Map*.json")) {
const map = JSON.parse(String(readFileSync(mapPath)));
const mapName = basename(mapPath, ".json");
handleEvents(map.events);
}
handleEvents(JSON.parse(String(readFileSync("isat-orig/data/Troops.json"))));
writeFileSync(targetLanguageCode + ".json", JSON.stringify(flat, undefined, 4));