Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiler hot path fix: unneeded LINQ .Any invocation #34

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion MenuAPI/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ protected virtual void DynamicListItemSelectEvent(Menu menu, MenuDynamicListItem

private int index = 0;

private bool visible = false;

public int ViewIndexOffset { get; private set; } = 0;

private List<MenuItem> VisibleMenuItems
Expand Down Expand Up @@ -371,7 +373,26 @@ private List<MenuItem> VisibleMenuItems

public int Size => filterActive ? FilterItems.Count : MenuItems.Count;

public bool Visible { get; set; } = false;
public bool Visible
{
get
{
return visible;
}
set
{
if (value)
{
MenuController.VisibleMenus.Add(this);
}
else
{
MenuController.VisibleMenus.Remove(this);
}

visible = value;
}
}

#if FIVEM
public bool LeftAligned => MenuController.MenuAlignment == MenuController.MenuAlignmentOption.Left;
Expand Down
11 changes: 6 additions & 5 deletions MenuAPI/MenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace MenuAPI
public class MenuController : BaseScript
{
public static List<Menu> Menus { get; protected set; } = new List<Menu>();
internal static HashSet<Menu> VisibleMenus { get; } = new HashSet<Menu>();
#if FIVEM
public const string _texture_dict = "commonmenu";
public const string _header_texture = "interaction_bgd";
Expand Down Expand Up @@ -52,11 +53,11 @@ public class MenuController : BaseScript
public static float ScreenHeight => 1080;
public static bool DisableMenuButtons { get; set; } = false;
#if FIVEM
public static bool AreMenuButtonsEnabled => Menus.Any((m) => m.Visible) && !Game.IsPaused && CitizenFX.Core.UI.Screen.Fading.IsFadedIn && !IsPlayerSwitchInProgress() && !DisableMenuButtons && !Game.Player.IsDead;
public static bool AreMenuButtonsEnabled => IsAnyMenuOpen() && !Game.IsPaused && CitizenFX.Core.UI.Screen.Fading.IsFadedIn && !IsPlayerSwitchInProgress() && !DisableMenuButtons && !Game.Player.IsDead;
#endif
#if REDM
public static bool AreMenuButtonsEnabled =>
Menus.Any((m) => m.Visible) &&
IsAnyMenuOpen() &&
!Call<bool>(IS_PAUSE_MENU_ACTIVE) &&
Call<bool>(IS_SCREEN_FADED_IN) &&
!DisableMenuButtons &&
Expand Down Expand Up @@ -268,16 +269,16 @@ private static void UnloadAssets()
/// <returns></returns>
public static Menu GetCurrentMenu()
{
if (Menus.Any((m) => m.Visible))
return Menus.Find((m) => m.Visible);
if (IsAnyMenuOpen())
return VisibleMenus.FirstOrDefault();
return null;
}

/// <summary>
/// Returns true if any menu is currently open.
/// </summary>
/// <returns></returns>
public static bool IsAnyMenuOpen() => Menus.Any((m) => m.Visible);
public static bool IsAnyMenuOpen() => VisibleMenus.Count > 0;


#region Process Menu Buttons
Expand Down