Skip to content

Commit

Permalink
detect Epic Games installation
Browse files Browse the repository at this point in the history
  • Loading branch information
hollowstrawberry committed Jun 14, 2020
1 parent 93d3e3a commit 7d89a01
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
Expand All @@ -13,15 +14,15 @@ public static class Program
public static int Main(string[] args)
{
bool hasArgs = args != null && args.Length > 0;
string assemblyPath = null;
string gamePath = null;

if (!hasArgs) Console.WriteLine("[#] Booting up PolyConverter");

if (File.Exists(ManualGamePath))
{
try
{
assemblyPath = $"{File.ReadAllText(ManualGamePath).Trim()}\\Poly Bridge 2_Data\\Managed";
gamePath = $"{File.ReadAllText(ManualGamePath).Trim()}\\Poly Bridge 2_Data\\Managed";
}
catch (Exception e) // Could happen if it can't read files, I suppose
{
Expand All @@ -38,19 +39,23 @@ public static int Main(string[] args)
}
else
{
Exception error = null;
try
var errors = new List<Exception>(2);

try { gamePath = GetPolyBridge2SteamPath(); }
catch (Exception e) { errors.Add(e); }

if (gamePath == null)
{
assemblyPath = GetPolyBridge2SteamPath();
try { gamePath = GetPolyBridge2EpicGamesPath(); }
catch (Exception e) { errors.Add(e); }
}
catch (Exception e) { error = e; }

if (error != null || assemblyPath == null)
if (gamePath == null)
{
Console.WriteLine($"[Fatal Error] Failed to locate Poly Bridge 2 installation folder on Steam.");
Console.WriteLine($"[Fatal Error] Failed to locate Poly Bridge 2 installation folder.");
Console.WriteLine($" You can manually set the location by creating a file here called \"{ManualGamePath}\"" +
"and writing the location of your game folder in that file, then restarting this program.");
if (error != null) Console.WriteLine($"\n[#] Error message: {error.Message}");
foreach (var e in errors) Console.WriteLine($"\n[#] Error message: {e.Message}");
if (!hasArgs)
{
Console.WriteLine("\n[#] The program will now close.");
Expand All @@ -64,15 +69,15 @@ public static int Main(string[] args)

try
{
PolyBridge2Assembly = Assembly.LoadFrom($"{assemblyPath}\\Assembly-CSharp.dll");
UnityAssembly = Assembly.LoadFrom($"{assemblyPath}\\UnityEngine.CoreModule.dll");
PolyBridge2Assembly = Assembly.LoadFrom($"{gamePath}\\Poly Bridge 2_Data\\Managed\\Assembly-CSharp.dll");
UnityAssembly = Assembly.LoadFrom($"{gamePath}\\Poly Bridge 2_Data\\Managed\\UnityEngine.CoreModule.dll");

object testObject = FormatterServices.GetUninitializedObject(VehicleProxy);
VehicleProxy.GetField("m_Pos").SetValue(testObject, Activator.CreateInstance(Vector2));
}
catch (Exception e)
{
Console.WriteLine($"[Fatal Error] Failed to load Poly Bridge 2 libraries at \"{assemblyPath}\":\n {e}");
Console.WriteLine($"[Fatal Error] Failed to load Poly Bridge 2 libraries at \"{gamePath}\":\n {e}");
if (!hasArgs)
{
Console.WriteLine("\n[#] The program will now close.");
Expand Down Expand Up @@ -131,10 +136,13 @@ public static int Main(string[] args)
}
}


static string GetPolyBridge2SteamPath()
{
var paths = new List<string>(10);
paths.Add((string)Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Valve\\Steam").GetValue("InstallPath"));

paths.Add((string)Registry.LocalMachine?.OpenSubKey("SOFTWARE\\WOW6432Node\\Valve\\Steam")?.GetValue("InstallPath"));
if (paths[0] == null || !Directory.Exists(paths[0])) return null;

string config = File.ReadAllText($"{paths[0]}\\config\\config.vdf");
foreach (Match match in Regex.Matches(config, "\"BaseInstallFolder_[0-9]\"\\s+\"([^\"]+)\""))
Expand All @@ -143,13 +151,30 @@ static string GetPolyBridge2SteamPath()
}
foreach (string path in paths)
{
string assembliesPath = $"{path}\\steamapps\\common\\Poly Bridge 2\\Poly Bridge 2_Data\\Managed";
string assembliesPath = $"{path}\\steamapps\\common\\Poly Bridge 2";
if (Directory.Exists(assembliesPath)) return assembliesPath;
}

return null;
}

static string GetPolyBridge2EpicGamesPath()
{
var launcher = (string)Registry.LocalMachine?.OpenSubKey("SOFTWARE\\WOW6432Node\\Epic Games\\EpicGamesLauncher")?.GetValue("AppDataPath");
if (launcher == null || !Directory.Exists(launcher)) return null;

foreach (var file in Directory.GetFiles(launcher).Where(f => f.EndsWith(".item")))
{
string content = File.ReadAllText(file);
if (content.Contains("\"DisplayName\": \"Poly Bridge 2\""))
{
var match = Regex.Match(content, "\"InstallLocation\": \"(.+)\"");
return match.Groups[1].Value.Replace("\\\\", "\\");
}
}
return null;
}


const int ExitCodeSuccessful = 0;
const int ExitCodeJsonError = 1;
Expand Down

0 comments on commit 7d89a01

Please sign in to comment.