4
0
mirror of https://github.com/AltarikMC/Launcher synced 2024-11-21 06:09:51 +01:00

Merge pull request #334 from AltarikMC/electron28

Electron 28, moving to ESM, add eslint
This commit is contained in:
Quentin Legot 2023-12-06 13:26:35 +01:00 committed by GitHub
commit f5a8ec27f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1765 additions and 666 deletions

29
.eslintrc.cjs Normal file
View File

@ -0,0 +1,29 @@
module.exports = {
env: {
browser: true,
es2021: true
},
extends: [
'standard'
],
overrides: [
{
env: {
node: true
},
files: [
'.eslintrc.{js,cjs}'
],
parserOptions: {
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [],
rules: {
}
}

View File

@ -1,49 +1,49 @@
const path = require('path');
const pkg = require('./package.json')
module.exports = {
packagerConfig: {
packageName: "altarik-launcher",
name: "Altarik Launcher",
productName: "altarik-launcher",
icon: path.resolve(__dirname, 'icon.ico'),
asar: true,
},
plugins: [
{
name: '@electron-forge/plugin-auto-unpack-natives',
config: {}
}
],
makers: [
{
name: "@electron-forge/maker-squirrel",
platforms: ['darwin', 'win32'],
config: {
name: pkg.name,
iconUrl: path.resolve(__dirname, 'icon.ico'),
//loadingGif: path.resolve(__dirname, 'src/assets/loading.gif'),
setupIcon: path.resolve(__dirname, 'icon.ico'),
setupExe: `${pkg.name}-${pkg.version}-win32-x64.exe`
}
},
{
name: '@electron-forge/maker-zip',
platforms: ['linux']
}
],
publishers: [
{
name: '@electron-forge/publisher-github',
config: {
repository: {
owner: 'AltarikMC',
name: 'Launcher'
},
preRelease: false,
draft: true,
tagPrefix: ''
}
}
]
}
const path = require('path');
const pkg = require('./package.json')
module.exports = {
packagerConfig: {
packageName: "altarik-launcher",
name: "Altarik Launcher",
productName: "altarik-launcher",
icon: path.resolve(__dirname, 'icon.ico'),
asar: true,
},
plugins: [
{
name: '@electron-forge/plugin-auto-unpack-natives',
config: {}
}
],
makers: [
{
name: "@electron-forge/maker-squirrel",
platforms: ['darwin', 'win32'],
config: {
name: pkg.name,
iconUrl: path.resolve(__dirname, 'icon.ico'),
//loadingGif: path.resolve(__dirname, 'src/assets/loading.gif'),
setupIcon: path.resolve(__dirname, 'icon.ico'),
setupExe: `${pkg.name}-${pkg.version}-win32-x64.exe`
}
},
{
name: '@electron-forge/maker-zip',
platforms: ['linux']
}
],
publishers: [
{
name: '@electron-forge/publisher-github',
config: {
repository: {
owner: 'AltarikMC',
name: 'Launcher'
},
preRelease: false,
draft: true,
tagPrefix: ''
}
}
]
}

View File

@ -4,6 +4,7 @@
"version": "2.1.0",
"description": "Altarik Launcher",
"main": "src/server/main.js",
"type": "module",
"homepage": "https://altarik.fr/",
"private": true,
"repository": "AltarikMC/Launcher",
@ -34,7 +35,12 @@
"@electron-forge/maker-zip": "^7.1.0",
"@electron-forge/plugin-auto-unpack-natives": "^7.1.0",
"@electron-forge/publisher-github": "^7.1.0",
"electron": "^27.1.0"
"electron": "28.0.0",
"eslint": "^8.0.1",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0 || ^16.0.0 ",
"eslint-plugin-promise": "^6.0.0"
},
"dependencies": {
"decompress": "^4.2.1",
@ -42,14 +48,14 @@
"electron-log": "^5.0.0",
"electron-squirrel-startup": "^1.0.0",
"extract-zip": "^2.0.1",
"hasha": "^5.2.2",
"hasha": "^6.0.0",
"izitoast": "^1.4.0",
"minecraft-launcher-core": "^3.17.3",
"msmc": "^4.1.0",
"node-fetch": "^2.7.0",
"node-fetch": "^3.0.0",
"vue": "^3.3.8"
},
"config": {
"forge": "./config.forge.js"
"forge": "./config.forge.cjs"
}
}

View File

@ -1,51 +1,47 @@
function handleSquirrelEvent(app) {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require('child_process');
const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function(command, args) {
let spawnedProcess;
try {
spawnedProcess = ChildProcess.spawn(command, args, {detached: true});
} catch (error) {}
return spawnedProcess;
};
const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
spawnUpdate(['--createShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-obsolete':
app.quit();
return true;
}
export default function handleSquirrelEvent (app) {
if (process.argv.length === 1) {
return false
}
module.exports = {
handleSquirrelEvent
}
const ChildProcess = require('child_process')
const path = require('path')
const appFolder = path.resolve(process.execPath, '..')
const rootAtomFolder = path.resolve(appFolder, '..')
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'))
const exeName = path.basename(process.execPath)
const spawn = function (command, args) {
let spawnedProcess
try {
spawnedProcess = ChildProcess.spawn(command, args, { detached: true })
} catch (error) {}
return spawnedProcess
}
const spawnUpdate = function (args) {
return spawn(updateDotExe, args)
}
const squirrelEvent = process.argv[1]
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
spawnUpdate(['--createShortcut', exeName])
setTimeout(app.quit, 1000)
return true
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName])
setTimeout(app.quit, 1000)
return true
case '--squirrel-obsolete':
app.quit()
return true
}
}

