1919using UniverseLib . UI . Models ;
2020using UniverseLib . Utility ;
2121using HarmonyLib ;
22+ using UniverseLib . Runtime ;
2223
2324namespace UnityExplorer . CSConsole
2425{
@@ -61,6 +62,8 @@ public static class ConsoleController
6162
6263 public static void Init ( )
6364 {
65+ InitEventSystemPropertyHandlers ( ) ;
66+
6467 // Make sure console is supported on this platform
6568 try
6669 {
@@ -384,37 +387,55 @@ private static void SetCaretPosition(int caretPosition)
384387 RuntimeHelper . StartCoroutine ( SetCaretCoroutine ( caretPosition ) ) ;
385388 }
386389
387- internal static MemberInfo selectionGuardMemberInfo ;
390+ static void InitEventSystemPropertyHandlers ( )
391+ {
392+ try
393+ {
394+ foreach ( var member in typeof ( EventSystem ) . GetMembers ( AccessTools . all ) )
395+ {
396+ if ( member . Name == "m_CurrentSelected" )
397+ {
398+ Type backingType ;
399+ if ( member . MemberType == MemberTypes . Property )
400+ backingType = ( member as PropertyInfo ) . PropertyType ;
401+ else
402+ backingType = ( member as FieldInfo ) . FieldType ;
388403
389- internal static MemberInfo GetSelectionGuardMemberInfo ( )
404+ usingEventSystemDictionaryMembers = ReflectionUtility . IsDictionary ( backingType ) ;
405+ break ;
406+ }
407+ }
408+ }
409+ catch ( Exception ex )
410+ {
411+ ExplorerCore . LogWarning ( $ "Exception checking EventSystem property backing type: { ex } ") ;
412+ }
413+ }
414+
415+ static bool usingEventSystemDictionaryMembers ;
416+
417+ static readonly AmbiguousMemberHandler < EventSystem , GameObject > m_CurrentSelected_Handler_Normal = new ( "m_CurrentSelected" , "m_currentSelected" ) ;
418+ static readonly AmbiguousMemberHandler < EventSystem , Dictionary < int , GameObject > > m_CurrentSelected_Handler_Dictionary = new ( "m_CurrentSelected" , "m_currentSelected" ) ;
419+
420+ static readonly AmbiguousMemberHandler < EventSystem , bool > m_SelectionGuard_Handler_Normal = new ( "m_SelectionGuard" , "m_selectionGuard" ) ;
421+ static readonly AmbiguousMemberHandler < EventSystem , Dictionary < int , bool > > m_SelectionGuard_Handler_Dictionary = new ( "m_SelectionGuard" , "m_selectionGuard" ) ;
422+
423+ static void SetCurrentSelectedGameObject ( EventSystem instance , GameObject value )
390424 {
391- if ( selectionGuardMemberInfo != null )
392- return selectionGuardMemberInfo ;
393-
394- if ( AccessTools . Property ( typeof ( EventSystem ) , "m_SelectionGuard" ) is PropertyInfo pi_m_SelectionGuard )
395- return selectionGuardMemberInfo = pi_m_SelectionGuard ;
396-
397- if ( AccessTools . Property ( typeof ( EventSystem ) , "m_selectionGuard" ) is PropertyInfo pi_m_selectionGuard )
398- return selectionGuardMemberInfo = pi_m_selectionGuard ;
399-
400- if ( AccessTools . Field ( typeof ( EventSystem ) , "m_SelectionGuard" ) is FieldInfo fi_m_SelectionGuard )
401- return selectionGuardMemberInfo = fi_m_SelectionGuard ;
402-
403- return selectionGuardMemberInfo = AccessTools . Field ( typeof ( EventSystem ) , "m_selectionGuard" ) ;
425+ instance . SetSelectedGameObject ( value ) ;
426+
427+ if ( usingEventSystemDictionaryMembers )
428+ m_CurrentSelected_Handler_Dictionary . GetValue ( instance ) [ 0 ] = value ;
429+ else
430+ m_CurrentSelected_Handler_Normal . SetValue ( instance , value ) ;
404431 }
405432
406- internal static void SetSelectionGuard ( EventSystem instance , bool value )
433+ static void SetSelectionGuard ( EventSystem instance , bool value )
407434 {
408- var member = GetSelectionGuardMemberInfo ( ) ;
409- if ( member == null )
410- return ;
411- if ( member is PropertyInfo pi )
412- {
413- pi . SetValue ( instance , value , null ) ;
414- return ;
415- }
416- var fi = member as FieldInfo ;
417- fi . SetValue ( instance , value ) ;
435+ if ( usingEventSystemDictionaryMembers )
436+ m_SelectionGuard_Handler_Dictionary . GetValue ( instance ) [ 0 ] = value ;
437+ else
438+ m_SelectionGuard_Handler_Normal . SetValue ( instance , value ) ;
418439 }
419440
420441 private static IEnumerator SetCaretCoroutine ( int caretPosition )
@@ -423,15 +444,15 @@ private static IEnumerator SetCaretCoroutine(int caretPosition)
423444 color . a = 0f ;
424445 Input . Component . selectionColor = color ;
425446
426- try { CursorUnlocker . CurrentEventSystem . m_CurrentSelected = null ; }
447+ try { SetCurrentSelectedGameObject ( CursorUnlocker . CurrentEventSystem , null ) ; }
427448 catch ( Exception ex ) { ExplorerCore . Log ( $ "Failed removing selected object: { ex } ") ; }
428449
429450 yield return null ; // ~~~~~~~ YIELD FRAME ~~~~~~~~~
430451
431452 try { SetSelectionGuard ( CursorUnlocker . CurrentEventSystem , false ) ; }
432453 catch ( Exception ex ) { ExplorerCore . Log ( $ "Failed setting selection guard: { ex } ") ; }
433454
434- try { CursorUnlocker . CurrentEventSystem . SetSelectedGameObject ( Input . GameObject , null ) ; }
455+ try { SetCurrentSelectedGameObject ( CursorUnlocker . CurrentEventSystem , Input . GameObject ) ; }
435456 catch ( Exception ex ) { ExplorerCore . Log ( $ "Failed setting selected gameobject: { ex } ") ; }
436457
437458 yield return null ; // ~~~~~~~ YIELD FRAME ~~~~~~~~~
@@ -695,7 +716,8 @@ public static void HelpSelected(int index)
695716 * Log(obj); - prints a message to the console log
696717 * Inspect(obj); - inspect the object with the Inspector
697718 * Inspect(someType); - inspect a Type with static reflection
698- * Start(enumerator); - starts the IEnumerator as a Coroutine
719+ * Start(enumerator); - Coroutine, starts the IEnumerator as a Coroutine, and returns the Coroutine.
720+ * Stop(coroutine); - stop the Coroutine ONLY if it was started with Start(ienumerator).
699721 * Copy(obj); - copies the object to the UnityExplorer Clipboard
700722 * Paste(); - System.Object, the contents of the Clipboard.
701723 * GetUsing(); - prints the current using directives to the console log
0 commit comments