Skip to content

Commit

Permalink
Dedicated error/warning methods
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed May 12, 2024
1 parent f274abd commit c949ac7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 211 deletions.
25 changes: 9 additions & 16 deletions CommandLineArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public void AddArguments(IEnumerable<string> args)
string argKey = argText[..equalsIndex];
if (!keyValueOptions.TryAdd(argKey, argText[(equalsIndex + 1)..]) && PrintWarnings)
{
PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Exists_KeyValue, argKey));
Program.PrintWarning(Strings.CommandLineArgs_Warning_Exists_KeyValue, argKey);
}
}
else if (!multiCharacterOptions.Add(argText) && PrintWarnings)
{
PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Exists_MultiCharacter, argText));
Program.PrintWarning(Strings.CommandLineArgs_Warning_Exists_MultiCharacter, argText);
}
}
else
Expand All @@ -102,7 +102,7 @@ public void AddArguments(IEnumerable<string> args)
{
if (!singleCharacterOptions.Add(c) && PrintWarnings)
{
PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Exists_SingleCharacter, c));
Program.PrintWarning(Strings.CommandLineArgs_Warning_Exists_SingleCharacter, c);
}
}
}
Expand Down Expand Up @@ -170,15 +170,15 @@ public void WarnUnconsumedOptions()
{
foreach (char singleArgument in singleCharacterOptions.Except(consumedSingleCharacterOptions))
{
PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Unconsumed_SingleCharacter, singleArgument));
Program.PrintWarning(Strings.CommandLineArgs_Warning_Unconsumed_SingleCharacter, singleArgument);
}
foreach (string multiArgument in multiCharacterOptions.Except(consumedMultiCharacterOptions))
{
PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Unconsumed_MultiCharacter, multiArgument));
Program.PrintWarning(Strings.CommandLineArgs_Warning_Unconsumed_MultiCharacter, multiArgument);
}
foreach (string key in keyValueOptions.Keys.Except(consumedKeyValueOptions))
{
PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Unconsumed_KeyValue, key));
Program.PrintWarning(Strings.CommandLineArgs_Warning_Unconsumed_KeyValue, key);
}
}

Expand All @@ -194,17 +194,10 @@ public void WarnUnconsumedOptions(int maxPositionalArgsLength)
if (positionalArgs.Count > maxPositionalArgsLength)
{
int excess = positionalArgs.Count - maxPositionalArgsLength;
PrintWarning(excess == 1
? string.Format(Strings.CommandLineArgs_Warning_Unconsumed_Positional_Single, excess)
: string.Format(Strings.CommandLineArgs_Warning_Unconsumed_Positional_Multiple, excess));
Program.PrintWarning(excess == 1
? Strings.CommandLineArgs_Warning_Unconsumed_Positional_Single
: Strings.CommandLineArgs_Warning_Unconsumed_Positional_Multiple, excess);
}
}

private static void PrintWarning([Localizable(true)] string warningText)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(warningText);
Console.ResetColor();
}
}
}
128 changes: 32 additions & 96 deletions Debugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public void LoadDebugFile(string debugFilePath)
}
catch (Exception exc)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_Debug_Info_File, exc.GetType().Name, exc.Message);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_Debug_Info_File, exc.GetType().Name, exc.Message);
#if DEBUG
throw;
#endif
Expand Down Expand Up @@ -305,18 +303,14 @@ public void StartDebugger()
CommandDebugHelp();
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Unrecognised_Command, command[0]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Unrecognised_Command, command[0]);
break;
}
}
}
if (DebuggingProcessor.Execute(false) && !InReplMode)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_HLT_Reached);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_HLT_Reached);
Console.Write(Strings.Debugger_Any_Key_Continue);
_ = Console.ReadKey(true);
Console.WriteLine();
Expand Down Expand Up @@ -352,23 +346,17 @@ private void CommandReadMemory(IReadOnlyList<string> command)
? 2 : command[1] == "dword" ? 4 : command[1] == "qword" ? 8 : 0U;
if (bytesToRead == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Size, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Size, command[1]);
return;
}
if (!ulong.TryParse(command[2], out ulong address))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Address, command[2]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Address, command[2]);
return;
}
if (address + bytesToRead > (ulong)DebuggingProcessor.Memory.LongLength)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_OutOfRange_Address, command[2]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_OutOfRange_Address, command[2]);
return;
}
ulong value = 0;
Expand All @@ -380,9 +368,7 @@ private void CommandReadMemory(IReadOnlyList<string> command)
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_2);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_2);
}
}

