From 0c52e1abfa7128891cb23c455fa8adead0e98be5 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Fri, 31 Mar 2023 23:16:25 +0000 Subject: [PATCH] intialize soundpacks --- packages/app/src/cores/sound/index.js | 41 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/app/src/cores/sound/index.js b/packages/app/src/cores/sound/index.js index 4cf930a1..5a22e91a 100755 --- a/packages/app/src/cores/sound/index.js +++ b/packages/app/src/cores/sound/index.js @@ -1,28 +1,53 @@ import Core from "evite/src/core" import { Howl } from "howler" import config from "config" +import axios from "axios" +import store from "store" export default class SoundCore extends Core { static refName = "sound" - + static namespace = "sound" + soundsPool = {} + public = { play: this.play, getSounds: this.getSounds, } - async getSounds() { - // TODO: Load custom soundpacks manifests - let soundPack = config.defaultSoundPack ?? {} + async initialize() { + let soundpack = config.defaultSoundPack ?? {} - return soundPack + const storedCustomSoundpack = store.get("soundpack_manifest") + + if (storedCustomSoundpack) { + // check if is valid url with regex + const urlRegex = /^(ftp|http|https):\/\/[^ "]+$/; + + if (urlRegex.test(storedCustomSoundpack)) { + // load with axios + const { data } = await axios.get(storedCustomSoundpack) + + soundpack = data + } else { + console.error(`Soundpack [${storedCustomSoundpack}] is not a valid url.`) + return false + } + } + + for (const [name, path] of Object.entries(soundpack)) { + this.soundsPool[name] = new Howl({ + volume: window.app.cores.settings.get("generalAudioVolume") ?? 0.5, + src: [path], + }) + } + + console.log(this.soundsPool) } async play(name, options) { - let soundPack = await this.getSounds() - - if (soundPack[name]) { + if (this.soundsPool[name]) { return new Howl({ volume: window.app.cores.settings.get("generalAudioVolume") ?? 0.5, ...options,