-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathminecraftLangfileHandler.ts
More file actions
95 lines (70 loc) · 2.43 KB
/
Copy pathminecraftLangfileHandler.ts
File metadata and controls
95 lines (70 loc) · 2.43 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
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats from "src/CommonFormats.ts";
class mclangHandler implements FormatHandler {
public name: string = "minecraft-lang";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
async init () {
this.supportedFormats = [
CommonFormats.JSON.builder("json")
.markLossless(true)
.allowFrom(true)
.allowTo(true),
{
name: "Minecraft Language Localization File",
format: "minecraft-lang",
extension: "lang",
mime: "text/plain",
from: true,
to: true,
internal: "minecraft-lang",
lossless: true,
}
];
this.ready = true;
}
async doConvert(
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
for (const file of inputFiles) {
const text = new TextDecoder().decode(file.bytes);
let resultText: string;
// JSON → LANG
if (inputFormat.format === "json" && outputFormat.format === "minecraft-lang") {
const obj = JSON.parse(text);
if (typeof obj !== "object" || Array.isArray(obj)) {
throw new TypeError("JSON must be a flat object");
}
resultText = Object.entries(obj)
.map(([k, v]) => `${k}=${v}`)
.join("\n");
}
// LANG → JSON
else if (inputFormat.format === "minecraft-lang" && outputFormat.format === "json") {
const result: Record<string, string> = {};
const lines = text.split(/\r?\n/);
for (const line of lines) {
if (!line.trim() || line.startsWith("#")) continue;
const index = line.indexOf("=");
if (index === -1) continue;
const key = line.slice(0, index).trim();
const value = line.slice(index + 1).trim();
result[key] = value;
}
resultText = JSON.stringify(result, null, 2);
}
else {
throw new TypeError(`Unsupported conversion direction: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
outputFiles.push({
name: file.name.split(".").slice(0, -1).join("."),
bytes: new TextEncoder().encode(resultText)
});
}
return outputFiles;
}
}
export default mclangHandler;