Skip to content

Commit

Permalink
Added new command list /info to SteamHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Klocman committed Aug 8, 2022
1 parent aed060b commit 847b1a9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 26 deletions.
54 changes: 38 additions & 16 deletions source/SteamHelper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace SteamHelper
internal static class Program
{
private static QueryType _queryType = QueryType.None;
private static bool _silent;
private static bool _silentSwitch, _infoSwitch;
private static int _appId;

/// <summary>
Expand All @@ -26,9 +26,10 @@ internal static class Program
/// 1223 - The operation was canceled by the user.
///
/// Commands
/// u[ninstall] [/s[ilent]] AppID - Uninstall an app
/// i[nfo] AppID - Show info about an app
/// l[ist] - List app ID's
/// u[ninstall] [/s[ilent]] AppID - Uninstall a Steam App
/// i[nfo] AppID - Show information about a Steam App
/// l[ist] - List all detected Steam App ID's
/// l[ist] /i[nfo] - List information about all detected Steam Apps
/// steam - Show Steam install location
/// </summary>
private static int Main(string[] args)
Expand All @@ -49,18 +50,34 @@ private static int Main(string[] args)
break;

case QueryType.Uninstall:
SteamUninstaller.UninstallSteamApp(SteamApplicationInfo.FromAppId(_appId), _silent);
SteamUninstaller.UninstallSteamApp(SteamApplicationInfo.FromAppId(_appId), _silentSwitch);
break;

case QueryType.List:
foreach (var result in SteamInstallation.Instance.SteamAppsLocations
.SelectMany(x => Directory.GetFiles(x, @"appmanifest_*.acf")
.Select(p => Path.GetFileNameWithoutExtension(p).Substring(12)))
.Select(x => int.TryParse(x, out var num) ? num : (int?)null)
.Where(x => x != null)
.Distinct()
.OrderBy(x => x))
Console.WriteLine(result);
if (!_infoSwitch)
{
foreach (var result in SteamInstallation.Instance.SteamAppsLocations
.SelectMany(x => Directory.GetFiles(x, @"appmanifest_*.acf")
.Select(p => Path.GetFileNameWithoutExtension(p).Substring(12)))
.Select(x => int.TryParse(x, out var num) ? num : (int?)null)
.Where(x => x != null)
.Distinct()
.OrderBy(x => x))
Console.WriteLine(result);
}
else
{
var query = from appsLocStr in SteamInstallation.Instance.SteamAppsLocations
let appsLocation = new DirectoryInfo(appsLocStr)
from manifest in appsLocation.GetFiles(@"appmanifest_*.acf")
let info = SteamApplicationInfo.FromAppManifest(manifest, appsLocation)
where info != null
orderby info.AppId
select info;
foreach (var applicationInfo in query)
Console.WriteLine(HelperTools.ObjectToConsoleOutput(applicationInfo));
}

break;

case QueryType.SteamDir:
Expand Down Expand Up @@ -103,11 +120,16 @@ private static void ProcessCommandlineArguments(IEnumerable<string> args)
_queryType = QueryType.GetInfo;
break;

case @"/i":
case @"/info":
if (_queryType != QueryType.List) throw new FormatException(@"/info must follow the list command");
_infoSwitch = true;
break;

case @"/s":
case @"/silent":
if (_queryType != QueryType.Uninstall)
throw new FormatException(@"/silent must follow the uninstall command");
_silent = true;
if (_queryType != QueryType.Uninstall) throw new FormatException(@"/silent must follow the uninstall command");
_silentSwitch = true;
break;

case @"l":
Expand Down
8 changes: 8 additions & 0 deletions source/SteamHelper/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"SteamHelper": {
"commandName": "Project",
"commandLineArgs": "list /info"
}
}
}
49 changes: 39 additions & 10 deletions source/SteamHelper/SteamApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ Apache License Version 2.0
*/

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

namespace SteamHelper
{
Expand All @@ -16,22 +19,33 @@ private SteamApplicationInfo(int appId)
AppId = appId;
}

