settings now working

This commit is contained in:
HorizonCode 2024-01-12 14:19:00 +01:00
parent 756ae1be58
commit 2c6b51cbb2
8 changed files with 159 additions and 41 deletions

30
main.js
View File

@ -1,5 +1,5 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow, Menu, ipcMain } = require("electron");
const { app, BrowserWindow, Menu, ipcMain, dialog } = require("electron");
const path = require("path");
const serve = require("electron-serve");
const loadURL = serve({ directory: "public" });
@ -7,6 +7,7 @@ const config = require("./src/config/config");
const { setupTitlebar, attachTitlebarToWindow } = require(
"custom-electron-titlebar/main",
);
const { isValidOsuFolder } = require("./src/util/osuUtil");
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
@ -112,6 +113,33 @@ function registerIPCPipes() {
return true;
});
ipcMain.handle("ezpplauncher:settings", async (e) => {
return config.all();
});
ipcMain.handle("ezpplauncher:set-folder", async (e) => {
const folderResult = await dialog.showOpenDialog({
title: "Select osu! installation directory",
properties: ["openDirectory"],
});
if (!folderResult.canceled) {
const folder = folderResult.filePaths[0];
if (await isValidOsuFolder(folder)) {
config.set("osuPath", folder);
mainWindow.webContents.send("ezpplauncher:alert", {
type: "success",
message: "osu! path successfully saved!",
});
} else {
mainWindow.webContents.send("ezpplauncher:alert", {
type: "error",
message: "invalid osu! path!",
});
}
}
return config.all();
});
ipcMain.handle("ezpplauncher:launch", async (e) => {
mainWindow.webContents.send("ezpplauncher:launchstatus", {
status: "Checking osu! directory...",

View File

@ -22,25 +22,49 @@ window.addEventListener("login-attempt", async (e) => {
);
});
window.addEventListener("autologin-attempt", async (e) => {
window.addEventListener("autologin-attempt", async () => {
const loginResult = await ipcRenderer.invoke("ezpplauncher:autologin");
window.dispatchEvent(
new CustomEvent("login-result", { detail: loginResult }),
);
});
window.addEventListener("logout", async (e) => {
window.addEventListener("logout", async () => {
await ipcRenderer.invoke("ezpplauncher:logout");
});
window.addEventListener("guest-login", async (e) => {
window.addEventListener("guest-login", async () => {
await ipcRenderer.invoke("ezpplauncher:guestlogin");
});
window.addEventListener("launch", async (e) => {
window.addEventListener("launch", async () => {
await ipcRenderer.invoke("ezpplauncher:launch");
});
window.addEventListener("settings-get", async () => {
const settings = await ipcRenderer.invoke("ezpplauncher:settings");
window.dispatchEvent(
new CustomEvent("settings-result", { detail: settings }),
);
});
window.addEventListener("folder-set", async (e) => {
const result = await ipcRenderer.invoke("ezpplauncher:set-folder");
window.dispatchEvent(
new CustomEvent("settings-result", { detail: result }),
);
});
window.addEventListener("settings-set", async (e) => {
await ipcRenderer.invoke("ezpplauncher:settings-set", e.detail);
});
ipcRenderer.addListener("ezpplauncher:alert", (e, args) => {
window.dispatchEvent(
new CustomEvent("alert", { detail: args }),
);
});
ipcRenderer.addListener("ezpplauncher:launchstatus", (e, args) => {
window.dispatchEvent(
new CustomEvent("launchStatusUpdate", { detail: args }),

View File

@ -22,7 +22,7 @@
import { Page } from "./consts/pages";
import Login from "./pages/Login.svelte";
import Launch from "./pages/Launch.svelte";
import { Toaster } from "svelte-french-toast";
import toast, { Toaster } from "svelte-french-toast";
import type { User } from "./types/user";
import Settings from "./pages/Settings.svelte";
@ -51,6 +51,40 @@
const progress = (e as CustomEvent).detail.progress;
launchPercentage.set(progress);
});
window.addEventListener("alert", (e) => {
console.log((e as CustomEvent).detail);
const toastMessage = (e as CustomEvent).detail;
switch (toastMessage.type) {
case "success": {
toast.success(toastMessage.message, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 1500,
});
break;
}
case "error": {
toast.error(toastMessage.message, {
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 1500,
});
break;
}
default: {
toast(toastMessage.message, {
icon: "",
position: "bottom-center",
className:
"dark:!bg-gray-800 border-1 dark:!border-gray-700 dark:!text-gray-100",
duration: 1500,
});
}
}
});
</script>
<Toaster></Toaster>

View File

@ -38,7 +38,15 @@ const get = (
return result ? result.val ?? undefined : undefined;
};
const all = () => {
const result = db.prepare(
`SELECT configKey key, configValue val FROM config WHERE 1`,
).all();
return result ?? undefined;
};
module.exports = {
all,
get,
set,
remove,

View File

@ -3,6 +3,19 @@
import { FolderSolid } from "flowbite-svelte-icons";
import { currentPage } from "../storage/localStore";
import { Page } from "../consts/pages";
let folderPath: string = "";
window.addEventListener("settings-result", (e) => {
const settings: Record<string, string>[] = (e as CustomEvent).detail;
const osuPath = settings.find((setting) => setting.key == "osuPath");
folderPath = osuPath ? osuPath.val : "";
});
window.dispatchEvent(new CustomEvent("settings-get"));
const setFolderPath = () => {
window.dispatchEvent(new CustomEvent("folder-set"));
};
</script>
<main
@ -15,9 +28,13 @@
<Input
type="text"
placeholder="Path to your osu! installation"
value={folderPath}
readonly
/>
<Button color="light" class="dark:active:!bg-gray-900"
<Button
color="light"
class="dark:active:!bg-gray-900"
on:click={setFolderPath}
><FolderSolid
size="sm"
class="dark:text-gray-300 text-gray-500 outline-none border-none select-none pointer-events-none"

View File

@ -1,33 +0,0 @@
import axios from "axios";
import type { Error } from "../types/error";
import type { User } from "../types/user";
const loginCheckEndpoint = "https://ez-pp.farm/login/check";
let retries = 0;
export const performLogin = async (
username: string,
password: string,
): Promise<Error | User> => {
const fetchResult = await fetch("https://ez-pp.farm/login/check", {
method: "POST",
mode: "cors",
body: JSON.stringify({ username, password }),
headers: {
"Content-Type": "application/json",
},
});
if (fetchResult.ok) {
const result = await fetchResult.json();
retries = 0;
return result.user;
} else {
if (retries++ >= 5) {
console.log("Login failed after 5 retries.");
retries = 0;
return { code: 403, message: "Login failed." } as Error;
}
return await performLogin(username, password);
}
};

View File

@ -1,3 +1,3 @@
export const clamp = (val: number, min: number, max: number) => {
return val <= min ? min : val >= max ? max : val;
return Math.max(min, Math.min(val, max));
};

40
src/util/osuUtil.js Normal file
View File

@ -0,0 +1,40 @@
const fs = require("fs");
const path = require("path");
const ignoredOsuEntities = [
"osu!auth.dll",
];
const osuEntities = [
"avcodec-51.dll",
"avformat-52.dll",
"avutil-49.dll",
"bass.dll",
"bass_fx.dll",
"collection.db",
"d3dcompiler_47.dll",
"libEGL.dll",
"libGLESv2.dll",
"Microsoft.Ink.dll",
"OpenTK.dll",
"osu!.cfg",
"osu!.db",
"osu!.exe",
"osu!auth.dll",
"osu!gameplay.dll",
"osu!seasonal.dll",
"osu!ui.dll",
"presence.db",
"pthreadGC2.dll",
"scores.db",
];
async function isValidOsuFolder(path) {
const allFiles = await fs.promises.readdir(path);
let matches = 0;
for (const file of allFiles) {
if (osuEntities.includes(file)) matches = matches + 1;
}
return (Math.round((matches / osuEntities.length) * 100) >= 60);
}
module.exports = { isValidOsuFolder };