update live2d assets detection to allow for subfolder organisation.

This commit is contained in:
Tony Ribeiro 2023-11-12 18:56:01 +01:00
parent dd12cacd16
commit 7f8994c1fd
1 changed files with 27 additions and 10 deletions

View File

@ -37,6 +37,25 @@ function checkAssetFileName(inputFilename) {
return path.normalize(inputFilename).replace(/^(\.\.(\/|\\|$))+/, '');; return path.normalize(inputFilename).replace(/^(\.\.(\/|\\|$))+/, '');;
} }
// Recursive function to get files
function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir)
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
// If it is a directory, recursively call the getFiles function with the directory path and the files array
getFiles(name, files)
} else {
// If it is a file, push the full path to the files array
files.push(name)
}
}
return files
}
/** /**
* Registers the endpoints for the asset management. * Registers the endpoints for the asset management.
* @param {import('express').Express} app Express app * @param {import('express').Express} app Express app
@ -70,16 +89,14 @@ function registerEndpoints(app, jsonParser) {
// Live2d assets // Live2d assets
if (folder == "live2d") { if (folder == "live2d") {
output[folder] = []; output[folder] = [];
const live2d_folders = fs.readdirSync(path.join(folderPath, folder)); const live2d_folder = path.normalize(path.join(folderPath, folder));
for (let model_folder of live2d_folders) { const files = getFiles(live2d_folder)
const live2d_model_path = path.join(folderPath, folder, model_folder); //console.debug("FILE FOUND:",files)
if (fs.statSync(live2d_model_path).isDirectory()) { for (let file of files) {
for (let file of fs.readdirSync(live2d_model_path)) { file = path.normalize(file.replace('public\\', ''));
if (file.includes("model")) { if (file.endsWith("model3.json")) {
//console.debug("Asset live2d model found:",file) //console.debug("Asset live2d model found:",file)
output[folder].push([`${model_folder}`, path.join("assets", folder, model_folder, file)]); output[folder].push(path.normalize(path.join(file)));
}
}
} }
} }
continue; continue;