public static SteamApplicationInfo FromAppId(int appId)
public static SteamApplicationInfo FromAppManifest(FileInfo manifestFile, DirectoryInfo containingAppsDir)
{
var steam = SteamInstallation.Instance;
var m = Regex.Match(manifestFile.Name, @"appmanifest_(\d+)\.acf");
if (m.Success)
{
var idStr = m.Groups[1].Value;
if (!string.IsNullOrEmpty(idStr))
{
return FromParams(int.Parse(idStr, CultureInfo.InvariantCulture), manifestFile, containingAppsDir);
}
}
return null;
}

public static SteamApplicationInfo FromAppId(int appId)
{
DirectoryInfo dir = null;
FileInfo manifestFile = null;
var appIdStr = appId.ToString("G");

var output = new SteamApplicationInfo(appId);

foreach (var steamAppsLocation in steam.SteamAppsLocations)
foreach (var steamAppsLocation in SteamInstallation.Instance.SteamAppsLocations)
{
var result = Directory.GetFiles(steamAppsLocation, @"appmanifest_*.acf")
.FirstOrDefault(x => appIdStr.Equals(Path.GetFileNameWithoutExtension(x).Substring(12), StringComparison.InvariantCulture));
if (!string.IsNullOrEmpty(result))
{
output.ManifestPath = result;
manifestFile = new FileInfo(result);
dir = new DirectoryInfo(steamAppsLocation);
break;
}
Expand All @@ -40,26 +54,35 @@ public static SteamApplicationInfo FromAppId(int appId)
if (dir == null)
throw new ArgumentException("Could not find Steam App with the ID " + appIdStr);

return FromParams(appId, manifestFile, dir);
}

private static SteamApplicationInfo FromParams(int appId, FileInfo manifestFile, DirectoryInfo containingAppsDir)
{
var appIdStr = appId.ToString("G");
var output = new SteamApplicationInfo(appId);
output.ManifestPath = manifestFile.FullName;

//"C:\Steam\steam.exe" steam://uninstall/123
output.UninstallString = $"\"{steam.MainExecutableFilename}\" steam://uninstall/{appIdStr}";
output.UninstallString = $"\"{SteamInstallation.Instance.MainExecutableFilename}\" steam://uninstall/{appIdStr}";

var manifestStrings = File.ReadAllLines(output.ManifestPath);
output.Name = GetManifestValue(manifestStrings, "name");
output.SizeOnDisk = GetManifestValue(manifestStrings, "SizeOnDisk");
var installDirName = GetManifestValue(manifestStrings, "installdir");
if (!string.IsNullOrEmpty(installDirName))
{
var path = Path.Combine(Path.Combine(dir.FullName, @"common"), installDirName);
var path = Path.Combine(Path.Combine(containingAppsDir.FullName, @"common"), installDirName);
if (Directory.Exists(path))
output.InstallDirectory = path;

if (output.Name == null)
output.Name = installDirName;
}

output.DownloadDirectory = dir.GetDirectories(@"downloading").SingleOrDefault()?.GetDirectories(appIdStr).SingleOrDefault()?.FullName;
output.DownloadDirectory = containingAppsDir.GetDirectories(@"downloading").SingleOrDefault()?.GetDirectories(appIdStr).SingleOrDefault()?.FullName;

var workshopDir = dir.GetDirectories(@"workshop").SingleOrDefault();
var workshopDir = containingAppsDir.GetDirectories(@"workshop").SingleOrDefault();
if (workshopDir != null)
{
output.WorkshopManifestPath = workshopDir.GetFiles($@"appworkshop_{appIdStr}.acf").SingleOrDefault()?.FullName;
Expand All @@ -86,5 +109,11 @@ public static string GetManifestValue(string[] manifestStrings, string keyName)
public string DownloadDirectory { get; private set; }
public string WorkshopManifestPath { get; private set; }
public string WorkshopDirectory { get; private set; }

public void WriteTo(TextWriter wr)
{
foreach (var property in typeof(SteamApplicationInfo).GetProperties(BindingFlags.Public | BindingFlags.Instance))
wr.WriteLine("{0} - {1}", property.Name, property.GetValue(this, null) ?? "N/A");
}
}
}

0 comments on commit 847b1a9

Please sign in to comment.