-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
346 lines (326 loc) · 11.2 KB
/
index.ts
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
345
346
import Bun, { $ } from "bun";
import {
copyFile as copy,
mkdir,
rename,
rmdir,
writeFile as write,
} from "node:fs/promises";
import { cpus } from "node:os";
import { basename, dirname, join } from "path";
import { dir_index, walkDir } from "./tools/dir_index";
import type {
Device,
Devices,
Items,
OldDevices,
ReagentItem,
Reagents,
} from "./tools/types";
console.info("Start building...");
const GODPromise: Promise<any>[] = [];
async function clearDist() {
console.time("Clear DIST");
await rmdir("./dist", { recursive: true });
console.timeEnd("Clear DIST");
}
async function optimizeImages() {
// const x = (cpus().length + 1) >> 1;
let x = cpus().length - 1;
if (x < 1) x = 1;
console.log("Optimize images using", x, "cores");
console.time("Optimize images");
await $`tools/oxipng -o max --strip safe --alpha ./source/images -r -q -t ${x} --dir ./dist/images`;
console.timeEnd("Optimize images");
}
async function moveImages() {
console.log("move images");
console.time("move images");
const distImages = new Bun.Glob("./dist/**/*.png");
const moves: Promise<void>[] = [];
for await (const file of distImages.scan(".")) {
const name = basename(file);
const dir = join(__dirname, "dist", findImage(name));
moves.push(moveFile(file, dirname(dir)));
}
await Promise.all(moves);
console.timeEnd("move images");
}
async function moveFiles() {
console.log("move other files");
console.time("move other files");
const moves: Promise<void>[] = [
copyJSON("source/languages/EN/colors.json", "./dist"),
copyJSON("source/languages/EN/consts.json", "./dist"),
];
const languages = new Bun.Glob("**/{constants,instructions}.json");
for await (const file of languages.scan("source/languages")) {
moves.push(
copyJSON(
join("source", "languages", file),
join("dist", "languages", dirname(file)),
),
);
}
await Promise.all(moves);
console.timeEnd("move other files");
}
async function moveData() {
console.log("move data files");
console.time("move data files");
const languages = new Bun.Glob("**/data.json");
const moves: Promise<void>[] = [];
for await (const file of languages.scan("source/languages")) {
const sFile = Bun.file(join("source", "languages", file));
moves.push(
sFile.json().then(async (json) => {
const result = strip(json);
await write(join("dist", "languages", file), JSON.stringify(result));
}),
);
}
await Promise.all(moves);
console.timeEnd("move data files");
}
async function optimizeData() {
//optimize data
const languages = new Bun.Glob("**/data.json");
for await (const file of languages.scan("./source/languages/")) {
try {
const [languages, name] = [dirname(file), basename(file)];
// if (languages.length !== 2) continue
if (name !== "data.json") continue;
const sFile = Bun.file(join("source", "languages", file));
const data = (await sFile.json()) as OldDevices;
// const data = require() as OldDevices
//OldDevice to Device TODO images
const devices: Devices = { data: [], images: {} };
const items: Items = { data: [] };
const reagents: Reagents = { data: [] };
const logics: any = { data: [] };
const oldDevices = Object.entries(data);
oldDevices.forEach(([key, oldDevice]) => {
if (!oldDevice.PrefabName) return true;
if (!oldDevice.MainImage) return true;
if (!oldDevice.tags.includes("hasLogic")) return true;
let hasChip = false;
if (oldDevice.tags.includes("hasChip")) hasChip = true;
const logics: {
name: string;
permissions: string[];
}[] = [];
for (const logic of oldDevice?.LogicInsert) {
const logicName = logic.LogicName.replace(/<[^>]*>?/gm, "");
const permissions = logic.LogicAccessTypes.split(" ");
logics.push({
name: logicName,
permissions: permissions,
});
}
const slots: {
SlotName: string;
SlotType: string;
SlotIndex: number;
logic: string[];
}[] = [];
const slotLogic: Record<number, string[]> = {};
oldDevice?.LogicSlotInsert?.forEach((sl) => {
const logicName = sl.LogicName.replace(/<[^>]*>?/gm, "");
const slotIndexs = sl.LogicAccessTypes.split(", ").map((index) =>
Number(index),
);
slotIndexs.forEach((index) => {
if (!slotLogic[index]) slotLogic[index] = [];
slotLogic[index].push(logicName);
});
});
oldDevice?.SlotInserts?.forEach((slot) => {
const slotName = slot.SlotName;
const slotType = slot.SlotType;
const slotIndex = Number(slot.SlotIndex);
if (slot.image) {
devices.images[`SlotType.${slotType}`] = findImage(slot.image);
}
slots.push({
SlotName: slotName,
SlotType: slotType,
SlotIndex: slotIndex,
logic: slotLogic[slotIndex] ?? [],
});
});
const device: Device = {
id: oldDevice?.PrefabHash,
Title: oldDevice?.Title ?? key,
Key: key,
PrefabName: oldDevice?.PrefabName,
PrefabHash: oldDevice?.PrefabHash,
hasChip: hasChip,
deviceConnectCount: oldDevice.DeviceConnectCount ?? 0,
image: findImage(oldDevice.MainImage),
mods: oldDevice?.ModeInsert?.map((mod) => mod.LogicName),
connections: oldDevice?.ConnectionInsert?.map(
(connection) => connection?.LogicName,
),
slots: slots,
tags: oldDevice?.tags,
logics: logics,
};
devices.data.push(device);
return true;
});
oldDevices.forEach(([key, oldDevice]) => {
//Здесь остались только item
if (!oldDevice.tags.includes("item")) return true;
if (!oldDevice.MainImage) return true;
if (!oldDevice.PrefabName) return true;
items.data.push({
id: oldDevice.PrefabHash,
Title: oldDevice.Title ?? key,
Key: key,
PrefabName: oldDevice.PrefabName,
PrefabHash: oldDevice.PrefabHash,
StackSize: Number(oldDevice.StackSizeText),
image: findImage(oldDevice.MainImage),
tags: oldDevice.tags,
});
});
oldDevices.forEach(([key, oldDevice]) => {
//Здесь остались только item
if (oldDevice.TYPE !== "reagent") return true;
if (!oldDevice.MainImage) return true;
if (!oldDevice.Title) return true;
const reagentItems: ReagentItem[] = [];
oldDevice.FoundInOre.forEach((item) => {
const name = item.NameOfThing.replaceAll(/<[^>]*>?/gm, "");
const itemData = oldDevices.find(
([key, oldDevice]) => oldDevice.Title === name,
);
if (!itemData) return null;
if (!itemData[1].Title) return null;
if (!itemData[1].PrefabName) return null;
if (!itemData[1].MainImage) return null;
if (!itemData[1].PrefabHash) return null;
reagentItems.push({
title: itemData[1].Title,
name: itemData[1].PrefabName,
hash: itemData[1].PrefabHash,
count: isNaN(Number(item.QuantityOfThing))
? 0
: Number(item.QuantityOfThing),
image: findImage(itemData[1].MainImage),
});
});
reagents.data.push({
title: oldDevice.Title ?? "",
name: key,
hash: oldDevice.ReagentsHash ?? oldDevice.PrefabHash,
image: findImage(oldDevice.MainImage),
items: reagentItems,
});
});
oldDevices.forEach(([key, oldDevice]) => {
//Здесь остались только item
if (!oldDevice.Key.startsWith("LogicType")) return true;
const reagentItems: ReagentItem[] = [];
logics.data.push({
key: key,
name: oldDevice.Title,
description: oldDevice.Description,
});
});
devices.data.sort((a, b) => a.Key.localeCompare(b.Key));
items.data.sort((a, b) => a.Key.localeCompare(b.Key));
await writeFile(
"devices.json",
join("dist", "languages", languages),
devices,
);
await writeFile(
"items.json",
join("dist", "languages", languages),
items,
);
await writeFile(
"reagents.json",
join("dist", "languages", languages),
reagents,
);
await writeFile(
"logics.json",
join("dist", "languages", languages),
logics,
);
} catch (e) {
console.error(e);
}
}
}
async function generateIndex() {
process.chdir(join(__dirname, "dist"));
const promises: Promise<void>[] = [];
// Использование:
promises.push(dir_index(join(__dirname, "dist"), ""));
walkDir("./", function (dirPath: string) {
promises.push(dir_index(join(__dirname, "dist"), dirPath));
}).catch(console.error);
return await Promise.all(promises);
}
await clearDist();
GODPromise.push(optimizeImages().then(() => moveImages()));
GODPromise.push(moveFiles());
GODPromise.push(moveData());
await Promise.all(GODPromise);
await optimizeData();
await generateIndex();
//----------------------------------------------HELPERS----------------------------------------------
function findImage(fileName: string): string;
function findImage(fileName: null): null;
function findImage(fileName: string | null): string | null {
if (!fileName) return null;
if (fileName.includes("/") || fileName.includes("\\")) return fileName;
const firstLetter = fileName[0];
const secondLetter = fileName[1];
return join("images", firstLetter, secondLetter, fileName);
}
function strip(
obj: Record<string, Record<string, any>>,
): Record<string, Record<string, any>> {
const r: Record<string, any> = {};
Object.entries(obj).forEach(([key, o]) => {
const newO: Record<string, any> = {};
Object.entries(o).forEach(([k, v]) => {
if (v === null) return;
if (v === undefined) return;
if (v === "") return;
if (Array.isArray(v) && v.length === 0) return;
if (Object.keys(v).length === 0) return;
newO[k] = v;
});
r[key] = newO;
});
return r;
}
async function writeFile(file: string, dir: string, content?: {}) {
await mkdir(dir, { recursive: true });
return write(join(dir, file), JSON.stringify(content), {
encoding: "utf-8",
});
}
async function moveFile(file: string, dir: string, newName?: string) {
await mkdir(dir, { recursive: true });
return rename(file, join(dir, newName ?? basename(file)));
}
async function copyFile(file: string, dir: string, newName?: string) {
await mkdir(dir, { recursive: true });
return copy(file, join(dir, newName ?? basename(file)));
}
async function copyJSON(file: string, dir: string, newName?: string) {
const mk = mkdir(dir, { recursive: true });
const sFile = Bun.file(file);
const json = await sFile.json();
await mk;
return write(join(dir, newName ?? basename(file)), JSON.stringify(json), {
encoding: "utf-8",
});
// return copy(file, join(dir, newName ?? basename(file)))
}