View File

@ -1,98 +1,112 @@
const { app, BrowserWindow, Menu, ipcMain, autoUpdater, dialog } = require('electron')
const logger = require('electron-log')
const { join } = require('path')
const updater = require('./updater.js')
let updaterInstance = new updater.Updater(app, autoUpdater, dialog, logger, showNotification)
import { app, BrowserWindow, Menu, ipcMain, autoUpdater, dialog } from 'electron'
import isDev from 'electron-is-dev'
import logger from 'electron-log'
import { join, dirname } from 'path'
import Updater from './updater.js'
import electronStartup from 'electron-squirrel-startup'
import install from './install.js'
import Mc from './minecraft.js'
import { minimizeWindow, closeWindow } from './menubar.js'
import { fileURLToPath } from 'url'
const updaterInstance = new Updater(app, autoUpdater, dialog, logger, showNotification)
updaterInstance.configUpdater()
if (require('electron-squirrel-startup')) {
require("./install.js").handleSquirrelEvent(app)
app.quit()
return
}
const minecraft = require('./minecraft.js')
const minecraft = new Mc()
minecraft.showNotification = showNotification
const iconPath = join(__dirname, "icon.ico")
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const iconPath = join(__dirname, 'icon.ico')
let win = null
function createWindow () {
win = new BrowserWindow({
width: 1000,
height: 600,
resizable: false,
icon: iconPath,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
frame: false
})
//Menu.setApplicationMenu(null)
win.loadFile('src/client/checkingUpdate.html')
win.on("close", () => {
app.quit()
})
win = new BrowserWindow({
width: 1000,
height: 600,
resizable: false,
icon: iconPath,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
frame: false
})
if (!isDev) {
Menu.setApplicationMenu(null)
}
win.loadFile('src/client/checkingUpdate.html')
win.on('close', () => {
app.quit()
})
}
const { setWindow, minimizeWindow, closeWindow } = require("./menubar.js");
function showNotification (title, body = '', clazz = 'info') {
win.webContents.send('notification', { title, body, class: clazz })
}
app.whenReady().then(() => {
ipcMain.on('disconnect', () => {
minecraft.auth = null
win.loadFile('src/client/login.html').then(() => showNotification('Déconnecté', 'Vous avez été déconnecté de votre compte', 'success'))
})
ipcMain.on('pageReady', (event) => {
event.sender.send('nick', { name: minecraft.auth.name })
minecraft.getModsInformations(event)
})
ipcMain.on('checking-update', () => {
updaterInstance.checkForUpdates(win, showNotification)
})
function main () {
if (electronStartup) {
install.handleSquirrelEvent(app)
app.quit()
return
}
app.whenReady().then(() => {
createWindow()
setWindow(win)
})
})
app.on('window-all-closed', () => {
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
app.quit()
}
})
})
ipcMain.on('minimizeWindow', () => {
ipcMain.on('minimizeWindow', () => {
minimizeWindow(win)
})
})
ipcMain.on('closeWindow', () => {
ipcMain.on('closeWindow', () => {
closeWindow(win)
})
})
app.on('activate', () => {
if (win === null){
createWindow()
app.on('activate', () => {
if (win === null) {
createWindow()
}
})
})
ipcMain.on("login", (event, args) => {
ipcMain.on('login', (event, args) => {
minecraft.login(event, win, args.user, args.pass)
})
})
ipcMain.on("microsoft-login", (event) => {
ipcMain.on('microsoft-login', (event) => {
minecraft.microsoftLogin(event, win)
})
})
ipcMain.on("invalidateData", event => {
ipcMain.on('invalidateData', event => {
minecraft.invalidateData(event)
})
})
ipcMain.on("launch", (event, args) => {
minecraft.launch(event, args)
})
function showNotification(title, body="", clazz="info") {
win.webContents.send('notification', {title: title, body: body, class: clazz})
ipcMain.on('launch', (event, args) => {
minecraft.launch(event, args)
})
}
ipcMain.on("disconnect", () => {
minecraft.auth = null
win.loadFile('src/client/login.html').then(() => showNotification("Déconnecté", "Vous avez été déconnecté de votre compte", "success"))
})
ipcMain.on("pageReady", (event) => {
event.sender.send("nick", { name: minecraft.auth.name })
minecraft.getModsInformations(event)
})
ipcMain.on("checking-update", () => {
updaterInstance.checkForUpdates(win, showNotification)
})
main()

View File

@ -1,21 +1,9 @@
let win;
function setWindow(browserWindow) {
win = browserWindow;
export function minimizeWindow (browserWindow) {
if (browserWindow.minimizable) {
browserWindow.minimize()
}
}
function minimizeWindow(browserWindow = win) {
if(browserWindow.minimizable) {
browserWindow.minimize()
}
export function closeWindow (browserWindow) {
browserWindow.close()
}
function closeWindow(browserWindow = win) {
browserWindow.close()
}
module.exports = {
setWindow,
minimizeWindow,
closeWindow
}

View File

@ -1,385 +1,375 @@
const isDev = require("electron-is-dev")
const { Authenticator, Client } = require("minecraft-launcher-core")
const fetch = require("node-fetch").default
const hasha = require("hasha")
const fs = require("fs")
const { join } = require("path")
const constants = require("constants")
const zip = require("extract-zip")
const logger = require("electron-log")
const { Auth, lst } = require("msmc")
const decompress = require("decompress")
const decompressTar = require("decompress-targz")
import isDev from 'electron-is-dev'
import mlc from 'minecraft-launcher-core'
import fetch from 'node-fetch'
import { hashFile } from 'hasha'
import fs from 'fs'
import { join } from 'path'
import zip from 'extract-zip'
import logger from 'electron-log'
import { Auth, lst } from 'msmc'
import decompress from 'decompress'
import decompressTar from 'decompress-targz'
const { Authenticator, Client } = mlc
class Minecraft {
export default class Minecraft {
appdata = process.env.APPDATA || (process.platform === 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + '/.local/share')
localappdata = process.env.LOCALAPPDATA || (process.platform === 'darwin' ? process.env.HOME + '/Library/Application Support/' : process.env.HOME + '/.config')
minecraftpath = join(this.appdata, '.altarik')
launcher = new Client()
auth = null
modsList = undefined
showNotification = undefined
modsInformationsEndpoint = 'https://launcher.altarik.fr'
appdata = process.env.APPDATA || (process.platform === "darwin" ? process.env.HOME + "/Library/Preferences" : process.env.HOME + "/.local/share")
localappdata = process.env.LOCALAPPDATA || (process.platform === "darwin" ? process.env.HOME + "/Library/Application Support/" : process.env.HOME + "/.config")
minecraftpath = join(this.appdata, ".altarik")
launcher = new Client()
auth = null
modsList = undefined
showNotification = undefined
modsInformationsEndpoint = "https://launcher.altarik.fr"
setShowNotification (showNotification) {
this.showNotification = showNotification
}
setShowNotification(showNotification) {
this.showNotification = showNotification
}
/**
/**
* @deprecated Mojang removed this method of authentification
* Used to login through Mojang account
*/
login(event, win, username, password) {
this.auth = null
if(isDev || password.trim() !== "") {
this.auth = Authenticator.getAuth(username, password)
this.auth.then(v => {
win.loadFile("src/client/index.html")
}).catch(() => {
event.sender.send("loginError")
logger.error("[MJ login] User haven't purchase the game")
this.showNotification("Erreur de connexion", "Vous ne possèdez pas de licence Minecraft sur ce compte", "error")
})
} else {
this.showNotification("Erreur de connexion", "Veuillez renseignez un mot de passe", "warning")
}
login (event, win, username, password) {
this.auth = null
if (isDev || password.trim() !== '') {
this.auth = Authenticator.getAuth(username, password)
this.auth.then(v => {
win.loadFile('src/client/index.html')
}).catch(() => {
event.sender.send('loginError')
logger.error("[MJ login] User haven't purchase the game")
this.showNotification('Erreur de connexion', 'Vous ne possèdez pas de licence Minecraft sur ce compte', 'error')
})
} else {
this.showNotification('Erreur de connexion', 'Veuillez renseignez un mot de passe', 'warning')
}
}
/**
/**
* Used to login through a Microsoft account
*/
microsoftLogin(event, win) {
const authManager = new Auth("select_account")
authManager.launch("electron").then(async xboxManager => {
xboxManager.getMinecraft().then(async token => {
if(!token.isDemo()) {
this.auth = token.mclc()
logger.info("[MS login] User has been connected successfully to them account")
win.loadFile("src/client/index.html")
} else {
event.sender.send("loginError")
logger.error("[MS login] User haven't purchase the game")
this.showNotification("Erreur de connexion", "Vous ne possèdez pas de licence Minecraft sur ce compte", "error")
}
}).catch(err => {
event.sender.send("loginError")
logger.error("[MS login] " + lst(err))
this.showNotification("Erreur de connexion à Mojang", lst(err), "error")
})
}).catch(err => {
event.sender.send("loginError")
if(err != "error.gui.closed") {
logger.error("[MS login] " + lst(err))
this.showNotification("Une erreur de connexion à Xbox est survenue", lst(err), "error")
}
})
}
launch(event, 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
}
}).then(v => {
if(v === null) {
this.close(event, -1)
}
})
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) => {
this.close(event, e)
})
}).catch((err) => {
this.showNotification("Impossible de lancer le jeu", "Erreur inconnue", "error")
event.sender.send("close", 1)
logger.error("Unable to launch the game")
logger.error(err)
})
}).catch(err => {
this.showNotification("Impossible de lancer le jeu", "Impossible d'installer Java pour votre configuration", "error")
event.sender.send("close", 1)
logger.warn("Unable to install java")
logger.warn(err)
})
}
close(event, code) {
event.sender.send("close", code)
if(code !== 0) {
logger.warn("Minecraft didn't close properly")
logger.warn(code)
this.showNotification("Une erreur est survenue", "Minecraft ne s'est pas fermé correctement", "error")
}
}
getModsInformations(event) {
fetch(this.modsInformationsEndpoint).then(response => {
if(response.ok) {
response.json().then(data => {
let folder = join(this.localappdata, "altarik-launcher", "data")
if(!fs.existsSync(folder))
fs.mkdirSync(folder, {recursive: true})
let file = join(folder, "launcher.json")
if(fs.existsSync(file))
fs.rmSync(file)
fs.writeFileSync(file, JSON.stringify(data))
event.sender.send("modsInformations", this.extractModsInformations(data))
}).catch(err => {
event.sender.send("modsInformations", this.extractModsFromFileSystem())
logger.warn(err)
logger.warn("An error occured while trying to connect to server")
})
} else {
logger.warn("Unable to connect to server")
logger.warn(err)
event.sender.send("modsInformations", this.extractModsFromFileSystem())
}
}).catch(err => {
logger.warn("Unable to connect to server")
logger.warn(err)
event.sender.send("modsInformations", this.extractModsFromFileSystem())
})
}
extractModsFromFileSystem() {
let filepath = join(this.localappdata, "altarik-launcher/data/launcher.json")
if(fs.existsSync(filepath)) {
let content = fs.readFileSync(filepath)
if(content !== null) {
this.showNotification("Impossible de récupérer certaines informations en ligne", "utilisation des dernières données récupérées", "warning")
return this.extractModsInformations(JSON.parse(content))
} else {
this.showNotification("Impossible de récupérer certaines informations en ligne", "Veuillez réessayez en cliquant sur le bouton", "warning")
logger.error("Unable to get chapters informations from server or filesystem")
return null
}
microsoftLogin (event, win) {
const authManager = new Auth('select_account')
authManager.launch('electron').then(async xboxManager => {
xboxManager.getMinecraft().then(async token => {
if (!token.isDemo()) {
this.auth = token.mclc()
logger.info('[MS login] User has been connected successfully to them account')
win.loadFile('src/client/index.html')
} else {
return null;
event.sender.send('loginError')
logger.error("[MS login] User haven't purchase the game")
this.showNotification('Erreur de connexion', 'Vous ne possèdez pas de licence Minecraft sur ce compte', 'error')
}
}
}).catch(err => {
event.sender.send('loginError')
logger.error('[MS login] ' + lst(err))
this.showNotification('Erreur de connexion à Mojang', lst(err), 'error')
})
}).catch(err => {
event.sender.send('loginError')
if (err !== 'error.gui.closed') {
logger.error('[MS login] ' + lst(err))
this.showNotification('Une erreur de connexion à Xbox est survenue', lst(err), 'error')
}
})
}
extractModsInformations(json) {
this.modsList = json.chapters
return this.modsList
}
async extractMods(chapterId, event) {
return new Promise(async (resolve, reject) => {
const modsFolder = join(this.minecraftpath, "mods")
const shaderFolder = join(this.minecraftpath, "shaderpacks")
if(fs.existsSync(modsFolder))
fs.rmSync(modsFolder, { recursive: true })
if(fs.existsSync(shaderFolder))
fs.rmSync(shaderFolder, { recursive: true })
for(const i in this.modsList) {
if(Number(i) === chapterId) {
const chapter = this.modsList[i]
for(let j in chapter.modspack.mods) {
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 hasha.fromFile(path, {algorithm: "sha1"})
if(sha1 === chapter.modspack.sha1sum[j]) {
await this.unzipMods(path).catch(err => {
reject(err)
})
} else {
logger.warn(`sha1sum ${sha1} don't correspond to ${chapter.modspack.sha1sum[j]} of mods ${path}`)
await this.downloadAndExtractMods(chapter.modspack.mods[j], path).catch(err => {
reject(err)
})
}
event.sender.send("progress", {type: "mods", task: Number(j)+1, total: chapter.modspack.mods.length })
} catch (err) {
try {
await this.downloadAndExtractMods(chapter.modspack.mods[j], path)
} catch(e) {
reject({ err, e })
return
}
}
}
resolve(chapter)
return
}
}
reject("didn't found the correct chapter" + chapter)
launch (event, 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,
version: {
number: chapter.minecraftVersion,
type: chapter.type | 'release',
custom: chapter.customVersion
},
memory: {
max: args.maxMem,
min: args.minMem
}
}).then(v => {
if (v === null) {
this.close(event, -1)
}
})
}
downloadMods(link, path) {
return new Promise((resolve, reject) => {
fetch(link).then(response => {
if(response.ok) {
if(fs.existsSync(path))
fs.rmSync(path)
const dest = fs.createWriteStream(path)
response.body.pipe(dest)
response.body.on("end", () => {
logger.log("download completed");
resolve("download completed")
})
dest.on("error", () => {
reject("An error appenned when using stream")
});
} else {
reject(response.status)
}
}).catch(err => {
reject(err)
})
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}`)
})
}
async unzipMods(zipLocation, outLocation=this.minecraftpath) {
return new Promise(async (resolve, reject) => {
logger.info(`unzipping ${zipLocation} file to ${outLocation}`)
zip(zipLocation, { dir: outLocation }).then(() => {
resolve()
}).catch(err => {
logger.error(`failed to unzip file`)
reject(err)
})
this.launcher.on('arguments', (e) => {
event.sender.send('launch', e)
logger.info('launching the game')
logger.info(e)
})
}
async extractTar(tarLocation, outLocation=this.microsoftpath) {
return new Promise(async (resolve, reject) => {
logger.info(`Extracting targz ${tarLocation} file to ${outLocation}`)
decompress(tarLocation, outLocation, {
plugins: [
decompressTar()
]
}).then(() => {
resolve()
}).catch((e) => {
logger.error(`Failed to extract targz file`)
reject(e)
})
this.launcher.on('close', (e) => {
this.close(event, e)
})
}
}).catch((err) => {
this.showNotification('Impossible de lancer le jeu', 'Erreur inconnue', 'error')
event.sender.send('close', 1)
logger.error('Unable to launch the game')
logger.error(err)
})
}).catch(err => {
this.showNotification('Impossible de lancer le jeu', "Impossible d'installer Java pour votre configuration", 'error')
event.sender.send('close', 1)
logger.warn('Unable to install java')
logger.warn(err)
})
}
async downloadAndExtractMods(link, path) {
return new Promise(async (resolve, reject) => {
this.downloadMods(link, path).then(() => {
this.unzipMods(path).then(() => {
resolve()
}).catch(err => {
reject(err)
close (event, code) {
event.sender.send('close', code)
if (code !== 0) {
logger.warn("Minecraft didn't close properly")
logger.warn(code)
this.showNotification('Une erreur est survenue', "Minecraft ne s'est pas fermé correctement", 'error')
}
}
getModsInformations (event) {
fetch(this.modsInformationsEndpoint).then(response => {
if (response.ok) {
response.json().then(data => {
const folder = join(this.localappdata, 'altarik-launcher', 'data')
if (!fs.existsSync(folder)) { fs.mkdirSync(folder, { recursive: true }) }
const file = join(folder, 'launcher.json')
if (fs.existsSync(file)) { fs.rmSync(file) }
fs.writeFileSync(file, JSON.stringify(data))
event.sender.send('modsInformations', this.extractModsInformations(data))
}).catch(err => {
event.sender.send('modsInformations', this.extractModsFromFileSystem())
logger.warn(err)
logger.warn('An error occured while trying to connect to server')
})
} else {
logger.warn('Unable to connect to server')
logger.warn(response.status)
event.sender.send('modsInformations', this.extractModsFromFileSystem())
}
}).catch(err => {
logger.warn('Unable to connect to server')
logger.warn(err)
event.sender.send('modsInformations', this.extractModsFromFileSystem())
})
}
extractModsFromFileSystem () {
const filepath = join(this.localappdata, 'altarik-launcher/data/launcher.json')
if (fs.existsSync(filepath)) {
const content = fs.readFileSync(filepath)
if (content !== null) {
this.showNotification('Impossible de récupérer certaines informations en ligne', 'utilisation des dernières données récupérées', 'warning')
return this.extractModsInformations(JSON.parse(content))
} else {
this.showNotification('Impossible de récupérer certaines informations en ligne', 'Veuillez réessayez en cliquant sur le bouton', 'warning')
logger.error('Unable to get chapters informations from server or filesystem')
return null
}
} else {
return null
}
}
extractModsInformations (json) {
this.modsList = json.chapters
return this.modsList
}
async extractMods (chapterId, event) {
return new Promise(async (resolve, reject) => {
const modsFolder = join(this.minecraftpath, 'mods')
const shaderFolder = join(this.minecraftpath, 'shaderpacks')
if (fs.existsSync(modsFolder)) { fs.rmSync(modsFolder, { recursive: true }) }
if (fs.existsSync(shaderFolder)) { fs.rmSync(shaderFolder, { recursive: true }) }
for (const i in this.modsList) {
if (Number(i) === chapterId) {
const chapter = this.modsList[i]
for (const j in chapter.modspack.mods) {
event.sender.send('progress', { type: 'mods', task: 0, total: chapter.modspack.mods.length })
const 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, fs.W_OK)
const sha1 = await hashFile(path, { algorithm: 'sha1' })
if (sha1 === chapter.modspack.sha1sum[j]) {
await this.unzipMods(path).catch(err => {
reject(err)
})
}).catch(err => {
reject(err)
})
})
}
async extractJava(chapterId, event) {
return new Promise(async (resolve, reject) => {
const runtime = join(this.minecraftpath, "runtime")
if(this.modsList[chapterId].java.platform[process.platform] !== undefined
&& 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.extractJavaArchive(downloadFile, runtime)
let filename = process.platform == "win32" ? "java.exe" : "java"
resolve(join(jre, "bin", filename))
} else {
logger.warn(`java sha256sum ${sha1} don't correspond to ${infos.sha256sum}`)
await this.downloadAndExtractJava(infos, downloadFolder, runtime).then(() => resolve(join(jre, "bin", process.platform === "win32" ? "java.exe" : "java"))).catch(err => reject(err))
}
} else {
await this.downloadAndExtractJava(infos, downloadFolder, runtime).then(() => resolve(join(jre, "bin", process.platform === "win32" ? "java.exe" : "java"))).catch(err => reject(err))
}
event.sender.send("progress", {type: "java", task: 1, total: 1 })
} else {
reject("There is not available version for this system")
}
})
}
async downloadAndExtractJava(infos, downloadFolder, runtimeFolder) {
return new Promise((resolve, reject) => {
logger.info(`Downloading ${infos.name}`)
this.downloadMods(infos.link, join(downloadFolder, `${infos.name}.zip`)).then(() => {
logger.info(`download completed`)
this.extractJavaArchive(join(downloadFolder, `${infos.name}.zip`), runtimeFolder).then(() => {
logger.info(`File unzipped`)
resolve()
}).catch(err => {
let join_s = join(downloadFolder, `${infos.name}.zip`)
logger.info(`Failed to unzip ${join_s}`)
reject(err)
} else {
logger.warn(`sha1sum ${sha1} don't correspond to ${chapter.modspack.sha1sum[j]} of mods ${path}`)
await this.downloadAndExtractMods(chapter.modspack.mods[j], path).catch(err => {
reject(err)
})
}).catch(err => {
logger.err(`Failed to download ${infos.link} to ${infos.name}.zip`)
reject(err)
})
})
}
}
event.sender.send('progress', { type: 'mods', task: Number(j) + 1, total: chapter.modspack.mods.length })
} catch (err) {
try {
await this.downloadAndExtractMods(chapter.modspack.mods[j], path)
} catch (e) {
reject(new Error({ err, e }))
return
}
}
}
resolve(chapter)
return
}
}
reject(new Error("didn't found the correct chapter" + chapterId))
})
}
async extractJavaArchive(zipLocation, outLocation) {
if(process.platform === "win32") {
await this.unzipMods(zipLocation, outLocation)
downloadMods (link, path) {
return new Promise((resolve, reject) => {
fetch(link).then(response => {
if (response.ok) {
if (fs.existsSync(path)) { fs.rmSync(path) }
const dest = fs.createWriteStream(path)
response.body.pipe(dest)
response.body.on('end', () => {
logger.log('download completed')
resolve('download completed')
})
dest.on('error', () => {
reject(new Error('An error appenned when using stream'))
})
} else {
await this.extractTar(zipLocation, outLocation)
reject(response.status)
}
}
}).catch(err => {
reject(err)
})
})
}
invalidateData(event) {
logger.info("invalidate game data...")
const assets = join(this.minecraftpath, "assets")
const librairies = join(this.minecraftpath,"libraries")
const natives = join(this.minecraftpath, "natives")
const versions = join(this.minecraftpath, "versions")
if(fs.existsSync(assets))
fs.rmSync(assets, { recursive: true })
if(fs.existsSync(librairies))
fs.rmSync(librairies, { recursive: true })
if(fs.existsSync(natives))
fs.rmSync(natives, { recursive: true })
if(fs.existsSync(versions))
fs.rmSync(versions, { recursive: true })
logger.info("Game data invalidated")
event.sender.send("invalidated")
async unzipMods (zipLocation, outLocation = this.minecraftpath) {
return new Promise((resolve, reject) => {
logger.info(`unzipping ${zipLocation} file to ${outLocation}`)
zip(zipLocation, { dir: outLocation }).then(() => {
resolve()
}).catch(err => {
logger.error('failed to unzip file')
reject(err)
})
})
}
async extractTar (tarLocation, outLocation = this.microsoftpath) {
return new Promise((resolve, reject) => {
logger.info(`Extracting targz ${tarLocation} file to ${outLocation}`)
decompress(tarLocation, outLocation, {
plugins: [
decompressTar()
]
}).then(() => {
resolve()
}).catch((e) => {
logger.error('Failed to extract targz file')
reject(e)
})
})
}
async downloadAndExtractMods (link, path) {
return new Promise((resolve, reject) => {
this.downloadMods(link, path).then(() => {
this.unzipMods(path).then(() => {
resolve()
}).catch(err => {
reject(err)
})
}).catch(err => {
reject(err)
})
})
}
async extractJava (chapterId, event) {
return new Promise((resolve, reject) => {
const runtime = join(this.minecraftpath, 'runtime')
if (this.modsList[chapterId].java.platform[process.platform] !== undefined &&
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)) {
hashFile(downloadFile, { algorithm: 'sha256' }).then(sha1 => {
if (sha1 === infos.sha256sum) {
this.extractJavaArchive(downloadFile, runtime).then(() => {
const filename = process.platform === 'win32' ? 'java.exe' : 'java'
resolve(join(jre, 'bin', filename))
}).catch(err => {
reject(err)
})
} else {
logger.warn(`java sha256sum ${sha1} don't correspond to ${infos.sha256sum}`)
this.downloadAndExtractJava(infos, downloadFolder, runtime).then(() => resolve(join(jre, 'bin', process.platform === 'win32' ? 'java.exe' : 'java'))).catch(err => reject(err))
}
}).catch(err => {
reject(err)
})
} else {
this.downloadAndExtractJava(infos, downloadFolder, runtime).then(() => resolve(join(jre, 'bin', process.platform === 'win32' ? 'java.exe' : 'java'))).catch(err => reject(err))
}
event.sender.send('progress', { type: 'java', task: 1, total: 1 })
} else {
reject(new Error('There is not available version for this system'))
}
})
}
async downloadAndExtractJava (infos, downloadFolder, runtimeFolder) {
return new Promise((resolve, reject) => {
logger.info(`Downloading ${infos.name}`)
this.downloadMods(infos.link, join(downloadFolder, `${infos.name}.zip`)).then(() => {
logger.info('download completed')
this.extractJavaArchive(join(downloadFolder, `${infos.name}.zip`), runtimeFolder).then(() => {
logger.info('File unzipped')
resolve()
}).catch(err => {
const joinS = join(downloadFolder, `${infos.name}.zip`)
logger.info(`Failed to unzip ${joinS}`)
reject(err)
})
}).catch(err => {
logger.err(`Failed to download ${infos.link} to ${infos.name}.zip`)
reject(err)
})
})
}
async extractJavaArchive (zipLocation, outLocation) {
if (process.platform === 'win32') {
await this.unzipMods(zipLocation, outLocation)
} else {
await this.extractTar(zipLocation, outLocation)
}
}
invalidateData (event) {
logger.info('invalidate game data...')
const assets = join(this.minecraftpath, 'assets')
const librairies = join(this.minecraftpath, 'libraries')
const natives = join(this.minecraftpath, 'natives')
const versions = join(this.minecraftpath, 'versions')
if (fs.existsSync(assets)) { fs.rmSync(assets, { recursive: true }) }
if (fs.existsSync(librairies)) { fs.rmSync(librairies, { recursive: true }) }
if (fs.existsSync(natives)) { fs.rmSync(natives, { recursive: true }) }
if (fs.existsSync(versions)) { fs.rmSync(versions, { recursive: true }) }
logger.info('Game data invalidated')
event.sender.send('invalidated')
}
}
module.exports = new Minecraft

