diff --git a/public/src/js/plugins.js b/public/src/js/plugins.js index 7dfef2e..604e7a2 100644 --- a/public/src/js/plugins.js +++ b/public/src/js/plugins.js @@ -159,6 +159,34 @@ class Plugins{ } return input } + isObject(input){ + return input && typeof input === "object" && input.constructor === Object + } + deepMerge(target, ...sources){ + sources.forEach(source => { + if(this.isObject(target) && this.isObject(source)){ + for(var i in source){ + if(this.isObject(source[i])){ + if(!target[i]){ + target[i] = {} + } + this.deepMerge(target[i], source[i]) + }else if(source[i]){ + target[i] = source[i] + } + } + } + }) + return target + } + arrayDel(array, item){ + var index = array.indexOf(item) + if(index !== -1){ + array.splice(index, 1) + return true + } + return false + } hasSettings(){ for(var i = 0; i < this.allPlugins.length; i++){ @@ -499,14 +527,32 @@ class EditFunction extends EditValue{ class Patch{ edits = [] + addedLanguages = [] addEdits(...args){ args.forEach(arg => this.edits.push(arg)) } + addLanguage(lang, forceSet, fallback="en"){ + if(fallback){ + lang = plugins.deepMerge({}, allStrings[fallback], lang) + } + this.addedLanguages.push({ + lang: lang, + forceSet: forceSet + }) + } beforeStart(){ this.edits.forEach(edit => edit.start()) + this.addedLanguages.forEach(obj => { + settings.addLang(obj.lang, obj.forceSet) + }) } beforeStop(){ - this.edits.forEach(edit => edit.stop()) + for(var i = this.edits.length; i--;){ + this.edits[i].stop() + } + for(var i = this.addedLanguages.length; i--;){ + settings.removeLang(this.addedLanguages[i].lang) + } } beforeUnload(){ this.edits.forEach(edit => edit.unload()) diff --git a/public/src/js/settings.js b/public/src/js/settings.js index dc6e5b9..e9c9f2d 100644 --- a/public/src/js/settings.js +++ b/public/src/js/settings.js @@ -159,6 +159,58 @@ class Settings{ pageEvents.send("language-change", lang.id) } } + addLang(lang, forceSet){ + allStrings[lang.id] = lang + if(lang.categories){ + assets.categories.forEach(category => { + if("title_lang" in category && lang.categories[category.title_lang.en]){ + category.title_lang[lang.id] = lang.categories[category.title_lang.en] + } + }) + } + languageList.push(lang.id) + this.allLanguages.push(lang.id) + this.items.language.default = this.getLang() + if(forceSet){ + this.storage.language = lang.id + }else{ + try{ + this.storage.language = localStorage.getItem("lang") + }catch(e){} + if(this.items.language.options.indexOf(this.storage.language) === -1){ + this.storage.language = null + } + } + if(settings.getItem("language") === lang.id){ + settings.setLang(lang) + } + } + removeLang(lang){ + delete allStrings[lang.id] + assets.categories.forEach(category => { + if("title_lang" in category){ + delete category.title_lang[lang.id] + } + }) + var index = languageList.indexOf(lang.id) + if(index !== -1){ + languageList.splice(index, 1) + } + var index = this.allLanguages.indexOf(lang.id) + if(index !== -1){ + this.allLanguages.splice(index, 1) + } + this.items.language.default = this.getLang() + try{ + this.storage.language = localStorage.getItem("lang") + }catch(e){} + if(this.items.language.options.indexOf(this.storage.language) === -1){ + this.storage.language = null + } + if(lang.id === strings.id){ + settings.setLang(allStrings[this.getItem("language")]) + } + } } class SettingsView{ @@ -204,6 +256,10 @@ class SettingsView{ } }, this.windowSymbol) + if(this.customSettings){ + pageEvents.add(window, "language-change", event => this.setLang(), this.windowSymbol) + } + var gamepadEnabled = false if("getGamepads" in navigator){ var gamepads = navigator.getGamepads() @@ -441,7 +497,11 @@ class SettingsView{ this.mode = "latency" this.latencySet() } - pageEvents.send("settings") + if(this.customSettings){ + pageEvents.send("plugins") + }else{ + pageEvents.send("settings") + } } getElement(name){ return loader.screen.getElementsByClassName(name)[0] @@ -1014,7 +1074,9 @@ class SettingsView{ return title } setLang(lang){ - settings.setLang(lang) + if(lang){ + settings.setLang(lang) + } if(failedTests.length !== 0){ showUnsupported(strings) } @@ -1098,6 +1160,9 @@ class SettingsView{ this.gamepad.clean() assets.sounds["bgm_settings"].stop() pageEvents.remove(window, ["mouseup", "touchstart", "touchmove", "touchend", "blur"], this.windowSymbol) + if(this.customSettings){ + pageEvents.remove(window, "language-change", this.windowSymbol) + } for(var i in this.items){ this.removeTouchEnd(this.items[i].settingBox) }