Skip to content

Commit

Permalink
Fix steam launch support for custom login servers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoot committed Jul 28, 2018
1 parent 8787996 commit 5e4149c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
35 changes: 35 additions & 0 deletions src/common/injector.ts
@@ -1,5 +1,8 @@
import * as injector from 'node-dll-injector';
import { spawn } from 'child_process';
import { shell } from 'electron';
import * as Registry from 'winreg';
import { lookupRegistry } from './regutils';
import * as fs from 'fs-extra';
const isAdmin = require('is-admin');

Expand Down Expand Up @@ -64,6 +67,38 @@ export class Injector {
return args;
}

private static async lookupSteamExePath(): Promise<string | null> {
const regItem = await lookupRegistry(
Registry.HKCU,
'\\Software\\Valve\\Steam',
'SteamExe');
if (!regItem) {
return null;
}
return regItem.value;
}

public static async startProcessSteam(news: LauncherNews | null, config: LauncherConfig): Promise<void> {
const args = this.generateExecutableArgs(news, config);
const steamExe = await this.lookupSteamExePath();

if (steamExe) {
// Launch via steam directly (won't show warning prompt for custom args)
const child = spawn(steamExe, [
'-applaunch',
'17080',
...args
], {
detached: true,
stdio: 'ignore'
});
child.unref();
} else {
// Launch using Steam URL. Will show warning prompt for custom args
shell.openExternal(`steam://rungameid/17080//${args}`);
}
}

public static startProcess(news: LauncherNews | null, config: LauncherConfig): void {
// Start the child properly detached
// i.e. don't connect stdio, unref() to remove from node's reference counter
Expand Down
24 changes: 24 additions & 0 deletions src/common/regutils.ts
@@ -0,0 +1,24 @@
import * as Registry from 'winreg';

const getRegistryValuesPromise = async (regKey: Registry.Registry): Promise<Registry.RegistryItem[]> {
return new Promise((res: (items: Registry.RegistryItem[]) => void, rej: (err: Error) => void) => {
regKey.values((err, items) => {
if (err) {
rej(err);
} else {
res(items);
}
});
});
};

export const lookupRegistry = async (hive: string, path: string, item: string): Promise<Registry.RegistryItem | null> => {
const regKey = new Registry({
hive: hive,
key: path
});

const values = await getRegistryValuesPromise(regKey);
const result = values.find((regItem) => regItem.name === item);
return result || null;
};
30 changes: 8 additions & 22 deletions src/common/updater.ts
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs-extra';
const rmfr = require('rmfr');
import * as download from 'download';
import * as Registry from 'winreg';
import { lookupRegistry } from './regutils';
import { BrowserWindow } from 'electron';
import { homedir } from 'os';

Expand Down Expand Up @@ -165,34 +166,19 @@ export default class TAModsUpdater {
}
}

private static async regKeyValuesPromise(regKey: Registry.Registry): Promise<Registry.RegistryItem[]> {
return new Promise((res: (items: Registry.RegistryItem[]) => void, rej: (err: Error) => void) => {
regKey.values((err, items) => {
if (err) {
rej(err);
} else {
res(items);
}
});
});
}

public static async getConfigDirectory(): Promise<string> {
const regKey = new Registry({
hive: Registry.HKCU,
key: '\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders'
});

const values = await this.regKeyValuesPromise(regKey);
const result = values.find((regItem) => regItem.name === 'Personal');
if (!result) {
const regItem = await lookupRegistry(
Registry.HKCU,
'\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders',
'Personal');
if (!regItem) {
throw new Error('Could not retrieve user Documents directory from the registry');
}

if (result.value.includes('%USERPROFILE%')) {
if (regItem.value.includes('%USERPROFILE%')) {
return `${homedir()}\\Documents\\My Games\\Tribes Ascend\\TribesGame\\config\\`;
} else {
return `${result.value}/my games/Tribes Ascend/TribesGame/config/`;
return `${regItem.value}/my games/Tribes Ascend/TribesGame/config/`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/launcherButton.tsx
Expand Up @@ -3,7 +3,7 @@ import { Button } from 'semantic-ui-react';
import { LauncherState } from './app';
import { LauncherConfig } from '../../common/launcher-config';
import { Injector, InjectionResult } from '../../common/injector';
import { ipcRenderer, shell } from 'electron';
import { ipcRenderer } from 'electron';
import { Howl } from 'howler';
import * as fs from 'fs-extra';
import { LauncherNews } from '../../common/launcher-news';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class LauncherButton extends React.Component<LauncherButtonProps, Launche
break;
case LauncherState.READY_TO_LAUNCH:
if (this.props.config.launchViaSteam) {
shell.openExternal('steam://rungameid/17080');
await Injector.startProcessSteam(this.props.news, this.props.config);
} else {
Injector.startProcess(this.props.news, this.props.config);
}
Expand Down

0 comments on commit 5e4149c

Please sign in to comment.