-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathnbt.ts
More file actions
124 lines (111 loc) · 4.3 KB
/
Copy pathnbt.ts
File metadata and controls
124 lines (111 loc) · 4.3 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { FileData, FileFormat, FormatHandler } from "src/FormatHandler.ts";
import * as NBT from "nbtify";
import CommonFormats, { Category } from "src/CommonFormats.ts";
import { gzipSync, gunzipSync } from "fflate";
class nbtHandler implements FormatHandler {
public name: string = "nbt";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
public indent: number = 2
async init() {
this.supportedFormats = [
{
name: "Named Binary Tag",
format: "nbt",
extension: "nbt",
mime: "application/x-minecraft-nbt",
from: true,
to: true,
internal: "nbt",
category: Category.DATA,
lossless: true
},
CommonFormats.JSON.supported("json", true, true, true),
{
name: "String Named Binary Tag",
format: "snbt",
extension: "snbt",
mime: "application/x-minecraft-snbt",
from: true,
to: true,
internal: "snbt",
category: Category.DATA,
lossless: true // only compression data is lost
},
]
this.ready = true
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
const decoder = new TextDecoder()
const encoder = new TextEncoder()
// nbt -> json
if (inputFormat.internal === "nbt" && outputFormat.internal === "json") {
for (const file of inputFiles) {
const nbt = await NBT.read(file.bytes);
const j = JSON.stringify(nbt.data, (key, value) =>
typeof value === 'bigint' ? value.toString() : value,
this.indent);
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + ".json",
bytes: encoder.encode(j)
});
}
}
// json -> nbt
if (inputFormat.internal === "json" && outputFormat.internal === "nbt") {
for (const file of inputFiles) {
const text = decoder.decode(file.bytes)
const obj = JSON.parse(text)
const bd = await NBT.write(obj)
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + `.${outputFormat.extension}`,
bytes: bd
})
}
}
// snbt -> nbt
if (inputFormat.internal === "snbt" && outputFormat.internal === "nbt") {
for (const file of inputFiles) {
const text = decoder.decode(file.bytes)
const nbt = NBT.parse(text)
const bd = await NBT.write(nbt)
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + `.${outputFormat.extension}`,
bytes: bd
})
}
}
// nbt -> snbt
if (inputFormat.internal === "nbt" && outputFormat.internal === "snbt") {
for (const file of inputFiles) {
const nbt = await NBT.read(file.bytes)
const text = NBT.stringify(nbt, {
space: this.indent
})
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + ".snbt",
bytes: encoder.encode(text)
})
}
}
// nbt -> schem / schematic
if (inputFormat.internal === "nbt" && (outputFormat.internal === "schem" || outputFormat.internal === "schematic")) {
for (const file of inputFiles) {
outputFiles.push({
name: file.name.split(".").slice(0, -1).join(".") + `.${outputFormat.extension}`,
bytes: gzipSync(file.bytes)
})
}
}
if (outputFiles.length === 0) {
throw new TypeError(`nbtHandler does not support route: ${inputFormat.internal} -> ${outputFormat.internal}`);
}
return outputFiles
}
}
export default nbtHandler;