Expand All @@ -396,23 +382,17 @@ private void CommandWriteMemReg(IReadOnlyList<string> command)
{
if (!ulong.TryParse(command[2], out ulong address))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Address, command[2]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Address, command[2]);
return;
}
if (address >= (ulong)DebuggingProcessor.Memory.LongLength)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_OutOfRange_Address, command[2]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_OutOfRange_Address, command[2]);
return;
}
if (!byte.TryParse(command[3], out byte value))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Byte_Value, command[3]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Byte_Value, command[3]);
return;
}
DebuggingProcessor.Memory[address] = value;
Expand All @@ -423,34 +403,26 @@ private void CommandWriteMemReg(IReadOnlyList<string> command)
{
if (!Enum.TryParse(command[2], out Register register))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Register, command[2]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Register, command[2]);
return;
}
if (!ulong.TryParse(command[3], out ulong value))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Register_Value, command[3]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Register_Value, command[3]);
return;
}
DebuggingProcessor.Registers[(int)register] = value;
Console.WriteLine(Strings.Debugger_Success_Register_Value_Set, Enum.GetName(register), value);
break;
}
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Location, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Location, command[1]);
break;
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_3);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_3);
}
}

Expand All @@ -462,27 +434,21 @@ private void CommandMapMemory(string[] command)
{
if (!ulong.TryParse(command[1], out offset))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Offset, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Offset, command[1]);
return;
}
}
if (command.Length == 3)
{
if (!ulong.TryParse(command[2], out limit))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Limit, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Limit, command[1]);
return;
}
}
if (command.Length is not 1 and > 3)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_0to2);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_0to2);
return;
}
Console.ForegroundColor = ConsoleColor.Blue;
Expand Down Expand Up @@ -597,32 +563,24 @@ private void CommandFormatStack(string[] command)
{
if (!ulong.TryParse(command[1], out limit))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Limit, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Limit, command[1]);
return;
}
}
else if (command.Length != 1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_0to1);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_0to1);
return;
}

if (DebuggingProcessor.Registers[(int)Register.rso] >= (ulong)DebuggingProcessor.Memory.LongLength)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_Stack_Empty);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_Stack_Empty);
return;
}
if (DebuggingProcessor.Registers[(int)Register.rso] > DebuggingProcessor.Registers[(int)Register.rsb])
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_rso_GT_rsb);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_rso_GT_rsb);
return;
}

Expand Down Expand Up @@ -704,26 +662,20 @@ private void CommandFormatStack(string[] command)
}
else
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_Stack_Bottom);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_Stack_Bottom);
}
}

private static void CommandDecimalToHexadecimal(string[] command)
{
if (command.Length != 2)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_1);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_1);
return;
}
if (!ulong.TryParse(command[1], out ulong decValue))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Convert_Value, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Convert_Value, command[1]);
return;
}
Console.WriteLine(Strings.Debugger_Value_In_Hex, decValue, decValue);
Expand All @@ -733,9 +685,7 @@ private static void CommandHexadecimalToDecimal(string[] command)
{
if (command.Length != 2)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_1);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_1);
return;
}
try
Expand All @@ -745,9 +695,7 @@ private static void CommandHexadecimalToDecimal(string[] command)
}
catch
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Convert_Value, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Convert_Value, command[1]);
}
}

Expand All @@ -765,16 +713,12 @@ private void CommandBreakpointManage(string[] command)
{
if (!Enum.TryParse(command[2], out Register register))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Register, command[2]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Register, command[2]);
return;
}
if (!ulong.TryParse(command[3], out ulong value))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Break_Value, command[3]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Break_Value, command[3]);
return;
}
switch (command[1].ToLower())
Expand All @@ -787,35 +731,27 @@ private void CommandBreakpointManage(string[] command)
}
else
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_Breakpoint_Exists, register, value);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_Breakpoint_Exists, register, value);
}
break;
case "remove":
if (Breakpoints.RemoveAll(x => x.Register == register && x.Value == value) == 0)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(Strings.Debugger_Warning_Breakpoint_No_Matching);
Console.ResetColor();
Program.PrintWarning(Strings.Debugger_Warning_Breakpoint_No_Matching);
}
else
{
Console.WriteLine(Strings.Debugger_Success_Breakpoint_Remove, register, value);
}
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Invalid_Breakpoint_Action, command[1]);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Invalid_Breakpoint_Action, command[1]);
break;
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Strings.Debugger_Error_Args_Required_Breakpoint);
Console.ResetColor();
Program.PrintError(Strings.Debugger_Error_Args_Required_Breakpoint);
}
}

Expand Down
Loading

0 comments on commit c949ac7

Please sign in to comment.