diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/manaplus/manaApp/manaApp.interface.ts | 2 | ||||
-rw-r--r-- | src/main/manaplus/manaApp/windows.ts | 21 | ||||
-rw-r--r-- | src/main/manaplus/manaplus.ts | 61 | ||||
-rw-r--r-- | src/main/status.ts | 15 |
4 files changed, 74 insertions, 25 deletions
diff --git a/src/main/manaplus/manaApp/manaApp.interface.ts b/src/main/manaplus/manaApp/manaApp.interface.ts index f70b26a..4db7c15 100644 --- a/src/main/manaplus/manaApp/manaApp.interface.ts +++ b/src/main/manaplus/manaApp/manaApp.interface.ts @@ -1,5 +1,5 @@ export interface ManaPlusApp { - run(parameters:string[]):void, + readonly startCommand:string, getGameDir():string, getVersion():Promise<string>, isInstalled():boolean, diff --git a/src/main/manaplus/manaApp/windows.ts b/src/main/manaplus/manaApp/windows.ts index b5cde15..92acb7f 100644 --- a/src/main/manaplus/manaApp/windows.ts +++ b/src/main/manaplus/manaApp/windows.ts @@ -8,32 +8,15 @@ import * as extract from 'extract-zip'; export class ManaPlusAppWindows implements ManaPlusApp { private path:string; - + public readonly startCommand:string; constructor(){ const ManaPath = app.getPath('userData')+"\\manaplus"; fs.existsSync(ManaPath) || fs.mkdirSync(ManaPath); fs.existsSync(app.getPath('userData')+"\\temp") || fs.mkdirSync(app.getPath('userData')+"\\temp"); this.path = ManaPath; + this.startCommand = this.path + "\\Mana\\manaplus.exe" } - run(parameters: string[]): void { - Status.setActivity(`Starting ManaPlus`); - const gameExe = this.path+"\\Mana\\manaplus.exe"; - console.log(gameExe, parameters); - const child = require('child_process').execFile(gameExe, parameters, function(err:Error, data:any) { - console.log(err); - console.log(data.toString()); - Status.setActivity(`ManaPlus is running`); - }); - child.on('close', ()=>{ - Status.setPlaying(false); - Status.removeActivity(); - }); - child.on('error', ()=>{ - Status.setPlaying(false); - //// TODO: Handle Error - }); - } getGameDir(): string { return this.path+"\\Mana\\"; } diff --git a/src/main/manaplus/manaplus.ts b/src/main/manaplus/manaplus.ts index b69190d..da8d6d5 100644 --- a/src/main/manaplus/manaplus.ts +++ b/src/main/manaplus/manaplus.ts @@ -3,11 +3,13 @@ import { ManaPlusAppWindows } from './ManaApp/windows'; import * as os from 'os'; import * as path from 'path'; import * as fs from 'fs-extra'; -import { app, ipcMain } from 'electron'; -import { Status } from '../status'; +import { app, ipcMain, shell } from 'electron'; +import { Status, EventEmitter } from '../status'; let ManaPlusInstance:ManaPlusApp; +let CurrentServer:{serverID:string,address:string}=null; + export namespace ManaPlus{ export const ERRORS = { @@ -29,10 +31,14 @@ export namespace ManaPlus{ - export async function start(server: any,port: any,engine: any,username: any,password: any){ + export async function start(args: any){ + CurrentServer={ + serverID:args.serverID, + address:args.address + }; let params:string[]; try { - params = await makeParams(server,port,engine,username,password); + params = await makeParams(args.address,args.port,args.engine,args.username,args.password); } catch (e){ console.log(e); Status.showError("Launch Preperation Failed", e.message, "Launch preparation failed") @@ -50,8 +56,20 @@ export namespace ManaPlus{ } //IDEA have client data updated here to, if needed - ManaPlusInstance.run(params); + + // Run it + Status.setGameRunning(true); + EventEmitter.emit('closeWindow'); + EventEmitter.emit('openTray'); + await runManaProgram(params,ManaPlusInstance.startCommand); + // On Close + Status.setGameRunning(false); + Status.setPlaying(false); + Status.removeActivity(); + EventEmitter.emit('reopenWindow'); + EventEmitter.emit('closeTray'); + CurrentServer=null; } } @@ -66,6 +84,9 @@ ipcMain.on('getScreenshots', (event:any, arg:string)=> { event.sender.send('getScreenshots', {dir:screenshotsDir, screenshots:screenshots.reverse().slice(0, 24)}); }); }); +EventEmitter.on('Mana:openScreenshotDir', ()=>{ + shell.openItem(path.normalize(app.getPath('userData')+`/screenshots/${CurrentServer.address}/`)); +}); function wasInitilized(){ return typeof(ManaPlusInstance) !== "undefined" && typeof(ManaPlusInstance) !== "undefined"; @@ -102,3 +123,33 @@ async function makeParams(server: any,port: any,engine: any,username?: any,passw return parameters; } + +let manaplusInstance: any=null; + +function runManaProgram(parameters: string[],gameExe:string): Promise<any> { + return new Promise((resolve, reject)=>{ + Status.setActivity(`Starting ManaPlus`); + + console.log(gameExe, parameters); + const child = require('child_process').execFile(gameExe, parameters, function(err:Error, data:any) { + console.log(err); + console.log(data.toString()); + Status.setActivity(`ManaPlus is running`); + }); + child.on('close', ()=>{ + manaplusInstance = null; + resolve(); + }); + child.on('error', ()=>{ + manaplusInstance = null; + resolve(); + //// TODO: Handle Error + }); + manaplusInstance = child; + }); +} + +EventEmitter.on("Mana:killMana",()=>{ + if(manaplusInstance && manaplusInstance !== null) + manaplusInstance.kill('SIGINT'); +}); diff --git a/src/main/status.ts b/src/main/status.ts index 18fccd1..fb99b35 100644 --- a/src/main/status.ts +++ b/src/main/status.ts @@ -6,6 +6,7 @@ type STATUS = { activity:string, ActivityIsError:boolean, playing:boolean, + gameRunning:boolean, } const status:STATUS = { @@ -13,9 +14,14 @@ const status:STATUS = { activity:null, playing:false, //Is manaplus starting or started ActivityIsError:false, + gameRunning:false, } export namespace Status { + export function setGameRunning(value: boolean){ + status.gameRunning=value; + updateStatus(); + } export function setProgress(value: number){ status.progress=value; updateStatus(); @@ -50,6 +56,7 @@ export namespace Status { } function updateStatus(){ + if(mainWindow && mainWindow!== null){ if(status.progress==null || status.progress<0) mainWindow.setProgressBar(-1); @@ -59,4 +66,12 @@ function updateStatus(){ mainWindow.setProgressBar(status.progress/100); mainWindow.webContents.send('status-update', status); + } + EventEmitter.emit('status', status); } + +import * as events from 'events'; + +class MyEmitter extends events {} + +export const EventEmitter = new MyEmitter(); |