forked from alto-io/cross-game-renderer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
updatePartsConfig.jsx
109 lines (86 loc) · 2.16 KB
/
updatePartsConfig.jsx
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
// Run with node
// Part pngs must be arranged in arcadianPartsPath as:
// [Gender]/[BodyPart]/[PartName].png
// Output is at arcadianPartsPath/partsConfigFileName
const arcadianPartsPath = "./v1/arcadian-parts";
const partsConfigFileName = "partsConfig.json";
const fs = require("fs");
generatePartsConfig();
/**Run with node*/
function generatePartsConfig() {
let output = getAvatarFiles();
let json = JSON.stringify(output, null, "\t");
let path = arcadianPartsPath + "/" + partsConfigFileName;
fs.writeFile(path, json, function (error) {
if (error) throw error;
console.log("Saved at " + path);
});
}
function getAvatarFiles() {
// Sample Format of output
let returnVal = [
{
Gender: "Female",
Parts: [
{
Name: "Bottom",
Files: [
{
Name: "Alien-Queen-Bottom",
Path: "v1/arcadian-parts/Female/Bottom/Alien-Queen-Bottom.png",
},
],
},
],
},
];
returnVal.length = 0;
let allGenders = getSubdirectories(arcadianPartsPath);
for (let genderName of allGenders) {
let newGender = {
Gender: genderName,
Parts: [],
};
let genderPath = arcadianPartsPath + "/" + genderName;
newGender.Parts = getAllParts(genderPath);
returnVal.push(newGender);
}
return returnVal;
}
function getAllParts(genderPath) {
let returnVal = [];
let allParts = getSubdirectories(genderPath);
for (let partName of allParts) {
let newPart = {
Name: partName,
Files: [],
};
let partPath = genderPath + "/" + partName;
newPart.Files = getAllFiles(partPath);
returnVal.push(newPart);
}
return returnVal;
}
function getAllFiles(partPath) {
let returnVal = [];
let allFiles = getFileNames(partPath);
for (let fileName of allFiles) {
let newFile = {
Name: fileName.slice(0, -4), // remove file extension
Path: partPath + "/" + fileName,
};
returnVal.push(newFile);
}
return returnVal;
}
function getSubdirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + "/" + file).isDirectory();
});
}
function getFileNames(path) {
return fs
.readdirSync(path, { withFileTypes: true })
.filter((item) => !item.isDirectory())
.map((item) => item.name);
}