forked from Kattoor/nw-model-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnw-model-miner.mjs
59 lines (48 loc) · 2 KB
/
nw-model-miner.mjs
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
import {promises as fs} from 'fs';
import {globby} from 'globby';
import {fileURLToPath} from 'url';
import {dirname} from 'path';
import {extract as extractDatasheets, convert as convertDatasheets} from './datasheets/extract-and-convert.mjs';
import {dumpPakHeaders} from './pak-headers/pak-header-dumper.mjs';
import {extractAndAssemble as extractAndAssembleModels} from './models/extract-and-assemble.mjs';
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Usage: node nw-model-miner.mjs "PATH_TO_PAKS"');
console.log('Example: node nw-model-miner.mjs "C:/Program Files (x86)/Steam/steamapps/common/New World Closed Beta"');
process.exit();
}
let assetsPath = args[0].replace(/"/g, '').replace(/\\/g, '/');
if (assetsPath.endsWith('/')) {
assetsPath = assetsPath.slice(0, -1);
}
const pakFilePaths = await globby(assetsPath + '/**/*.pak');
const outPath = dirname(fileURLToPath(import.meta.url)).replace(/\\/g, '/') + '/out/';
await fs.mkdir(outPath, {recursive: true});
await extractDatasheets(pakFilePaths, outPath);
await convertDatasheets(outPath);
await dumpPakHeaders(pakFilePaths, outPath);
await extractAndAssembleModels(outPath);
/* Remove empty folders */
const dirs = await fs.readdir(outPath, {withFileTypes: true});
for (let dir of dirs) {
await removeIfEmpty(outPath, dir);
}
async function removeIfEmpty(pathPrefix, path) {
if (!path.isDirectory()) {
return false;
}
const dirsAndFiles = await fs.readdir(pathPrefix + '/' + path.name, {withFileTypes: true});
if (dirsAndFiles.length === 0) {
await fs.rmdir(pathPrefix + '/' + path.name);
return true;
}
const removeIfEmptyResults = [];
for (let dirOrFile of dirsAndFiles) {
removeIfEmptyResults.push(await removeIfEmpty(pathPrefix + '/' + path.name, dirOrFile));
}
const subDirsAreEmpty = removeIfEmptyResults.every(isEmpty => isEmpty === true);
if (subDirsAreEmpty) {
await fs.rmdir(pathPrefix + '/' + path.name);
return true;
}
}