Skip to content

Commit

Permalink
fix: Use tasklist for scanning and use native method as fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Reder committed Sep 26, 2018
1 parent aad5d74 commit d0ab73e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
72 changes: 70 additions & 2 deletions src/main/Emulator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import winProcess, { Process } from 'winprocess'
import { snapshot } from 'process-list'
import * as parse from 'csv-parse'

import * as fs from 'fs'
import * as path from 'path'
import { promisify } from 'util'
import { spawn } from 'child_process'

import { connector, deleteEmulator } from '.'
import { FilteredEmulator } from '../models/Emulator.model'
Expand All @@ -29,12 +31,78 @@ export class Emulator {
private process: Process

public static async updateEmulators () {
const emulators = (await tasklist({
let emulators: FilteredEmulator[] = []
try {
emulators = await this.getEmulatorsFromTasklist()
} catch (err) {
emulators = await this.getEmulatorsFromNativeNodeBinaries()
}
connector.updateEmulators(emulators)
}

private static async getEmulatorsFromTasklist (): Promise<FilteredEmulator[]> {
return (await Promise.all((await new Promise<string[][]>((resolve, reject) => {
try {
const tasklist = spawn('tasklist', ['/FO', 'CSV', '/NH'])
let stdout = ''
tasklist.stdout.on('data', data => {
stdout += data.toString()
})
tasklist.stderr.on('data', data => {
console.error(`tasklist stderr: ${data}`)
})
tasklist.on('error', err => reject(err))
tasklist.on('close', code => {
if (code !== 0) {
console.warn(`tasklist process exited with code ${code}`)
return
}
parse(stdout, {}, (err: Error, data: string[][]) => {
if (err) reject(err)
resolve(data)
})
})
} catch (err) {
console.error(err)
reject(err)
}
}))
.filter((process: string[]) => process[0].match(/project64/i))
.map(process =>
new Promise<string[]>((resolve, reject) => {
const tasklist = spawn('tasklist', ['/FI', `PID eq ${process[1]}`, '/FO', 'CSV', '/NH', '/V'])
let stdout = ''
tasklist.stdout.on('data', data => {
stdout += data.toString()
})
tasklist.stderr.on('data', data => {
console.error(`tasklist stderr: ${data}`)
})
tasklist.on('close', code => {
if (code !== 0) {
console.warn(`tasklist process exited with code ${code}`)
}
parse(stdout, {}, (err: Error, data: string[][]) => {
if (err) reject(err)
if (data.length === 0) reject(new Error(`tasklist couldn't find process with PID ${process[1]}`))
resolve(data[0])
})
})
})
)))
.map((process: string[]) => ({
name: process[0],
pid: parseInt(process[1]),
windowName: process[8]
}))
}

private static async getEmulatorsFromNativeNodeBinaries (): Promise<FilteredEmulator[]> {
return (await tasklist({
name: true,
pid: true
}))
.filter(({ name }: FilteredEmulator) => name.match(/project64/i))
connector.updateEmulators(emulators)
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/models/Emulator.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface FilteredEmulator {
name: string
pid: number
windowName?: string
}
14 changes: 14 additions & 0 deletions src/renderer/components/views/EmulatorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,23 @@ class View extends React.PureComponent<EmulatorViewProps, EmulatorViewState> {
const onSelect = this.onSelectEmulator
return emulators.map(
emulator => {
let romName: string | null = null
const windowName = emulator.windowName
try {
if (windowName) {
romName = windowName.includes(' - ')
? windowName.split(' - Project64')[0]
: null
}
} catch (err) {}
return (
<div style={li} key={emulator.pid}>
<div>
{ emulator.name } | pid: { emulator.pid }
{
windowName &&
` | ${romName || 'Game is not running'}`
}
</div>
<SMMButton
text='Select'
Expand All @@ -127,6 +140,7 @@ class View extends React.PureComponent<EmulatorViewProps, EmulatorViewState> {
padding: '3px'
}
}}
enabled={windowName == null || romName != null}
onClick={onSelect.bind(null, emulator)}
/>
</div>
Expand Down

0 comments on commit d0ab73e

Please sign in to comment.