forked from frankvalentine/OneLifeData7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenumberSprites.js
66 lines (56 loc) · 2.05 KB
/
renumberSprites.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
const execSync = require('child_process').execSync;
const fs = require('fs');
if (!process.argv[3]) {
console.log(`Run with node ${process.argv[1]} <start_number> <offset>`);
return;
}
const startNumber = Number(process.argv[2]);
const offset = Number(process.argv[3]);
let highestId = 0;
let spriteIds = [];
console.log('Reading sprite files');
const spriteFileList = execSync('ls ../sprites').toString().split('\n');
for (const file of spriteFileList) {
if (!file || isNaN(parseInt(file[0]))) {
continue;
}
if (!file.endsWith('.txt')) {
continue;
}
const thisId = Number(file.replace('.txt', ''));
if (thisId >= startNumber) {
spriteIds.push(thisId);
highestId = thisId + offset > highestId ? thisId + offset : highestId;
}
}
console.log('Moving sprite files');
for (const id of spriteIds) {
execSync(`mv ../sprites/${id}.txt ../sprites/new_${id + offset}.txt`);
execSync(`mv ../sprites/${id}.tga ../sprites/new_${id + offset}.tga`);
}
for (const id of spriteIds) {
execSync(`mv ../sprites/new_${id + offset}.txt ../sprites/${id + offset}.txt`);
execSync(`mv ../sprites/new_${id + offset}.tga ../sprites/${id + offset}.tga`);
}
fs.writeFileSync(`../sprites/nextSpriteNumber.txt`, highestId + 1);
console.log('Reading object files');
const objectFileList = execSync('ls ../objects').toString().split('\n');
console.log('Updating object files');
for (const file of objectFileList) {
if (!file || isNaN(parseInt(file[0]))) {
continue;
}
let fileContent = fs.readFileSync(`../objects/${file}`).toString();
const spriteMatch = fileContent.match(/spriteID=(\d+)?\r?\n/g);
let spriteIds = [];
for (const match of spriteMatch) {
const spriteId = Number(match.match(/spriteID=(\d+)?/)[1]);
if (spriteId >= startNumber) {
spriteIds.push(spriteId);
}
}
for (const id of spriteIds) {
fileContent = fileContent.replace(`spriteID=${id}`, `spriteID=${id + offset}`);
}
fs.writeFileSync(`../objects/${file}`, fileContent);
}