From c3ed135a63edbda49226f8c3f5b1e53191ee8472 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Mon, 1 Nov 2021 12:46:10 +0200 Subject: [PATCH] Constant values moved to separate class --- src/ts/index.ts | 51 ++++++++++++++++++------------------- src/ts/lib/Genshinlib.ts | 54 ++++++++++++++++------------------------ src/ts/lib/constants.ts | 27 ++++++++++++++++++++ src/ts/settings.ts | 28 +++++++++++---------- 4 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 src/ts/lib/constants.ts diff --git a/src/ts/index.ts b/src/ts/index.ts index 3b99011..c97f1a5 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -7,6 +7,7 @@ const semver = require('semver'); import $ from 'cash-dom'; +import { constants } from './lib/constants'; import { Genshinlib } from './lib/Genshinlib'; import { LauncherUI } from './lib/LauncherUI'; import { Tools } from './lib/Tools'; @@ -14,18 +15,18 @@ import { DiscordRPC } from './lib/DiscordRPC'; const launcher_version = require('../../package.json').version; -if (!fs.existsSync(Genshinlib.prefixDir)) - fs.mkdirSync(Genshinlib.prefixDir, { recursive: true }); +if (!fs.existsSync(constants.prefixDir)) + fs.mkdirSync(constants.prefixDir, { recursive: true }); -if (!fs.existsSync(Genshinlib.runnersDir)) - fs.mkdirSync(Genshinlib.runnersDir, { recursive: true }); +if (!fs.existsSync(constants.runnersDir)) + fs.mkdirSync(constants.runnersDir, { recursive: true }); -if (!fs.existsSync(Genshinlib.dxvksDir)) - fs.mkdirSync(Genshinlib.dxvksDir, { recursive: true }); +if (!fs.existsSync(constants.dxvksDir)) + fs.mkdirSync(constants.dxvksDir, { recursive: true }); $(() => { if (Genshinlib.version !== null) - document.title = 'Genshin Impact Linux Launcher - ' + Genshinlib.version; + document.title = `${constants.gamePlaceholder.full} Linux Launcher - ${Genshinlib.version}`; // On Start configuration of LauncherUI LauncherUI.updateLang(Genshinlib.getConfig('lang.launcher') ?? 'en-us'); @@ -164,14 +165,14 @@ $(() => { $('#launch').on('click', async () => { // Creating wine prefix - if (!Genshinlib.isPrefixInstalled(Genshinlib.prefixDir)) + if (!Genshinlib.isPrefixInstalled(constants.prefixDir)) { console.log(`%c> Creating wineprefix...`, 'font-size: 16px'); $('#launch').css('display', 'none'); $('#downloader-panel').css('display', 'block'); - await Genshinlib.installPrefix(Genshinlib.prefixDir, (output: string, current: number, total: number) => { + await Genshinlib.installPrefix(constants.prefixDir, (output: string, current: number, total: number) => { output = output.trim(); console.log(output); @@ -208,7 +209,7 @@ $(() => { if (Genshinlib.getConfig('runner') !== null) { wineExeutable = path.join( - Genshinlib.runnersDir, + constants.runnersDir, Genshinlib.getConfig('runner.folder'), Genshinlib.getConfig('runner.executable') ); @@ -234,10 +235,10 @@ $(() => { } exec(`${wineExeutable} launcher.bat`, { - cwd: Genshinlib.gameDir, + cwd: constants.gameDir, env: { ...process.env, - WINEPREFIX: Genshinlib.prefixDir, + WINEPREFIX: constants.prefixDir, ...Genshinlib.getConfig('env') } }, (err: any, stdout: any, stderr: any) => { @@ -294,8 +295,8 @@ $(() => { break; } - if (fs.existsSync(path.join(Genshinlib.gameDir, diff.name))) - fs.unlinkSync(path.join(Genshinlib.gameDir, diff.name)); + if (fs.existsSync(path.join(constants.gameDir, diff.name))) + fs.unlinkSync(path.join(constants.gameDir, diff.name)); /** * Downloading game @@ -303,7 +304,7 @@ $(() => { LauncherUI.initProgressBar(); - Tools.downloadFile(diff.path, path.join(Genshinlib.launcherDir, diff.name), (current: number, total: number, difference: number) => { + Tools.downloadFile(diff.path, path.join(constants.launcherDir, diff.name), (current: number, total: number, difference: number) => { LauncherUI.updateProgressBar(LauncherUI.i18n.translate('Downloading'), current, total, difference); }).then(() => { /** @@ -312,12 +313,12 @@ $(() => { console.log(`%c> Unpacking game data...`, 'font-size: 16px'); - if (!fs.existsSync(Genshinlib.gameDir)) - fs.mkdirSync(Genshinlib.gameDir, { recursive: true }); + if (!fs.existsSync(constants.gameDir)) + fs.mkdirSync(constants.gameDir, { recursive: true }); LauncherUI.initProgressBar(); - Tools.unzip(path.join(Genshinlib.launcherDir, diff.name), Genshinlib.gameDir, (current: number, total: number, difference: number) => { + Tools.unzip(path.join(constants.launcherDir, diff.name), constants.gameDir, (current: number, total: number, difference: number) => { LauncherUI.updateProgressBar(LauncherUI.i18n.translate('Unpack'), current, total, difference); }).then(() => { /** @@ -326,7 +327,7 @@ $(() => { console.log(`%c> Downloading voice data...`, 'font-size: 16px'); - fs.unlinkSync(path.join(Genshinlib.launcherDir, diff.name)); + fs.unlinkSync(path.join(constants.launcherDir, diff.name)); let voicePack = diff.voice_packs[1]; // en-us @@ -340,7 +341,7 @@ $(() => { LauncherUI.initProgressBar(); - Tools.downloadFile(voicePack.path, path.join(Genshinlib.launcherDir, voicePack.name), (current: number, total: number, difference: number) => { + Tools.downloadFile(voicePack.path, path.join(constants.launcherDir, voicePack.name), (current: number, total: number, difference: number) => { LauncherUI.updateProgressBar(LauncherUI.i18n.translate('Downloading'), current, total, difference); }).then(() => { /** @@ -351,18 +352,18 @@ $(() => { LauncherUI.initProgressBar(); - Tools.unzip(path.join(Genshinlib.launcherDir, voicePack.name), Genshinlib.gameDir, (current: number, total: number, difference: number) => { + Tools.unzip(path.join(constants.launcherDir, voicePack.name), constants.gameDir, (current: number, total: number, difference: number) => { LauncherUI.updateProgressBar(LauncherUI.i18n.translate('Unpack'), current, total, difference); }).then(() => { - fs.unlinkSync(path.join(Genshinlib.launcherDir, voicePack.name)); + fs.unlinkSync(path.join(constants.launcherDir, voicePack.name)); // If this update has excess files we should delete them - if (fs.existsSync(path.join(Genshinlib.gameDir, 'deletefiles.txt'))) + if (fs.existsSync(path.join(constants.gameDir, 'deletefiles.txt'))) { - let deleteFiles = fs.readFileSync(path.join(Genshinlib.gameDir, 'deletefiles.txt')); + let deleteFiles = fs.readFileSync(path.join(constants.gameDir, 'deletefiles.txt')); deleteFiles.split(/\r\n|\r|\n/).forEach((file: string) => { - fs.unlinkSync(path.join(Genshinlib.gameDir, file.trim())); + fs.unlinkSync(path.join(constants.gameDir, file.trim())); }); } diff --git a/src/ts/lib/Genshinlib.ts b/src/ts/lib/Genshinlib.ts index 5e47e10..1d516f6 100644 --- a/src/ts/lib/Genshinlib.ts +++ b/src/ts/lib/Genshinlib.ts @@ -1,4 +1,5 @@ import GIJSON from '../types/GIJSON'; +import { constants } from './constants'; import { Tools } from './Tools'; const store = require('electron-store'); @@ -57,19 +58,6 @@ type DXVK = { export class Genshinlib { - public static readonly launcherDir: string = path.join(os.homedir(), '.local', 'share', 'anime-game-launcher'); - - public static readonly prefixDir: string = path.join(this.launcherDir, 'game'); // TODO: rename every game's name entry to something like below - public static readonly gameDir: string = path.join(this.prefixDir, 'drive_c', 'Program Files', Buffer.from('R2Vuc2hpbiBJbXBhY3Q=', 'base64').toString('utf-8')); - public static readonly runnersDir: string = path.join(this.launcherDir, 'runners'); - public static readonly dxvksDir: string = path.join(this.launcherDir, 'dxvks'); - - protected static readonly versionsUri: string = 'https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?key=gcStgarh&launcher_id=10'; - protected static readonly backgroundUri: string = 'https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/content?filter_adv=true&launcher_id=10&language='; - protected static readonly patchUri: string = 'https://notabug.org/Krock/GI-on-Linux/archive/master.zip'; - protected static readonly runnersUri: string = 'https://notabug.org/nobody/an-anime-game-launcher/raw/main/runners.json'; - protected static readonly dxvksUri: string = 'https://notabug.org/nobody/an-anime-game-launcher/raw/main/dxvks.json'; - public static get version(): string|null { return this.getConfig('version'); @@ -118,7 +106,7 @@ export class Genshinlib public static async getData (): Promise { return new Promise((resolve, reject) => { - https.get(this.versionsUri, (response: any) => { + https.get(constants.versionsUri, (response: any) => { let data = ''; response.on('data', (chunk: any) => data += chunk); @@ -138,7 +126,7 @@ 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.getConfig('lang.launcher')) + await fetch(constants.backgroundUri + this.getConfig('lang.launcher')) .then(res => res.json()) .then(async resdone => { let prevBackground = this.getConfig('background.file'); @@ -146,23 +134,23 @@ export class Genshinlib 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')); + if (fs.existsSync(path.join(constants.launcherDir, this.getConfig('background.file')))) + background = path.join(constants.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(constants.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)); + fs.unlinkSync(path.join(constants.launcherDir, prevBackground)); - background = path.join(this.launcherDir, this.getConfig('background.file')); + background = path.join(constants.launcherDir, this.getConfig('background.file')); }); }; }); } - else background = path.join(this.launcherDir, this.getConfig('background.file')); + else background = path.join(constants.launcherDir, this.getConfig('background.file')); return background; } @@ -295,12 +283,12 @@ export class Genshinlib public static patchGame (onFinish: () => void, onData: (data: string) => void) { Genshinlib.getPatchInfo().then(pathInfo => { - Tools.downloadFile(this.patchUri, path.join(this.launcherDir, 'patch.zip'), (current: number, total: number, difference: number) => null).then(() => { - Tools.unzip(path.join(this.launcherDir, 'patch.zip'), this.launcherDir, (current: number, total: number, difference: number) => null).then(() => { + Tools.downloadFile(constants.patchUri, path.join(constants.launcherDir, 'patch.zip'), (current: number, total: number, difference: number) => null).then(() => { + Tools.unzip(path.join(constants.launcherDir, 'patch.zip'), constants.launcherDir, (current: number, total: number, difference: number) => null).then(() => { // Delete zip file and assign patch directory. - fs.unlinkSync(path.join(this.launcherDir, 'patch.zip')); + fs.unlinkSync(path.join(constants.launcherDir, 'patch.zip')); - let patchDir = path.join(this.launcherDir, 'gi-on-linux', pathInfo.version.replaceAll('.', '')); + let patchDir = path.join(constants.launcherDir, 'gi-on-linux', pathInfo.version.replaceAll('.', '')); // Patch out the testing phase content from the shell files if active and make sure the shell files are executable. exec(`cd ${patchDir} && sed -i '/^echo "If you would like to test this patch, modify this script and remove the line below this one."/,+5d' patch.sh`); @@ -310,10 +298,10 @@ export class Genshinlib // Execute the patch file with "yes yes" in the beginning to agree to the choices. let patcherProcess = exec(`yes yes | ${path.join(patchDir, 'patch.sh')}`, { - cwd: this.gameDir, + cwd: constants.gameDir, env: { ...process.env, - WINEPREFIX: this.prefixDir + WINEPREFIX: constants.prefixDir } }); @@ -321,21 +309,21 @@ export class Genshinlib patcherProcess.on('close', () => { // Make sure that launcher.bat exists if not run patch.sh again. - if (!path.join(this.gameDir, 'launcher.bat')) + if (!path.join(constants.gameDir, 'launcher.bat')) exec(`yes yes | ${path.join(patchDir, 'patch.sh')}`, { - cwd: this.gameDir, + cwd: constants.gameDir, env: { ...process.env, - WINEPREFIX: this.prefixDir + WINEPREFIX: constants.prefixDir } }); // Execute the patch file with "yes" in the beginning to agree to the choice. let patcherAntiCrashProcess = exec(`yes | ${path.join(patchDir, 'patch_anti_logincrash.sh')}`, { - cwd: this.gameDir, + cwd: constants.gameDir, env: { ...process.env, - WINEPREFIX: this.prefixDir + WINEPREFIX: constants.prefixDir } }); @@ -345,7 +333,7 @@ export class Genshinlib Genshinlib.updateConfig('patch.version', pathInfo.version); Genshinlib.updateConfig('patch.state', pathInfo.state); - fs.rmSync(path.join(this.launcherDir, 'gi-on-linux'), { recursive: true }); + fs.rmSync(path.join(constants.launcherDir, 'gi-on-linux'), { recursive: true }); onFinish(); }); diff --git a/src/ts/lib/constants.ts b/src/ts/lib/constants.ts new file mode 100644 index 0000000..70056cc --- /dev/null +++ b/src/ts/lib/constants.ts @@ -0,0 +1,27 @@ +const path = require('path'); +const os = require('os'); + +export class constants +{ + public static readonly gamePlaceholder = { + first: Buffer.from('R2Vuc2hpbg==', 'base64').toString(), + second: Buffer.from('SW1wYWN0', 'base64').toString(), + full: Buffer.from('R2Vuc2hpbiBJbXBhY3Q=', 'base64').toString(), + company: Buffer.from('bWlob3lv', 'base64').toString() + }; + + public static readonly launcherDir: string = path.join(os.homedir(), '.local', 'share', 'anime-game-launcher'); + + public static readonly prefixDir: string = path.join(this.launcherDir, 'game'); + public static readonly gameDir: string = path.join(this.prefixDir, 'drive_c', 'Program Files', this.gamePlaceholder.full); + public static readonly runnersDir: string = path.join(this.launcherDir, 'runners'); + public static readonly dxvksDir: string = path.join(this.launcherDir, 'dxvks'); + + public static readonly versionsUri: string = `https://sdk-os-static.${this.gamePlaceholder.company}.com/hk4e_global/mdk/launcher/api/resource?key=gcStgarh&launcher_id=10`; + public static readonly backgroundUri: string = `https://sdk-os-static.${this.gamePlaceholder.company}.com/hk4e_global/mdk/launcher/api/content?filter_adv=true&launcher_id=10&language=`; + + public static readonly patchUri: string = 'https://notabug.org/Krock/GI-on-Linux/archive/master.zip'; + + public static readonly runnersUri: string = 'https://notabug.org/nobody/an-anime-game-launcher/raw/main/runners.json'; + public static readonly dxvksUri: string = 'https://notabug.org/nobody/an-anime-game-launcher/raw/main/dxvks.json'; +} diff --git a/src/ts/settings.ts b/src/ts/settings.ts index 1901568..8921681 100644 --- a/src/ts/settings.ts +++ b/src/ts/settings.ts @@ -4,6 +4,8 @@ const { ipcRenderer } = require('electron'); const { exec } = require('child_process'); import $ from 'cash-dom'; + +import { constants } from './lib/constants'; import { Genshinlib } from './lib/Genshinlib'; import { LauncherUI } from './lib/LauncherUI'; import { Tools } from './lib/Tools'; @@ -114,7 +116,7 @@ $(() => { category.runners.forEach(runner => { let item = $(`
${runner.name}
`).appendTo('#runners-list'); - if (fs.existsSync(path.join(Genshinlib.runnersDir, runner.folder))) + if (fs.existsSync(path.join(constants.runnersDir, runner.folder))) { item.find('div').css('display', 'none'); @@ -130,22 +132,22 @@ $(() => { let div = item.find('div'); - Tools.downloadFile(runner.uri, path.join(Genshinlib.launcherDir, runner.name), (current: number, total: number, difference: number) => { + Tools.downloadFile(runner.uri, path.join(constants.launcherDir, runner.name), (current: number, total: number, difference: number) => { div.text(`${ Math.round(current / total * 100) }%`); }).then(() => { let unpacker = runner.archive === 'tar' ? Tools.untar : Tools.unzip; unpacker( - path.join(Genshinlib.launcherDir, runner.name), + path.join(constants.launcherDir, runner.name), runner.makeFolder ? - path.join(Genshinlib.runnersDir, runner.folder) : - Genshinlib.runnersDir, + path.join(constants.runnersDir, runner.folder) : + constants.runnersDir, (current: number, total: number, difference: number) => { div.text(`${ Math.round(current / total * 100) }%`); } ).then(() => { - fs.unlinkSync(path.join(Genshinlib.launcherDir, runner.name)); + fs.unlinkSync(path.join(constants.launcherDir, runner.name)); item.removeClass('list-item-disabled'); div.css('display', 'none'); @@ -181,7 +183,7 @@ $(() => { dxvks.forEach(dxvk => { let item = $(`
${dxvk.version}
`).appendTo('#dxvk-list'); - if (fs.existsSync(path.join(Genshinlib.dxvksDir, 'dxvk-' + dxvk.version))) + if (fs.existsSync(path.join(constants.dxvksDir, 'dxvk-' + dxvk.version))) { item.find('div').css('display', 'none'); @@ -197,17 +199,17 @@ $(() => { let div = item.find('div'); - Tools.downloadFile(dxvk.uri, path.join(Genshinlib.launcherDir, 'dxvk-' + dxvk.version), (current: number, total: number, difference: number) => { + Tools.downloadFile(dxvk.uri, path.join(constants.launcherDir, 'dxvk-' + dxvk.version), (current: number, total: number, difference: number) => { div.text(`${ Math.round(current / total * 100) }%`); }).then(() => { Tools.untar( - path.join(Genshinlib.launcherDir, 'dxvk-' + dxvk.version), - Genshinlib.dxvksDir, + path.join(constants.launcherDir, 'dxvk-' + dxvk.version), + constants.dxvksDir, (current: number, total: number, difference: number) => { div.text(`${ Math.round(current / total * 100) }%`); } ).then(() => { - fs.unlinkSync(path.join(Genshinlib.launcherDir, 'dxvk-' + dxvk.version)); + fs.unlinkSync(path.join(constants.launcherDir, 'dxvk-' + dxvk.version)); item.removeClass('list-item-disabled'); div.css('display', 'none'); @@ -229,10 +231,10 @@ $(() => { .text('Applying...'); let installer = exec('./setup_dxvk.sh install', { - cwd: path.join(Genshinlib.dxvksDir, 'dxvk-' + dxvk.version), + cwd: path.join(constants.dxvksDir, 'dxvk-' + dxvk.version), env: { ...process.env, - WINEPREFIX: Genshinlib.prefixDir + WINEPREFIX: constants.prefixDir } });