Improved Genshinlib.getConfig function

now it can take argument that describes
the path to the value you want to get
for example: Genshinlib.getConfig('lang.voice')
This commit is contained in:
Observer KRypt0n_ 2021-10-24 10:22:53 +02:00
parent 36470e1403
commit 9a309d8214
No known key found for this signature in database
GPG key ID: DC5D4EC1303465DA
4 changed files with 48 additions and 40 deletions

View file

@ -35,7 +35,7 @@ $(() => {
LauncherUI.updateSocial(); LauncherUI.updateSocial();
}); });
if (Genshinlib.getConfig().rpc) if (Genshinlib.getConfig('rpc'))
DiscordRPC.init(); DiscordRPC.init();
ipcRenderer.on('rpc-toggle', () => { ipcRenderer.on('rpc-toggle', () => {
@ -102,7 +102,7 @@ $(() => {
LauncherUI.setState(Genshinlib.version === null ? 'game-installation-available' : 'game-update-available'); LauncherUI.setState(Genshinlib.version === null ? 'game-installation-available' : 'game-update-available');
// Patch version is incorrect // 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 // Patch is not available
if (Genshinlib.getPatchInfo().version !== data.game.latest.version) if (Genshinlib.getPatchInfo().version !== data.game.latest.version)
@ -130,7 +130,7 @@ $(() => {
// Current patch is in testing phase, // Current patch is in testing phase,
// but stable is available // 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'); console.log(`%c> Applying patch...`, 'font-size: 16px');
@ -185,12 +185,12 @@ $(() => {
{ {
let wineExeutable = 'wine'; let wineExeutable = 'wine';
if (Genshinlib.getConfig().runner !== null) if (Genshinlib.getConfig('runner') !== null)
{ {
wineExeutable = path.join( wineExeutable = path.join(
Genshinlib.runnersDir, Genshinlib.runnersDir,
Genshinlib.getConfig().runner?.folder, Genshinlib.getConfig('runner.folder'),
Genshinlib.getConfig().runner?.executable Genshinlib.getConfig('runner.executable')
); );
if (!fs.existsSync(wineExeutable)) if (!fs.existsSync(wineExeutable))

View file

@ -72,12 +72,12 @@ export class Genshinlib
public static get version(): Config['version'] public static get version(): Config['version']
{ {
return this.getConfig().version; return this.getConfig('version');
} }
public static get lang(): Config['lang'] public static get lang(): Config['lang']
{ {
return this.getConfig().lang; return this.getConfig('lang');
} }
public static getRunners (): Promise<[{ title: string, runners: Runner[] }]> 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'))))); // 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)) if (!fs.existsSync(this.launcherJson))
fs.writeFileSync(this.launcherJson, JSON.stringify({ fs.writeFileSync(this.launcherJson, JSON.stringify({
@ -120,7 +120,20 @@ export class Genshinlib
rpc: false rpc: false
}, null, 4)); }, 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 public static setConfig (info: Config): Genshinlib
@ -159,12 +172,12 @@ export class Genshinlib
{ {
let background = ''; 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) await fetch(this.backgroundUri + this.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: { background: {
@ -173,23 +186,23 @@ export class Genshinlib
} }
}); });
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'));
else 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 ? !prevBackground ?
console.log('No old background found') : console.log('No old background found') :
fs.unlinkSync(path.join(this.launcherDir, prevBackground)); 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; return background;
} }
@ -253,13 +266,13 @@ export class Genshinlib
let installationProgress = 0; let installationProgress = 0;
let installerProcess; let installerProcess;
if (this.getConfig().runner) if (this.getConfig('runner'))
{ {
installerProcess = spawn('winetricks', ['corefonts', 'usetakefocus=n', 'dxvk191'], { installerProcess = spawn('winetricks', ['corefonts', 'usetakefocus=n', 'dxvk191'], {
env: { env: {
...process.env, ...process.env,
WINEPREFIX: prefixpath, 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'))
} }
}); });
} }

View file

@ -33,7 +33,7 @@ $(() => {
$(`#voice-list option[value="${Genshinlib.lang.voice}"]`).prop('selected', true); $(`#voice-list option[value="${Genshinlib.lang.voice}"]`).prop('selected', true);
$(`#language-list option[value="${Genshinlib.lang.launcher}"]`).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').prop('checked', true);
$('#drpc').on('change', () => { $('#drpc').on('change', () => {
@ -74,32 +74,27 @@ $(() => {
lang: { lang: {
launcher: e.target.value, launcher: e.target.value,
voice: Genshinlib.lang.voice 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 // 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. // if the time is null or expired we set time to null here.
Genshinlib.updateConfig({
background: { background: {
time: null, time: null,
file: Genshinlib.getConfig().background.file file: Genshinlib.getConfig('background.file')
} }
}); });
// Send language updates
LauncherUI.updateLang(e.target.value); LauncherUI.updateLang(e.target.value);
ipcRenderer.send('change-lang', { 'lang': e.target.value });
/*$('*[i18id]').each((i, element) => { // Send language updates
element.innerText = i18n.translate(element.getAttribute('i18id')?.toString()!); ipcRenderer.send('change-lang', { 'lang': e.target.value });
});*/
$(`#language-list option[value="${activeLang}"]`).removeProp('selected'); $(`#language-list option[value="${activeLang}"]`).removeProp('selected');
$(`#language-list option[value="${e.target.value}"]`).prop('selected', true); $(`#language-list option[value="${e.target.value}"]`).prop('selected', true);
} }
}); });
let activeRunner = Genshinlib.getConfig().runner; let activeRunner = Genshinlib.getConfig('runner');
Genshinlib.getRunners().then(runners => { Genshinlib.getRunners().then(runners => {
runners.forEach(category => { runners.forEach(category => {
@ -173,7 +168,7 @@ $(() => {
}); });
}); });
let activeDXVK = Genshinlib.getConfig().dxvk; let activeDXVK = Genshinlib.getConfig('dxvk');
Genshinlib.getDXVKs().then(dxvks => { Genshinlib.getDXVKs().then(dxvks => {
dxvks.forEach(dxvk => { dxvks.forEach(dxvk => {