-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.js
112 lines (88 loc) · 3.29 KB
/
convert.js
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
#!/usr/bin/env node
'use strict';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
import {
generatePatchFilename,
convertToSpicePatch,
parsePatcherFile,
} from './lib/convert.js';
/**
* Paths to files from command-line arguments
*/
if (process.argv.length < 5)
{
console.error('usage: convert.js <metadata> <patchers> <output>');
process.exit(1);
}
const METADATA_DIR = process.argv[2];
const PATCHERS_DIR = process.argv[3];
const OUTPUT_DIR = process.argv[4];
/**
* Ensure the output directory exists
*/
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
/**
* Start iterating over metadata files
*/
for (const metaFile of fs.readdirSync(METADATA_DIR))
{
if (!metaFile.endsWith('.json'))
continue;
const patchFile = path.join(PATCHERS_DIR, metaFile.slice(0, -4) + 'html');
console.log(`\nProcessing HTML patcher file '${patchFile}'...`);
if (!fs.existsSync(patchFile))
{
console.warn(`No matching patcher exists for metadata file '${metaFile}', skipping...`);
continue;
}
const meta = JSON.parse(fs.readFileSync(path.join(METADATA_DIR, metaFile), { encoding: 'utf8' }));
console.log(`Converting patches for '${meta.name}'...`);
for (const patcher of parsePatcherFile(patchFile).patches)
{
if (!meta.patches[patcher.fname])
{
console.warn(`No metadata for file '${patcher.fname}' in '${metaFile}', skipping...`);
continue;
}
if (!meta.patches[patcher.fname][patcher.description])
{
console.warn(`No metadata for file '${patcher.fname}' version '${patcher.description}' in '${metaFile}', skipping...`);
continue;
}
let metaBinaries = meta.patches[patcher.fname][patcher.description];
if (!Array.isArray(metaBinaries))
metaBinaries = [metaBinaries];
for (const metaItem of metaBinaries)
{
console.log(`Converting patches for '${patcher.fname}' version '${patcher.description}'...`);
let convertedPatches = [];
for (const patch of patcher.args)
{
const converted = convertToSpicePatch(patch, metaItem.GameCodePrefix, patcher.fname);
if (!converted)
continue;
convertedPatches.push(converted);
}
const outputFilename = path.join(OUTPUT_DIR, generatePatchFilename(metaItem));
const outputContents = JSON.stringify(convertedPatches, null, 4);
let operation = 'Add';
if (fs.existsSync(outputFilename))
{
if (fs.readFileSync(outputFilename, { encoding: 'utf8' }) === outputContents)
{
console.log(`Output file '${outputFilename}' is already up-to-date...`);
continue;
}
operation = 'Update';
}
console.log(`Writing output file '${outputFilename}'...`);
fs.writeFileSync(outputFilename, outputContents);
if (process.env.GIT_NO_COMMIT)
continue;
execSync(`git add ${outputFilename}`);
execSync(`git commit -m "${operation} ${meta.id} '${patcher.fname}' ${patcher.description} patches"`);
}
}
}