diff --git a/src/ts/index.ts b/src/ts/index.ts index b65a027..3238725 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -35,7 +35,7 @@ $(() => { LauncherUI.updateSocial(); }); - if (Genshinlib.getConfig().rpc) + if (Genshinlib.getConfig('rpc')) DiscordRPC.init(); ipcRenderer.on('rpc-toggle', () => { @@ -102,7 +102,7 @@ $(() => { LauncherUI.setState(Genshinlib.version === null ? 'game-installation-available' : 'game-update-available'); // Patch version is incorrect - else if (Genshinlib.getConfig().patch && Genshinlib.getConfig().patch.version != Genshinlib.getPatchInfo().version) + else if (Genshinlib.getConfig('patch') && Genshinlib.getConfig('patch.version') != Genshinlib.getPatchInfo().version) { // Patch is not available if (Genshinlib.getPatchInfo().version !== data.game.latest.version) @@ -130,7 +130,7 @@ $(() => { // Current patch is in testing phase, // but stable is available - else if (Genshinlib.getConfig().patch && Genshinlib.getConfig().patch.version == Genshinlib.getPatchInfo().version && Genshinlib.getConfig().patch.state == 'testing' && Genshinlib.getPatchInfo().state == 'stable') + else if (Genshinlib.getConfig('patch') && Genshinlib.getConfig('patch.version') == Genshinlib.getPatchInfo().version && Genshinlib.getConfig('patch.state') == 'testing' && Genshinlib.getPatchInfo().state == 'stable') { console.log(`%c> Applying patch...`, 'font-size: 16px'); @@ -185,12 +185,12 @@ $(() => { { let wineExeutable = 'wine'; - if (Genshinlib.getConfig().runner !== null) + if (Genshinlib.getConfig('runner') !== null) { wineExeutable = path.join( Genshinlib.runnersDir, - Genshinlib.getConfig().runner?.folder, - Genshinlib.getConfig().runner?.executable + Genshinlib.getConfig('runner.folder'), + Genshinlib.getConfig('runner.executable') ); if (!fs.existsSync(wineExeutable)) diff --git a/src/ts/lib/Genshinlib.ts b/src/ts/lib/Genshinlib.ts index 134da6c..9a8f6bb 100644 --- a/src/ts/lib/Genshinlib.ts +++ b/src/ts/lib/Genshinlib.ts @@ -72,12 +72,12 @@ export class Genshinlib public static get version(): Config['version'] { - return this.getConfig().version; + return this.getConfig('version'); } public static get lang(): Config['lang'] { - return this.getConfig().lang; + return this.getConfig('lang'); } public static getRunners (): Promise<[{ title: string, runners: Runner[] }]> @@ -102,7 +102,7 @@ export class Genshinlib // return new Promise(resolve => resolve(JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'dxvks.json'))))); } - public static getConfig (): Config + public static getConfig (property: string|null = null, splitProperty: boolean = true): any { if (!fs.existsSync(this.launcherJson)) fs.writeFileSync(this.launcherJson, JSON.stringify({ @@ -120,7 +120,20 @@ export class Genshinlib rpc: false }, null, 4)); - return JSON.parse(fs.readFileSync(this.launcherJson)); + let config = JSON.parse(fs.readFileSync(this.launcherJson)); + + if (property === null) + return config; + + else + { + if (!splitProperty) + return config[property]; + + property.split('.').forEach(prop => config = config[prop]); + + return config; + } } public static setConfig (info: Config): Genshinlib @@ -159,12 +172,12 @@ export class Genshinlib { let background = ''; - if (!this.getConfig().background.time || new Date(new Date().setHours(0,0,0,0)).setDate(new Date(new Date().setHours(0,0,0,0)).getDate()).toString() >= this.getConfig().background.time!) + if (!this.getConfig('background.time') || new Date(new Date().setHours(0,0,0,0)).setDate(new Date(new Date().setHours(0,0,0,0)).getDate()).toString() >= this.getConfig('background.time')!) { await fetch(this.backgroundUri + this.lang.launcher) .then(res => res.json()) .then(async resdone => { - let prevBackground = this.getConfig().background.file; + let prevBackground = this.getConfig('background.file'); this.updateConfig({ background: { @@ -173,23 +186,23 @@ export class Genshinlib } }); - if (fs.existsSync(path.join(this.launcherDir, this.getConfig().background.file))) - background = path.join(this.launcherDir, this.getConfig().background.file); + if (fs.existsSync(path.join(this.launcherDir, this.getConfig('background.file')))) + background = path.join(this.launcherDir, this.getConfig('background.file')); else { - await Tools.downloadFile(resdone.data.adv.background, path.join(this.launcherDir, this.getConfig().background.file), (current: number, total: number, difference: number) => null).then(() => { + await Tools.downloadFile(resdone.data.adv.background, path.join(this.launcherDir, this.getConfig('background.file')), (current: number, total: number, difference: number) => null).then(() => { !prevBackground ? console.log('No old background found') : fs.unlinkSync(path.join(this.launcherDir, prevBackground)); - background = path.join(this.launcherDir, this.getConfig().background.file); + background = path.join(this.launcherDir, this.getConfig('background.file')); }); }; }); } - else background = path.join(this.launcherDir, this.getConfig().background.file); + else background = path.join(this.launcherDir, this.getConfig('background.file')); return background; } @@ -253,13 +266,13 @@ export class Genshinlib let installationProgress = 0; let installerProcess; - if (this.getConfig().runner) + if (this.getConfig('runner')) { installerProcess = spawn('winetricks', ['corefonts', 'usetakefocus=n', 'dxvk191'], { env: { ...process.env, WINEPREFIX: prefixpath, - WINE: path.join(this.runnersDir, this.getConfig().runner?.folder, this.getConfig().runner?.executable) + WINE: path.join(this.runnersDir, this.getConfig('runner.folder'), this.getConfig('runner.executable')) } }); } diff --git a/src/ts/lib/LauncherUI.ts b/src/ts/lib/LauncherUI.ts index 77ceed9..9f2c591 100644 --- a/src/ts/lib/LauncherUI.ts +++ b/src/ts/lib/LauncherUI.ts @@ -159,14 +159,14 @@ export class LauncherUI public static updateSocial (): void { fetch(`https://genshin.mihoyo.com/launcher/10/${Genshinlib.lang.launcher}?api_url=https%3A%2F%2Fapi-os-takumi.mihoyo.com%2Fhk4e_global&prev=false`) - .then(res => res.text()) - .then(body => { - $('#__layout').remove(); - $(body).find('#__layout').appendTo('#launchcontent'); + .then(res => res.text()) + .then(body => { + $('#__layout').remove(); + $(body).find('#__layout').appendTo('#launchcontent'); - $('#launchcontent .home__main .home-swiper-wrap').remove(); - $('#launchcontent .home__main .home-news').remove(); - }); + $('#launchcontent .home__main .home-swiper-wrap').remove(); + $('#launchcontent .home__main .home-news').remove(); + }); } public static updateLang (lang: string|null = null): void diff --git a/src/ts/settings.ts b/src/ts/settings.ts index bac13f5..cd627d3 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -33,7 +33,7 @@ $(() => { $(`#voice-list option[value="${Genshinlib.lang.voice}"]`).prop('selected', true); $(`#language-list option[value="${Genshinlib.lang.launcher}"]`).prop('selected', true); - if (Genshinlib.getConfig().rpc) + if (Genshinlib.getConfig('rpc')) $('#drpc').prop('checked', true); $('#drpc').on('change', () => { @@ -74,32 +74,27 @@ $(() => { lang: { launcher: e.target.value, voice: Genshinlib.lang.voice - } - }); + }, - // This is required as the file name changes on the API but since we don't call the API before checking - // if the time is null or expired we set time to null here. - Genshinlib.updateConfig({ + // This is required as the file name changes on the API but since we don't call the API before checking + // if the time is null or expired we set time to null here. background: { time: null, - file: Genshinlib.getConfig().background.file + file: Genshinlib.getConfig('background.file') } }); - // Send language updates LauncherUI.updateLang(e.target.value); - ipcRenderer.send('change-lang', { 'lang': e.target.value }); - /*$('*[i18id]').each((i, element) => { - element.innerText = i18n.translate(element.getAttribute('i18id')?.toString()!); - });*/ + // Send language updates + ipcRenderer.send('change-lang', { 'lang': e.target.value }); $(`#language-list option[value="${activeLang}"]`).removeProp('selected'); $(`#language-list option[value="${e.target.value}"]`).prop('selected', true); } }); - let activeRunner = Genshinlib.getConfig().runner; + let activeRunner = Genshinlib.getConfig('runner'); Genshinlib.getRunners().then(runners => { runners.forEach(category => { @@ -173,7 +168,7 @@ $(() => { }); }); - let activeDXVK = Genshinlib.getConfig().dxvk; + let activeDXVK = Genshinlib.getConfig('dxvk'); Genshinlib.getDXVKs().then(dxvks => { dxvks.forEach(dxvk => {