DisplayMagician/HeliosPlus/GameLibraries/SteamGame.cs
terrymacdonald 71928a9b44 [WIP] Created SteamLibrary class to handle library
Pulled out the the library list mgmt from SteamGame
and put it in a new SteamLibrary class. This means I
can replicate the learnings from the ShortcutRepo
amd Profile Repo, and can save separate JSON files
in the future if I so desire.

There is a little bit outstanding to make the SteamGame
Properties to be writeable as well as readable, otherwise
the SteamGame.CopyTo function won't work.
2020-07-21 23:40:33 +12:00

193 lines
6.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using HeliosPlus.Resources;
using HeliosPlus.Shared;
using HtmlAgilityPack;
using Microsoft.Win32;
using Newtonsoft.Json;
//using VdfParser;
//using Gameloop.Vdf;
using System.Collections.ObjectModel;
using ValveKeyValue;
using System.Security.Cryptography;
using System.ServiceModel.Configuration;
using HeliosPlus.GameLibraries.SteamAppInfoParser;
using TsudaKageyu;
using System.Drawing.IconLib;
using System.Drawing.IconLib.Exceptions;
namespace HeliosPlus.GameLibraries
{
public class SteamGame
{
/*private static string SteamLibrary.SteamExe;
private static string SteamLibrary.SteamPath;
private static string _steamConfigVdfFile;
private static string _registrySteamKey = @"SOFTWARE\\Valve\\Steam";
private static string _registryAppsKey = $@"{_registrySteamKey}\\Apps";*/
private string _gameRegistryKey;
private uint _steamGameId;
private string _steamGameName;
private string _steamGamePath;
private string _steamGameExe;
private string _steamGameIconPath;
private static List<SteamGame> _allInstalledSteamGames = null;
/*private struct SteamAppInfo
{
public uint GameID;
public string GameName;
public List<string> GameExes;
public string GameInstallDir;
public string GameSteamIconPath;
}*/
static SteamGame()
{
ServicePointManager.ServerCertificateValidationCallback +=
(send, certificate, chain, sslPolicyErrors) => true;
}
public SteamGame(uint steamGameId, string steamGameName, string steamGamePath, string steamGameExe, string steamGameIconPath)
{
_gameRegistryKey = $@"{SteamLibrary.SteamAppsRegistryKey}\\{steamGameId}";
_steamGameId = steamGameId;
_steamGameName = steamGameName;
_steamGamePath = steamGamePath;
_steamGameExe = steamGameExe;
_steamGameIconPath = steamGameIconPath;
}
public uint GameId { get => _steamGameId; }
public SupportedGameLibrary GameLibrary { get => SupportedGameLibrary.Steam; }
public string GameIconPath { get => _steamGameIconPath; }
public bool IsRunning
{
get
{
try
{
using (
var key = Registry.CurrentUser.OpenSubKey(_gameRegistryKey, RegistryKeyPermissionCheck.ReadSubTree))
{
if ((int)key?.GetValue(@"Running", 0) == 1)
{
return true;
}
return false;
}
}
catch (SecurityException ex)
{
Console.WriteLine($"SteamGame/IsRunning securityexception: {ex.Message}: {ex.InnerException}");
if (ex.Source != null)
Console.WriteLine("SecurityException source: {0} - Message: {1}", ex.Source, ex.Message);
throw;
}
catch (IOException ex)
{
// Extract some information from this exception, and then
// throw it to the parent method.
Console.WriteLine($"SteamGame/IsRunning ioexception: {ex.Message}: {ex.InnerException}");
if (ex.Source != null)
Console.WriteLine("IOException source: {0} - Message: {1}", ex.Source, ex.Message);
throw;
}
}
}
public bool IsUpdating
{
get
{
try
{
using (
var key = Registry.CurrentUser.OpenSubKey(_gameRegistryKey, RegistryKeyPermissionCheck.ReadSubTree))
{
if ((int)key?.GetValue(@"Updating", 0) == 1)
{
return true;
}
return false;
}
}
catch (SecurityException ex)
{
Console.WriteLine($"SteamGame/IsUpdating securityexception: {ex.Message}: {ex.InnerException}");
if (ex.Source != null)
Console.WriteLine("SecurityException source: {0} - Message: {1}", ex.Source, ex.Message);
throw;
}
catch (IOException ex)
{
// Extract some information from this exception, and then
// throw it to the parent method.
Console.WriteLine($"SteamGame/IsUpdating ioexception: {ex.Message}: {ex.InnerException}");
if (ex.Source != null)
Console.WriteLine("IOException source: {0} - Message: {1}", ex.Source, ex.Message);
throw;
}
}
}
public string GameName { get => _steamGameName; }
public string GamePath { get => _steamGamePath; }
public bool CopyTo(SteamGame steamGame)
{
if (!(steamGame is SteamGame))
return false;
// Copy all the shortcut data over to the other Shortcut
steamGame.GameIconPath = GameIconPath;
steamGame.GameId = GameId;
steamGame.GameName = GameName;
steamGame.GamePath = GamePath;
steamGame.IsRunning = IsRunning;
steamGame.IsUpdating = IsUpdating;
return true;
}
public override string ToString()
{
var name = _steamGameName;
if (string.IsNullOrWhiteSpace(name))
{
name = Language.Unknown;
}
if (IsRunning)
{
return name + " " + Language.Running;
}
if (IsUpdating)
{
return name + " " + Language.Updating;
}
return name;
}
}
}