GameLauncher/PSLauncher/Util.cs

257 lines
8.6 KiB
C#
Raw Permalink Normal View History

using PSLauncher.Properties;
using System;
using System.Collections.Generic;
2016-06-19 20:49:51 +00:00
using System.Collections.Specialized;
using System.ComponentModel;
2016-06-19 20:49:51 +00:00
using System.IO;
using System.Linq;
2016-06-19 20:49:51 +00:00
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
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);
}
}
}
2016-06-19 20:49:51 +00:00
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 List<ServerEntry> LoadServerList()
{
List<ServerEntry> entries = new List<ServerEntry>();
StringCollection serverList = Settings.Default.ServerList;
foreach (String entry in serverList)
{
String[] tokens = entry.Split(',');
if (tokens.Length != 3)
{
Console.WriteLine("LoadServerList: Failed to load server entry " + entry);
continue;
}
ServerEntry newEntry = new ServerEntry();
newEntry.name = tokens[0];
newEntry.hostname = tokens[1];
newEntry.port = int.Parse(tokens[2]);
entries.Add(newEntry);
}
return entries;
}
2016-06-19 20:49:51 +00:00
public static string getDefaultPlanetSideDirectory()
{
// paths are in order of newest (most likely to have the right planetside) to oldest
2016-06-19 20:49:51 +00:00
Microsoft.Win32.RegistryKey key = null;
string psFolder = "";
List<string> pathsToCheck = new List<String>();
2016-06-19 20:49:51 +00:00
// non-steam install
// Known to be: C:\Users\Public\Daybreak Game Company\Installed Games\PlanetSide 2 Test\LaunchPad.exe
2016-06-19 20:49:51 +00:00
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("PSDiscover: LaunchPad.exe key found {0}", defaultDirectory);
2016-06-19 20:49:51 +00:00
defaultDirectory = Path.GetDirectoryName(defaultDirectory);
// verify that we aren't mistakingly returning a PlanetSide 2 directory...
pathsToCheck.Add(defaultDirectory);
2016-06-19 20:49:51 +00:00
// 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("PSDiscover: No LaunchPad.exe key found");
2016-06-19 20:49:51 +00:00
}
// 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)
2016-06-19 20:49:51 +00:00
psFolder = Path.Combine(ProgramFilesx86(), "Sony\\PlanetSide");
pathsToCheck.Add(psFolder);
2016-06-19 20:49:51 +00:00
int i = 1;
foreach(var path in pathsToCheck)
{
Console.WriteLine("PSDiscover: M{0} - {1}", i, path);
2016-06-19 20:49:51 +00:00
if (Directory.Exists(path) && checkDirForPlanetSide(path))
{
Console.WriteLine("PSDiscover: Path found using M{0}", i);
return path;
}
2016-06-19 20:49:51 +00:00
i += 1;
}
Console.WriteLine("PSDiscover: No default planetside.exe path found!");
2016-06-19 20:49:51 +00:00
// give up :'(
2016-06-19 20:49:51 +00:00
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");
}
public static string CalculateFileHash(string _filePath)
{
using (var hashingAlgorithm = HashAlgorithm.Create(Program.hashingAlgoType.ToString()))
{
using (var fileHandle = File.OpenRead(_filePath))
{
return CalculateFileHash(hashingAlgorithm, fileHandle);
}
}
}
public static string CalculateFileHash(FileStream _fileHandle)
{
using (var hashingAlgorithm = HashAlgorithm.Create(Program.hashingAlgoType.ToString()))
{
return CalculateFileHash(hashingAlgorithm, _fileHandle);
}
}
private static string CalculateFileHash(HashAlgorithm _hashingAlgorithm, FileStream _fileHandle)
{
const int bufferSize = 1 * 1000 * 1000; // 1MB
// read file in bigger buffer to improve hashing performance (slightly)
using (var stream2 = new BufferedStream(_fileHandle, bufferSize))
{
return BitConverter.ToString(_hashingAlgorithm.ComputeHash(stream2)).Replace("-", "").ToLower();
}
}
public static string CalculateStringHash(string _string)
{
using (var hashingAlgorithm = HashAlgorithm.Create(Program.hashingAlgoType.ToString()))
{
return CalculateStringHash(hashingAlgorithm, _string);
}
}
public static string CalculateStringHash(EHashingAlgoType _algoType, string _string)
{
using (var hashingAlgorithm = HashAlgorithm.Create(_algoType.ToString()))
{
return CalculateStringHash(hashingAlgorithm, _string);
}
}
private static string CalculateStringHash(HashAlgorithm _hashAlgorithm, string _string)
{
byte[] stringBytes = System.Text.Encoding.ASCII.GetBytes(_string);
return BitConverter.ToString(_hashAlgorithm.ComputeHash(stringBytes)).Replace("-", "").ToLower();
}
// 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; }
}
}
// https://stackoverflow.com/a/250400
public static DateTime UnixTimestampToDateTime( double unixTimestmap )
{
// Unix timestamp is seconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(unixTimestmap).ToLocalTime();
return dateTime;
}
2016-06-19 20:49:51 +00:00
}
}