LemonUIbyLemon / LemonUI

LemonUI for .NET (FiveM, RageMP, RagePluginHook and ScriptHookVDotNet 3)
MIT License
177 stars 40 forks source link

Trying to move up or down in a menu with only separators will make the game hang #158

Closed justalemon closed 3 months ago

justalemon commented 7 months ago

Tried making a menu that ended up accidentally with only separators. Turns out, the game hangs if they don't have any other item to select.

WithLithum commented 6 months ago

Explanation

The NativeMenu.Next() and NativeMenu.Previous() methods are used to navigate to the Next or Previous items in a NativeMenu. (Note: these methods are publicly callable, so if you call one of them in this situation the game hangs as well) Two methods works the exact same way other than one moves forwards and the other one moves backwards.

The Next and Previous item logic is ran inside a while loop:

  1. It peeks the next item by moving nextIndex forwards or backwards by one - we call it cursor.
  2. If the cursor encounters a separator, it skips to the next item, and goes back to step 1.
  3. If the cursor goes beyond (before) all of the items, it returns to the beginning (end), and goes back to step 1.
  4. If the cursor hits the current item (if there are only one valid item, step 2 and 3 will cause cursor to eventually go back to the only valid item), the loop is broken and the method returns.
  5. If the cursor hits another item than the current item, the loop is broken and the method returns.

This way it works fine as long as the menu have at least one valid item among all items. However, when there is a menu consisted entirely of separators, as the logic is completely unaware about the menu is entirely consisted of separators, the code will keep going back and forth between step 1 and step 2, completely blocking the "game thread" and not allowing the game engine and other scripts to execute.

Hince, the game hangs, until the script ticking the offending menu is aborted.

justalemon commented 6 months ago

I'm aware of that. I just remembered to push the fix as it's already fixed on my local repo.