1+ using System ;
2+ using System . Collections . Generic ;
13using System . Linq ;
24
35using AngleSharp . Dom ;
68using CounterStrikeSharp . API . Core ;
79using CounterStrikeSharp . API . Modules . Menu ;
810
11+ using CS2ScreenMenuAPI ;
12+
913using CSSUniversalMenuAPI ;
1014using CSSUniversalMenuAPI . Extensions ;
1115
1216using HarmonyLib ;
1317
18+ using IUniversalMenu = CSSUniversalMenuAPI . IMenu ;
19+
1420namespace UniversalMenu . Compat . CSSharp ;
1521
1622public static class CSSSharpCompatPluginShared
1723{
1824 private static Harmony ? Harmony { get ; set ; }
25+ private static Dictionary < nint , IUniversalMenu > ActiveMenus { get ; } = new ( ) ;
1926
2027 public static void Patch ( )
2128 {
22- Harmony = new Harmony ( "com.universalmenu.cssharp" ) ;
29+ Harmony = new Harmony ( "com.universalmenu.compat. cssharp" ) ;
2330
2431 {
2532 var original = AccessTools . Method ( typeof ( MenuManager ) , nameof ( MenuManager . OpenCenterHtmlMenu ) ) ;
@@ -29,13 +36,19 @@ public static void Patch()
2936
3037 {
3138 var original = AccessTools . Method ( typeof ( MenuManager ) , nameof ( MenuManager . OpenChatMenu ) ) ;
32- var pre = SymbolExtensions . GetMethodInfo ( ( ) => MenuManager_OpenChatMenu ( null ! , null ! , null ! ) ) ;
39+ var pre = SymbolExtensions . GetMethodInfo ( ( ) => MenuManager_OpenChatMenu ( null ! , null ! ) ) ;
3340 Harmony . Patch ( original , prefix : new HarmonyMethod ( pre ) ) ;
3441 }
3542
3643 {
3744 var original = AccessTools . Method ( typeof ( MenuManager ) , nameof ( MenuManager . OpenConsoleMenu ) ) ;
38- var pre = SymbolExtensions . GetMethodInfo ( ( ) => MenuManager_OpenConsoleMenu ( null ! , null ! , null ! ) ) ;
45+ var pre = SymbolExtensions . GetMethodInfo ( ( ) => MenuManager_OpenConsoleMenu ( null ! , null ! ) ) ;
46+ Harmony . Patch ( original , prefix : new HarmonyMethod ( pre ) ) ;
47+ }
48+
49+ {
50+ var original = AccessTools . Method ( typeof ( MenuManager ) , nameof ( MenuManager . CloseActiveMenu ) ) ;
51+ var pre = SymbolExtensions . GetMethodInfo ( ( ) => MenuManager_CloseActiveMenu ( null ! ) ) ;
3952 Harmony . Patch ( original , prefix : new HarmonyMethod ( pre ) ) ;
4053 }
4154 }
@@ -45,8 +58,16 @@ public static void Unpatch()
4558 Harmony ? . UnpatchAll ( ) ;
4659 }
4760
48- public static bool BaseMenu_Open ( BasePlugin plugin , CCSPlayerController player , BaseMenu menu )
61+ public static bool BaseMenu_Open ( BasePlugin ? plugin , CCSPlayerController player , BaseMenu menu )
4962 {
63+ // this API assumes there may only be 1 menu at a time, so tell the implementation
64+ // that this menu is expected to be closed
65+ if ( ActiveMenus . TryGetValue ( player . Handle , out var activeMenu ) )
66+ {
67+ ActiveMenus . Remove ( player . Handle ) ;
68+ activeMenu . Close ( ) ;
69+ }
70+
5071 var api = IMenuAPI . PluginCapability . Get ( ) ;
5172
5273 if ( api is null ) // fall back to builtin menu
@@ -77,29 +98,46 @@ public static bool BaseMenu_Open(BasePlugin plugin, CCSPlayerController player,
7798 if ( item . OnSelect is not null )
7899 newItem . Selected += ( selectedItem ) =>
79100 {
101+ item . OnSelect ( player , item ) ;
80102 if ( menu . PostSelectAction == PostSelectAction . Close )
103+ {
104+ if ( ActiveMenus . TryGetValue ( player . Handle , out var activeMenu ) && activeMenu == newMenu )
105+ ActiveMenus . Remove ( player . Handle ) ;
81106 newMenu . Close ( ) ;
82- item . OnSelect ( player , item ) ;
107+ }
83108 } ;
84109 }
85110
111+ ActiveMenus [ player . Handle ] = newMenu ;
86112 newMenu . Display ( ) ;
87113 return false ;
88114 }
89115
116+ public static bool MenuManager_CloseActiveMenu ( CCSPlayerController player )
117+ {
118+ Console . WriteLine ( $ "Close active menu 1") ;
119+ if ( ActiveMenus . TryGetValue ( player . Handle , out var activeMenu ) )
120+ {
121+ Console . WriteLine ( $ "Close active menu 2") ;
122+ ActiveMenus . Remove ( player . Handle ) ;
123+ activeMenu . Close ( ) ;
124+ }
125+ return true ;
126+ }
127+
90128 public static bool MenuManager_OpenCenterHtmlMenu ( BasePlugin plugin , CCSPlayerController player , CenterHtmlMenu menu )
91129 {
92130 return BaseMenu_Open ( plugin , player , menu ) ;
93131 }
94132
95- public static bool MenuManager_OpenChatMenu ( BasePlugin plugin , CCSPlayerController player , ChatMenu menu )
133+ public static bool MenuManager_OpenChatMenu ( CCSPlayerController player , ChatMenu menu )
96134 {
97- return BaseMenu_Open ( plugin , player , menu ) ;
135+ return BaseMenu_Open ( null , player , menu ) ;
98136 }
99137
100- public static bool MenuManager_OpenConsoleMenu ( BasePlugin plugin , CCSPlayerController player , ConsoleMenu menu )
138+ public static bool MenuManager_OpenConsoleMenu ( CCSPlayerController player , ConsoleMenu menu )
101139 {
102- return BaseMenu_Open ( plugin , player , menu ) ;
140+ return BaseMenu_Open ( null , player , menu ) ;
103141 }
104142
105143 private static readonly HtmlParser HtmlParser = new ( new HtmlParserOptions ( ) { IsStrictMode = false } ) ;
0 commit comments