Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
Massive improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Q-Sharp committed Dec 23, 2018
1 parent 6e8f0a2 commit 7cbb17e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 118 deletions.
7 changes: 7 additions & 0 deletions BethesdaGameType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Fallout76Proxy
{
public enum BethesdaGameType
{
Fallout76 = 20
}
}
7 changes: 0 additions & 7 deletions BethesdaGames.cs

This file was deleted.

27 changes: 6 additions & 21 deletions BethesdaLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,14 @@

namespace Fallout76Proxy
{
static class BethesdaLauncher
public class BethesdaLauncher : IBethesdaLauncher
{
public static bool Installed()
{
RegistryKey bethesdaNet = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\BethesdaNet\\Shell\\Open\\Command");
public static BethesdaLauncher Default { get; set; } = new BethesdaLauncher();

return bethesdaNet != null;
}
public bool IsInstalled => Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\BethesdaNet\Shell\Open\Command") != null;
public bool IsActive => Process.GetProcessesByName("BethesdaNetLauncher").Any();

public static void Start(int GameIdx)
{
Process.Start($"bethesdanet://run/{GameIdx}");
}

public static void Stop()
{
foreach (Process bethesdaLauncher in Process.GetProcessesByName("BethesdaNetLauncher"))
bethesdaLauncher.Kill();
}

public static bool Active()
{
return Process.GetProcessesByName("BethesdaNetLauncher").Count() > 0;
}
public void Start(BethesdaGameType GameIdx) => Process.Start($"bethesdanet://run/{(int)GameIdx}");
public void Stop() => Process.GetProcessesByName("BethesdaNetLauncher").FirstOrDefault()?.Kill();
}
}
23 changes: 15 additions & 8 deletions Fallout76Proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
<OutputType>Exe</OutputType>
<RootNamespace>Fallout76Proxy</RootNamespace>
<AssemblyName>Fallout 76</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -35,26 +36,32 @@
<PropertyGroup>
<ApplicationIcon>Fallout76.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<LangVersion>7.2</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BethesdaGames.cs" />
<Compile Include="BethesdaGameType.cs" />
<Compile Include="BethesdaLauncher.cs" />
<Compile Include="GameManager.cs" />
<Compile Include="IBethesdaLauncher.cs" />
<Compile Include="IGameManager.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Fallout76.ico" />
</ItemGroup>
<ItemGroup>
<None Include="app.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
70 changes: 30 additions & 40 deletions GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,62 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace Fallout76Proxy
{
class GameManager
public class GameManager : IGameManager
{
readonly string processName;
private readonly string sProcessName;
private volatile Process oProcess;

public GameManager(string processName)
public GameManager(string sProcessName)
{
this.processName = processName;
this.sProcessName = sProcessName;
}

public void WaitFor()
public async Task WaitForAsync()
{
Process[] processes;

do
await Task.Run(() =>
{
processes = Process.GetProcessesByName(processName);
}
while (processes.Count() == 0);
if(SpinWait.SpinUntil(() => Process.GetProcesses().Any(x => x.ProcessName == sProcessName), TimeSpan.FromMinutes(1)))
oProcess = Process.GetProcesses().FirstOrDefault(x => x.ProcessName == sProcessName);
});
}

public void RestartAsChild()
{
Process[] processes = Process.GetProcessesByName(processName);

if (processes.Count() == 0)
throw new NotStartedException($"For some reason {processName} not started.");

if (processes.Count() > 1)
throw new TooManyStartedException($"Too many {processName} launched. Stop others!");
var regex = new Regex("\"(.+?)\"\\s(.+)");
var match = regex.Match(GetCommandLine($"{sProcessName}.exe"));

Regex regex = new Regex("\"(.+?)\"\\s(.+)");
Match match = regex.Match(GetCommandLine($"{processName}.exe"));

processes.First().Kill();

if (match.Groups.Count == 0)
throw new StrangeArguments($"For some reason {processName} have no token!");

string TargetPath = match.Groups[1].Value;
string TargetArguments = match.Groups[2].Value;
oProcess.Kill();
var TargetPath = match.Groups[1].Value;
var TargetArguments = match.Groups[2].Value;

Directory.SetCurrentDirectory(Path.GetDirectoryName(TargetPath));

Process process = new Process();
process.StartInfo.FileName = TargetPath;
process.StartInfo.Arguments = TargetArguments;
process.Start();
oProcess = new Process();
oProcess.StartInfo.FileName = TargetPath;
oProcess.StartInfo.Arguments = TargetArguments;
oProcess.Start();
}

string GetCommandLine(string processName)
public string GetCommandLine(string processName)
{
ManagementClass mngmtClass = new ManagementClass("Win32_Process");
foreach (ManagementObject o in mngmtClass.GetInstances())
var mngmtClass = new ManagementClass("Win32_Process");
foreach(var o in mngmtClass.GetInstances())
{
if (o["Name"].Equals(processName))
{
if(o["Name"].Equals(processName))
return (string)o["CommandLine"];
}
}

throw new NotStartedException($"Can't get {processName} arguments");
throw new SystemException($"Can't get {processName} arguments");
}

public static bool Fallout76Exists() => Process.GetProcessesByName("Fallout76").Count() > 0;
}
}
11 changes: 11 additions & 0 deletions IBethesdaLauncher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Fallout76Proxy
{
public interface IBethesdaLauncher
{
bool IsInstalled { get; }
bool IsActive { get; }

void Start(BethesdaGameType GameIdx);
void Stop();
}
}
11 changes: 11 additions & 0 deletions IGameManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace Fallout76Proxy
{
public interface IGameManager
{
string GetCommandLine(string processName);
void RestartAsChild();
Task WaitForAsync();
}
}
55 changes: 13 additions & 42 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,67 +1,37 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Threading.Tasks;

