Added some error handling in the shell.
This commit is contained in:
parent
2abadc4071
commit
5b2b0abfa3
@ -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);
|
||||||
LibraryFolder[]? folders = SerializeVkv.Deserialize<LibraryFolder[]>(gameDirData);
|
try
|
||||||
if (folders is null)
|
|
||||||
{
|
{
|
||||||
Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow);
|
LibraryFolder[]? folders = SerializeVkv.Deserialize<LibraryFolder[]>(gameDirData);
|
||||||
GameDirectories = Array.Empty<string>();
|
if (folders is null)
|
||||||
|
{
|
||||||
|
if (!p_printedLastSteamWarning)
|
||||||
|
Write("[WARNING] Error parsing Steam game directories.", ConsoleColor.DarkYellow);
|
||||||
|
GameDirectories = Array.Empty<string>();
|
||||||
|
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];
|
if (!p_printedLastSteamWarning)
|
||||||
for (int i = 0; i < folders.Length; i++) GameDirectories[i] = folders[i].path;
|
{
|
||||||
|
#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;
|
||||||
|
|||||||
@ -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)
|
||||||
@ -309,12 +320,32 @@ public class Shell
|
|||||||
|
|
||||||
public void ReloadDirectoryInfo()
|
public void ReloadDirectoryInfo()
|
||||||
{
|
{
|
||||||
ActiveMod = Mod.ReadDirectory(WorkingDirectory);
|
try
|
||||||
|
{
|
||||||
|
ActiveMod = Mod.ReadDirectory(WorkingDirectory);
|
||||||
|
|
||||||
// Update title.
|
// Update title.
|
||||||
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)
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
namespace Valve.Vkv;
|
using System.Reflection.PortableExecutable;
|
||||||
|
|
||||||
|
namespace Valve.Vkv;
|
||||||
|
|
||||||
public class VkvSerializer
|
public class VkvSerializer
|
||||||
{
|
{
|
||||||
@ -16,11 +18,18 @@ 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);
|
||||||
VkvNode? result = VkvConvert.DeserializeNode(reader, p_options);
|
try
|
||||||
reader.Close();
|
{
|
||||||
|
VkvNode? result = VkvConvert.DeserializeNode(reader, p_options);
|
||||||
if (!p_options.closeWhenFinished && p_options.resetStreamPosition) stream.Seek(pos, SeekOrigin.Begin);
|
reader.Close();
|
||||||
return result;
|
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<T>(Stream stream)
|
public T? Deserialize<T>(Stream stream)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user