feat(background): add other in-game backgrouds (1/2)

Co-authored-by: yueqiaocertik <yueqiaocertik@users.noreply.github.com>
This commit is contained in:
Haoyu Xu
2023-01-19 00:22:34 -05:00
parent f067e8f194
commit 5669f78009
9 changed files with 190 additions and 114 deletions

63
libs/background.js Normal file
View File

@@ -0,0 +1,63 @@
import path from 'path';
import fs from 'fs';
import sharp from "sharp";
export default class Background {
#config
#rootDir
#backgroundFolder
#extractFolder
#files
constructor(config, rootDir) {
this.#config = config;
this.#rootDir = rootDir;
this.#backgroundFolder = path.join(rootDir, config.folder.operator, '_share', config.folder.background);
this.#extractFolder = path.join(this.#backgroundFolder, 'extracted');
}
async process() {
this.#files = fs.readdirSync(this.#extractFolder).filter((f) => {
return f.endsWith('.png') && f.includes('_left');
})
if (this.#files.length + 2 !== fs.readdirSync(this.#backgroundFolder).length) {
await Promise.all(this.#files.map(async (f) => {
const filenamePrefix = path.parse(f).name.replace('_left', '');
await this.#composite(filenamePrefix, '.png');
}))
} else {
console.log('Background images already exist, skip generation.')
}
}
async #composite(filenamePrefix, fileExt) {
const image = sharp(path.join(this.#extractFolder, `${filenamePrefix}_left${fileExt}`))
const metadata = await image.metadata()
image
.resize(2 * metadata.width, metadata.height, {
kernel: sharp.kernel.nearest,
fit: 'contain',
position: 'left top',
background: { r: 255, g: 255, b: 255, alpha: 0 }
})
.composite([
{
input: path.join(this.#extractFolder, `${filenamePrefix}_right${fileExt}`),
top: 0,
left: metadata.width,
},
])
.toFile(path.join(this.#backgroundFolder, `${filenamePrefix}${fileExt}`));
}
getFilesToCopy(publicAssetsDir) {
return this.#files.map((f) => {
return {
filename: f.replace('_left', ''),
source: path.join(this.#backgroundFolder),
target: path.join(publicAssetsDir, this.#config.folder.background)
};
})
}
}