Merge pull request #159 from erri120/game-handler

Better Game Handlers
This commit is contained in:
Timothy Baldridge 2019-11-09 14:07:30 -07:00 committed by GitHub
commit f8c7144071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,64 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;
namespace Wabbajack.Common
{
public class GOGGame
{
public int GameID;
public string Path;
public string GameName;
}
/// <summary>
/// Class for all GOG operations
/// </summary>
public class GOGHandler
{
private const string GOGRegKey = @"Software\GOG.com\Games";
private const string GOG64RegKey = @"Software\WOW6432Node\GOG.com\Games";
public HashSet<GOGGame> Games { get; internal set; }
public RegistryKey GOGKey { get; internal set; }
public GOGHandler(bool init)
{
var gogKey = Registry.LocalMachine.OpenSubKey(GOGRegKey) ?? Registry.LocalMachine.OpenSubKey(GOG64RegKey);
GOGKey = gogKey;
if (!init) return;
LoadAllGames();
}
/// <summary>
/// Finds the installation path of a GOG game by ID
/// </summary>
/// <param name="id">ID of the GOG game</param>
/// <returns></returns>
public string GetGamePathById(int id)
{
return Games.FirstOrDefault(f => f.GameID == id)?.Path;
}
/// <summary>
/// Enumerates through all subkeys found in the GOG registry entry to get all
/// GOG games
/// </summary>
public void LoadAllGames()
{
Games = new HashSet<GOGGame>();
string[] keys = GOGKey.GetSubKeyNames();
foreach (var key in keys)
{
var game = new GOGGame
{
GameID = int.Parse(key),
GameName = GOGKey.OpenSubKey(key)?.GetValue("GAMENAME").ToString(),
Path = GOGKey.OpenSubKey(key)?.GetValue("PATH").ToString()
};
Games.Add(game);
}
}
}
}

View File

@ -0,0 +1,112 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;
namespace Wabbajack.Common
{
public class SteamGame
{
public int AppId;
public string Name;
public string InstallDir;
}
/// <summary>
/// Class for all Steam operations
/// </summary>
public class SteamHandler
{
private const string SteamRegKey = @"Software\Valve\Steam";
/// <summary>
/// Path to the Steam folder
/// </summary>
public string SteamPath { get; internal set; }
/// <summary>
/// HashSet of all known Steam Libraries
/// </summary>
public HashSet<string> InstallFolders { get; internal set; }
/// <summary>
/// HashSet of all known SteamGames
/// </summary>
public HashSet<SteamGame> Games { get; internal set; }
private string SteamConfig => Path.Combine(SteamPath, "config", "config.vdf");
public SteamHandler(bool init)
{
var steamKey = Registry.CurrentUser.OpenSubKey(SteamRegKey);
SteamPath = steamKey?.GetValue("SteamPath").ToString();
if(!init) return;
LoadInstallFolders();
LoadAllSteamGames();
}
/// <summary>
/// Finds the installation path of a Steam game by ID
/// </summary>
/// <param name="id">ID of the Steam game</param>
/// <returns></returns>
public string GetGamePathById(int id)
{
return Games.FirstOrDefault(f => f.AppId == id)?.InstallDir;
}
/// <summary>
/// Reads the config file and adds all found installation folders to the HashSet
/// </summary>
public void LoadInstallFolders()
{
var paths = new HashSet<string>();
File.ReadLines(SteamConfig, Encoding.UTF8).Do(l =>
{
if (!l.Contains("BaseInstallFolder_")) return;
var s = GetVdfValue(l);
s = Path.Combine(s, "steamapps");
paths.Add(s);
});
InstallFolders = paths;
}
/// <summary>
/// Enumerates through all Steam Libraries to find and read all .afc files, adding the found game
/// to the HashSet
/// </summary>
public void LoadAllSteamGames()
{
var games = new HashSet<SteamGame>();
InstallFolders.Do(p =>
{
Directory.EnumerateFiles(p, "*.acf", SearchOption.TopDirectoryOnly).Do(f =>
{
var steamGame = new SteamGame();
File.ReadAllLines(f, Encoding.UTF8).Do(l =>
{
if(l.Contains("\"appid\""))
steamGame.AppId = int.Parse(GetVdfValue(l));
if (l.Contains("\"name\""))
steamGame.Name = GetVdfValue(l);
if (l.Contains("\"installdir\""))
steamGame.InstallDir = Path.Combine(p, "common", GetVdfValue(l));
});
games.Add(steamGame);
});
});
Games = games;
}
private static string GetVdfValue(string line)
{
var trim = line.Trim('\t').Replace("\t", "");
string[] s = trim.Split('\"');
return s[3].Replace("\\\\", "\\");
}
}
}

View File

@ -98,8 +98,10 @@
<Compile Include="Extensions\TaskExt.cs" />
<Compile Include="FileExtractor.cs" />
<Compile Include="GameMetaData.cs" />
<Compile Include="GOGHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SplittingStream.cs" />
<Compile Include="SteamHandler.cs" />
<Compile Include="Utils.cs" />
<Compile Include="WorkQueue.cs" />
</ItemGroup>