Change to electron-store

This commit is contained in:
Maroxy 2021-10-24 11:57:10 +02:00
parent a758d82da0
commit 394525b8ca
5 changed files with 57 additions and 68 deletions

View file

@ -9,6 +9,9 @@ const {
const path = require('path'); const path = require('path');
const Store = require('electron-store');
Store.initRenderer();
const { Genshinlib } = require('./public/js/Genshinlib'); const { Genshinlib } = require('./public/js/Genshinlib');
let mainWindow; let mainWindow;
@ -70,8 +73,6 @@ function createWindow ()
// mainWindow.webContents.openDevTools(); // 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 // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
@ -89,8 +90,6 @@ app.whenReady().then(() => {
// This has to be here otherwise webContents is invalid // This has to be here otherwise webContents is invalid
ipcMain.on('change-lang', (event, args) => { ipcMain.on('change-lang', (event, args) => {
app.commandLine.appendSwitch('lang', Genshinlib.lang.launcher);
mainWindow.webContents.send('change-lang', { 'lang': args.lang }); mainWindow.webContents.send('change-lang', { 'lang': args.lang });
}); });

View file

@ -22,8 +22,8 @@ $(() => {
if (Genshinlib.version !== null) if (Genshinlib.version !== null)
document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version; document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version;
LauncherUI.updateLang(Genshinlib.getConfig('lang.launcher') ?? 'en-us');
LauncherUI.setState('game-launch-available'); LauncherUI.setState('game-launch-available');
LauncherUI.updateBackground(); LauncherUI.updateBackground();
LauncherUI.updateSocial(); LauncherUI.updateSocial();
@ -51,7 +51,7 @@ $(() => {
let old; let old;
for (let i = 0; i < data.game.latest.voice_packs.length; ++i) 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]; voicePack = data.game.latest.voice_packs[i];
@ -195,9 +195,7 @@ $(() => {
{ {
wineExeutable = 'wine'; wineExeutable = 'wine';
Genshinlib.updateConfig({ Genshinlib.updateConfig('runner', null);
runner: null
});
} }
} }
@ -309,7 +307,7 @@ $(() => {
let voicePack = diff.voice_packs[1]; // en-us let voicePack = diff.voice_packs[1]; // en-us
for (let i = 0; i < diff.voice_packs.length; ++i) 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]; voicePack = diff.voice_packs[i];
@ -334,9 +332,7 @@ $(() => {
}).then(() => { }).then(() => {
fs.unlinkSync(path.join(Genshinlib.launcherDir, voicePack.name)); fs.unlinkSync(path.join(Genshinlib.launcherDir, voicePack.name));
Genshinlib.updateConfig({ Genshinlib.updateConfig('version', data.game.latest.version);
version: data.game.latest.version
});
// Patch available // Patch available
if (Genshinlib.getPatchInfo().version === data.game.latest.version) if (Genshinlib.getPatchInfo().version === data.game.latest.version)

View file

@ -1,5 +1,6 @@
import GIJSON from '../types/GIJSON'; import GIJSON from '../types/GIJSON';
import { Tools } from './Tools'; import { Tools } from './Tools';
const Store = require('electron-store');
const https = require('follow-redirects').https; const https = require('follow-redirects').https;
@ -9,6 +10,23 @@ const os = require('os');
const { spawn, exec } = require('child_process'); const { spawn, exec } = require('child_process');
const dns = require('dns'); 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 = { type Runner = {
name: string, // Runner title which will be showed in the list name: string, // Runner title which will be showed in the list
version: string, // Runner version version: string, // Runner version
@ -75,11 +93,6 @@ export class Genshinlib
return this.getConfig('version'); return this.getConfig('version');
} }
public static get lang(): Config['lang']
{
return this.getConfig('lang');
}
public static getRunners (): Promise<[{ title: string, runners: Runner[] }]> public static getRunners (): Promise<[{ title: string, runners: Runner[] }]>
{ {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -104,6 +117,12 @@ export class Genshinlib
public static getConfig (property: string|null = null, splitProperty: boolean = true): any 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)) if (!fs.existsSync(this.launcherJson))
fs.writeFileSync(this.launcherJson, JSON.stringify({ fs.writeFileSync(this.launcherJson, JSON.stringify({
lang: { lang: {
@ -133,7 +152,7 @@ export class Genshinlib
property.split('.').forEach(prop => config = config[prop]); property.split('.').forEach(prop => config = config[prop]);
return config; return config;
} }*/
} }
public static setConfig (info: Config): Genshinlib public static setConfig (info: Config): Genshinlib
@ -143,12 +162,13 @@ export class Genshinlib
return this; return this;
} }
public static updateConfig (config: any): Genshinlib public static updateConfig (cname: string, value: string|boolean|null|number): Genshinlib
{ {
return this.setConfig({ return config.set(cname, value);
...this.getConfig(), //return this.setConfig({
...config // ...this.getConfig(),
}); // ...config
//});
} }
public static async getData (): Promise<any> public static async getData (): Promise<any>
@ -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')!) 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(res => res.json())
.then(async resdone => { .then(async resdone => {
let prevBackground = this.getConfig('background.file'); let prevBackground = this.getConfig('background.file');
this.updateConfig({ 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());
background: { this.updateConfig('background.file', resdone.data.adv.background.replace(/.*\//, ''));
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(/.*\//, '')
}
});
if (fs.existsSync(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')); background = path.join(this.launcherDir, this.getConfig('background.file'));

View file

@ -166,7 +166,7 @@ export class LauncherUI
public static updateSocial (): void 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(res => res.text())
.then(body => { .then(body => {
$('#__layout').remove(); $('#__layout').remove();

View file

@ -10,6 +10,8 @@ import { Tools } from './lib/Tools';
$(() => { $(() => {
LauncherUI.updateLang(Genshinlib.getConfig('lang.launcher') ?? 'en-us');
$('*[i18id]').each((i, element) => { $('*[i18id]').each((i, element) => {
element.innerText = LauncherUI.i18n.translate(element.getAttribute('i18id')?.toString()!); element.innerText = LauncherUI.i18n.translate(element.getAttribute('i18id')?.toString()!);
}); });
@ -29,31 +31,24 @@ $(() => {
}); });
// Select the saved options in launcher.json on load // Select the saved options in launcher.json on load
$(`#voice-list option[value="${Genshinlib.lang.voice}"]`).prop('selected', true); $(`#voice-list option[value="${Genshinlib.getConfig('lang.voice')}"]`).prop('selected', true);
$(`#language-list option[value="${Genshinlib.lang.launcher}"]`).prop('selected', true); $(`#language-list option[value="${Genshinlib.getConfig('lang.launcher')}"]`).prop('selected', true);
if (Genshinlib.getConfig('rpc')) if (Genshinlib.getConfig('rpc'))
$('#drpc').prop('checked', true); $('#drpc').prop('checked', true);
$('#drpc').on('change', () => { $('#drpc').on('change', () => {
Genshinlib.updateConfig({ Genshinlib.updateConfig('rpc', $('#drpc').prop('checked'));
rpc: $('#drpc').prop('checked')
});
ipcRenderer.send('rpc-toggle'); ipcRenderer.send('rpc-toggle');
}); });
$('#voice-list').on('change', (e) => { $('#voice-list').on('change', (e) => {
let activeVP = Genshinlib.lang.voice; let activeVP = Genshinlib.getConfig('voice');
if (activeVP != e.target.value) if (activeVP != e.target.value)
{ {
Genshinlib.updateConfig({ Genshinlib.updateConfig('lang.voice', e.target.value);
lang: {
launcher: Genshinlib.lang.launcher,
voice: e.target.value
}
});
ipcRenderer.send('updateVP', { 'oldvp': activeVP }); ipcRenderer.send('updateVP', { 'oldvp': activeVP });
@ -65,23 +60,12 @@ $(() => {
}); });
$('#language-list').on('change', (e) => { $('#language-list').on('change', (e) => {
let activeLang = Genshinlib.lang.launcher; let activeLang = Genshinlib.getConfig('lang.launcher');
if (activeLang != e.target.value) if (activeLang != e.target.value)
{ {
Genshinlib.updateConfig({ Genshinlib.updateConfig('lang.launcher', e.target.value);
lang: { Genshinlib.updateConfig('background.time', null);
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')
}
});
LauncherUI.updateLang(e.target.value); LauncherUI.updateLang(e.target.value);
@ -150,13 +134,9 @@ $(() => {
if (item.find('div').css('display') === 'none') if (item.find('div').css('display') === 'none')
{ {
Genshinlib.updateConfig({ Genshinlib.updateConfig('runner.name', runner.name);
runner: { Genshinlib.updateConfig('runner.folder', runner.folder);
name: runner.name, Genshinlib.updateConfig('runner.executable', runner.executable);
folder: runner.folder,
executable: runner.executable
}
});
$('#runners-list > .list-item').removeClass('list-item-active'); $('#runners-list > .list-item').removeClass('list-item-active');
item.addClass('list-item-active'); item.addClass('list-item-active');
@ -229,9 +209,7 @@ $(() => {
}); });
installer.on('close', () => { installer.on('close', () => {
Genshinlib.updateConfig({ Genshinlib.updateConfig('dxvk', dxvk.version);
dxvk: dxvk.version
});
$('#dxvk-list > .list-item').removeClass('list-item-active'); $('#dxvk-list > .list-item').removeClass('list-item-active');
item.addClass('list-item-active'); item.addClass('list-item-active');