summaryrefslogtreecommitdiff
path: root/src/main/manaplus/manaApp/windows.ts
blob: b5cde159c587ee55b6ec10df92878226473bb78b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { ManaPlusApp } from './manaApp.interface';
import { app } from 'electron';
import * as fs from 'fs-extra';
import { Status } from '../../status';
import {download, Progress as ProgressType} from '../../util/downloader';
import * as extract from 'extract-zip';


export class ManaPlusAppWindows implements ManaPlusApp {
  private path: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;
  }
	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\\";
	}
	getVersion(): Promise<string> {
		throw new Error("Not Implemented yet");
	}
  isInstalled(): boolean {
		return fs.existsSync(this.path+"\\Mana\\manaplus.exe");
	}

  async update() {
    fs.existsSync(app.getPath('userData')+"\\temp") || fs.mkdirSync(app.getPath('userData')+"\\temp");
		// Get Update URL
    Status.setProgress(500);
    Status.setActivity("Fetching Download URL");
    //// TODO: Fetch update url
    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}`);
        console.log(state);
      });
    } catch (e){
      console.log(e);

    }
    Status.setProgress(500);
    // Backup old files
    Status.setActivity(`Backup Old version`);
    try {
      await fs.remove(this.path+'\\Mana2')
      if(fs.existsSync(this.path+'\\Mana'))
        await fs.move(this.path+'\\Mana', this.path+'\\Mana2');
      console.log("Backup old version done.");
    } catch (err) {
      Status.showError("Backup old version Failed", err.message, `Backup old version Failed: ${err.message}`);
      throw new Error("Backup error");
    }

    Status.setProgress(500);
    Status.setActivity(`ManaPlus download completed. Unziping..`);
    const extraction = new Promise((resolve, reject)=>{
      extract(updateDestination, {dir: this.path }, function (err) {
        if(err){
          console.log(err);
          Status.showError("ManaPlus unziping failed", err.message, `Unzip Failed: ${err.message}`);
          reject(new Error("Extraction Error"));
        }
        resolve();
      });
    });
    await extraction;

    Status.setActivity('Unziping completed');

    //IDEA: Check Integrity of gamefiles


    // DELETE Old File and remove update file
    Status.setActivity('Cleaning up (Deleting update files)');
    try {
      await fs.remove(this.path+'\\Mana2')
      await fs.remove(updateDestination)
      console.log('Cleanup done');
    } catch (err) {
      console.error(err);
      Status.showError("Clean up Failed", "Please remove '"+updateDestination+"' and '"+'this.path'+'\\Mana2'+"' manualy", `Unzip Failed: ${err.message}`);
    }
    Status.setProgress(-1);
    //Status.showError("Unziping isn't implemented yet", "WIP", "not further implemented")
    Status.setActivity('Update successfull');

    return 0;
	}
	updateAvailible(): Promise<{ isNewVersion: boolean; newestVersion: string; }> {
		throw new Error("Method not implemented.");
	}
}