Assets extension, factorised using the received json type to organise the assets in the UI/folders.

This commit is contained in:
Tony Ribeiro 2023-08-23 05:10:55 +02:00
parent 8e38229ed4
commit 13dac1f4d3
5 changed files with 89 additions and 177 deletions

View File

@ -16,15 +16,8 @@ let ASSETS_JSON_URL = "https://raw.githubusercontent.com/SillyTavern/SillyTavern
// DBG
//if (DEBUG_TONY_SAMA_FORK_MODE)
// ASSETS_JSON_URL = "https://raw.githubusercontent.com/Tony-sama/SillyTavern-Content/main/index.json"
let assetDictFormat = {
"audio":{
"ambient": [],
"bgm":[],
}};
let availableAssets = JSON.parse(JSON.stringify(assetDictFormat));
let currentAssets = JSON.parse(JSON.stringify(assetDictFormat));
let availableAssets = {};
let currentAssets = {};
//#############################//
// Extension UI and Settings //
@ -41,89 +34,51 @@ function loadSettings() {
console.debug(DEBUG_PREFIX,"Received assets dictionary", json);
for(const i of json){
console.log(DEBUG_PREFIX,i)
if (i["type"] == "ambient"){
availableAssets["audio"]["ambient"].push(i);
}
if (i["type"] == "bgm"){
availableAssets["audio"]["bgm"].push(i);
}
//console.log(DEBUG_PREFIX,i)
if (availableAssets[i["type"]] === undefined)
availableAssets[i["type"]] = [];
availableAssets[i["type"]].push(i);
}
console.debug(DEBUG_PREFIX,"Updated available assets to",availableAssets);
// Audio
// Ambient
for(const i in availableAssets["audio"]["ambient"]) {
const assetId = availableAssets["audio"]["ambient"][i]["id"]
let assetUrl = availableAssets["audio"]["ambient"][i]["url"]
const elemId = `assets_install_ambient_${i}`;
let element = $('<input />', { type: 'checkbox', id: elemId})
//if (DEBUG_TONY_SAMA_FORK_MODE)
// assetUrl = assetUrl.replace("https://github.com/SillyTavern/","https://github.com/Tony-sama/"); // DBG
console.debug(DEBUG_PREFIX,"Checking asset",assetId, assetUrl);
if (isAmbientInstalled(assetId)) {
console.debug(DEBUG_PREFIX,"installed, checked")
element.prop("disabled",true);
element.prop("checked",true);
}
else {
console.debug(DEBUG_PREFIX,"not installed, unchecked")
element.prop("checked",false);
element.on("click", function(){
installAmbient(assetUrl,assetId);
for (const assetType in availableAssets) {
let assetTypeMenu = $('<div />', {id:"assets_audio_ambient_div", class:"assets-list-div"});
assetTypeMenu.append(`<h3>${assetType}</h3>`)
for (const i in availableAssets[assetType]) {
const asset = availableAssets[assetType][i];
const elemId = `assets_install_${assetType}_${i}`;
let element = $('<input />', { type: 'checkbox', id: elemId})
//if (DEBUG_TONY_SAMA_FORK_MODE)
// assetUrl = assetUrl.replace("https://github.com/SillyTavern/","https://github.com/Tony-sama/"); // DBG
console.debug(DEBUG_PREFIX,"Checking asset",asset["id"], asset["url"]);
if (isAssetInstalled(assetType, asset["id"])) {
console.debug(DEBUG_PREFIX,"installed, checked")
element.prop("disabled",true);
element.off("click");
})
element.prop("checked",true);
}
else {
console.debug(DEBUG_PREFIX,"not installed, unchecked")
element.prop("checked",false);
element.on("click", function(){
installAsset(asset["url"], assetType, asset["id"]);
element.prop("disabled",true);
element.off("click");
})
}
console.debug(DEBUG_PREFIX,"Created element for BGM",asset["id"])
$(`<i></i>`)
.append(element)
.append(`<p>${asset["id"]}</p>`)
.appendTo(assetTypeMenu);
}
console.debug(DEBUG_PREFIX,"Created element for BGM",assetId)
$(`<i></i>`)
.append(element)
.append(`<p>${assetId}</p>`)
.appendTo("#assets_audio_ambient_div");
}
// BGM
for(const i in availableAssets["audio"]["bgm"]) {
const assetId = availableAssets["audio"]["bgm"][i]["id"]
let assetUrl = availableAssets["audio"]["bgm"][i]["url"]
const elemId = `assets_install_bgm_${i}`;
let element = $('<input />', { type: 'checkbox', id: elemId})
//if (DEBUG_TONY_SAMA_FORK_MODE)
// assetUrl = assetUrl.replace("https://github.com/SillyTavern/","https://github.com/Tony-sama/"); // DBG
console.debug(DEBUG_PREFIX,"Checking asset",assetId, assetUrl);
if (isBgmInstalled(assetId)) {
console.debug(DEBUG_PREFIX,"installed, checked")
element.prop("disabled",true);
element.prop("checked",true);
}
else {
console.debug(DEBUG_PREFIX,"not installed, unchecked")
element.prop("checked",false);
element.on("click", function(){
installBgm(assetUrl,assetId);
element.prop("disabled",true);
element.off("click");
})
}
console.debug(DEBUG_PREFIX,"Created element for BGM",assetId)
$(`<i></i>`)
.append(element)
.append(`<p>${assetId}</p>`)
.appendTo("#assets_audio_bgm_div");
assetTypeMenu.appendTo("#assets_menu");
}
});
});
@ -139,15 +94,6 @@ $(document).ready(function () {
<div class="inline-drawer-icon fa-solid fa-circle-chevron-down down"></div>
</div>
<div class="inline-drawer-content" id="assets_menu">
<div id="assets_audio_div">
<h3>Audio</h3>
<h4>Ambient</h4>
<div id="assets_audio_ambient_div" class="assets-list-div">
</div>
<h4>BGM</h4>
<div id="assets_audio_bgm_div" class="assets-list-div">
</div>
</div>
</div>
</div>
</div>
@ -164,9 +110,9 @@ $(document).ready(function () {
//moduleWorker();
})
function isAmbientInstalled(filename) {
for(const i of currentAssets["audio"]["ambient"]){
console.debug(DEBUG_PREFIX,i,filename)
function isAssetInstalled(assetType,filename) {
for(const i of currentAssets[assetType]){
//console.debug(DEBUG_PREFIX,i,filename)
if(i.includes(filename))
return true;
}
@ -174,37 +120,13 @@ function isAmbientInstalled(filename) {
return false;
}
function isBgmInstalled(filename) {
for(const i of currentAssets["audio"]["bgm"]){
console.debug(DEBUG_PREFIX,i,filename)
if(i.includes(filename))
return true;
}
return false;
}
async function installAmbient(url, filename) {
async function installAsset(url, assetType, filename) {
console.debug(DEBUG_PREFIX,"Downloading ",url);
const save_path = "public/assets/audio/ambient/"+filename;
const save_path = "public/assets/"+assetType+"/"+filename;
try {
const result = await fetch(`/asset_download?url=${url}&save_path=${save_path}`);
let musics = result.ok ? (await result.json()) : [];
return musics;
}
catch (err) {
console.log(err);
return [];
}
}
async function installBgm(url, filename) {
console.debug(DEBUG_PREFIX,"Downloading ",url);
const save_path = "public/assets/audio/bgm/"+filename;
try {
const result = await fetch(`/asset_download?url=${url}&save_path=${save_path}`);
let musics = result.ok ? (await result.json()) : [];
return musics;
let assets = result.ok ? (await result.json()) : [];
return assets;
}
catch (err) {
console.log(err);
@ -218,42 +140,16 @@ async function installBgm(url, filename) {
async function updateCurrentAssets() {
console.debug(DEBUG_PREFIX,"Checking installed assets...")
let assetList
// Check Ambient list
assetList = await getAmbientList();
currentAssets["audio"]["ambient"] = assetList;
// Check BGM list
assetList = await getBgmList();
currentAssets["audio"]["bgm"] = assetList;
try {
const result = await fetch(`/get_assets`);
currentAssets = result.ok ? (await result.json()) : {};
}
catch (err) {
console.log(err);
}
console.debug(DEBUG_PREFIX,"Current assets found:",currentAssets)
}
async function getAmbientList() {
try {
const result = await fetch(`/get_default_ambient_list`);
let musics = result.ok ? (await result.json()) : [];
return musics;
}
catch (err) {
console.log(err);
return [];
}
}
async function getBgmList() {
try {
const result = await fetch(`/get_default_bgm_list`);
let musics = result.ok ? (await result.json()) : [];
return musics;
}
catch (err) {
console.log(err);
return [];
}
}
//#############################//
// Module Worker //

View File

@ -17,8 +17,8 @@ const MODULE_NAME = 'Audio';
const DEBUG_PREFIX = "<Audio module> ";
const UPDATE_INTERVAL = 1000;
const ASSETS_BGM_FOLDER = "audio/bgm";
const ASSETS_AMBIENT_FOLDER = "audio/ambient";
const ASSETS_BGM_FOLDER = "bgm";
const ASSETS_AMBIENT_FOLDER = "ambient";
const CHARACTER_BGM_FOLDER = "bgm"
const FALLBACK_EXPRESSION = "neutral";
@ -106,6 +106,8 @@ function loadSettings() {
}
$("#audio_bgm_cooldown").val(extension_settings.audio.bgm_cooldown);
$("#audio_debug_div").hide(); // DBG
}
async function onEnabledClick() {
@ -176,10 +178,12 @@ $(document).ready(function () {
<input type="checkbox" id="audio_enabled" name="audio_enabled">
<small>Enabled</small>
</label>
<label class="checkbox_label" for="audio_debug">
<input type="checkbox" id="audio_debug" name="audio_debug">
<small>Debug</small>
</label>
<div id="audio_debug_div">
<label class="checkbox_label" for="audio_debug">
<input type="checkbox" id="audio_debug" name="audio_debug">
<small>Debug</small>
</label>
</div>
</div>
<div>
<div>
@ -253,13 +257,14 @@ $(document).ready(function () {
// API Calls //
//#############################//
async function getAssetsList(folderPath) {
console.debug(DEBUG_PREFIX, "getting files from",folderPath);
async function getAssetsList(type) {
console.debug(DEBUG_PREFIX, "getting assets of type",type);
try {
const result = await fetch(`/get_assets_list?folderPath=${folderPath}`);
let file_paths = result.ok ? (await result.json()) : [];
return file_paths;
const result = await fetch(`/get_assets`);
const assets = result.ok ? (await result.json()) : {type:[]};
console.debug(DEBUG_PREFIX, "Found assets:",assets);
return assets[type];
}
catch (err) {
console.log(err);
@ -553,6 +558,9 @@ async function updateAmbient() {
if (audio_file_path === null) {
console.debug(DEBUG_PREFIX,"No ambient file found for background",currentBackground);
const audio = $("#audio_ambient");
audio.attr("src","");
audio[0].pause();
return;
}

View File

@ -4943,20 +4943,28 @@ app.post('/delete_extension', jsonParser, async (request, response) => {
*
* @returns {void}
*/
app.get('/get_assets_list', jsonParser, function (request, response) {
const folderPath = path.join(directories.assets,request.query.folderPath);
let output = []
app.get('/get_assets', jsonParser, function (request, response) {
const folderPath = path.join(directories.assets);
let output = {}
//console.info("Checking files into",folderPath);
try {
if (fs.existsSync(folderPath) && fs.statSync(folderPath).isDirectory()) {
const files = fs.readdirSync(folderPath)
const folders = fs.readdirSync(folderPath)
.filter(filename => {
return filename != ".placeholder";
return fs.statSync(path.join(folderPath,filename)).isDirectory();
});
for (i of files)
output.push(`/assets/${request.query.folderPath}/${i}`);
for (const folder of folders) {
const files = fs.readdirSync(path.join(folderPath,folder))
.filter(filename => {
return filename != ".placeholder";
});
output[folder] = [];
for (const file of files){
output[folder].push(path.join("assets",folder,file));
}
}
}
}
catch (err) {