build-avatar: refactoring + parallel processing
This commit is contained in:
parent
221fe2eb8c
commit
4d7d4763fd
154
build-avatars.js
154
build-avatars.js
@ -4,8 +4,53 @@
|
|||||||
const sharp = require('sharp')
|
const sharp = require('sharp')
|
||||||
const fs = require('node:fs')
|
const fs = require('node:fs')
|
||||||
const path = require('node:path')
|
const path = require('node:path')
|
||||||
|
const {
|
||||||
|
Worker, isMainThread, parentPort, workerData,
|
||||||
|
} = require('node:worker_threads')
|
||||||
|
|
||||||
{
|
const avatarPartsDef = {
|
||||||
|
// Available parts:
|
||||||
|
// Note: some part files are empty, so that David's generator don't always add every part.
|
||||||
|
// But this make my algorithm generate a lot of avatars that have no part other that the body and the yes.
|
||||||
|
// So i don't include all empty files
|
||||||
|
'sepia': {
|
||||||
|
body: 25,
|
||||||
|
pattern: 14, // 12 to 20 are empty
|
||||||
|
mouth: 10,
|
||||||
|
eyes: 10,
|
||||||
|
accessories: 17, // 14 to 20 are empty
|
||||||
|
misc: 16, // 15 to 20 are empty
|
||||||
|
hat: 20 // 13 to 20 are empty
|
||||||
|
},
|
||||||
|
'cat': {
|
||||||
|
body: 15,
|
||||||
|
fur: 10,
|
||||||
|
eyes: 15,
|
||||||
|
mouth: 10,
|
||||||
|
accessorie: 20 // 17 to 20 are empty
|
||||||
|
},
|
||||||
|
'bird': {
|
||||||
|
tail: 9, // here we must begin with the tail
|
||||||
|
hoop: 10,
|
||||||
|
body: 9,
|
||||||
|
wing: 9,
|
||||||
|
eyes: 9,
|
||||||
|
bec: 9,
|
||||||
|
accessorie: 16 // 15 to 20 are empty
|
||||||
|
},
|
||||||
|
'fenec': {
|
||||||
|
body: 25,
|
||||||
|
nose: 10,
|
||||||
|
tail: 5,
|
||||||
|
eyes: 10,
|
||||||
|
mouth: 10,
|
||||||
|
accessories: 16, // 14 to 20 are empty
|
||||||
|
misc: 17, // 15 to 20 are empty
|
||||||
|
hat: 15 // 13 to 20 are empty
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateLegacyAvatars () {
|
||||||
// Legacy avatars generation
|
// Legacy avatars generation
|
||||||
const inputDir = './assets/images/avatars/legacy'
|
const inputDir = './assets/images/avatars/legacy'
|
||||||
const outputDir = './dist/server/avatars/legacy'
|
const outputDir = './dist/server/avatars/legacy'
|
||||||
@ -50,55 +95,14 @@ const path = require('node:path')
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
// 2024 avatars generation
|
// 2024 avatars generation
|
||||||
|
async function generateAvatars (part) {
|
||||||
async function generate () {
|
console.log('Starting generating ' + part)
|
||||||
const partsDef = {
|
const parts = avatarPartsDef[part]
|
||||||
// Available parts:
|
if (!parts) {
|
||||||
// Note: some part files are empty, so that David's generator don't always add every part.
|
throw new Error('Missing part\'s conf: ' + part)
|
||||||
// But this make my algorithm generate a lot of avatars that have no part other that the body and the yes.
|
|
||||||
// So i don't include all empty files
|
|
||||||
'sepia': {
|
|
||||||
body: 25,
|
|
||||||
pattern: 14, // 12 to 20 are empty
|
|
||||||
mouth: 10,
|
|
||||||
eyes: 10,
|
|
||||||
accessories: 17, // 14 to 20 are empty
|
|
||||||
misc: 16, // 15 to 20 are empty
|
|
||||||
hat: 20 // 13 to 20 are empty
|
|
||||||
},
|
|
||||||
'cat': {
|
|
||||||
body: 15,
|
|
||||||
fur: 10,
|
|
||||||
eyes: 15,
|
|
||||||
mouth: 10,
|
|
||||||
accessorie: 20 // 17 to 20 are empty
|
|
||||||
},
|
|
||||||
'bird': {
|
|
||||||
tail: 9, // here we must begin with the tail
|
|
||||||
hoop: 10,
|
|
||||||
body: 9,
|
|
||||||
wing: 9,
|
|
||||||
eyes: 9,
|
|
||||||
bec: 9,
|
|
||||||
accessorie: 16 // 15 to 20 are empty
|
|
||||||
},
|
|
||||||
'fenec': {
|
|
||||||
body: 25,
|
|
||||||
nose: 10,
|
|
||||||
tail: 5,
|
|
||||||
eyes: 10,
|
|
||||||
mouth: 10,
|
|
||||||
accessories: 16, // 14 to 20 are empty
|
|
||||||
misc: 17, // 15 to 20 are empty
|
|
||||||
hat: 15 // 13 to 20 are empty
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const part in partsDef) {
|
|
||||||
const parts = partsDef[part]
|
|
||||||
|
|
||||||
const inputDir = path.join('./assets/images/avatars/', part)
|
const inputDir = path.join('./assets/images/avatars/', part)
|
||||||
const outputDir = path.join('./dist/server/avatars/', part)
|
const outputDir = path.join('./dist/server/avatars/', part)
|
||||||
fs.mkdirSync(outputDir, { recursive: true })
|
fs.mkdirSync(outputDir, { recursive: true })
|
||||||
@ -126,13 +130,14 @@ const path = require('node:path')
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const firstPart = Object.keys(parts)[0]
|
const partsToCombine = Object.keys(parts)
|
||||||
|
const firstPart = partsToCombine.shift()
|
||||||
const firstFile = computeFilename(firstPart, i)
|
const firstFile = computeFilename(firstPart, i)
|
||||||
|
|
||||||
// We just have to combinate different parts into one file, then output at the wanted size.
|
// We just have to combinate different parts into one file, then output at the wanted size.
|
||||||
const composites = []
|
const composites = []
|
||||||
let j = 0
|
let j = 0
|
||||||
for (const part of Object.keys(parts).filter(p => p !== firstPart)) {
|
for (const part of partsToCombine) {
|
||||||
j++ // introduce an offset so we don't get all empty parts at the same time
|
j++ // introduce an offset so we don't get all empty parts at the same time
|
||||||
composites.push({
|
composites.push({
|
||||||
input: computeFilename(part, i + (j * 7))
|
input: computeFilename(part, i + (j * 7))
|
||||||
@ -153,6 +158,7 @@ const path = require('node:path')
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function generateBotsAvatars () {
|
||||||
{
|
{
|
||||||
// Moderation bot avatar: choosing some parts, and turning it so he is facing left.
|
// Moderation bot avatar: choosing some parts, and turning it so he is facing left.
|
||||||
const inputDir = path.join('./assets/images/avatars/', 'sepia')
|
const inputDir = path.join('./assets/images/avatars/', 'sepia')
|
||||||
@ -256,12 +262,56 @@ const path = require('node:path')
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generate().then(
|
if (isMainThread) {
|
||||||
|
const what = [
|
||||||
|
'legacy',
|
||||||
|
...Object.keys(avatarPartsDef),
|
||||||
|
'bots'
|
||||||
|
]
|
||||||
|
|
||||||
|
// Creating Worker threads to process each avatar type in parallel
|
||||||
|
for (const part of what) {
|
||||||
|
const p = new Promise((resolve, reject) => {
|
||||||
|
const worker = new Worker(__filename, {
|
||||||
|
workerData: part
|
||||||
|
})
|
||||||
|
worker.on('message', resolve)
|
||||||
|
worker.on('error', reject)
|
||||||
|
worker.on('exit', (code) => {
|
||||||
|
if (code !== 0) {
|
||||||
|
reject (new Error(`Worker stopped with exit code ${code}`))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
p.then(
|
||||||
|
(msg) => console.log('Part ' + part + ': ' + msg),
|
||||||
|
() => console.error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const part = workerData
|
||||||
|
if (part === 'legacy') {
|
||||||
|
generateLegacyAvatars()
|
||||||
|
parentPort.postMessage('done')
|
||||||
|
} else if (part === 'bots') {
|
||||||
|
generateBotsAvatars().then(
|
||||||
() => {
|
() => {
|
||||||
console.log('Done.')
|
parentPort.postMessage('done')
|
||||||
},
|
},
|
||||||
(err) => {
|
(err) => {
|
||||||
console.error(err)
|
throw err
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
generateAvatars(part).then(
|
||||||
|
() => {
|
||||||
|
parentPort.postMessage('done')
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
throw err
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user