GameLauncher/PSLauncher/Util.cs
Chord fce71df0f3 Fix path ordering bug
EXE discovery is now ordered from newest to oldest.
2016-06-20 01:45:40 -04:00

167 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace PSLauncher
{
public static class ISynchronizeInvokeExtensions
{
public static void SafeInvoke<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
{
if (@this.InvokeRequired)
{
@this.Invoke(action, new object[] { @this });
}
else
{
action(@this);
}
}
}
public static class QueryExtensions
{
public static string ToQueryString(this NameValueCollection nvc)
{
IEnumerable<string> segments = from key in nvc.AllKeys
from value in nvc.GetValues(key)
select string.Format("{0}={1}", key, value);
return string.Join("&", segments);
}
}
public static class Win32
{
public const int WM_SETREDRAW = 0x0b;
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
public static void SuspendPainting(IntPtr hWnd)
{
SendMessage(hWnd, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
}
public static void ResumePainting(IntPtr hWnd)
{
SendMessage(hWnd, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
}
}
public static class Util
{
public static string getDefaultPlanetSideDirectory()
{
// paths are in order of newest (most likely to have the right planetside) to oldest
Microsoft.Win32.RegistryKey key = null;
string psFolder = "";
List<string> pathsToCheck = new List<String>();
// non-steam install
// Known to be: C:\Users\Public\Daybreak Game Company\Installed Games\PlanetSide 2 Test\LaunchPad.exe
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths\LaunchPad.exe");
if (key != null && key.GetValue("") != null)
{
String defaultDirectory;
defaultDirectory = key.GetValue("").ToString();
Console.WriteLine("LaunchPad.exe key found {0}", defaultDirectory);
defaultDirectory = Path.GetDirectoryName(defaultDirectory);
// verify that we aren't mistakingly returning a PlanetSide 2 directory...
pathsToCheck.Add(defaultDirectory);
// try to go up a directory and find the PlanetSide folder
string upOne = Directory.GetParent(defaultDirectory).FullName;
psFolder = Path.Combine(upOne, "Planetside");
pathsToCheck.Add(psFolder);
}
else
{
Console.WriteLine("No LaunchPad.exe key found");
}
// HACK: Should work on Win7 and above
psFolder = "C:\\Users\\Public\\Daybreak Game Company\\Installed Games\\Planetside";
pathsToCheck.Add(psFolder);
// HACK 2: our last attempt
psFolder = "C:\\Users\\Public\\Sony Online Entertainment\\Installed Games\\Planetside";
pathsToCheck.Add(psFolder);
// worth a shot! (for windows XP or old installs)
psFolder = Path.Combine(ProgramFilesx86(), "Sony\\PlanetSide");
pathsToCheck.Add(psFolder);
int i = 1;
foreach(var path in pathsToCheck)
{
Console.WriteLine("M{0} - {1}", i, path);
if (Directory.Exists(path) && checkDirForPlanetSide(path))
{
Console.WriteLine("Path found using M{0}", i);
return path;
}
i += 1;
}
Console.WriteLine("No default planetside.exe path found");
// give up :'(
return "";
}
public static bool checkDirForPlanetSide(string dir)
{
return File.Exists(Path.Combine(dir, SettingsForm.PS_EXE_NAME));
}
public static string ProgramFilesx86()
{
if (8 == IntPtr.Size
|| (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
}
return Environment.GetEnvironmentVariable("ProgramFiles");
}
// from https://stackoverflow.com/questions/18726852/redirecting-console-writeline-to-textbox
public class ControlWriter : TextWriter
{
private Control textbox;
public ControlWriter(Control textbox)
{
this.textbox = textbox;
}
public override void Write(char value)
{
textbox.Text += value;
}
public override void Write(string value)
{
textbox.Text += value;
}
public override Encoding Encoding
{
get { return Encoding.ASCII; }
}
}
}
}