mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Assets extension, factorised using the received json type to organise the assets in the UI/folders.
This commit is contained in:
@ -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 //
|
||||
|
Reference in New Issue
Block a user