From 759a5aa6ea8bf075240d62fb173d44a936c5eee5 Mon Sep 17 00:00:00 2001 From: LawnCable Date: Mon, 11 Jun 2018 18:03:42 +0200 Subject: Update for Windows is now working --- src/main.ts | 2 +- src/main/manaplus/manaApp/manaApp.interface.ts | 2 +- src/main/manaplus/manaApp/windows.ts | 45 ++++++++++++++++++++++---- src/main/manaplus/manaplus.ts | 45 ++++++++++++++++++++++---- src/main/richpresence.ts | 2 +- src/main/util/webrequest.ts | 35 ++++++++++++++++++++ src/renderer/gameserver/data.ts | 2 +- 7 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 src/main/util/webrequest.ts diff --git a/src/main.ts b/src/main.ts index 0f60c34..9df2c55 100644 --- a/src/main.ts +++ b/src/main.ts @@ -156,7 +156,7 @@ ManaPlus.init(); ipcMain.on('play', async (event:any, arg:any) => { if(Status.getStatus().playing)return; - console.log("play", arg); + //console.log("play", arg); Status.setPlaying(true); await ManaPlus.start(arg); //Status.showError("Failed To Launch Mana Plus","Not implemented yet!","Launch Manaplus faild: Not Implemented"); diff --git a/src/main/manaplus/manaApp/manaApp.interface.ts b/src/main/manaplus/manaApp/manaApp.interface.ts index 4db7c15..a4fa795 100644 --- a/src/main/manaplus/manaApp/manaApp.interface.ts +++ b/src/main/manaplus/manaApp/manaApp.interface.ts @@ -3,6 +3,6 @@ export interface ManaPlusApp { getGameDir():string, getVersion():Promise, isInstalled():boolean, - updateAvailible():Promise<{isNewVersion:boolean, newestVersion:string}>, + updateAvailable():Promise<{isNewVersion:boolean, newestVersion:string}>, update():Promise } diff --git a/src/main/manaplus/manaApp/windows.ts b/src/main/manaplus/manaApp/windows.ts index 92acb7f..832a140 100644 --- a/src/main/manaplus/manaApp/windows.ts +++ b/src/main/manaplus/manaApp/windows.ts @@ -4,11 +4,13 @@ import * as fs from 'fs-extra'; import { Status } from '../../status'; import {download, Progress as ProgressType} from '../../util/downloader'; import * as extract from 'extract-zip'; +import { getRequest } from '../../util/webrequest'; export class ManaPlusAppWindows implements ManaPlusApp { private path:string; public readonly startCommand:string; + versionRegEx:RegExp = /.*ManaPlus ([\d.]+) Windows.*/g; constructor(){ const ManaPath = app.getPath('userData')+"\\manaplus"; fs.existsSync(ManaPath) || fs.mkdirSync(ManaPath); @@ -21,7 +23,19 @@ export class ManaPlusAppWindows implements ManaPlusApp { return this.path+"\\Mana\\"; } getVersion(): Promise { - throw new Error("Not Implemented yet"); + return new Promise((res, rej) => { + let output:string; + const child = require('child_process').execFile(this.startCommand, ['-v'], function(err:Error, data:any) { + output = data.toString(); + }); + child.on('close', ()=>{ + output = output.replace(this.versionRegEx, "$1"); + res(output); + }); + child.on('error', ()=>{ + rej(new Error("Version check failed")); + }); + }); } isInstalled(): boolean { return fs.existsSync(this.path+"\\Mana\\manaplus.exe"); @@ -32,20 +46,27 @@ export class ManaPlusAppWindows implements ManaPlusApp { // Get Update URL Status.setProgress(500); Status.setActivity("Fetching Download URL"); - //// TODO: Fetch update url + let downloadURL; + try { + let versions = await getRequest("https://tmw2.org/manalauncher/versions.json?"+Date.now()); + downloadURL = versions.windows64.file; + } catch (e){ + console.log(e); + throw new Error("Download Url fetching error"); + } Status.setProgress(-1); - const downloadURL = 'https://[URL]/Mana.zip'; const updateDestination:string = `${app.getPath('userData')}\\temp\\update.zip`; try { await download(downloadURL, updateDestination, (state:ProgressType) => { Status.setProgress(Math.floor(state.percent*100)); - Status.setActivity(`Downloading ManaPlus... ${state.speed}`); + const speed = Math.floor(Math.floor(state.speed)/1024); + Status.setActivity(`Downloading ManaPlus... ${speed} KiB/s`); console.log(state); }); } catch (e){ console.log(e); - + throw new Error("Download error"); } Status.setProgress(500); // Backup old files @@ -95,7 +116,17 @@ export class ManaPlusAppWindows implements ManaPlusApp { return 0; } - updateAvailible(): Promise<{ isNewVersion: boolean; newestVersion: string; }> { - throw new Error("Method not implemented."); + async updateAvailable(): Promise<{ isNewVersion: boolean; newestVersion: string; }> { + try { + let versions = await getRequest("https://tmw2.org/manalauncher/versions.json?"+Date.now()); + let currect_version = await this.isInstalled ? await this.getVersion(): "-"; + return { + isNewVersion:currect_version.indexOf(versions.windows64.version) === -1, + newestVersion:versions.windows64.version + }; + }catch (e){ + throw e; + } + } } diff --git a/src/main/manaplus/manaplus.ts b/src/main/manaplus/manaplus.ts index da8d6d5..590b9f4 100644 --- a/src/main/manaplus/manaplus.ts +++ b/src/main/manaplus/manaplus.ts @@ -3,7 +3,7 @@ import { ManaPlusAppWindows } from './ManaApp/windows'; import * as os from 'os'; import * as path from 'path'; import * as fs from 'fs-extra'; -import { app, ipcMain, shell } from 'electron'; +import { app, ipcMain, shell, dialog } from 'electron'; import { Status, EventEmitter } from '../status'; let ManaPlusInstance:ManaPlusApp; @@ -39,6 +39,7 @@ export namespace ManaPlus{ let params:string[]; try { params = await makeParams(args.address,args.port,args.engine,args.username,args.password); + //params = ['-v'];// DEBUG: This option is to disable manaplus for testing (open it just for getting the version) } catch (e){ console.log(e); Status.showError("Launch Preperation Failed", e.message, "Launch preparation failed") @@ -46,10 +47,20 @@ export namespace ManaPlus{ } let willUpdate:boolean=false; // check if it is installed - willUpdate = !ManaPlusInstance.isInstalled(); - //Check if update is availible and ask if the user wants to update - //TODO - + if(ManaPlusInstance.isInstalled()){ + // Check if update is available and ask if the user wants to update + try { + let version = await ManaPlusInstance.updateAvailable(); + willUpdate = (version.isNewVersion && await updateDialog(version.newestVersion)); + } catch (e){ + console.log(e); + Status.showError("Launch Preperation Failed", e.message, "Launch preparation failed") + return; + } + } else { + // Not installed will install it + willUpdate = true; + } // Install/Update the gameclient if needed if(willUpdate){ await update(); @@ -119,7 +130,7 @@ async function makeParams(server: any,port: any,engine: any,username?: any,passw "-L", localDataDir, "--screenshot-dir", screenshotsDir ]); - console.log(parameters); + //console.log(parameters); return parameters; } @@ -130,7 +141,7 @@ function runManaProgram(parameters: string[],gameExe:string): Promise { return new Promise((resolve, reject)=>{ Status.setActivity(`Starting ManaPlus`); - console.log(gameExe, parameters); + //console.log(gameExe, parameters); const child = require('child_process').execFile(gameExe, parameters, function(err:Error, data:any) { console.log(err); console.log(data.toString()); @@ -153,3 +164,23 @@ EventEmitter.on("Mana:killMana",()=>{ if(manaplusInstance && manaplusInstance !== null) manaplusInstance.kill('SIGINT'); }); + +async function updateDialog(newestVersion:string):Promise{ + const options = { + type: 'info', + title: `ManaPlus Update available: ${newestVersion}`, + message: "Do you want to update now? Please note that you'll have to update eventually to ensure further compability with the servers", + buttons: ['Nah, LATER', 'Update NOW'] + } + let dialog = await dialogPromiseWrapper(options); + console.log("....",dialog); + return dialog === 1; +} + +function dialogPromiseWrapper(options:any):Promise{ + return new Promise((res, rej) => { + dialog.showMessageBox(options, (index) => { + res(index); + }) + }); +} diff --git a/src/main/richpresence.ts b/src/main/richpresence.ts index f39341b..4ef8f42 100644 --- a/src/main/richpresence.ts +++ b/src/main/richpresence.ts @@ -20,7 +20,7 @@ async function setActivity() { // smallImageKey: 'snek_small', // smallImageText: 'i am my own pillows', partySize:2, //TODO get online players - partyMax:100, + partyMax:999, partyId:"ae488379-351d-4a4f-ad32-2b9b01c91657", joinSecret:"MTI4NzM0OjFpMmhuZToxMjMxMjM=" }); diff --git a/src/main/util/webrequest.ts b/src/main/util/webrequest.ts new file mode 100644 index 0000000..aba8bfd --- /dev/null +++ b/src/main/util/webrequest.ts @@ -0,0 +1,35 @@ +import * as https from 'https'; +import * as http from 'http'; + +export function getRequest(url:string):Promise { + const webrequest:any = url.indexOf('https') !== -1 ? https:http; + const t1 = Date.now(); + return new Promise((resolve, reject) => { + webrequest.get(url, (res:any) => { + const { statusCode } = res; + + let error; + if (statusCode !== 200) { + error = new Error('Request Failed.\n' + + `Status Code: ${statusCode}`); + } + if (error) { + res.resume(); + reject(error); + } else { + res.setEncoding('utf8'); + let rawData = ''; + res.on('data', (chunk:any) => { rawData += chunk; }); + res.on('end', () => { + try { + resolve(JSON.parse(rawData)); + } catch (e) { + reject(e); + } + }); + } + }).on('error', (e:Error) => { + reject(e); + }); + }); +} diff --git a/src/renderer/gameserver/data.ts b/src/renderer/gameserver/data.ts index 570fdb2..92cf250 100644 --- a/src/renderer/gameserver/data.ts +++ b/src/renderer/gameserver/data.ts @@ -13,7 +13,7 @@ const TMW2 = new GameServer( "TMW2" ), "TMW2", - "TMW2: Monster Wars", + "TMW2: Moubootaur Legends", "What happens if you mix TMW with Evol and add many new Ideas", "https://tmw2.org/news", {url:"http://updates.tmw2.org/news.txt",type:NewsType.ManaPlus}, -- cgit v1.2.3-70-g09d2