altmp / coreclr-module

CoreClr (.NET Core Common Language Runtime) community made module
MIT License
17 stars 8 forks source link

[C# Client] Client Crashes on dynamic KeyDown Registration #23

Open riffy opened 4 months ago

riffy commented 4 months ago

Description of the problem

When dynamically registering (outside of ctors) / subscribing to Alt.OnKeyDown event, crashes the client. The ShowMenu function is invoked as an Action, meaning Action?.Invoke -> Alt.OnKeyDown -> crash.

private void ShowMenu()
{
  if (IsActive) return;
  // Register an alt keydown press since keycontroller will be disabled
  Alt.OnKeyDown += CloseMenuOnEscape;
  ....
}

/// <summary>
/// Closes the menu if Escape key is pressed
/// </summary>
private void CloseMenuOnEscape(Key key)
{
  if (key != Key.Escape) return;
  HideMenu();
}

private void HideMenu()
{
  if (!IsActive) return;
  Alt.OnKeyDown -= CloseMenuOnEscape;
  ...
}

The following Error is logged / produced:

[10:56:12][Error] FATAL EXCEPTION:
[10:56:12][Error] System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

   at System.Collections.Generic.HashSet`1.Enumerator.MoveNext()

   at AltV.Net.Client.Extensions.EnumerableExtensions.ForEachCatching[T](IEnumerable`1 enumerable, Action`1 action, String exceptionLocation)
Exception at event OnKeyDown: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at Client.Services.Input.Controllers.KeyController.OnKeyDown(Key key) in E:\MintV\gameserver\Client\Services\Allowance\Controllers\AvailabilityController.cs:line 66
   at AltV.Net.Client.Extensions.EnumerableExtensions.ForEachCatching[T](IEnumerable`1 enumerable, Action`1 action, String exceptionLocation) 

The only other OnKeyDown registration happens in a constructor, which doesn't crash the client:

public KeyController()
{
  Alt.OnKeyDown += OnKeyDown;
  Alt.OnKeyUp += OnKeyUp;
}

Environment