diff --git a/back/mc-connector.js b/back/mc-connector.js index 8719292..e31e1e0 100644 --- a/back/mc-connector.js +++ b/back/mc-connector.js @@ -13,6 +13,7 @@ const {getMainWindow} = require("./electron-tools"); const runningClients = onArrayChange([], () => { invoke("mc:runningClients", runningClients.length) }) +let mcInstance = null function afterLaunchCalls(){ @@ -33,13 +34,50 @@ function askLogin() { const msMc = new auth('select_account') return new Promise((resolve) => { - msMc.on('load', console.log).launch('electron').then(async e => { + msMc.launch('electron').then(async e => { const mc = await e.getMinecraft() let credentials = mc.mclc() settings.set("credentials", credentials) + settings.set("refresh-token", e.msToken) + + mcInstance = e + resolve(credentials) + }).catch(() => { + mcInstance = null + resolve(null) + }) + }) +} +function askValidate(){ + if(!mcInstance) return false + + return mcInstance.validate() +} +function refreshLogin(forceReLogin = false){ + if(!settings.get("refresh-token")) return null + + const msMc = new auth("none") + return new Promise((resolve) => { + msMc.refresh(settings.get("refresh-token")).then(async e => { + const mc = await e.getMinecraft() + let credentials = mc.mclc() + + if(credentials.access_token !== settings.get("credentials").access_token){ + settings.set("credentials", credentials) + settings.set("refresh-token", e.msToken) + } + + mcInstance = e resolve(credentials) - }).catch(() => {resolve(null)}) + }).catch(() => { + mcInstance = null + if(forceReLogin){ + askLogin().then(resolve) + }else{ + resolve(null) + } + }) }) } function logout(){ @@ -47,6 +85,14 @@ function logout(){ } function launchVanilla(version) { + invoke("mc:initGame") + + if(!askValidate()){ + refreshLogin(true).then(() => launchVanilla(version)) + invoke("mc:close", 0) + return + } + const launcher = new Client() runningClients.push(launcher) @@ -69,8 +115,6 @@ function launchVanilla(version) { } } - invoke("mc:initGame") - launcher.launch(opts).then(() => { invoke("mc:gameLaunched") afterLaunchCalls() @@ -93,6 +137,14 @@ function launchVanilla(version) { } function launchModded(manifest) { + invoke("mc:initGame") + + if(!askValidate()){ + refreshLogin(true).then(() => launchModded(manifest)) + invoke("mc:close", 0) + return + } + const rootPath = path.join(settings.get("mcPath"), "mod-packs", manifest.id) let currentManifest @@ -113,8 +165,6 @@ function launchModded(manifest) { installNeeded = true } - invoke("mc:initGame") - if(installNeeded){ let del = [ "bin", @@ -183,10 +233,15 @@ function launchModded(manifest) { } registerIpcListener("dialog:askLogin", askLogin) +registerIpcListener("dialog:askValidate", askValidate) +registerIpcListener("dialog:refreshLogin", refreshLogin) registerIpcListener("dialog:logout", logout) registerIpcListener("mc:launchVanilla", (e, v) => launchVanilla(v)) registerIpcListener("mc:launchModded", (e, v) => launchModded(v)) module.exports = { - askLogin + askLogin, + askValidate, + refreshLogin, + logout } diff --git a/back/utils.js b/back/utils.js index adf2c39..f3bb3c8 100644 --- a/back/utils.js +++ b/back/utils.js @@ -1,11 +1,12 @@ const os = require("os"); -const {registerIpcListener} = require("./ipc-handler"); +const {registerIpcListener, registerIpcListenerSync} = require("./ipc-handler"); const {platform} = require("./config"); const childProcess = require("child_process"); const fs = require("fs"); const path = require("path"); +registerIpcListenerSync("log", console.log) registerIpcListener("get-ram-amount", () => { return Math.round(os.totalmem() / 1024 / 1024); }) diff --git a/front_src/src/App.tsx b/front_src/src/App.tsx index 41c2e91..eb824b1 100644 --- a/front_src/src/App.tsx +++ b/front_src/src/App.tsx @@ -11,6 +11,7 @@ import LaunchBarPane from "./components/LaunchBarPane"; import LaunchProgressPane from "./components/LaunchProgressPane"; import UpdateInfo from "./components/UpdateInfo"; import GameInfo from "./components/GameInfo"; +import {exposedFunctions} from "./utils/constants"; function App(){ @@ -28,6 +29,9 @@ function App(){ loadModPacks().then(setModPacks) getSetting("credentials").then(setUserData) + exposedFunctions("dialog").askValidate().then((bool: boolean) => { + if(!bool) exposedFunctions("dialog").refreshLogin().then(setUserData) + }) }, []) useEffect(() => { diff --git a/preload.js b/preload.js index 8985278..ecfb17d 100644 --- a/preload.js +++ b/preload.js @@ -2,6 +2,7 @@ const { contextBridge, ipcRenderer } = require("electron") contextBridge.exposeInMainWorld("utils", { + log: (message) => ipcRenderer.send("log", message), hashing: { sha512: (string) => crypto.createHash("sha512").update(string).digest("hex") }, @@ -42,6 +43,15 @@ contextBridge.exposeInMainWorld("dialog", { }, askLogin: () => { return ipcRenderer.invoke("dialog:askLogin") + }, + askValidate: () => { + return ipcRenderer.invoke("dialog:askValidate") + }, + refreshLogin: () => { + return ipcRenderer.invoke("dialog:refreshLogin") + }, + logout: () => { + return ipcRenderer.invoke("dialog:logout") } }) contextBridge.exposeInMainWorld("mc", {