Commit 6598a640 authored by wudizhanche1000's avatar wudizhanche1000

导入游戏

parent fe5dec80
...@@ -102,7 +102,7 @@ export class AppDetailComponent implements OnInit { ...@@ -102,7 +102,7 @@ export class AppDetailComponent implements OnInit {
try { try {
await this.appsService.install(targetApp, options); await this.appsService.install(targetApp, options);
for (let [id,install] of Object.entries(referencesInstall)) { for (let [id, install] of Object.entries(referencesInstall)) {
if (install) { if (install) {
let reference = targetApp.references.get(id)!; let reference = targetApp.references.get(id)!;
console.log("reference install ", id, targetApp, targetApp.references, reference); console.log("reference install ", id, targetApp, targetApp.references, reference);
...@@ -147,10 +147,18 @@ export class AppDetailComponent implements OnInit { ...@@ -147,10 +147,18 @@ export class AppDetailComponent implements OnInit {
this.appsService.runApp(app, 'custom'); this.appsService.runApp(app, 'custom');
} }
importGame(targetApp: App, options: InstallOption, referencesInstall: {[id: string]: boolean}) { async importGame(targetApp: App, option: InstallOption, referencesInstall: {[id: string]: boolean}) {
let dir = path.basename(this.import_path); $('#import-modal').modal('hide');
let dir = path.dirname(this.import_path);
// TODO: 执行依赖和references安装 // TODO: 执行依赖和references安装
this.appsService.importApp(targetApp, dir); await this.appsService.importApp(targetApp, dir, option);
for (let [id, install] of Object.entries(referencesInstall)) {
if (install) {
let reference = targetApp.references.get(id)!;
console.log("reference install ", id, targetApp, targetApp.references, reference);
await this.appsService.install(reference, option);
}
}
} }
async verifyFiles(app: App) { async verifyFiles(app: App) {
...@@ -186,7 +194,7 @@ export class AppDetailComponent implements OnInit { ...@@ -186,7 +194,7 @@ export class AppDetailComponent implements OnInit {
let filePaths = await new Promise((resolve, reject) => { let filePaths = await new Promise((resolve, reject) => {
remote.dialog.showOpenDialog({ remote.dialog.showOpenDialog({
filters: [{name: filename, extensions: [extname]}], filters: [{name: filename, extensions: [extname]}],
properties: ['openFile', 'openDirectory', 'multiSelections'] properties: ['openFile',]
}, resolve) }, resolve)
}); });
......
...@@ -19,6 +19,8 @@ import {Observable, Observer} from "rxjs/Rx"; ...@@ -19,6 +19,8 @@ import {Observable, Observer} from "rxjs/Rx";
import Timer = NodeJS.Timer; import Timer = NodeJS.Timer;
// import mkdirp = require("mkdirp"); // import mkdirp = require("mkdirp");
import ReadableStream = NodeJS.ReadableStream; import ReadableStream = NodeJS.ReadableStream;
import {createReadStream} from "fs";
import {createWriteStream} from "fs";
const Aria2 = require('aria2'); const Aria2 = require('aria2');
const sudo = require('electron-sudo'); const sudo = require('electron-sudo');
...@@ -212,7 +214,7 @@ export class AppsService { ...@@ -212,7 +214,7 @@ export class AppsService {
// 设置App关系 // 设置App关系
for (let [id,app] of apps) { for (let [id, app] of apps) {
let temp = app.actions; let temp = app.actions;
let map = new Map<string,any>(); let map = new Map<string,any>();
for (let action of Object.keys(temp)) { for (let action of Object.keys(temp)) {
...@@ -268,12 +270,59 @@ export class AppsService { ...@@ -268,12 +270,59 @@ export class AppsService {
this.findChildren(app).every((child) => (child.isInstalled() && child.isReady()) || !child.isInstalled()); this.findChildren(app).every((child) => (child.isInstalled() && child.isReady()) || !child.isInstalled());
} }
async importApp(app: App, appPath: string) { async copyFile(src: string, dst: string): Promise<any> {
return new Promise((resolve, reject) => {
let readable = createReadStream(src);
readable.on('open', () => {
let writable = createWriteStream(dst);
writable.on("error", reject);
writable.on("close", resolve);
readable.pipe(writable);
});
readable.on("error", reject);
});
}
async importApp(app: App, appPath: string, option: InstallOption) {
if (!app.isInstalled()) { if (!app.isInstalled()) {
app.status.status = "ready"; app.status.status = "updating";
let checksumFiles = await this.getChecksumFile(app);
await this.createDirectory(option.installDir);
await new Promise((resolve, reject) => {
this.ngZone.runOutsideAngular(async() => {
try {
let sortedFiles = Array.from(checksumFiles.entries()).sort((a: string[], b: string[]): number => {
if (a[0] > b[0]) {
return 1;
} else if (a[0] < b[0]) {
return -1;
} else {
return 0;
}
});
for (let [file, checksum] of sortedFiles) {
let src = path.join(appPath, file);
let dst = path.join(option.installDir, file);
if (checksum === "") {
await this.createDirectory(dst);
} else {
try {
await this.copyFile(src, dst);
} catch (e) {
}
}
}
resolve();
} catch (e) {
reject(e);
}
});
});
app.local = new AppLocal(); app.local = new AppLocal();
app.local.path = appPath; app.local.path = option.installDir;
app.status.status = "ready";
await this.update(app, true); await this.update(app, true);
this.saveAppLocal(app);
} }
} }
...@@ -299,7 +348,7 @@ export class AppsService { ...@@ -299,7 +348,7 @@ export class AppsService {
async verifyFiles(app: App, checksumFiles: Map<string,string>): Promise<Map<string,string>> { async verifyFiles(app: App, checksumFiles: Map<string,string>): Promise<Map<string,string>> {
let result = new Map<string,string>(); let result = new Map<string,string>();
for (let [file,checksum] of checksumFiles) { for (let [file, checksum] of checksumFiles) {
let filePath = path.join(app.local!.path, file); let filePath = path.join(app.local!.path, file);
// 如果文件不存在,随便生成一个checksum // 如果文件不存在,随便生成一个checksum
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
...@@ -356,7 +405,7 @@ export class AppsService { ...@@ -356,7 +405,7 @@ export class AppsService {
let changedFiles: Set<string> = new Set<string>(); let changedFiles: Set<string> = new Set<string>();
let deletedFiles: Set<string> = new Set<string>(); let deletedFiles: Set<string> = new Set<string>();
// 遍历寻找新增加的文件 // 遍历寻找新增加的文件
for (let [file,checksum] of latestFiles) { for (let [file, checksum] of latestFiles) {
if (checksum !== "" && !localFiles!.has(file)) { if (checksum !== "" && !localFiles!.has(file)) {
addedFiles.add(file); addedFiles.add(file);
// changedFiles包含addedFiles,addedFiles仅供mod更新的时候使用。 // changedFiles包含addedFiles,addedFiles仅供mod更新的时候使用。
...@@ -366,7 +415,7 @@ export class AppsService { ...@@ -366,7 +415,7 @@ export class AppsService {
} }
} }
// 遍历寻找旧版本与新版本不一样的文件和新版本比旧版少了的文件 // 遍历寻找旧版本与新版本不一样的文件和新版本比旧版少了的文件
for (let [file,checksum] of localFiles!) { for (let [file, checksum] of localFiles!) {
if (latestFiles.has(file)) { if (latestFiles.has(file)) {
let latestChecksum = latestFiles.get(file); let latestChecksum = latestFiles.get(file);
if (latestChecksum !== checksum && latestChecksum !== "") { if (latestChecksum !== checksum && latestChecksum !== "") {
...@@ -507,7 +556,7 @@ export class AppsService { ...@@ -507,7 +556,7 @@ export class AppsService {
} }
await this.doInstall(task); await this.doInstall(task);
}; };
const addDownloadTask = async(app: App, dir: string): Promise<{app: App,files: string[]} > => { const addDownloadTask = async(app: App, dir: string): Promise<{app: App, files: string[]} > => {
let metalinkUrl = app.download; let metalinkUrl = app.download;
if (app.id === "ygopro") { if (app.id === "ygopro") {
metalinkUrl = "https://thief.mycard.moe/metalinks/ygopro-" + process.platform + ".meta4"; metalinkUrl = "https://thief.mycard.moe/metalinks/ygopro-" + process.platform + ".meta4";
...@@ -569,7 +618,7 @@ export class AppsService { ...@@ -569,7 +618,7 @@ export class AppsService {
findChildren(app: App): App[] { findChildren(app: App): App[] {
let children: App[] = []; let children: App[] = [];
for (let [id,child] of this.apps) { for (let [id, child] of this.apps) {
if (child.parent === app || child.dependencies && child.dependencies.has(app.id)) { if (child.parent === app || child.dependencies && child.dependencies.has(app.id)) {
children.push(child); children.push(child);
} }
...@@ -961,7 +1010,7 @@ export class AppsService { ...@@ -961,7 +1010,7 @@ export class AppsService {
} }
} }
async backupFiles(dir: string, backupDir: string, files: Iterable<string>, callback?: (progress: number)=>void) { async backupFiles(dir: string, backupDir: string, files: Iterable<string>, callback?: (progress: number) => void) {
let n = 0; let n = 0;
for (let file of files) { for (let file of files) {
await new Promise(async(resolve, reject) => { await new Promise(async(resolve, reject) => {
...@@ -979,7 +1028,7 @@ export class AppsService { ...@@ -979,7 +1028,7 @@ export class AppsService {
} }
} }
async restoreFiles(dir: string, backupDir: string, files: Iterable<string>, callback?: (progress: number)=>{}) { async restoreFiles(dir: string, backupDir: string, files: Iterable<string>, callback?: (progress: number) => {}) {
let n = 0; let n = 0;
for (let file of files) { for (let file of files) {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
...@@ -1006,7 +1055,7 @@ export class AppsService { ...@@ -1006,7 +1055,7 @@ export class AppsService {
let map = new Map<string,string>(); let map = new Map<string,string>();
for (let line of response.text().split('\n')) { for (let line of response.text().split('\n')) {
if (line !== "") { if (line !== "") {
let [checksum,filename]=line.split(' ', 2); let [checksum, filename]=line.split(' ', 2);
// checksum文件里没有文件夹,这里添加上 // checksum文件里没有文件夹,这里添加上
map.set(path.dirname(filename), ""); map.set(path.dirname(filename), "");
map.set(filename, checksum); map.set(filename, checksum);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment