From 7f8994c1fd74f0ea1774f418c25990cbbd27fbc9 Mon Sep 17 00:00:00 2001 From: Tony Ribeiro Date: Sun, 12 Nov 2023 18:56:01 +0100 Subject: [PATCH 1/4] update live2d assets detection to allow for subfolder organisation. --- src/assets.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/assets.js b/src/assets.js index b969f62b3..2c7bd036f 100644 --- a/src/assets.js +++ b/src/assets.js @@ -37,6 +37,25 @@ function checkAssetFileName(inputFilename) { 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. * @param {import('express').Express} app Express app @@ -70,16 +89,14 @@ function registerEndpoints(app, jsonParser) { // Live2d assets if (folder == "live2d") { output[folder] = []; - const live2d_folders = fs.readdirSync(path.join(folderPath, folder)); - for (let model_folder of live2d_folders) { - const live2d_model_path = path.join(folderPath, folder, model_folder); - if (fs.statSync(live2d_model_path).isDirectory()) { - for (let file of fs.readdirSync(live2d_model_path)) { - if (file.includes("model")) { - //console.debug("Asset live2d model found:",file) - output[folder].push([`${model_folder}`, path.join("assets", folder, model_folder, file)]); - } - } + const live2d_folder = path.normalize(path.join(folderPath, folder)); + const files = getFiles(live2d_folder) + //console.debug("FILE FOUND:",files) + for (let file of files) { + file = path.normalize(file.replace('public\\', '')); + if (file.endsWith("model3.json")) { + //console.debug("Asset live2d model found:",file) + output[folder].push(path.normalize(path.join(file))); } } continue; From e7e4f75c86bea2083065f925508d544c3bbe88e6 Mon Sep 17 00:00:00 2001 From: Tony Ribeiro Date: Sun, 12 Nov 2023 19:08:29 +0100 Subject: [PATCH 2/4] fix new live2d asset listing for character folder. --- src/assets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/assets.js b/src/assets.js index 2c7bd036f..385b1d7a2 100644 --- a/src/assets.js +++ b/src/assets.js @@ -274,7 +274,7 @@ function registerEndpoints(app, jsonParser) { for (let file of fs.readdirSync(live2dModelPath)) { //console.debug("Character live2d model found:", file) if (file.includes("model")) - output.push([`${modelFolder}`, path.join("characters", name, category, modelFolder, file)]); + output.push(path.join("characters", name, category, modelFolder, file)); } } } From c5ea3cfce783c63e825b93e8277d855d640f766c Mon Sep 17 00:00:00 2001 From: Tony Ribeiro Date: Sun, 12 Nov 2023 19:21:01 +0100 Subject: [PATCH 3/4] normalize path of live2d assets --- src/assets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/assets.js b/src/assets.js index 385b1d7a2..059fa804c 100644 --- a/src/assets.js +++ b/src/assets.js @@ -93,7 +93,7 @@ function registerEndpoints(app, jsonParser) { const files = getFiles(live2d_folder) //console.debug("FILE FOUND:",files) for (let file of files) { - file = path.normalize(file.replace('public\\', '')); + file = path.normalize(file.replace(path.normalize('public\\'), '')); if (file.endsWith("model3.json")) { //console.debug("Asset live2d model found:",file) output[folder].push(path.normalize(path.join(file))); From f082420fc777aa5327677bbb142e714dde4a250c Mon Sep 17 00:00:00 2001 From: Tony Ribeiro Date: Sun, 12 Nov 2023 19:25:13 +0100 Subject: [PATCH 4/4] Use cross os separator for live2d assets path --- src/assets.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/assets.js b/src/assets.js index 059fa804c..0087d3a62 100644 --- a/src/assets.js +++ b/src/assets.js @@ -93,7 +93,7 @@ function registerEndpoints(app, jsonParser) { const files = getFiles(live2d_folder) //console.debug("FILE FOUND:",files) for (let file of files) { - file = path.normalize(file.replace(path.normalize('public\\'), '')); + file = path.normalize(file.replace('public'+path.sep, '')); if (file.endsWith("model3.json")) { //console.debug("Asset live2d model found:",file) output[folder].push(path.normalize(path.join(file)));