namespace Fallout76Proxy
{
class BethesdaLauncherMissedException : Exception { public BethesdaLauncherMissedException(string message) : base(message) { } };
class NotStartedException : Exception { public NotStartedException(string message) : base(message) { } };
class TooManyStartedException : Exception { public TooManyStartedException(string message) : base(message) { } };
class StrangeArguments : Exception { public StrangeArguments(string message) : base(message) { } };

class Program
public static class Program
{
static string GetCommandLine(string ProcessName)
{
ManagementClass mngmtClass = new ManagementClass("Win32_Process");
foreach (ManagementObject o in mngmtClass.GetInstances())
{
if (o["Name"].Equals(ProcessName))
{
return (string) o["CommandLine"];
}
}

throw new NotStartedException("Can't get Fallout76 arguments");
}

static bool Fallout76Exists()
public static async Task Launch()
{
Process[] fallouts76 = Process.GetProcessesByName("Fallout76");
return fallouts76.Count() > 0;
}

static void Launch()
{
if (!BethesdaLauncher.Installed())
throw new BethesdaLauncherMissedException("Try to reinstall bethesda launcher.");
if(!BethesdaLauncher.Default.IsInstalled)
throw new Exception("Try to reinstall bethesda launcher.");

Console.WriteLine("Starting Fallout76 from BethesdaLauncher.");

BethesdaLauncher.Start(BethesdaGames.Fallout76);

BethesdaLauncher.Default.Start(BethesdaGameType.Fallout76);
Console.WriteLine("Waiting for game started.");

GameManager fallout76 = new GameManager("Fallout76");

fallout76.WaitFor();
var fallout76 = new GameManager("Fallout76");
await fallout76.WaitForAsync();

Console.WriteLine("Restarting Fallout 76 as child process.");

fallout76.RestartAsChild();

Console.WriteLine("Closing BethesdaLauncher.");
BethesdaLauncher.Stop();
BethesdaLauncher.Default.Stop();
}

static void Main(string[] args)
public static async Task Main(string[] args)
{
try
{
Launch();
await Launch();
}
catch (Exception e)
catch(Exception e)
{
Console.WriteLine(e.ToString());
Console.WriteLine("\nPress any key to exit...");
Expand All @@ -70,3 +40,4 @@ static void Main(string[] args)
}
}
}

3 changes: 3 additions & 0 deletions app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup></configuration>
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CSharp" version="4.6.0-preview.18571.3" targetFramework="net472" />
</packages>

0 comments on commit 7cbb17e

Please sign in to comment.