Automatically detects steam install location now.

This commit is contained in:
That-One-Nerd 2023-05-11 15:38:41 -04:00
parent 321d6acb7f
commit c0afa7dbb4
3 changed files with 52 additions and 30 deletions

View File

@ -1,10 +1,12 @@
global using Nerd_STF.Mathematics; global using Microsoft.Win32;
global using Nerd_STF.Mathematics;
global using Newtonsoft.Json; global using Newtonsoft.Json;
global using SharpCompress.Archives.Rar; global using SharpCompress.Archives.Rar;
global using SharpCompress.Archives.SevenZip; global using SharpCompress.Archives.SevenZip;
global using SharpCompress.Readers; global using SharpCompress.Readers;
global using SrcMod.Shell.Extensions; global using SrcMod.Shell.Extensions;
global using SrcMod.Shell.Interop; global using SrcMod.Shell.Interop;
global using SrcMod.Shell.Modules;
global using SrcMod.Shell.Modules.ObjectModels; global using SrcMod.Shell.Modules.ObjectModels;
global using SrcMod.Shell.ObjectModels; global using SrcMod.Shell.ObjectModels;
global using SrcMod.Shell.ObjectModels.Steam; global using SrcMod.Shell.ObjectModels.Steam;

View File

@ -22,8 +22,11 @@ public class Config
private static Config p_applied; private static Config p_applied;
private static Changes? p_changes; private static Changes? p_changes;
private static string p_steamLocation;
static Config() static Config()
{ {
// Generate shared fields between the config class and its changes equivalent.
p_applied = Defaults; p_applied = Defaults;
FieldInfo[] configFields = (from field in typeof(Config).GetFields() FieldInfo[] configFields = (from field in typeof(Config).GetFields()
@ -65,8 +68,25 @@ public class Config
internal Config() internal Config()
{ {
// TODO: This won't work if the steam installation is somewhere else. // Locate some steam stuff.
const string gameDirDataPath = @"C:\Program Files (x86)\Steam\steamapps\libraryfolders.vdf"; const string steamLocationKey = @"Software\Valve\Steam";
RegistryKey? key = Registry.CurrentUser.OpenSubKey(steamLocationKey);
if (key is null)
{
Write("[FATAL] Cannot locate Steam installation. Do you have Steam installed?",
ConsoleColor.DarkRed);
Thread.Sleep(1000);
BaseModule.QuitShell(-1);
// This should never run, and is just here to supress
// a couple compiler warnings.
p_steamLocation = string.Empty;
RunUnsafeCommands = AskMode.Ask;
return;
}
p_steamLocation = (string)key.GetValue("SteamPath")!;
string gameDirDataPath = Path.Combine(p_steamLocation, @"steamapps\libraryfolders.vdf");
VkvSerializer serializer = new(new() VkvSerializer serializer = new(new()
{ {
@ -78,7 +98,7 @@ public class Config
LibraryFolder[]? folders = serializer.Deserialize<LibraryFolder[]>(gameDirData); LibraryFolder[]? folders = serializer.Deserialize<LibraryFolder[]>(gameDirData);
if (folders is null) if (folders is null)
{ {
Write("[WARNING] Error parsing steam game directories."); Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow);
GameDirectories = Array.Empty<string>(); GameDirectories = Array.Empty<string>();
} }
else else

View File

@ -16,10 +16,10 @@ public class Shell
public List<HistoryItem> History; public List<HistoryItem> History;
public string WorkingDirectory; public string WorkingDirectory;
private bool lastCancel; private bool p_lastCancel;
private bool printedCancel; private bool p_printedCancel;
private BackgroundWorker? activeCommand; private BackgroundWorker? p_activeCommand;
public Shell() public Shell()
{ {
@ -88,8 +88,8 @@ public class Shell
Write(" by ", ConsoleColor.White, false); Write(" by ", ConsoleColor.White, false);
Write($"{Author}", ConsoleColor.DarkYellow); Write($"{Author}", ConsoleColor.DarkYellow);
lastCancel = false; p_lastCancel = false;
activeCommand = null; p_activeCommand = null;
Console.CancelKeyPress += HandleCancel; Console.CancelKeyPress += HandleCancel;
ActiveGame = null; ActiveGame = null;
@ -149,7 +149,7 @@ public class Shell
bool printed = false; bool printed = false;
if (lastCancel && !printedCancel) if (p_lastCancel && !p_printedCancel)
{ {
// Print the warning. A little bit of mess because execution must // Print the warning. A little bit of mess because execution must
// continue without funny printing errors but it's alright I guess. // continue without funny printing errors but it's alright I guess.
@ -160,7 +160,7 @@ public class Shell
Write("Press ^C again to exit the shell.", ConsoleColor.Red); Write("Press ^C again to exit the shell.", ConsoleColor.Red);
PlayWarningSound(); PlayWarningSound();
printedCancel = true; p_printedCancel = true;
Console.CursorTop += 2; Console.CursorTop += 2;
Console.CursorLeft = originalLeft; Console.CursorLeft = originalLeft;
@ -175,8 +175,8 @@ public class Shell
if (!printed) if (!printed)
{ {
lastCancel = false; p_lastCancel = false;
printedCancel = false; p_printedCancel = false;
} }
return message; return message;
@ -262,18 +262,18 @@ public class Shell
} }
} }
activeCommand = new(); p_activeCommand = new();
activeCommand.DoWork += runCommand; p_activeCommand.DoWork += runCommand;
activeCommand.RunWorkerAsync(); p_activeCommand.RunWorkerAsync();
activeCommand.WorkerSupportsCancellation = command.CanBeCancelled; p_activeCommand.WorkerSupportsCancellation = command.CanBeCancelled;
while (activeCommand is not null && activeCommand.IsBusy) Thread.Yield(); while (p_activeCommand is not null && p_activeCommand.IsBusy) Thread.Yield();
if (activeCommand is not null) if (p_activeCommand is not null)
{ {
activeCommand.Dispose(); p_activeCommand.Dispose();
activeCommand = null; p_activeCommand = null;
} }
if (ShellDirectory is null) Write("[WARNING] Could not save config to shell location. Any changes will be ignored."); if (ShellDirectory is null) Write("[WARNING] Could not save config to shell location. Any changes will be ignored.");
@ -310,14 +310,14 @@ public class Shell
private void HandleCancel(object? sender, ConsoleCancelEventArgs args) private void HandleCancel(object? sender, ConsoleCancelEventArgs args)
{ {
if (activeCommand is not null && activeCommand.IsBusy) if (p_activeCommand is not null && p_activeCommand.IsBusy)
{ {
if (activeCommand.WorkerSupportsCancellation) if (p_activeCommand.WorkerSupportsCancellation)
{ {
// Kill the active command. // Kill the active command.
activeCommand.CancelAsync(); p_activeCommand.CancelAsync();
activeCommand.Dispose(); p_activeCommand.Dispose();
activeCommand = null; p_activeCommand = null;
} }
else else
{ {
@ -326,18 +326,18 @@ public class Shell
PlayErrorSound(); PlayErrorSound();
} }
lastCancel = false; p_lastCancel = false;
printedCancel = false; p_printedCancel = false;
args.Cancel = true; args.Cancel = true;
return; return;
} }
// Due to some funny multithreading issues, we want to make the warning label // Due to some funny multithreading issues, we want to make the warning label
// single-threaded on the shell. // single-threaded on the shell.
if (!lastCancel) if (!p_lastCancel)
{ {
// Enable the warning. The "ReadLine" method will do the rest. // Enable the warning. The "ReadLine" method will do the rest.
lastCancel = true; p_lastCancel = true;
args.Cancel = true; // "Cancel" referring to the cancellation of the cancel operation. args.Cancel = true; // "Cancel" referring to the cancellation of the cancel operation.
return; return;
} }