From d9fec1193ef00ac7ccbc31227e6e853bfee9e257 Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Fri, 12 Jan 2024 16:10:19 +0100 Subject: [PATCH] file update check, also on launch --- main.js | 17 +++++++++++++---- preload.js | 6 ++++++ src/App.svelte | 6 ++++++ src/util/osuUtil.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- tests/osuUpdate.js | 7 +++++++ 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tests/osuUpdate.js diff --git a/main.js b/main.js index 513aa9c..d56be3a 100644 --- a/main.js +++ b/main.js @@ -144,17 +144,26 @@ function registerIPCPipes() { mainWindow.webContents.send("ezpplauncher:launchstatus", { status: "Checking osu! directory...", }); - await new Promise((res) => setTimeout(res, 1500)); + await new Promise((res) => setTimeout(res, 1000)); + const osuPath = config.get("osuPath"); + if (!(await isValidOsuFolder(osuPath))) { + mainWindow.webContents.send("ezpplauncher:launchabort"); + mainWindow.webContents.send("ezpplauncher:alert", { + type: "error", + message: "invalid osu! path!", + }); + return; + } mainWindow.webContents.send("ezpplauncher:launchstatus", { status: "Checking for osu! updates...", }); - mainWindow.webContents.send("ezpplauncher:launchprogress", { + await new Promise((res) => setTimeout(res, 1000)); + /* mainWindow.webContents.send("ezpplauncher:launchprogress", { progress: 0, }); - await new Promise((res) => setTimeout(res, 1500)); mainWindow.webContents.send("ezpplauncher:launchprogress", { progress: 100, - }); + }); */ return true; }); } diff --git a/preload.js b/preload.js index 62af955..b6e29f9 100644 --- a/preload.js +++ b/preload.js @@ -59,6 +59,12 @@ window.addEventListener("settings-set", async (e) => { await ipcRenderer.invoke("ezpplauncher:settings-set", e.detail); }); +ipcRenderer.addListener("ezpplauncher:launchabort", (e, args) => { + window.dispatchEvent( + new CustomEvent("launch-abort"), + ); +}); + ipcRenderer.addListener("ezpplauncher:alert", (e, args) => { window.dispatchEvent( new CustomEvent("alert", { detail: args }), diff --git a/src/App.svelte b/src/App.svelte index e7c4213..bc47fbd 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -52,6 +52,12 @@ launchPercentage.set(progress); }); + window.addEventListener("launchabort", () => { + launchPercentage.set(-1); + launchStatus.set(""); + launching.set(false); + }); + window.addEventListener("alert", (e) => { console.log((e as CustomEvent).detail); const toastMessage = (e as CustomEvent).detail; diff --git a/src/util/osuUtil.js b/src/util/osuUtil.js index 5b83d37..f0211bc 100644 --- a/src/util/osuUtil.js +++ b/src/util/osuUtil.js @@ -1,6 +1,8 @@ const fs = require("fs"); const path = require("path"); +const checkUpdateURL = + "https://osu.ppy.sh/web/check-updates.php?action=check&stream="; const ignoredOsuEntities = [ "osu!auth.dll", ]; @@ -37,4 +39,45 @@ async function isValidOsuFolder(path) { return (Math.round((matches / osuEntities.length) * 100) >= 60); } -module.exports = { isValidOsuFolder }; +async function getUserConfig(osuPath) { + const configFileInfo = { + name: "", + path: "", + get: async (key) => { + if (!configFileInfo.path) { + return ""; + } + const fileStream = await fs.promises.readFile( + configFileInfo.path, + "utf-8", + ); + const lines = fileStream.split(/\r?\n/); + for (const line of lines) { + if (line.includes(" = ")) { + const argsPair = line.split(" = ", 2); + const keyname = argsPair[0]; + const value = argsPair[1]; + if (keyname == key) { + return value; + } + } + } + }, + }; + const userOsuConfig = path.join( + osuPath, + `osu!.${process.env["USERNAME"]}.cfg`, + ); + if (fs.existsSync(userOsuConfig)) { + configFileInfo.name = `osu!.${process.env["USERNAME"]}.cfg`; + configFileInfo.path = userOsuConfig; + } + return configFileInfo; +} + +async function getUpdateFiles(releaseStream) { + const releaseData = await fetch(checkUpdateURL + releaseStream); + return releaseData.ok ? await releaseData.json() : undefined; +} + +module.exports = { isValidOsuFolder, getUserConfig, getUpdateFiles }; diff --git a/tests/osuUpdate.js b/tests/osuUpdate.js new file mode 100644 index 0000000..017b2a5 --- /dev/null +++ b/tests/osuUpdate.js @@ -0,0 +1,7 @@ +const { getUpdateFiles } = require("../src/util/osuUtil"); + +(async () => { + const osuPath = ""; + const latestFiles = await getUpdateFiles("stable40"); + console.log(latestFiles); +})();