This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
forked from Enclave2020/Fallout76Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
50 lines (42 loc) · 1.57 KB
/
GameManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using System.Threading;
namespace Fallout76.Proxy
{
public class GameManager : IGameManager
{
private volatile Process oProcess;
public void GetProcess(string sProcessName)
{
SpinWait.SpinUntil(() => Process.GetProcessesByName(sProcessName).Any(), TimeSpan.FromMinutes(5));
oProcess = Process.GetProcessesByName(sProcessName).FirstOrDefault();
}
public void RestartAsChild(string sProcessName)
{
var regex = new Regex("\"(.+?)\"\\s(.+)");
var match = regex.Match(GetCommandLine($"{sProcessName}.exe"));
oProcess?.Kill();
var TargetPath = match.Groups[1].Value;
var TargetArguments = match.Groups[2].Value;
Directory.SetCurrentDirectory(Path.GetDirectoryName(TargetPath));
oProcess = new Process();
oProcess.StartInfo.FileName = TargetPath;
oProcess.StartInfo.Arguments = TargetArguments;
oProcess.Start();
}
public string GetCommandLine(string sProcessName)
{
var mngmtClass = new ManagementClass("Win32_Process");
foreach (var o in mngmtClass.GetInstances())
{
if (o["Name"].Equals(sProcessName))
return (string)o["CommandLine"];
}
throw new SystemException(string.Format(Fallout76ProxyResource.Arguments, sProcessName));
}
}
}