Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zh99998 committed Dec 5, 2016
2 parents 7168416 + a20ada6 commit 73ceaaf
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 169 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/bin/
/app/*.js
/app/*.js.map
/app/**/*.js
/app/**/*.js.map
/app/*.metadata.json
/node_modules/
/dist/
Expand Down
22 changes: 13 additions & 9 deletions app/app-detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>{{currentApp.name}}</h1>
<div>
<span *ngIf="currentApp.isDownloading()">正在下载</span>
<span *ngIf="currentApp.isInstalling()">正在安装...</span>
<span *ngIf="currentApp.isUninstalling()">正在卸载...</span>
<span *ngIf="currentApp.isWaiting()">等待安装...</span>
<span *ngIf="currentApp.status.total">{{(currentApp.status.progress/currentApp.status.total * 100).toFixed()}}%</span>
<span>{{currentApp.progressMessage()}}</span>
Expand Down Expand Up @@ -59,13 +60,15 @@ <h3>{{item.title}}</h3>
<tr *ngFor="let mod of mods; let i = index">
<th scope="row">{{i + 1}}</th>
<td>{{mod.name}}</td>
<td *ngIf="mod.isInstalled()">
<td *ngIf="mod.isReady()">
<button i18n type="button" (click)="uninstall(mod)" class="btn btn-danger btn-sm">卸载</button>
</td>
<td *ngIf="!mod.isInstalled()">
<button i18n (click)="installMod(mod)" type="button" *ngIf="mod.status.status==='init'" class="btn btn-primary btn-sm">安装</button>
<progress *ngIf="mod.status.status==='downloading'" class="progress progress-striped progress-animated" value="{{mod.status.progress}}" max="{{mod.status.total}}"></progress>
<div i18n *ngIf="mod.status.status==='waiting'">等待安装...</div>
<button i18n (click)="installMod(mod)" type="button" *ngIf="!mod.isInstalled()" class="btn btn-primary btn-sm">安装</button>
</td>
<td *ngIf="mod.isInstalled()&&!mod.isReady()">
<progress class="progress progress-striped progress-animated" value="{{mod.status.progress}}" max="{{mod.status.total}}"></progress>
<!--<div i18n *ngIf="mod.isWaiting()">等待安装...</div>-->
</td>
</tr>
</tbody>
Expand All @@ -91,8 +94,9 @@ <h4 i18n class="modal-title" id="myModalLabel">安装 {{currentApp.name}}</h4>
<p i18n>即将开始安装 {{currentApp.name}}</p>
<h4 i18n>安装位置</h4>
<div class="form-group">
<select class="form-control" name="installPath" [(ngModel)]="installOption.installLibrary" title="path">
<select class="form-control" name="installPath" (change)="selectLibrary()" [(ngModel)]="installOption.installLibrary" title="path">
<option *ngFor="let library of libraries" value="{{library}}"> {{library}}</option>
<option *ngFor="let library of availableLibraries" value="create_{{library}}">在 {{library}}\ 盘新建 MyCard 库</option>
</select></div>
<h4 i18n>快捷方式</h4>
<div class="checkbox">
Expand All @@ -104,10 +108,10 @@ <h4 i18n>快捷方式</h4>
<input id="create_desktop_shortcut" type="checkbox" name="desktop" [(ngModel)]="installOption.createDesktopShortcut">
<label i18n for="create_desktop_shortcut">创建桌面快捷方式</label>
</div>
<!--<h4 *ngIf="installOption.references.length">扩展内容</h4>-->
<!--<div *ngFor="let reference of installOption.references"><label>-->
<!--<input type="checkbox" [(ngModel)]="reference.install" name="references" value="{{reference.app.id}}"> {{reference.app.name}}-->
<!--</label></div>-->
<h4 *ngIf="references.length>0">扩展内容</h4>
<div *ngFor="let reference of references"><label>
<input type="checkbox" [(ngModel)]="referencesInstall[reference.id]" name="references"> {{reference.name}}
</label></div>
<div *ngIf="currentApp.findDependencies().length">
<span i18n>依赖:</span>
<span class="dependency" *ngFor="let dependency of currentApp.findDependencies()">{{dependency.name}}</span>
Expand Down
87 changes: 71 additions & 16 deletions app/app-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {App} from "./app";
import {DownloadService} from "./download.service";
import {clipboard, remote} from "electron";
import * as path from "path";
import * as fs from 'fs';
import {InstallService} from "./install.service";
import mkdirp = require("mkdirp");

declare const Notification: any;
declare const $: any;
Expand All @@ -23,22 +25,61 @@ export class AppDetailComponent implements OnInit {
platform = process.platform;

installOption: InstallOption;
availableLibraries: string[] = [];
references: App[];
referencesInstall: {[id: string]: boolean};

constructor(private appsService: AppsService, private settingsService: SettingsService,
private downloadService: DownloadService, private installService: InstallService,
private ref: ChangeDetectorRef) {
}

// public File[] listRoots() {
// int ds = listRoots0();
// int n = 0;
// for (int i = 0; i < 26; i++) {
// if (((ds >> i) & 1) != 0) {
// if (!access((char)('A' + i) + ":" + slash))
// ds &= ~(1 << i);
// else
// n++;
// }
// }
// File[] fs = new File[n];
// int j = 0;
// char slash = this.slash;
// for (int i = 0; i < 26; i++) {
// if (((ds >> i) & 1) != 0)
// fs[j++] = new File((char)('A' + i) + ":" + slash);
// }
// return fs;
// }
ngOnInit() {
let volume = 'A';
for (let i = 0; i < 26; i++) {
new Promise((resolve, reject) => {
let currentVolume = String.fromCharCode(volume.charCodeAt(0) + i) + ":";
fs.access(currentVolume, (err) => {
if (!err) {
//判断是否已经存在Library
if (this.libraries.every((library) => !library.startsWith(currentVolume))) {
this.availableLibraries.push(currentVolume);
}
}
})
})
}
}

updateInstallOption(app: App) {
this.installOption = new InstallOption(app);
this.installOption.installLibrary = this.settingsService.getDefaultLibrary().path;
// this.installOption.references = [];
// for (let reference of app.references.values()) {
// this.installOption.references.push(new InstallOption(reference))
// }
this.references = Array.from(app.references.values());
console.log(this.references);
this.referencesInstall = {};
for (let reference of this.references) {
this.referencesInstall[reference.id] = true;
}
}

get libraries(): string[] {
Expand Down Expand Up @@ -71,25 +112,39 @@ export class AppDetailComponent implements OnInit {

let options = this.installOption;

// if (options) {
// for (let reference of options.references) {
// if (reference.install && !reference.app.isInstalled()) {
// apps.push(reference.app);
// apps.push(...reference.app.findDependencies().filter((app) => {
// return !app.isInstalled()
// }))
// }
// }
// }

try {
this.appsService.install(this.currentApp, options);
await this.appsService.install(targetApp, options);
if (this.references.length > 0) {
for (let [id,isInstalled] of Object.entries(this.referencesInstall)) {
if (isInstalled) {
let reference = targetApp.references.get(id)!;
await this.appsService.install(reference, options);
}
}
}
} catch (e) {
console.error(e);
new Notification(targetApp.name, {body: "下载失败"});
}
}

async selectLibrary() {
if (this.installOption.installLibrary.startsWith('create_')) {
let volume = this.installOption.installLibrary.slice(7);
let library = path.join(volume, "MyCardLibrary");
try {
await this.installService.createDirectory(library);
this.installOption.installLibrary = library;
this.settingsService.addLibrary(library, true);
} catch (e) {
this.installOption.installLibrary = this.settingsService.getDefaultLibrary().path;
alert("无法创建指定目录");
}
} else {
this.settingsService.setDefaultLibrary({path: this.installOption.installLibrary, "default": true})
}
}

selectDir() {
let dir = remote.dialog.showOpenDialog({properties: ['openFile', 'openDirectory']});
console.log(dir);
Expand Down
3 changes: 3 additions & 0 deletions app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export class App {
isDownloading(): boolean {
return this.status.status === "downloading";
}
isUninstalling():boolean{
return this.status.status==="uninstalling";
}

runable(): boolean {
return [Category.game].includes(this.category);
Expand Down
127 changes: 95 additions & 32 deletions app/apps.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {remote} from "electron";
import "rxjs/Rx";
import {AppLocal} from "./app-local";
import * as ini from "ini";
import Timer = NodeJS.Timer;
import {DownloadService} from "./download.service";
import {InstallOption} from "./install-option";
import {InstallService} from "./install.service";
import Timer = NodeJS.Timer;
import {ComparableSet} from "./shared/ComparableSet";

const Aria2 = require('aria2');
const sudo = require('electron-sudo');
Expand Down Expand Up @@ -116,6 +117,62 @@ export class AppsService {
return apps;
};

// async update(app: App) {
// const updateServer = "https://thief.mycard.moe/update/metalinks/";
//
// if (app.isReady() && app.local!.version != app.version) {
// let checksumMap = await this.installService.getChecksumFile(app)
//
// let latestFiles = new ComparableSet();
//
// }
//
// if (app.isInstalled() && app.version != (<AppLocal>app.local).version) {
// let checksumMap = await this.installService.getChecksumFile(app);
// let filesMap = (<AppLocal>app.local).files;
// let deleteList: string[] = [];
// let addList: string[] = [];
// let changeList: string[] = [];
// for (let [file,checksum] of filesMap) {
// let t = checksumMap.get(file);
// if (!t) {
// deleteList.push(file);
// } else if (t !== checksum) {
// changeList.push(file);
// }
// }
// for (let file of checksumMap.keys()) {
// if (!filesMap.has(file)) {
// changeList.push(file);
// }
// }
// let metalink = await this.http.post(updateServer + app.id, changeList).map((response) => response.text())
// .toPromise();
// let meta = new DOMParser().parseFromString(metalink, "text/xml");
// let filename = meta.getElementsByTagName('file')[0].getAttribute('name');
// let dir = path.join(path.dirname((<AppLocal>app.local).path), "downloading");
// let a = await this.downloadService.addMetalink(metalink, dir);
//
// for (let file of deleteList) {
// await this.installService.deleteFile(file);
// }
// (<AppLocal>app.local).version = app.version;
// (<AppLocal>app.local).files = checksumMap;
// localStorage.setItem(app.id, JSON.stringify(app.local));
// await this.installService.extract(path.join(dir, filename), (<AppLocal>app.local).path);
// let children = this.appsService.findChildren(app);
// for (let child of children) {
// if (child.isInstalled()) {
// await this.installService.uninstall(child, false);
// // this.installService.add(child, new InstallOption(child, path.dirname(((<AppLocal>app.local).path))));
// await this.installService.getComplete(child);
// console.log("282828")
// }
// }
//
// }
// }

async install(app: App, option: InstallOption) {
const addDownloadTask = async(app: App, dir: string) => {
let metalinkUrl = app.download;
Expand Down Expand Up @@ -158,27 +215,30 @@ export class AppsService {
})
});
};
try {
let apps: App[] = [];
let dependencies = app.findDependencies();
apps.push(...dependencies, app);
let downloadPath = path.join(option.installLibrary, 'downloading');
let tasks: Promise<any>[] = [];
for (let a of apps) {
tasks.push(addDownloadTask(a, downloadPath));
}
let downloadResults = await Promise.all(tasks);
for (let result of downloadResults) {
console.log(result);
let o = new InstallOption(result.app, option.installLibrary);
o.downloadFiles = result.files;
this.installService.push({app: result.app, option: o});
if (!app.isInstalled()) {
try {
let apps: App[] = [];
let dependencies = app.findDependencies().filter((dependency) => {
return !dependency.isInstalled();
});
apps.push(...dependencies, app);
let downloadPath = path.join(option.installLibrary, 'downloading');
let tasks: Promise<any>[] = [];
for (let a of apps) {
tasks.push(addDownloadTask(a, downloadPath));
}
let downloadResults = await Promise.all(tasks);
for (let result of downloadResults) {
console.log(result);
let o = new InstallOption(result.app, option.installLibrary);
o.downloadFiles = result.files;
this.installService.push({app: result.app, option: o});
}
} catch (e) {
app.status.status = 'init';
console.log(e);
throw e;
}
// this.installService.push({app: app, option: option})

} catch (e) {
console.log(e);
throw e;
}
}

Expand Down Expand Up @@ -231,12 +291,12 @@ export class AppsService {
}

if (action.open) {
let np2 = <App>action.open;
let np2 = action.open;
let openAction: Action;
openAction = <Action>np2.actions.get('main');
let openPath = (<AppLocal>np2.local).path;
openAction = np2.actions.get('main')!;
let openPath = np2.local!.path;
if (action.open.id == 'np2fmgen') {
const config_file = path.join((<AppLocal>(<App>action.open).local).path, 'np21nt.ini');
const config_file = path.join(action.open!.local!.path, 'np21nt.ini');
let config = await new Promise((resolve, reject) => {
fs.readFile(config_file, {encoding: 'utf-8'}, (error, data) => {
if (error) return reject(error);
Expand All @@ -252,7 +312,7 @@ export class AppsService {
windtype: '0'
};
config['NekoProject21'] = Object.assign({}, default_config, config['NekoProject21']);
config['NekoProject21']['HDD1FILE'] = path.win32.join(process.platform == 'win32' ? '' : 'Z:', (<AppLocal>app.local).path, action.execute);
config['NekoProject21']['HDD1FILE'] = path.win32.join(process.platform == 'win32' ? '' : 'Z:', app.local!.path, action.execute);
await new Promise((resolve, reject) => {
fs.writeFile(config_file, ini.stringify(config), (error) => {
if (error) {
Expand All @@ -262,12 +322,15 @@ export class AppsService {
}
})
});
args.push(openAction.execute);
args = args.concat(openAction.args);
let wine = <App>openAction.open;
openPath = (<AppLocal>wine.local).path;
openAction = <Action>(<App>openAction.open).actions.get('main');
cwd = (<AppLocal>np2.local).path;

if (process.platform != 'win32') {
args.push(openAction.execute);
args = args.concat(openAction.args);
let wine = openAction.open!;
openPath = wine.local!.path;
openAction = openAction!.open!.actions.get('main')!;
}
cwd = np2.local!.path;
}
args = args.concat(openAction.args);
args.push(action.execute);
Expand Down
10 changes: 6 additions & 4 deletions app/download.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ export class DownloadService {
let newDownloadSpeed = 0;
for (let [index,gid] of gidList.entries()) {
let task = this.taskList.get(gid)!;
statusList[index] = task.status;
newCompletedLength += parseInt(task.completedLength);
newTotalLength += parseInt(task.totalLength);
newDownloadSpeed += parseInt(task.downloadSpeed);
if (task) {
statusList[index] = task.status;
newCompletedLength += parseInt(task.completedLength);
newTotalLength += parseInt(task.totalLength);
newDownloadSpeed += parseInt(task.downloadSpeed);
}
}
if (newCompletedLength !== completedLength || newTotalLength !== totalLength) {
completedLength = newCompletedLength;
Expand Down
Loading

0 comments on commit 73ceaaf

Please sign in to comment.