Commit 2fe2094d authored by nanahira's avatar nanahira

rework app open

parent 5a2e9e54
import { AppLocal } from './app-local'; import { AppLocal } from './app-local';
import * as path from 'path';
import * as ini from 'ini';
import * as fs from 'fs';
import * as child_process from 'child_process';
export enum Category { export enum Category {
game, game,
...@@ -21,13 +25,21 @@ export enum Category { ...@@ -21,13 +25,21 @@ export enum Category {
// uninstalling, // uninstalling,
// waiting, // waiting,
// } // }
export interface Action { export interface BaseAction {
interpreter?: string;
execute: string; execute: string;
args: string[]; args: string[];
env: {}; env: {};
}
export interface Action extends BaseAction {
interpreter?: string;
open?: App; open?: App;
} }
export interface SpawnAction extends BaseAction {
cwd?: string;
}
export class FileOptions { export class FileOptions {
sync: boolean; sync: boolean;
ignore: boolean; ignore: boolean;
...@@ -214,4 +226,103 @@ export class App { ...@@ -214,4 +226,103 @@ export class App {
return dependencies.every((dependency) => dependency.isReady()); return dependencies.every((dependency) => dependency.isReady());
} }
async getSpawnAction(children: App[], action_name = 'main', referencedApp?: App, referencedAction?: Action, cwd?: string): Promise<SpawnAction> {
const appCwd = (<AppLocal>this.local).path;
if(!cwd) {
cwd = appCwd;
}
if (this.id === 'np2fmgen') {
const config_file = path.join(this.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);
}
resolve(ini.parse(data));
});
});
const default_config = {
clk_mult: '48',
DIPswtch: '3e f3 7b',
SampleHz: '44100',
Latencys: '100',
MIX_TYPE: 'true',
windtype: '0'
};
config['NekoProject21'] = Object.assign({}, default_config, config['NekoProject21']);
config['NekoProject21']['HDD1FILE'] =
path.win32.join(process.platform === 'win32' ? '' : 'Z:', referencedApp.local!.path, referencedAction.execute);
config['NekoProject21']['fontfile'] =
path.win32.join(process.platform === 'win32' ? '' : 'Z:', referencedApp.local!.path, 'font.bmp');
await new Promise((resolve, reject) => {
fs.writeFile(config_file, ini.stringify(config), (error) => {
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
cwd = appCwd;
}
let action: Action = <Action>this.actions.get(action_name);
let args: string[] = [];
let env = Object.assign({}, process.env);
for (let child of children) {
if (child.isInstalled()) {
let _action = child.actions.get(action_name);
if (_action) {
action = _action;
}
}
}
let execute: string;
const appExecute = path.join(cwd, action.execute);
if (action.interpreter) {
execute = action.interpreter;
args.push(appExecute);
} else {
execute = appExecute;
}
if (action.open) {
const np2 = action.open;
const openAction = await np2.getSpawnAction([], 'main', this, action, cwd);
args = args.concat(openAction.args);
args.push(action.execute);
execute = openAction.execute;
cwd = openAction.cwd;
}
args = args.concat(action.args);
env = Object.assign(env, action.env);
return {
execute,
args,
env,
cwd
}
}
async spawnApp(children: App[], action_name = 'main') {
if (this.id === 'th123') {
let th105 = <App>this.references.get('th105');
if (th105.isInstalled()) {
console.log(`Reference of th123: ${th105}`);
const config_file = path.join((<AppLocal>this.local).path, 'configex123.ini');
let config = ini.parse(await fs.promises.readFile(config_file, { encoding: 'utf-8' }));
const th105LocalApp = (<AppLocal>th105.local);
const targetTh105Path = th105LocalApp ? th105LocalApp.path : (<AppLocal>this.local).path.replace(/th123/g, 'th105');
config['th105path'] = {path: targetTh105Path};
await fs.promises.writeFile(config_file, ini.stringify(config));
}
}
const appCwd = (<AppLocal>this.local).path;
const {execute, args, env, cwd} = await this.getSpawnAction(children, action_name);
console.log(execute, args, env, cwd, appCwd);
return child_process.spawn(execute, args, {env: env, cwd: cwd || appCwd});
}
} }
...@@ -763,95 +763,8 @@ export class AppsService { ...@@ -763,95 +763,8 @@ export class AppsService {
async runApp(app: App, action_name = 'main') { async runApp(app: App, action_name = 'main') {
let children = this.findChildren(app); let children = this.findChildren(app);
let cwd = (<AppLocal>app.local).path;
let action: Action = <Action>app.actions.get(action_name);
let args: string[] = [];
let env = Object.assign({}, process.env);
for (let child of children) {
if (child.isInstalled()) {
let _action = child.actions.get(action_name);
if (_action) {
action = _action;
}
}
}
let execute: string;
const appExecute = path.join(cwd, action.execute);
if (action.interpreter) {
execute = action.interpreter;
args.push(appExecute)
} else {
execute = appExecute;
}
if (app.id === 'th123') {
let th105 = <App>app.references.get('th105');
if (th105.isInstalled()) {
console.log(`Reference of th123: ${th105}`);
const config_file = path.join((<AppLocal>app.local).path, 'configex123.ini');
let config = ini.parse(await fs.promises.readFile(config_file, { encoding: 'utf-8' }));
const th105LocalApp = (<AppLocal>th105.local);
const targetTh105Path = th105LocalApp ? th105LocalApp.path : (<AppLocal>app.local).path.replace(/th123/g, 'th105');
config['th105path'] = {path: targetTh105Path};
await fs.promises.writeFile(config_file, ini.stringify(config));
}
}
if (action.open) {
let np2 = action.open;
let openAction: Action;
openAction = np2.actions.get('main')!;
let openPath = np2.local!.path;
if (action.open.id === 'np2fmgen') {
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);
}
resolve(ini.parse(data));
});
});
const default_config = {
clk_mult: '48',
DIPswtch: '3e f3 7b',
SampleHz: '44100',
Latencys: '100',
MIX_TYPE: 'true',
windtype: '0'
};
config['NekoProject21'] = Object.assign({}, default_config, config['NekoProject21']);
config['NekoProject21']['HDD1FILE'] =
path.win32.join(process.platform === 'win32' ? '' : 'Z:', app.local!.path, action.execute);
config['NekoProject21']['fontfile'] =
path.win32.join(process.platform === 'win32' ? '' : 'Z:', app.local!.path, 'font.bmp');
await new Promise((resolve, reject) => {
fs.writeFile(config_file, ini.stringify(config), (error) => {
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
if (process.platform !== 'win32') { const handle = await app.spawnApp(children, action_name);
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);
execute = path.join(openPath, openAction.execute);
env = Object.assign(env, openAction.env);
}
args = args.concat(action.args);
env = Object.assign(env, action.env);
console.log(execute, args, env, cwd);
let handle = child_process.spawn(execute, args, {env: env, cwd: cwd});
handle.stdout.on('data', (data) => { handle.stdout.on('data', (data) => {
console.log(`stdout: ${data}`); console.log(`stdout: ${data}`);
......
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