19
19
using UniverseLib . UI . Models ;
20
20
using UniverseLib . Utility ;
21
21
using HarmonyLib ;
22
+ using UniverseLib . Runtime ;
22
23
23
24
namespace UnityExplorer . CSConsole
24
25
{
@@ -61,6 +62,8 @@ public static class ConsoleController
61
62
62
63
public static void Init ( )
63
64
{
65
+ InitEventSystemPropertyHandlers ( ) ;
66
+
64
67
// Make sure console is supported on this platform
65
68
try
66
69
{
@@ -384,37 +387,55 @@ private static void SetCaretPosition(int caretPosition)
384
387
RuntimeHelper . StartCoroutine ( SetCaretCoroutine ( caretPosition ) ) ;
385
388
}
386
389
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 ;
388
403
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 )
390
424
{
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 ) ;
404
431
}
405
432
406
- internal static void SetSelectionGuard ( EventSystem instance , bool value )
433
+ static void SetSelectionGuard ( EventSystem instance , bool value )
407
434
{
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 ) ;
418
439
}
419
440
420
441
private static IEnumerator SetCaretCoroutine ( int caretPosition )
@@ -423,15 +444,15 @@ private static IEnumerator SetCaretCoroutine(int caretPosition)
423
444
color . a = 0f ;
424
445
Input . Component . selectionColor = color ;
425
446
426
- try { CursorUnlocker . CurrentEventSystem . m_CurrentSelected = null ; }
447
+ try { SetCurrentSelectedGameObject ( CursorUnlocker . CurrentEventSystem , null ) ; }
427
448
catch ( Exception ex ) { ExplorerCore . Log ( $ "Failed removing selected object: { ex } ") ; }
428
449
429
450
yield return null ; // ~~~~~~~ YIELD FRAME ~~~~~~~~~
430
451
431
452
try { SetSelectionGuard ( CursorUnlocker . CurrentEventSystem , false ) ; }
432
453
catch ( Exception ex ) { ExplorerCore . Log ( $ "Failed setting selection guard: { ex } ") ; }
433
454
434
- try { CursorUnlocker . CurrentEventSystem . SetSelectedGameObject ( Input . GameObject , null ) ; }
455
+ try { SetCurrentSelectedGameObject ( CursorUnlocker . CurrentEventSystem , Input . GameObject ) ; }
435
456
catch ( Exception ex ) { ExplorerCore . Log ( $ "Failed setting selected gameobject: { ex } ") ; }
436
457
437
458
yield return null ; // ~~~~~~~ YIELD FRAME ~~~~~~~~~
@@ -695,7 +716,8 @@ public static void HelpSelected(int index)
695
716
* Log(obj); - prints a message to the console log
696
717
* Inspect(obj); - inspect the object with the Inspector
697
718
* 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).
699
721
* Copy(obj); - copies the object to the UnityExplorer Clipboard
700
722
* Paste(); - System.Object, the contents of the Clipboard.
701
723
* GetUsing(); - prints the current using directives to the console log
0 commit comments