-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite_metadata.js
More file actions
executable file
·56 lines (46 loc) · 1.57 KB
/
write_metadata.js
File metadata and controls
executable file
·56 lines (46 loc) · 1.57 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
#!/usr/bin/env node
const fs = require("node:fs/promises");
const path = require("node:path");
const [, , binaryFile, metadataFile] = process.argv;
if (!binaryFile || !metadataFile) {
console.error("Usage: node scripts/write_metadata.js <binaryFile> <metadataFile>");
process.exit(1);
}
async function main() {
const binary = await fs.readFile(binaryFile);
const offsets = [];
const MAGIC_TEXT = "NSMDSectionHeader";
const decoder = new TextDecoder();
for (let i = 0; i < binary.byteLength; i++) {
const byte = binary[i];
if (byte === "N".charCodeAt(0)) {
const magic = decoder.decode(binary.subarray(i, i + MAGIC_TEXT.length));
if (magic === MAGIC_TEXT) {
const arch = decoder.decode(
binary.subarray(i + MAGIC_TEXT.length, i + MAGIC_TEXT.length + 3),
);
offsets.push([arch, i]);
}
}
}
if (offsets.length < 1) {
console.log("No empty metadata section found");
}
const metadataBaseName = metadataFile.split(".nsmd")[0];
const metadataRoot = path.resolve(__dirname, "..", "metadata-generator", "metadata");
for (const [arch, offset] of offsets) {
console.log(`Writing metadata to offset ${offset}, arch: ${arch}`);
const archSuffix = arch === "ARM" ? "arm64" : "x86_64";
const metadataPath = path.join(
metadataRoot,
`${metadataBaseName}.${archSuffix}.nsmd`,
);
const metadata = await fs.readFile(metadataPath);
metadata.copy(binary, offset);
}
await fs.writeFile(binaryFile, binary);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});