diff --git a/SrcMod/Shell/ObjectModels/Config.cs b/SrcMod/Shell/ObjectModels/Config.cs index 9ef3cdf..af83ac4 100644 --- a/SrcMod/Shell/ObjectModels/Config.cs +++ b/SrcMod/Shell/ObjectModels/Config.cs @@ -4,6 +4,9 @@ public class Config { public const string FilePath = "config.json"; + public static bool HasDisplayableError => false; + public static bool HasDisplayableWarning => p_printedLastSteamWarning; + public static Config Defaults => new(); private static readonly FieldInfo[] p_configSharedFields; @@ -22,6 +25,8 @@ public class Config private static Config p_applied; 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. private readonly string p_steamLocation; @@ -93,16 +98,35 @@ public class Config string gameDirDataPath = Path.Combine(p_steamLocation, @"steamapps\libraryfolders.vdf"); FileStream gameDirData = new(gameDirDataPath, FileMode.Open); - LibraryFolder[]? folders = SerializeVkv.Deserialize(gameDirData); - if (folders is null) + try { - Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow); - GameDirectories = Array.Empty(); + LibraryFolder[]? folders = SerializeVkv.Deserialize(gameDirData); + if (folders is null) + { + if (!p_printedLastSteamWarning) + Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow); + GameDirectories = Array.Empty(); + p_printedLastSteamWarning = true; + } + else + { + GameDirectories = new string[folders.Length]; + for (int i = 0; i < folders.Length; i++) GameDirectories[i] = folders[i].path; + p_printedLastSteamWarning = false; + } } - else + catch (Exception ex) { - GameDirectories = new string[folders.Length]; - for (int i = 0; i < folders.Length; i++) GameDirectories[i] = folders[i].path; + if (!p_printedLastSteamWarning) + { +#if RELEASE + Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow); +#else + Write(ex, ConsoleColor.DarkYellow); +#endif + } + GameDirectories = Array.Empty(); + p_printedLastSteamWarning = true; } RunUnsafeCommands = AskMode.Ask; diff --git a/SrcMod/Shell/Shell.cs b/SrcMod/Shell/Shell.cs index ef5f7b0..e21cc26 100644 --- a/SrcMod/Shell/Shell.cs +++ b/SrcMod/Shell/Shell.cs @@ -6,6 +6,12 @@ public class Shell public const string Name = "SrcMod"; 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 List LoadedCommands; @@ -19,6 +25,8 @@ public class Shell private bool p_lastCancel; private bool p_printedCancel; + private bool p_printedLastReloadError; + private BackgroundWorker? p_activeCommand; public Shell() @@ -140,6 +148,9 @@ public class Shell public string ReadLine() { 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) @@ -309,12 +320,32 @@ public class Shell public void ReloadDirectoryInfo() { - ActiveMod = Mod.ReadDirectory(WorkingDirectory); + try + { + ActiveMod = Mod.ReadDirectory(WorkingDirectory); - // Update title. - string title = "SrcMod"; - if (ActiveMod is not null) title += $" - {ActiveMod.Name}"; - Console.Title = title; + // Update title. + string title = "SrcMod"; + if (ActiveMod is not null) title += $" - {ActiveMod.Name}"; + 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) diff --git a/SrcMod/Valve.NET/Vkv/VkvSerializer.cs b/SrcMod/Valve.NET/Vkv/VkvSerializer.cs index 135845b..fe3e8d0 100644 --- a/SrcMod/Valve.NET/Vkv/VkvSerializer.cs +++ b/SrcMod/Valve.NET/Vkv/VkvSerializer.cs @@ -1,4 +1,6 @@ -namespace Valve.Vkv; +using System.Reflection.PortableExecutable; + +namespace Valve.Vkv; public class VkvSerializer { @@ -16,11 +18,18 @@ public class VkvSerializer { long pos = stream.Position; StreamReader reader = new(stream, leaveOpen: !p_options.closeWhenFinished); - VkvNode? result = VkvConvert.DeserializeNode(reader, p_options); - reader.Close(); - - if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin); - return result; + try + { + VkvNode? result = VkvConvert.DeserializeNode(reader, p_options); + reader.Close(); + if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin); + return result; + } + finally + { + reader.Close(); + if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin); + } } public T? Deserialize(Stream stream) {