Skip to content

Commit

Permalink
Use cross-platform ctrl+c handler for EOBot
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Feb 10, 2022
1 parent d52a781 commit f56d5fd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 33 deletions.
16 changes: 4 additions & 12 deletions EOBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace EOBot
static class Program
{
private static BotFramework f;
private static Win32.ConsoleCtrlDelegate consoleControlHandler;

[AutoMappedType]
class NpcWalkNotifier : INPCActionNotifier
Expand Down Expand Up @@ -167,12 +166,7 @@ static async Task<int> Main(string[] args)
"EOLib.Logger"
};

// this needs to be a delegate because it is getting garbage collected
consoleControlHandler = new Win32.ConsoleCtrlDelegate(HandleCtrl);
if (!Win32.SetConsoleCtrlHandler(consoleControlHandler, true))
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Warning, "Unable to set console control handler! CTRL+C will not terminate cleanly.", ConsoleColor.DarkYellow);
}
Console.CancelKeyPress += HandleCtrlC;

ArgumentsParser parsedArgs = new ArgumentsParser(args);

Expand Down Expand Up @@ -230,14 +224,12 @@ static async Task<int> Main(string[] args)
return 0;
}

static bool HandleCtrl(Win32.CtrlTypes type)
static void HandleCtrlC(object sender, ConsoleCancelEventArgs e)
{
var name = Enum.GetName(type.GetType(), type);
var name = Enum.GetName(e.SpecialKey.GetType(), e.SpecialKey);
ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, $"Exiting due to {name} event from system");

if (f != null)
f.TerminateBots();
return true;
f?.TerminateBots();
}

static void ShowError(ArgumentsParser args)
Expand Down
21 changes: 0 additions & 21 deletions EOBot/Win32.cs

This file was deleted.

0 comments on commit f56d5fd

Please sign in to comment.