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

View File

@@ -2,15 +2,6 @@ import sharp from "sharp";
import path from "path";
export default class AlphaComposite {
#config
#operatorName
#operatorSourceFolder
constructor(config, operatorName, rootDir) {
this.#config = config
this.#operatorName = operatorName
this.#operatorSourceFolder = path.join(rootDir, this.#config.folder.operator, this.#operatorName)
}
async process(filename, extractedDir) {
const image = sharp(path.join(extractedDir, filename))

View File

@@ -1,11 +1,18 @@
import path from 'path'
import { appendSync, readSync } from './file.js'
export function append(config, operatorName, rootDir) {
export function appendReadme(config, operatorName, rootDir) {
const operatorConfig = config.operators[operatorName]
const projectJson = JSON.parse(readSync(path.join(rootDir, config.folder.operator, operatorName, 'project.json')))
appendSync(
`\n| ${operatorConfig.title.split(' - ')[0].split('Arknights: ')[1]} | [Link](https://arknights.halyul.dev/${operatorConfig.link}/) | [Link](https://steamcommunity.com/sharedfiles/filedetails/?id=${projectJson.workshopid}) |`,
path.join(rootDir, 'README.md')
)
}
export function appendMainConfig(operatorName, rootDir) {
appendSync(
`\n ${operatorName}: !include config/${operatorName}.yaml`,
path.join(rootDir, 'config.yaml')
)
}

View File

@@ -35,7 +35,6 @@ export default class AssetsProcessor {
const fallbackFilename = `${this.#config.operators[this.#operatorName].fallback_name}.png`
const fallbackBuffer = await this.#alphaCompositer.process(fallbackFilename, extractedDir)
await write(fallbackBuffer, path.join(this.#operatorSourceFolder, this.#operatorName, fallbackFilename))
await copy(path.join(this.#operatorSourceFolder, this.#operatorName, fallbackFilename), path.join(publicAssetsDir, fallbackFilename))
return {
dimensions,
assetsJson

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)
};
})
}
}

View File

@@ -19,10 +19,8 @@ export function runDev(rootDir) {
})()
}
export function runBuild(rootDir) {
; (async () => {
await build({
root: rootDir,
})
})()
export async function runBuild(rootDir) {
await build({
root: rootDir,
})
}

View File

@@ -1,7 +1,9 @@
import path from 'path'
import { mkdir, copy } from './file.js'
import { appendMainConfig } from './append.js'
export default function init(operatorName, __dirname, extractedDir) {
mkdir(extractedDir)
copy(path.join(__dirname, 'config', '_template.yaml'), path.join(__dirname, 'config', `${operatorName}.yaml`))
appendMainConfig(operatorName, __dirname)
}

View File

@@ -38,6 +38,7 @@ export default class ProjectJson {
if (matcher.match() !== null) {
this.#json.description = matcher.process()
}
// TODO: move the template generation to here
this.#json = {
...this.#json,
description: this.#json.description,