View File

@ -1,101 +1,94 @@
const isDev = require('electron-is-dev')
const fetch = require('node-fetch').default
const pkg = require('../../package.json')
import isDev from 'electron-is-dev'
import fetch from 'node-fetch'
import pkg from '../../package.json' assert {type: 'json'}
const server = 'https://update.electronjs.org'
class Updater {
export default class Updater {
constructor (app, autoUpdater, dialog, logger, ipcMain) {
this.app = app
this.autoUpdater = autoUpdater
this.dialog = dialog
this.logger = logger
this.ipcMain = ipcMain
}
constructor(app, autoUpdater, dialog, logger, ipcMain) {
this.app = app
this.autoUpdater = autoUpdater
this.dialog = dialog
this.logger = logger
this.ipcMain = ipcMain
configUpdater () {
this.logger.info(`electron version: ${process.versions.electron}`)
this.logger.info(`chrome version: ${process.versions.chrome}`)
this.logger.info(`Node version: ${process.versions.node}`)
this.logger.info(`platform: ${process.platform}`)
this.logger.info(`arch: ${process.arch}`)
if (isDev) {
this.logger.info(`developpement version ${this.app.getVersion()}`)
return
}
this.logger.info(`production version ${this.app.getVersion()}`)
// TODO : replace dialog by automatic restart
this.autoUpdater.on('update-downloaded', (_event, releaseNotes, releaseName) => {
this.logger.info(`update downloaded ${releaseName}`)
this.logger.info('Leaving application to install update...')
this.autoUpdater.quitAndInstall()
})
}
configUpdater() {
this.logger.info(`electron version: ${process.versions['electron']}`)
this.logger.info(`chrome version: ${process.versions['chrome']}`)
this.logger.info(`Node version: ${process.versions['node']}`)
this.logger.info(`platform: ${process.platform}`)
this.logger.info(`arch: ${process.arch}`)
if(isDev) {
this.logger.info(`developpement version ${this.app.getVersion()}`)
return
}
this.logger.info(`production version ${this.app.getVersion()}`)
// TODO : replace dialog by automatic restart
this.autoUpdater.on('update-downloaded', (_event, releaseNotes, releaseName) => {
this.logger.info(`update downloaded ${releaseName}`)
this.logger.info("Leaving application to install update...")
this.autoUpdater.quitAndInstall()
})
checkForUpdates (win, showNotification) {
if (isDev) {
win.loadFile('src/client/login.html')
return
}
this.logger.info('Checking for update...')
const feed = `${server}/${pkg.repository}/${process.platform}-${process.arch}/${this.app.getVersion()}`
if (process.platform !== 'linux') {
this.autoUpdater.setFeedURL(feed)
this.autoUpdater.on('error', message => {
this.displayError(win, showNotification, message)
})
checkForUpdates(win, showNotification) {
if(isDev) {
win.loadFile('src/client/login.html')
return;
}
this.logger.info("Checking for update...")
const feed = `${server}/${pkg.repository}/${process.platform}-${process.arch}/${this.app.getVersion()}`
if(process.platform != 'linux') {
this.autoUpdater.setFeedURL(feed)
this.autoUpdater.on('error', message => {
this.displayError(win, showNotification, message)
})
this.autoUpdater.on('update-available', () => {
this.logger.info("update available, downloading...")
win.webContents.send("update-available")
})
this.autoUpdater.on("update-not-available", () => {
this.logger.info("update not available")
win.loadFile('src/client/login.html')
})
this.autoUpdater.checkForUpdates()
} else {
this.searchUpdateLinux(win, showNotification)
}
this.autoUpdater.on('update-available', () => {
this.logger.info('update available, downloading...')
win.webContents.send('update-available')
})
this.autoUpdater.on('update-not-available', () => {
this.logger.info('update not available')
win.loadFile('src/client/login.html')
})
this.autoUpdater.checkForUpdates()
} else {
this.searchUpdateLinux(win, showNotification)
}
}
searchUpdateLinux(win, showNotification) {
const url = 'https://api.github.com/repos/AltarikMc/Launcher/releases/latest'
fetch(url).then(response => {
if(response.status === 200) {
response.json().then(json => {
if(json.tag_name !== pkg.version) {
let asset = json.assets.filter(el => el.browser_download_url.includes(".zip"))
if(asset.length === 1) {
let downloadUrl = asset[0].browser_download_url
win.webContents.send("please-download-update", { url: downloadUrl} )
this.logger.info("update available, please download")
} else {
this.displayError(win, showNotification, "Can't find right asset in last update")
}
} else {
this.logger.info("update not available")
win.loadFile('src/client/login.html')
}
}).catch(err => this.displayError(win, showNotification, err))
searchUpdateLinux (win, showNotification) {
const url = 'https://api.github.com/repos/AltarikMc/Launcher/releases/latest'
fetch(url).then(response => {
if (response.status === 200) {
response.json().then(json => {
if (json.tag_name !== pkg.version) {
const asset = json.assets.filter(el => el.browser_download_url.includes('.zip'))
if (asset.length === 1) {
const downloadUrl = asset[0].browser_download_url
win.webContents.send('please-download-update', { url: downloadUrl })
this.logger.info('update available, please download')
} else {
this.displayError(win, showNotification, "Server unavailable")
this.displayError(win, showNotification, "Can't find right asset in last update")
}
}).catch(err => this.displayError(win, showNotification, err))
}
displayError(win, showNotification, errorMessage) {
this.logger.error('There was a problem updating the application')
this.logger.error(errorMessage)
win.loadFile('src/client/login.html').then(() => {
showNotification("Une erreur est survenue lors de la vérification de la mise à jour", "Veuillez vérifier votre connexion internet et réessayer", "error")
})
}
} else {
this.logger.info('update not available')
win.loadFile('src/client/login.html')
}
}).catch(err => this.displayError(win, showNotification, err))
} else {
this.displayError(win, showNotification, 'Server unavailable')
}
}).catch(err => this.displayError(win, showNotification, err))
}
}
module.exports = {
Updater
displayError (win, showNotification, errorMessage) {
this.logger.error('There was a problem updating the application')
this.logger.error(errorMessage)
win.loadFile('src/client/login.html').then(() => {
showNotification('Une erreur est survenue lors de la vérification de la mise à jour', 'Veuillez vérifier votre connexion internet et réessayer', 'error')
})
}
}

1145
yarn.lock

File diff suppressed because it is too large Load Diff