Added some error handling in the shell.

This commit is contained in:
That_One_Nerd 2023-05-12 10:47:13 -04:00
parent 2abadc4071
commit 5b2b0abfa3
3 changed files with 82 additions and 18 deletions

View File

@ -4,6 +4,9 @@ public class Config
{ {
public const string FilePath = "config.json"; public const string FilePath = "config.json";
public static bool HasDisplayableError => false;
public static bool HasDisplayableWarning => p_printedLastSteamWarning;
public static Config Defaults => new(); public static Config Defaults => new();
private static readonly FieldInfo[] p_configSharedFields; private static readonly FieldInfo[] p_configSharedFields;
@ -22,6 +25,8 @@ 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 bool p_printedLastSteamWarning;
// These variables should only exist in the Config class so they aren't marked as shared. // These variables should only exist in the Config class so they aren't marked as shared.
private readonly string p_steamLocation; private readonly string p_steamLocation;
@ -93,16 +98,35 @@ public class Config
string gameDirDataPath = Path.Combine(p_steamLocation, @"steamapps\libraryfolders.vdf"); string gameDirDataPath = Path.Combine(p_steamLocation, @"steamapps\libraryfolders.vdf");
FileStream gameDirData = new(gameDirDataPath, FileMode.Open); FileStream gameDirData = new(gameDirDataPath, FileMode.Open);
try
{
LibraryFolder[]? folders = SerializeVkv.Deserialize<LibraryFolder[]>(gameDirData); LibraryFolder[]? folders = SerializeVkv.Deserialize<LibraryFolder[]>(gameDirData);
if (folders is null) if (folders is null)
{ {
if (!p_printedLastSteamWarning)
Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow); Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow);
GameDirectories = Array.Empty<string>(); GameDirectories = Array.Empty<string>();
p_printedLastSteamWarning = true;
} }
else else
{ {
GameDirectories = new string[folders.Length]; GameDirectories = new string[folders.Length];
for (int i = 0; i < folders.Length; i++) GameDirectories[i] = folders[i].path; for (int i = 0; i < folders.Length; i++) GameDirectories[i] = folders[i].path;
p_printedLastSteamWarning = false;
}
}
catch (Exception ex)
{
if (!p_printedLastSteamWarning)
{
#if RELEASE
Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow);
#else
Write(ex, ConsoleColor.DarkYellow);
#endif
}
GameDirectories = Array.Empty<string>();
p_printedLastSteamWarning = true;
} }
RunUnsafeCommands = AskMode.Ask; RunUnsafeCommands = AskMode.Ask;

View File

@ -6,6 +6,12 @@ public class Shell
public const string Name = "SrcMod"; public const string Name = "SrcMod";
public const string Version = "Beta 0.5.0"; public const string Version = "Beta 0.5.0";
public bool HasAnyDisplayableError => HasDisplayableError || Config.HasDisplayableError;
public bool HasAnyDisplayableWarning => HasDisplayableWarning || Config.HasDisplayableWarning;
public bool HasDisplayableError => p_printedLastReloadError;
public bool HasDisplayableWarning => false;
public readonly string? ShellDirectory; public readonly string? ShellDirectory;
public List<CommandInfo> LoadedCommands; public List<CommandInfo> LoadedCommands;
@ -19,6 +25,8 @@ public class Shell
private bool p_lastCancel; private bool p_lastCancel;
private bool p_printedCancel; private bool p_printedCancel;
private bool p_printedLastReloadError;
private BackgroundWorker? p_activeCommand; private BackgroundWorker? p_activeCommand;
public Shell() public Shell()
@ -140,6 +148,9 @@ public class Shell
public string ReadLine() public string ReadLine()
{ {
Write("\n", newLine: false); Write("\n", newLine: false);
if (HasAnyDisplayableError) Write($"(Error) ", ConsoleColor.DarkRed, false);
else if (HasAnyDisplayableWarning) Write($"(Warning) ", ConsoleColor.DarkYellow, false);
if (ActiveMod is not null) Write($"{ActiveMod} ", ConsoleColor.Magenta, false); if (ActiveMod is not null) Write($"{ActiveMod} ", ConsoleColor.Magenta, false);
if (ActiveMod is not null) if (ActiveMod is not null)
@ -308,6 +319,8 @@ public class Shell
} }
public void ReloadDirectoryInfo() public void ReloadDirectoryInfo()
{
try
{ {
ActiveMod = Mod.ReadDirectory(WorkingDirectory); ActiveMod = Mod.ReadDirectory(WorkingDirectory);
@ -315,6 +328,24 @@ public class Shell
string title = "SrcMod"; string title = "SrcMod";
if (ActiveMod is not null) title += $" - {ActiveMod.Name}"; if (ActiveMod is not null) title += $" - {ActiveMod.Name}";
Console.Title = title; Console.Title = title;
p_printedLastReloadError = false;
}
catch (Exception ex)
{
if (!p_printedLastReloadError)
{
#if RELEASE
Write("[ERROR] Error reloading directory information. Some data may not update.",
ConsoleColor.Red);
#else
Write(ex, ConsoleColor.Red);
#endif
}
p_printedLastReloadError = true;
Console.Title = "SrcMod (Error)";
}
} }
private void HandleCancel(object? sender, ConsoleCancelEventArgs args) private void HandleCancel(object? sender, ConsoleCancelEventArgs args)

View File

@ -1,4 +1,6 @@
namespace Valve.Vkv; using System.Reflection.PortableExecutable;
namespace Valve.Vkv;
public class VkvSerializer public class VkvSerializer
{ {
@ -16,12 +18,19 @@ public class VkvSerializer
{ {
long pos = stream.Position; long pos = stream.Position;
StreamReader reader = new(stream, leaveOpen: !p_options.closeWhenFinished); StreamReader reader = new(stream, leaveOpen: !p_options.closeWhenFinished);
try
{
VkvNode? result = VkvConvert.DeserializeNode(reader, p_options); VkvNode? result = VkvConvert.DeserializeNode(reader, p_options);
reader.Close(); reader.Close();
if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin); if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin);
return result; return result;
} }
finally
{
reader.Close();
if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin);
}
}
public T? Deserialize<T>(Stream stream) public T? Deserialize<T>(Stream stream)
{ {
VkvNode? result = Deserialize(stream); VkvNode? result = Deserialize(stream);