4
0
mirror of https://github.com/AltarikMC/Launcher synced 2025-12-15 12:01:46 +00:00

Added java download support

This commit is contained in:
2021-05-14 16:21:22 +02:00
parent 0b85d5c84a
commit 2a5e8f7805
8 changed files with 178 additions and 64 deletions

View File

@@ -64,6 +64,10 @@ ipcMain.on("login", (event, args) => {
minecraft.login(event, win, showNotification, args.user, args.pass)
})
ipcMain.on("launch", (event, args) => {
minecraft.launch(event, showNotification, args)
})
function showNotification(title, body="") {
new Notification({ title: title, body: body }).show()
}
@@ -72,10 +76,6 @@ ipcMain.on("notification", (event, args) => {
showNotification(args.title, args.body)
})
ipcMain.on("launch", (event, args) => {
minecraft.launch(event, args)
})
ipcMain.on("disconnect", (e) => {
win.loadFile('src/client/login.html')
})

View File

@@ -1,9 +1,9 @@
const isDev = require('electron-is-dev')
const { Client, Authenticator } = require('minecraft-launcher-core')
const axios = require('axios').default
const sha1File = require('sha1-file')
const hasha = require('hasha');
const fs = require('fs')
const { join } = require('path')
const { join, resolve } = require('path')
const constants = require("constants")
const zip = require('extract-zip')
const logger = require('electron-log')
@@ -15,7 +15,6 @@ class Minecraft {
launcher = new Client()
auth = null
modsList = undefined
jre8 = "https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u292-b10/OpenJDK8U-jre_x64_windows_hotspot_8u292b10.zip"
login(event, win, showNotification, username, password) {
this.auth = null
@@ -34,46 +33,55 @@ class Minecraft {
}
}
launch(event, args) {
this.extractMods(Number(args.chapter), event).then((chapter) => {
this.launcher.launch({
authorization: this.auth,
root: this.minecraftpath,
version: {
number: chapter.minecraftVersion,
type: "release",
custom: chapter.customVersion
},
memory: {
max: args.maxMem,
min: args.minMem
}
launch(event, showNotification, args) {
this.extractJava(Number(args.chapter), event).then((javaPath) => {
this.extractMods(Number(args.chapter), event).then((chapter) => {
this.launcher.launch({
authorization: this.auth,
root: this.minecraftpath,
javaPath: javaPath,
version: {
number: chapter.minecraftVersion,
type: chapter.type | "release",
custom: chapter.customVersion
},
memory: {
max: args.maxMem,
min: args.minMem
}
})
this.launcher.on('debug', (e) => logger.info(`debug: ${e}`));
// this.launcher.on('data', (e) => logger.info(`data: ${e}`));
this.launcher.on('progress', (e) => {
event.sender.send("progress", e)
logger.info(`progress ${e.type} :${e.task} / ${e.total}`)
})
this.launcher.on('arguments', (e) => {
event.sender.send("launch", e)
logger.info("launching the game")
logger.info(e)
})
this.launcher.on('close', (e) => {
event.sender.send("close", e)
if(e !== 0) {
logger.warn("Minecraft didn't close properly")
logger.warn(e)
showNotification("Une erreur est survenue", "Minecraft ne s'est pas fermé correctement")
}
})
}).catch((err) => {
showNotification("Impossible de lancer le jeu")
event.sender.send("close", 1)
logger.error('Unable to launch the game')
logger.error(err)
})
// this.launcher.on('debug', (e) => console.log("debug", e));
// this.launcher.on('data', (e) => console.log("data", e));
this.launcher.on('progress', (e) => {
event.sender.send("progress", e)
logger.info(`progress ${e.type} :${e.task} / ${e.total}`)
})
this.launcher.on('arguments', (e) => {
event.sender.send("launch", e)
logger.info("launching the game")
logger.info(e)
})
this.launcher.on('close', (e) => {
event.sender.send("close", e)
if(e !== 0) {
logger.warn("Minecraft didn't close properly")
logger.warn(e)
showNotification("Une erreur est survenue", "Minecraft ne s'est pas fermé correctement")
}
})
}).catch((err) => {
showNotification("Impossible de lancer le jeu")
}).catch(err => {
showNotification("Impossible d'intaller Java pour votre configuration")
event.sender.send("close", 1)
logger.error('Unable to launch the game')
logger.error(err)
logger.warn("Unable to install java")
logger.warn(err)
})
}
getModsInformations(event) {
@@ -123,14 +131,14 @@ class Minecraft {
if(Number(i) === chapterId) {
const chapter = this.modsList[i]
for(let j in chapter.modspack.mods) {
event.sender.send("progress", {type: "mods", task: i, total: chapter.modspack.mods.length })
event.sender.send("progress", {type: "mods", task: 0, total: chapter.modspack.mods.length })
let modpackFolder = join(this.minecraftpath, "modpack", chapter.title)
if(!fs.existsSync(modpackFolder))
fs.mkdirSync(modpackFolder, { recursive: true })
const path = join(modpackFolder, `modpack${j}.zip`)
try {
fs.accessSync(path, constants.W_OK)
let sha1 = await sha1File(path)
let sha1 = await hasha.fromFile(path, {algorithm: 'sha1'})
if(sha1 === chapter.modspack.sha1sum[j]) {
await this.unzipMods(path).catch(err => {
reject(err)
@@ -169,12 +177,13 @@ class Minecraft {
responseType: "stream"
}).then(res => {
if(res.status === 200) {
fs.rmSync(path)
if(fs.existsSync(path))
fs.rmSync(path)
res.data.pipe(fs.createWriteStream(path));
res.data.on("end", () => {
logger.log("download completed");
resolve("download completed")
});
})
} else {
reject(res.status)
}
@@ -184,9 +193,9 @@ class Minecraft {
})
}
async unzipMods(path) {
async unzipMods(zipLocation, outLocation=this.minecraftpath) {
return new Promise(async (resolve, reject) => {
zip(path, { dir: this.minecraftpath }).then(() => {
zip(zipLocation, { dir: outLocation }).then(() => {
resolve()
}).catch(err => {
reject(err)
@@ -197,7 +206,7 @@ class Minecraft {
}
async downloadAndExtractMods(link, path) {
return new Promise(async(resolve, reject) => {
return new Promise(async (resolve, reject) => {
this.downloadMods(link, path).then(() => {
this.unzipMods(path).then(() => {
resolve()
@@ -210,6 +219,48 @@ class Minecraft {
})
}
async extractJava(chapterId, event) {
return new Promise(async (resolve, reject) => {
const runtime = join(this.minecraftpath, "runtime")
if(this.modsList[chapterId].java.platform[process.platform][process.arch] !== undefined) {
event.sender.send("progress", {type: "java", task: 0, total: 1 })
const infos = this.modsList[chapterId].java.platform[process.platform][process.arch]
const jre = join(runtime, infos.name)
const downloadFolder = join(runtime, "download")
const downloadFile = join(downloadFolder, `${infos.name}.zip`)
if(fs.existsSync(jre))
fs.rmSync(jre, { recursive: true })
if(!fs.existsSync(downloadFolder))
fs.mkdirSync(downloadFolder, { recursive: true })
if(fs.existsSync(downloadFile)) {
let sha1 = await hasha.fromFile(downloadFile, {algorithm: 'sha256'})
if(sha1 === infos.sha256sum) {
await this.unzipMods(downloadFile, runtime)
resolve(join(jre, 'bin', 'java.exe'))
} else {
logger.warn(`java sha256sum ${sha1} don't correspond to ${infos.sha256sum}`)
await this.downloadAndExtractJava(infos, downloadFolder, runtime).then(() => resolve(join(jre, 'bin', 'java.exe'))).catch(err => reject(err))
}
} else {
await this.downloadAndExtractJava(infos, downloadFolder, runtime).then(() => resolve(join(jre, 'bin', 'java.exe'))).catch(err => reject(err))
}
event.sender.send("progress", {type: "java", task: 1, total: 1 })
} else {
reject("There is not available version for your system")
}
})
}
async downloadAndExtractJava(infos, downloadFolder, runtimeFolder) {
return new Promise((resolve, reject) => {
this.downloadMods(infos.link, join(downloadFolder, `${infos.name}.zip`)).then(() => {
this.unzipMods(join(downloadFolder, `${infos.name}.zip`), runtimeFolder).then(() => resolve()).catch(err => reject(err))
}).catch(err => {
reject(err)
})
})
}
}
module.exports = new Minecraft

View File

@@ -12,6 +12,8 @@ function initUpdater(autoUpdater) {
}
function configUpdater(app, autoUpdater, dialog, logger) {
logger.info(`platform: ${process.platform}`)
logger.info(`arch: ${process.arch}`)
if(isDev) {
logger.info(`developpement version ${app.getVersion()}`)
return