Skip to content

Commit 8f03ff9

Browse files
committed
Added auto updater
1 parent bb4225d commit 8f03ff9

File tree

5 files changed

+103
-8
lines changed

5 files changed

+103
-8
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simplelauncher",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "Launcher for SimpleClient",
55
"main": "src/main.js",
66
"scripts": {
@@ -20,6 +20,7 @@
2020
"homepage": "https://simpleclient.github.io",
2121
"dependencies": {
2222
"child_process": "^1.0.2",
23+
"dialog-node": "^0.2.1",
2324
"electron-is-dev": "^2.0.0",
2425
"os": "^0.1.2",
2526
"path": "^0.12.7"

src/gui/index.html

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,48 @@
1313
<div id="accounts" style="visibility: hidden;">
1414
<button class="account" onclick="electronAPI.send('login')" style="margin-bottom: 0.5vh;">Add Account</button>
1515
</div>
16+
<button id="account" onclick="clickAccountsButton()"><p style="visibility: hidden;">Click to switch</p></button>
1617
<script>
1718
const accounts = document.getElementById('accounts')
1819
function clickAccountsButton() {
1920
if (accounts.children.length <= 1) electronAPI.send('login')
2021
else if (accounts.style.visibility == 'visible') accounts.style.visibility = 'hidden'
2122
else accounts.style.visibility = 'visible'
2223
}
24+
electronAPI.on('launch', (event, status) => {
25+
if (status == 'starting') {
26+
document.getElementById('launch').disabled = true
27+
document.getElementById('launch').innerText = 'Launching'
28+
} else if (status == 'download_starting') {
29+
document.getElementById('launch').innerText = 'Downloading'
30+
} else if (status == 'downloading_java_starting') {
31+
document.getElementById('launch').innerText = 'Downloading Java'
32+
} else if (status == 'downloading_java_done') {
33+
document.getElementById('launch').innerText = 'Downloading'
34+
} else if (status == 'downloading_libraries_starting') {
35+
document.getElementById('launch').innerText = 'Downloading Libraries'
36+
} else if (status == 'downloading_libraries_done') {
37+
document.getElementById('launch').innerText = 'Downloading'
38+
} else if (status == 'downloading_assets_starting') {
39+
document.getElementById('launch').innerText = 'Downloading Assets'
40+
} else if (status == 'downloading_assets_done') {
41+
document.getElementById('launch').innerText = 'Downloading'
42+
} else if (status == 'downloading_mods_starting') {
43+
document.getElementById('launch').innerText = 'Downloading Mods'
44+
} else if (status == 'downloading_mods_done') {
45+
document.getElementById('launch').innerText = 'Downloading'
46+
} else if (status == 'download_done') {
47+
document.getElementById('launch').innerText = 'Launching'
48+
} else if (status == 'authenticating') {
49+
document.getElementById('launch').innerText = 'Authenticating'
50+
} else if (status == 'launching') {
51+
document.getElementById('launch').innerText = 'Launching'
52+
} else if (status == 'done' || status == 'error') {
53+
document.getElementById('launch').disabled = false
54+
document.getElementById('launch').innerText = `LAUNCH ${document.getElementById('versions-button').innerText}`
55+
}
56+
})
2357
</script>
24-
<button id="account" onclick="clickAccountsButton()"><p style="visibility: hidden;">Click to switch</p></button>
2558
<img id="avatar" src="https://minotar.net/helm/mhf_question/64.png" draggable="false">
2659
<div id="versions" style="visibility: hidden;"></div>
2760
<script>
@@ -87,9 +120,9 @@
87120
electronAPI.on('simpleclient_versions', (event, versionList) => {
88121
Array.from(versions.children).forEach(child => child.remove())
89122
const changeVersion = version => {
90-
document.getElementById('launch').innerText = 'LAUNCH ' + version.minecraft_version
91-
document.getElementById('launch').value = version.id
92-
document.getElementById('versions-button').innerText = version.minecraft_version
123+
document.getElementById('launch').innerText = `LAUNCH ${version.minecraft_version}`
124+
document.getElementById('launch').value = version.id
125+
document.getElementById('versions-button').innerText = version.minecraft_version
93126
}
94127
versionList.forEach(version => {
95128
const button = document.createElement('button')

src/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ const { exit } = require('process');
55
const electronIsDev = require('electron-is-dev');
66
const { getSimpleClientVersions, launch } = require('./minecraft');
77
const { openAuthWindow, getAccounts } = require('./auth');
8+
const { checkForUpdates, update } = require('./updater');
89

910
if (!electronIsDev) {
11+
checkForUpdates(() => {
12+
require('dialog-node').question('An update for SimpleLauncher is available.\nShould it be downloaded in the background?', 'SimpleLauncher', 0, (code, value, stderr) => {
13+
if (value == 'OK') update()
14+
})
15+
})
1016
}
1117

1218
if (platform() == 'win32' || platform() == 'linux') {

src/minecraft.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const { existsSync } = require("original-fs")
2-
const { getDirectory, runSync, runAsync, getPath } = require("./util")
2+
const { getDirectory, runSync, getPath } = require("./util")
33
const { join, dirname } = require("path")
44
const { spawnSync, spawn, } = require("child_process")
5-
const { mkdirSync, readFileSync, statSync, writeFileSync, renameSync, rmdirSync, rm, rmSync, rename, copyFileSync, cp, cpSync, readdirSync } = require("fs")
5+
const { mkdirSync, readFileSync, statSync, rm, rmSync, cp, cpSync, readdirSync } = require("fs")
66
const { homedir, platform } = require("os")
77
const { downloadJava, getJavaVersion } = require("./java")
88
const { arch, env, stdout } = require("process")
@@ -121,7 +121,7 @@ async function downloadVersion(version, dir, statusCallback) {
121121
log(`Successfully downloaded ${version.minecraft_version}/${version.minecraft_version}.json`)
122122
}
123123
const minecraftVersionMeta = JSON.parse(readFileSync(join(minecraftVersionDir, `${version.minecraft_version}.json`)))
124-
if (!getJavaVersion() || parseInt(getJavaVersion().split('.')) < 17) {
124+
if (!getJavaVersion() || parseInt(getJavaVersion().split('.')[0]) < 17) {
125125
statusCallback('downloading_java_starting')
126126
if (downloadJava(minecraftVersionMeta.javaVersion.component)) {
127127
statusCallback('downloading_java_done')

src/updater.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { platform } = require("os")
2+
const { join } = require("path")
3+
const { env } = require("process")
4+
const { getDirectory } = require("./util")
5+
const { execFile, spawnSync, spawn } = require("child_process")
6+
const { existsSync, mkdirSync } = require("fs")
7+
8+
const repository = 'FabiPunktExe/SimpleLauncher'
9+
10+
const log = (...data) => console.log('[Updater] ' + data)
11+
12+
function getLocalVersion() {
13+
return env.npm_package_version
14+
}
15+
16+
async function getRemoteVersion() {
17+
const response = await fetch(`https://api.github.com/repos/${repository}/releases/latest`)
18+
if (response && response.ok) {
19+
const json = await response.json()
20+
return json.tag_name.replace('v', '')
21+
} else return undefined
22+
}
23+
24+
const checkForUpdates = async callback => {
25+
const localVersion = getLocalVersion().split('.')
26+
const remoteVersion = (await getRemoteVersion(repository)).split('.')
27+
for (let i = 0, j = Math.min(localVersion.length, remoteVersion.length); i <= j; i += 1) {
28+
const localVersionPart = parseInt(localVersion.shift())
29+
const remoteVersionPart = parseInt(remoteVersion.shift())
30+
if (remoteVersionPart > localVersionPart) {
31+
callback()
32+
return
33+
} else if (remoteVersionPart < localVersionPart) return
34+
}
35+
if (remoteVersion.length > 0) callback()
36+
}
37+
38+
const update = async () => {
39+
const response = await fetch(`https://api.github.com/repos/${repository}/releases/latest`)
40+
if (response && response.ok) {
41+
const json = await response.json()
42+
const dir = join(getDirectory(), 'updates')
43+
if (!existsSync(dir)) mkdirSync(dir, {recursive: true})
44+
if (platform() == 'win32') {
45+
log('Downloading update...')
46+
spawnSync('curl', ['-L', json.assets[0].browser_download_url, '-o', join(dir, json.assets[0].name)])
47+
log('Successfully downloaded update')
48+
log('Installing update...')
49+
execFile(join(dir, json.assets[0].name), {shell: true}).unref()
50+
log('Successfully installed update')
51+
}
52+
} else return undefined
53+
}
54+
55+
module.exports = {checkForUpdates, update}

0 commit comments

Comments
 (0)