From 394525b8ca743603125e6a9290338ef293922d0e Mon Sep 17 00:00:00 2001 From: Maroxy Date: Sun, 24 Oct 2021 11:57:10 +0200 Subject: [PATCH] Change to electron-store --- entry.js | 7 +++--- src/ts/index.ts | 14 ++++------- src/ts/lib/Genshinlib.ts | 52 ++++++++++++++++++++++++++-------------- src/ts/lib/LauncherUI.ts | 2 +- src/ts/settings.ts | 50 +++++++++++--------------------------- 5 files changed, 57 insertions(+), 68 deletions(-) diff --git a/entry.js b/entry.js index 255db22..d4f8f6f 100644 --- a/entry.js +++ b/entry.js @@ -9,6 +9,9 @@ const { const path = require('path'); +const Store = require('electron-store'); +Store.initRenderer(); + const { Genshinlib } = require('./public/js/Genshinlib'); let mainWindow; @@ -70,8 +73,6 @@ function createWindow () // mainWindow.webContents.openDevTools(); } -// Set language on start -app.commandLine.appendSwitch('lang', Genshinlib.lang.launcher ?? 'en-us'); // This method will be called when Electron has finished // initialization and is ready to create browser windows. @@ -89,8 +90,6 @@ app.whenReady().then(() => { // This has to be here otherwise webContents is invalid ipcMain.on('change-lang', (event, args) => { - app.commandLine.appendSwitch('lang', Genshinlib.lang.launcher); - mainWindow.webContents.send('change-lang', { 'lang': args.lang }); }); diff --git a/src/ts/index.ts b/src/ts/index.ts index 90ea0bc..e452b66 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -22,8 +22,8 @@ $(() => { if (Genshinlib.version !== null) document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version; + LauncherUI.updateLang(Genshinlib.getConfig('lang.launcher') ?? 'en-us'); LauncherUI.setState('game-launch-available'); - LauncherUI.updateBackground(); LauncherUI.updateSocial(); @@ -51,7 +51,7 @@ $(() => { let old; for (let i = 0; i < data.game.latest.voice_packs.length; ++i) - if (data.game.latest.voice_packs[i].language == Genshinlib.lang.voice) + if (data.game.latest.voice_packs[i].language == Genshinlib.getConfig('lang.voice')) { voicePack = data.game.latest.voice_packs[i]; @@ -195,9 +195,7 @@ $(() => { { wineExeutable = 'wine'; - Genshinlib.updateConfig({ - runner: null - }); + Genshinlib.updateConfig('runner', null); } } @@ -309,7 +307,7 @@ $(() => { let voicePack = diff.voice_packs[1]; // en-us for (let i = 0; i < diff.voice_packs.length; ++i) - if (diff.voice_packs[i].language == Genshinlib.lang.voice) + if (diff.voice_packs[i].language == Genshinlib.getConfig('voice')) { voicePack = diff.voice_packs[i]; @@ -334,9 +332,7 @@ $(() => { }).then(() => { fs.unlinkSync(path.join(Genshinlib.launcherDir, voicePack.name)); - Genshinlib.updateConfig({ - version: data.game.latest.version - }); + Genshinlib.updateConfig('version', data.game.latest.version); // Patch available if (Genshinlib.getPatchInfo().version === data.game.latest.version) diff --git a/src/ts/lib/Genshinlib.ts b/src/ts/lib/Genshinlib.ts index f531899..27b0016 100644 --- a/src/ts/lib/Genshinlib.ts +++ b/src/ts/lib/Genshinlib.ts @@ -1,5 +1,6 @@ import GIJSON from '../types/GIJSON'; import { Tools } from './Tools'; +const Store = require('electron-store'); const https = require('follow-redirects').https; @@ -9,6 +10,23 @@ const os = require('os'); const { spawn, exec } = require('child_process'); const dns = require('dns'); +const config = new Store({ + defaults: { + lang: { + launcher: 'en-us', + voice: 'en-us' + }, + background: { + time: null, + file: null + }, + version: null, + patch: null, + runner: null, + rpc: false, + }, +}); + type Runner = { name: string, // Runner title which will be showed in the list version: string, // Runner version @@ -75,11 +93,6 @@ export class Genshinlib return this.getConfig('version'); } - public static get lang(): Config['lang'] - { - return this.getConfig('lang'); - } - public static getRunners (): Promise<[{ title: string, runners: Runner[] }]> { return new Promise((resolve, reject) => { @@ -104,6 +117,12 @@ export class Genshinlib public static getConfig (property: string|null = null, splitProperty: boolean = true): any { + if (property === null) + return config; + + return config.get(property) + + /* if (!fs.existsSync(this.launcherJson)) fs.writeFileSync(this.launcherJson, JSON.stringify({ lang: { @@ -133,7 +152,7 @@ export class Genshinlib property.split('.').forEach(prop => config = config[prop]); return config; - } + }*/ } public static setConfig (info: Config): Genshinlib @@ -143,12 +162,13 @@ export class Genshinlib return this; } - public static updateConfig (config: any): Genshinlib + public static updateConfig (cname: string, value: string|boolean|null|number): Genshinlib { - return this.setConfig({ - ...this.getConfig(), - ...config - }); + return config.set(cname, value); + //return this.setConfig({ + // ...this.getConfig(), + // ...config + //}); } public static async getData (): Promise @@ -174,17 +194,13 @@ export class Genshinlib 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) + await fetch(this.backgroundUri + this.getConfig('lang.launcher')) .then(res => res.json()) .then(async resdone => { let prevBackground = this.getConfig('background.file'); - this.updateConfig({ - background: { - time: new Date(new Date().setHours(0,0,0,0)).setDate(new Date(new Date().setHours(0,0,0,0)).getDate() + 7).toString(), - file: resdone.data.adv.background.replace(/.*\//, '') - } - }); + this.updateConfig('background.time', new Date(new Date().setHours(0,0,0,0)).setDate(new Date(new Date().setHours(0,0,0,0)).getDate() + 7).toString()); + this.updateConfig('background.file', resdone.data.adv.background.replace(/.*\//, '')); if (fs.existsSync(path.join(this.launcherDir, this.getConfig('background.file')))) background = path.join(this.launcherDir, this.getConfig('background.file')); diff --git a/src/ts/lib/LauncherUI.ts b/src/ts/lib/LauncherUI.ts index 2f94158..438d6b3 100644 --- a/src/ts/lib/LauncherUI.ts +++ b/src/ts/lib/LauncherUI.ts @@ -166,7 +166,7 @@ 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`) + fetch(`https://genshin.mihoyo.com/launcher/10/${Genshinlib.getConfig('lang.launcher')}?api_url=https%3A%2F%2Fapi-os-takumi.mihoyo.com%2Fhk4e_global&prev=false`) .then(res => res.text()) .then(body => { $('#__layout').remove(); diff --git a/src/ts/settings.ts b/src/ts/settings.ts index a64747f..4144b9f 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -10,6 +10,8 @@ import { Tools } from './lib/Tools'; $(() => { + LauncherUI.updateLang(Genshinlib.getConfig('lang.launcher') ?? 'en-us'); + $('*[i18id]').each((i, element) => { element.innerText = LauncherUI.i18n.translate(element.getAttribute('i18id')?.toString()!); }); @@ -29,31 +31,24 @@ $(() => { }); // Select the saved options in launcher.json on load - $(`#voice-list option[value="${Genshinlib.lang.voice}"]`).prop('selected', true); - $(`#language-list option[value="${Genshinlib.lang.launcher}"]`).prop('selected', true); + $(`#voice-list option[value="${Genshinlib.getConfig('lang.voice')}"]`).prop('selected', true); + $(`#language-list option[value="${Genshinlib.getConfig('lang.launcher')}"]`).prop('selected', true); if (Genshinlib.getConfig('rpc')) $('#drpc').prop('checked', true); $('#drpc').on('change', () => { - Genshinlib.updateConfig({ - rpc: $('#drpc').prop('checked') - }); + Genshinlib.updateConfig('rpc', $('#drpc').prop('checked')); ipcRenderer.send('rpc-toggle'); }); $('#voice-list').on('change', (e) => { - let activeVP = Genshinlib.lang.voice; + let activeVP = Genshinlib.getConfig('voice'); if (activeVP != e.target.value) { - Genshinlib.updateConfig({ - lang: { - launcher: Genshinlib.lang.launcher, - voice: e.target.value - } - }); + Genshinlib.updateConfig('lang.voice', e.target.value); ipcRenderer.send('updateVP', { 'oldvp': activeVP }); @@ -65,23 +60,12 @@ $(() => { }); $('#language-list').on('change', (e) => { - let activeLang = Genshinlib.lang.launcher; + let activeLang = Genshinlib.getConfig('lang.launcher'); if (activeLang != e.target.value) { - Genshinlib.updateConfig({ - 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. - background: { - time: null, - file: Genshinlib.getConfig('background.file') - } - }); + Genshinlib.updateConfig('lang.launcher', e.target.value); + Genshinlib.updateConfig('background.time', null); LauncherUI.updateLang(e.target.value); @@ -150,13 +134,9 @@ $(() => { if (item.find('div').css('display') === 'none') { - Genshinlib.updateConfig({ - runner: { - name: runner.name, - folder: runner.folder, - executable: runner.executable - } - }); + Genshinlib.updateConfig('runner.name', runner.name); + Genshinlib.updateConfig('runner.folder', runner.folder); + Genshinlib.updateConfig('runner.executable', runner.executable); $('#runners-list > .list-item').removeClass('list-item-active'); item.addClass('list-item-active'); @@ -229,9 +209,7 @@ $(() => { }); installer.on('close', () => { - Genshinlib.updateConfig({ - dxvk: dxvk.version - }); + Genshinlib.updateConfig('dxvk', dxvk.version); $('#dxvk-list > .list-item').removeClass('list-item-active'); item.addClass('list-item-active');