Skip to content

Instantly share code, notes, and snippets.

@ihorbond
Created December 30, 2021 23:27
Show Gist options
  • Save ihorbond/f0febb14dff0ba153782c4abb25b692b to your computer and use it in GitHub Desktop.
Save ihorbond/f0febb14dff0ba153782c4abb25b692b to your computer and use it in GitHub Desktop.
Drop this file into your layers folder, set the percentages sorted alphabetically and run with node ./setRarities.js
const fs = require('fs');
const path = require('path')
function validate(basePath) {
console.log("running validation")
Object.entries(percentages).forEach(([key,val]) => {
const folderPath = path.join(basePath, key)
fs.readdir(folderPath, (err, files) => {
if(err) {
throw new Error(err)
}
const pngImages = files.filter(f => path.extname(f) === '.png')
if(val.length !== pngImages.length) {
console.log(key, val.length, pngImages.length, pngImages)
throw new Error("Number of files doesn't match the percentages!")
}
})
})
}
const [, , ...args ] = process.argv
const basePath = args[0] ? ('./' + args[0]) : './'
console.log("Base path: ", basePath)
const percentages = {
Background: [
2.00,
2.00,
3.00,
2.00,
1.00,
3.00,
3.00,
3.00,
3.00,
4.00,
3.00,
50.00,
1.00,
6.00,
7.00,
4.00,
3.00,
],
Base: [
8.00,
15.00,
12.00,
8.00,
4.00,
5.00,
7.50,
4.00,
4.00,
8.00,
8.00,
2.00,
8.00,
5.00,
1.50,
],
Clothes: [
6.00,
4.00,
1.00,
1.00,
2.00,
5.00,
5.00,
2.00,
2.50,
2.00,
0.30,
1.90,
1.00,
4.00,
3.00,
2.00,
3.00,
3.00,
2.00,
4.00,
2.00,
1.00,
0.50,
2.00,
0.30,
2.00,
3.00,
1.00,
1.00,
1.00,
1.00,
2.00,
0.08,
0.92,
1.00,
2.00,
3.00,
2.00,
3.00,
1.00,
0.20,
0.80,
4.00,
3.00,
1.00,
2.50,
4.00,
],
Eyes: [
0.74,
0.80,
0.99,
15.00,
6.00,
2.00,
0.70,
4.00,
4.00,
4.00,
0.80,
3.50,
2.00,
12.00,
4.00,
4.00,
4.52,
6.00,
0.95,
2.00,
1.00,
4.00,
2.00,
3.00,
2.00,
4.00,
2.00,
4.00,
],
Headwear: [
2.00,
4.00,
6.00,
0.75,
5.00,
6.00,
8.00,
1.00,
5.00,
0.80,
0.95,
5.00,
4.00,
5.00,
0.50,
35.00,
4.00,
3.00,
4.00,
],
Mouth: [
1.00,
4.00,
6.00,
4.00,
4.00,
8.00,
6.00,
8.00,
4.00,
5.00,
15.00,
3.00,
3.00,
12.00,
2.00,
4.00,
6.00,
3.00,
2.00,
]
}
validate(basePath)
Object.entries(percentages).forEach(([key,val]) => {
const folderPath = path.join(basePath, key)
fs.readdir(folderPath, (err, files) => {
if(err) {
throw new Error(err)
}
files.filter(f => path.extname(f) === '.png').forEach((file,idx) => {
console.log("Current file name: ", file);
const ext = path.extname(file)
const nameWithoutExt = path.basename(file, '.png')
const formattedName = nameWithoutExt.replace(/-/g, ' ')
const newname = `${folderPath}/${formattedName}#${val[idx]}${ext}`;
console.log("New full file name: ", newname);
fs.renameSync(`${folderPath}/${file}`, newname);
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment