-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathtxtToInfiniteCraft.ts
More file actions
83 lines (66 loc) · 2.46 KB
/
Copy pathtxtToInfiniteCraft.ts
File metadata and controls
83 lines (66 loc) · 2.46 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
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";
class txtToInfiniteCraftHandler implements FormatHandler {
public name: string = "txtToInfiniteCraft";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
async init () {
this.supportedFormats = [
CommonFormats.TEXT.supported("text", true, false),
{
name: "Infinite Craft Save File",
format: "ic",
extension: "ic",
mime: "application/x-infinite-craft-ic",
from: false,
to: true,
internal: "ic",
category: Category.ARCHIVE,
lossless: false
},
];
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const inputFile = inputFiles[0];
const text = new TextDecoder().decode(inputFile.bytes);
const words = text
.split(/[^a-zA-Z0-9']+/)
.filter(Boolean);
const emojis = ["💧", "🔥", "🌬️", "🌍", "⚡", "❄️", "🌟", "🌈", "🌊", "🍃"];
function getRandomEmoji(): string {
return emojis[Math.floor(Math.random() * emojis.length)];
}
const jsonData = {
name: "Save 1",
version: "1.0",
created: Date.now(),
updated: 0,
instances: [] as any[],
items: words.map((word, index) => ({
id: index,
text: word,
emoji: getRandomEmoji(),
})),
};
const outputBytes = new TextEncoder().encode(JSON.stringify(jsonData, null, 2));
const cs = new CompressionStream("gzip");
const inputStream = new Response(outputBytes).body!;
const compressedStream = inputStream.pipeThrough(cs);
const compressedBytes = new Uint8Array(await new Response(compressedStream).arrayBuffer());
const inputFileName = inputFile.name;
const outputFileName = inputFileName.replace(/\.txt$/i, ".ic");
const outputFiles: FileData[] = [
{
name: outputFileName,
bytes: compressedBytes,
},
];
return outputFiles;
}
}
export default txtToInfiniteCraftHandler;