-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from project-yuki/b0.12
B0.12
- Loading branch information
Showing
22 changed files
with
526 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const debug = require('debug')('yuki:game') | ||
import { EventEmitter } from 'events' | ||
import Hooker from './Hooker' | ||
import { registerProcessExitCallback } from './Win32' | ||
|
||
export default abstract class BaseGame extends EventEmitter { | ||
protected pids: number[] | ||
|
||
constructor () { | ||
super() | ||
this.pids = [] | ||
} | ||
|
||
public abstract start (): void | ||
|
||
public getPids () { | ||
return this.pids | ||
} | ||
|
||
public abstract getInfo (): yuki.Game | ||
|
||
protected afterGetPids () { | ||
this.injectProcessByPid() | ||
this.registerProcessExitCallback() | ||
this.emit('started', this) | ||
} | ||
|
||
private injectProcessByPid () { | ||
this.pids.map((pid) => Hooker.getInstance().injectProcess(pid)) | ||
} | ||
|
||
private registerProcessExitCallback () { | ||
registerProcessExitCallback(this.pids, () => { | ||
this.emit('exited', this) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import BaseGame from './BaseGame' | ||
|
||
export default class GameFromProcess extends BaseGame { | ||
private process: yuki.Process | ||
|
||
constructor (process: yuki.Process) { | ||
super() | ||
this.process = process | ||
this.pids = [process.pid] | ||
} | ||
|
||
public getInfo (): yuki.Game { | ||
return { | ||
name: this.process.name.replace('.exe', ''), | ||
code: '', | ||
path: '', | ||
localeChanger: '' | ||
} | ||
} | ||
|
||
public start () { | ||
this.afterGetPids() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { exec } from 'child_process' | ||
const debug = require('debug')('yuki:processes') | ||
|
||
export default class Processes { | ||
public static async get () { | ||
return new Promise<yuki.Processes>((resolve, reject) => { | ||
exec(Processes.TASK_LIST_COMMAND, | ||
(err, stdout, stderr) => { | ||
if (err) { | ||
debug('exec failed !> %s', err) | ||
reject() | ||
return | ||
} | ||
|
||
if (this.findsProcessIn(stdout)) { | ||
const result = this.parseProcessesFrom(stdout) | ||
debug('get %d processes', result.length) | ||
resolve(result) | ||
} else { | ||
debug('exec failed. no process') | ||
reject() | ||
} | ||
} | ||
) | ||
}) | ||
} | ||
private static TASK_LIST_COMMAND = 'tasklist /nh /fo csv /fi "sessionname eq Console"' | ||
|
||
private static findsProcessIn (value: string) { | ||
return value.startsWith('"') | ||
} | ||
|
||
private static parseProcessesFrom (value: string) { | ||
const processes: yuki.Processes = [] | ||
|
||
const regexResult = value.match(/"([^"]+)"/g) | ||
if (!regexResult) return [] | ||
|
||
let onePair: yuki.Process = { name: '', pid: -1 } | ||
for (let i = 0; i < regexResult.length; i++) { | ||
if (i % 5 === 0) {// process name | ||
onePair.name = regexResult[i].substr(1, regexResult[i].length - 2) | ||
} else if (i % 5 === 1) {// process id | ||
onePair.pid = parseInt(regexResult[i].substr(1, regexResult[i].length - 2), 10) | ||
processes.push(onePair) | ||
onePair = { name: '', pid: -1 } | ||
} | ||
} | ||
|
||
return processes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.