forked from N0ctrnl/eqemupatcher
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnexus.js
More file actions
96 lines (88 loc) · 2.42 KB
/
nexus.js
File metadata and controls
96 lines (88 loc) · 2.42 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
import fs from "node:fs";
import path from "node:path";
import xxhash from "xxhash-wasm";
const manifestTemplate = {
shortName: "",
longName: "",
customFilesUrl: "",
filesUrlPrefix: "",
version: "0.1",
website: "",
description: "",
hosts: [],
required: [],
files: {},
};
(async () => {
try {
let manifest;
try {
manifest = await import("./manifest.json", { assert: { type: "json" } });
manifest = manifest.default;
} catch {
console.log(
"Manifest file did not exist. Creating a new file from template."
);
manifest = manifestTemplate;
}
const { create64 } = await xxhash();
const files = getFilesRecursively("./rof");
const newFiles = files.reduce(
(acc, val) => ({
...acc,
...generateFileHashSync(create64, val),
}),
{}
);
manifest.files = newFiles;
fs.writeFileSync("./manifest.json", JSON.stringify(manifest, null, 4));
} catch (error) {
console.error("Error generating hash:", error);
}
})();
/**
*
* @param {import('xxhash-wasm').XXHashAPI.create64} create64
* @param {string} filePath
* @returns
*/
function generateFileHashSync(create64, filePath) {
const fp = path.join("rof", filePath);
const hasher = create64();
const bufferSize = 4096;
const fileDescriptor = fs.openSync(fp, "r");
const buffer = Buffer.alloc(bufferSize);
try {
let bytesRead;
do {
bytesRead = fs.readSync(fileDescriptor, buffer, 0, bufferSize, null);
hasher.update(buffer.slice(0, bytesRead));
} while (bytesRead > 0);
const hash = hasher.digest();
return { [filePath]: hash.toString(16).toUpperCase().padStart(16, "0") };
} catch (err) {
console.error(`Failed to process file: ${filePath}`, err);
return "";
} finally {
fs.closeSync(fileDescriptor);
}
}
function getFilesRecursively(startFolder) {
const result = [];
function readDirRecursive(currentFolder) {
const entries = fs.readdirSync(currentFolder, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(currentFolder, entry.name);
if (entry.isDirectory()) {
readDirRecursive(fullPath);
} else if (entry.isFile()) {
const relativePath = path
.relative(startFolder, fullPath)
.replace(/\\/g, "/");
result.push(relativePath);
}
}
}
readDirRecursive(startFolder);
return result;
}