Skip to content

Commit 2008e43

Browse files
committed
Auto Updater + Multithreading
1 parent 9ccf800 commit 2008e43

File tree

7 files changed

+51
-37
lines changed

7 files changed

+51
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simplelauncher",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "Launcher for SimpleClient",
55
"main": "src/main.js",
66
"scripts": {

src/download/fabricdownloader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const downloadFabric = async (version, meta) => {
1818
if (!downloadFiles([[fabricInstallerJson[0].url, fabricInstallerFile]], undefined, log)) return false
1919
log('Executing Fabric installer...')
2020
const args = ['-jar', fabricInstallerFile, 'client', '-dir', getMinecraftDir(), '-mcversion', version.minecraft_version, '-loader', version.fabric_version, '-noprofile']
21-
if (spawnSync('javaw', args, {
21+
if (spawnSync(join(getJavaPath(meta.javaVersion.component), 'bin', 'javaw'), args, {
2222
cwd: getDirectory(),
23-
env: {PATH: getJavaPath(meta.javaVersion.component)}
23+
env: {PATH: env.PATH + separator + getJavaPath(meta.javaVersion.component)}
2424
}).signal != 0) return false
2525
log('Successfully installed Fabric Loader')
2626
}

src/gui/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';">
77
<link rel="stylesheet" href="style.css">
8+
<script>
9+
electronAPI.on('confirm', (event, data) => {
10+
const result = confirm(data.message, data.title)
11+
console.log(result)
12+
electronAPI.send(data.channel, result)
13+
})
14+
</script>
815
</head>
916
<body>
1017
<img id="background-image" src="background.jpg" draggable="false">

src/launch.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { resolve, join } = require('path')
2+
const { Worker, workerData } = require('worker_threads')
3+
const { isMainThread, parentPort } = require('worker_threads')
4+
5+
if (!isMainThread) require('./minecraft').launch(workerData.version, workerData.account, status => parentPort.postMessage(status))
6+
7+
const launch = async (version, account, statusCallback) => {
8+
new Worker(resolve(join(__dirname, 'launch.js')), {
9+
workerData: {
10+
version: version,
11+
account: account
12+
}
13+
}).on('message', statusCallback)
14+
}
15+
16+
module.exports = {launch}

src/main.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
const { platform } = require('os')
22
const { app, ipcMain } = require('electron')
3-
const { openWindow } = require('./gui/gui');
4-
const { exit } = require('process');
5-
const electronIsDev = require('electron-is-dev');
6-
const { getSimpleClientVersions, launch } = require('./minecraft');
7-
const { openAuthWindow, getAccounts } = require('./auth');
8-
const { checkForUpdates, update } = require('./updater');
9-
const { spawn } = require('child_process');
10-
const { join } = require('path');
11-
const { path } = require('app-root-path');
12-
13-
checkForUpdates(async () => {
14-
if (platform() == 'win32') {
15-
spawn('cscript', [
16-
'//nologo',
17-
join(path, 'src', 'yesno.vbs'),
18-
'SimpleClient',
19-
'An update for SimpleLauncher is available.\nShould it be downloaded in the background?'
20-
]).stdout.on('data', data => {if (data.toString().includes('6')) update()})
21-
}
22-
})
3+
const { openWindow } = require('./gui/gui')
4+
const { exit } = require('process')
5+
const { getSimpleClientVersions } = require('./minecraft')
6+
const { openAuthWindow, getAccounts } = require('./auth')
7+
const { checkForUpdates, update } = require('./updater')
238

249
if (platform() == 'win32' || platform() == 'linux') {
2510
app.disableHardwareAcceleration()
2611
app.whenReady().then(() => {
2712
const window = openWindow()
28-
window.on('ready-to-show', () => {
13+
window.on('ready-to-show', async () => {
14+
if (await checkForUpdates()) {
15+
window.webContents.send('confirm', {channel: 'update', message: 'An update for SimpleLauncher is available.\nShould it be downloaded in the background?'})
16+
}
2917
window.webContents.setZoomFactor(1)
3018
getSimpleClientVersions(versions => window.webContents.send('simpleclient_versions', versions))
3119
window.webContents.send('accounts', getAccounts(), 0)
3220
})
21+
ipcMain.on('update', (event, shouldUpdate) => {if (shouldUpdate) update()})
3322
ipcMain.on('login', event => {
3423
openAuthWindow((status, selectedAccount) => {
3524
window.webContents.send('auth', status)
@@ -44,7 +33,9 @@ if (platform() == 'win32' || platform() == 'linux') {
4433
getSimpleClientVersions(versions => {
4534
versions.forEach(version => {
4635
if (version.id == versionId) {
47-
launch(version, getAccounts().find(account => account.uuid == uuid), status => window.webContents.send('launch', status))
36+
require('./launch').launch(version,
37+
getAccounts().find(account => account.uuid == uuid),
38+
status => window.webContents.send('launch', status))
4839
}
4940
});
5041
})

src/minecraft.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ const launch = async (version, account, statusCallback) => {
8686
meta.arguments.jvm = meta.arguments.jvm.concat(fabricMeta.arguments.jvm)
8787
meta.arguments.game = meta.arguments.game.concat(fabricMeta.arguments.game)
8888
const separator = platform() == 'win32' ? ';' : platform() == 'linux' ? ':' : undefined
89-
const path = env.PATH + separator + getJavaPath(meta.javaVersion.component)
9089
const nativesDirectory = join(dir, 'natives', version.minecraft_version)
9190
const libraryDir = join(dir, 'libraries')
9291
const libraries = meta.libraries.map(library => {
@@ -137,9 +136,9 @@ const launch = async (version, account, statusCallback) => {
137136
cpSync(join(dir, 'mods'), join(getDirectory(), 'tmpmods'), {recursive: true, force: true})
138137
rmSync(join(dir, 'mods'), {recursive: true, force: true})
139138
log('Launching...')
140-
const process = spawn('java', insertValues(arguments, values), {
139+
const process = spawn(join(getJavaPath(meta.javaVersion.component), 'bin', 'javaw'), insertValues(arguments, values), {
141140
cwd: dir,
142-
env: {PATH: path},
141+
env: {PATH: env.PATH + separator + getJavaPath(meta.javaVersion.component)},
143142
detached: true
144143
})
145144
process.stdout.on('data', data => {

src/updater.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { platform } = require("os")
2-
const { join } = require("path")
2+
const { join, resolve } = require("path")
33
const { getDirectory } = require("./util")
44
const { execFile, spawnSync } = require("child_process")
55
const { existsSync, mkdirSync, readFileSync } = require("fs")
66
const { path } = require("app-root-path")
7+
const { isMainThread } = require("worker_threads")
78

89
const repository = 'FabiPunktExe/SimpleLauncher'
910

@@ -22,21 +23,19 @@ async function getRemoteVersion() {
2223
} else return undefined
2324
}
2425

25-
const checkForUpdates = async callback => {
26+
const checkForUpdates = async () => {
2627
const localVersion = getLocalVersion().split('.')
2728
const remoteVersion = (await getRemoteVersion(repository)).split('.')
2829
for (let i = 0, j = Math.min(localVersion.length, remoteVersion.length); i <= j; i += 1) {
2930
const localVersionPart = parseInt(localVersion.shift())
3031
const remoteVersionPart = parseInt(remoteVersion.shift())
31-
if (remoteVersionPart > localVersionPart) {
32-
callback()
33-
return
34-
} else if (remoteVersionPart < localVersionPart) return
32+
if (remoteVersionPart > localVersionPart) return true
33+
if (remoteVersionPart < localVersionPart) return false
3534
}
36-
if (remoteVersion.length > 0) callback()
35+
return remoteVersion.length > 0
3736
}
3837

39-
const update = async () => {
38+
if (!isMainThread) {
4039
const response = await fetch(`https://api.github.com/repos/${repository}/releases/latest`)
4140
if (response && response.ok) {
4241
const json = await response.json()
@@ -48,9 +47,11 @@ const update = async () => {
4847
log('Successfully downloaded update')
4948
log('Installing update...')
5049
execFile(join(dir, json.assets[0].name), {shell: true}).unref()
51-
log('Successfully installed update')
50+
exit(0)
5251
}
5352
} else return undefined
5453
}
5554

55+
const update = async () => new Worker(resolve(join(__dirname, 'updater.js')))
56+
5657
module.exports = {checkForUpdates, update}

0 commit comments

Comments
 (0)