1
- using System ;
1
+ using HarmonyLib ;
2
+ using System ;
3
+ using System . Collections ;
2
4
using System . Collections . Generic ;
3
5
using System . IO ;
4
6
using System . Linq ;
7
+ using System . Reflection ;
5
8
using System . Text ;
6
9
using UnityEngine ;
7
10
using UnityEngine . EventSystems ;
@@ -40,18 +43,15 @@ public enum VerticalAnchor
40
43
41
44
public static bool Initializing { get ; internal set ; } = true ;
42
45
43
- private static readonly Dictionary < Panels , UIPanel > UIPanels = new Dictionary < Panels , UIPanel > ( ) ;
44
-
45
46
public static VerticalAnchor NavbarAnchor = VerticalAnchor . Top ;
46
47
47
- // References
48
48
public static GameObject CanvasRoot { get ; private set ; }
49
49
public static Canvas Canvas { get ; private set ; }
50
50
public static EventSystem EventSys { get ; private set ; }
51
51
52
52
internal static GameObject PoolHolder { get ; private set ; }
53
-
54
53
internal static GameObject PanelHolder { get ; private set ; }
54
+ private static readonly Dictionary < Panels , UIPanel > UIPanels = new Dictionary < Panels , UIPanel > ( ) ;
55
55
56
56
internal static Font ConsoleFont { get ; private set ; }
57
57
internal static Font DefaultFont { get ; private set ; }
@@ -158,10 +158,6 @@ public static void Update()
158
158
if ( ! ShowMenu )
159
159
return ;
160
160
161
- // In case the bundle has been unloaded, reload it.
162
- if ( ! DefaultFont )
163
- LoadBundle ( ) ;
164
-
165
161
// Check forceUnlockMouse toggle
166
162
if ( InputManager . GetKeyDown ( ConfigManager . Force_Unlock_Toggle . Value ) )
167
163
CursorUnlocker . Unlock = ! CursorUnlocker . Unlock ;
@@ -200,15 +196,9 @@ public static void Update()
200
196
201
197
// Panels
202
198
203
- public static UIPanel GetPanel ( Panels panel )
204
- {
205
- return UIPanels [ panel ] ;
206
- }
199
+ public static UIPanel GetPanel ( Panels panel ) => UIPanels [ panel ] ;
207
200
208
- public static T GetPanel < T > ( Panels panel ) where T : UIPanel
209
- {
210
- return ( T ) UIPanels [ panel ] ;
211
- }
201
+ public static T GetPanel < T > ( Panels panel ) where T : UIPanel => ( T ) UIPanels [ panel ] ;
212
202
213
203
public static void TogglePanel ( Panels panel )
214
204
{
@@ -329,9 +319,14 @@ private static void CreateRootCanvas()
329
319
CanvasRoot . layer = 5 ;
330
320
CanvasRoot . transform . position = new Vector3 ( 0f , 0f , 1f ) ;
331
321
322
+ CanvasRoot . SetActive ( false ) ;
323
+
332
324
EventSys = CanvasRoot . AddComponent < EventSystem > ( ) ;
333
325
InputManager . AddUIModule ( ) ;
334
326
327
+ EventSys . enabled = false ;
328
+ CanvasRoot . SetActive ( true ) ;
329
+
335
330
Canvas = CanvasRoot . AddComponent < Canvas > ( ) ;
336
331
Canvas . renderMode = RenderMode . ScreenSpaceCamera ;
337
332
Canvas . referencePixelsPerUnit = 100 ;
@@ -419,9 +414,12 @@ private static void CreateTopNavBar()
419
414
420
415
// UI AssetBundle
421
416
417
+ internal static AssetBundle ExplorerBundle ;
418
+
422
419
private static void LoadBundle ( )
423
420
{
424
- AssetBundle bundle ;
421
+ SetupAssetBundlePatches ( ) ;
422
+
425
423
try
426
424
{
427
425
// Get the Major and Minor of the Unity version
@@ -432,18 +430,18 @@ private static void LoadBundle()
432
430
// Use appropriate AssetBundle for Unity version
433
431
// >= 2017
434
432
if ( major >= 2017 )
435
- bundle = LoadBundle ( "modern" ) ;
433
+ ExplorerBundle = LoadBundle ( "modern" ) ;
436
434
// 5.6.0 to <2017
437
435
else if ( major == 5 && minor >= 6 )
438
- bundle = LoadBundle ( "legacy.5.6" ) ;
436
+ ExplorerBundle = LoadBundle ( "legacy.5.6" ) ;
439
437
// < 5.6.0
440
438
else
441
- bundle = LoadBundle ( "legacy" ) ;
439
+ ExplorerBundle = LoadBundle ( "legacy" ) ;
442
440
}
443
441
catch
444
442
{
445
443
ExplorerCore . LogWarning ( $ "Exception parsing Unity version, falling back to old AssetBundle load method...") ;
446
- bundle = LoadBundle ( "modern" ) ?? LoadBundle ( "legacy.5.6" ) ?? LoadBundle ( "legacy" ) ;
444
+ ExplorerBundle = LoadBundle ( "modern" ) ?? LoadBundle ( "legacy.5.6" ) ?? LoadBundle ( "legacy" ) ;
447
445
}
448
446
449
447
AssetBundle LoadBundle ( string id )
@@ -455,7 +453,7 @@ AssetBundle LoadBundle(string id)
455
453
. GetManifestResourceStream ( $ "UnityExplorer.Resources.{ id } .bundle") ) ) ;
456
454
}
457
455
458
- if ( bundle == null )
456
+ if ( ExplorerBundle == null )
459
457
{
460
458
ExplorerCore . LogWarning ( "Could not load the ExplorerUI Bundle!" ) ;
461
459
DefaultFont = ConsoleFont = Resources . GetBuiltinResource < Font > ( "Arial.ttf" ) ;
@@ -464,15 +462,17 @@ AssetBundle LoadBundle(string id)
464
462
465
463
// Bundle loaded
466
464
467
- ConsoleFont = bundle . LoadAsset < Font > ( "CONSOLA" ) ;
465
+ ConsoleFont = ExplorerBundle . LoadAsset < Font > ( "CONSOLA" ) ;
468
466
ConsoleFont . hideFlags = HideFlags . HideAndDontSave ;
469
467
UnityEngine . Object . DontDestroyOnLoad ( ConsoleFont ) ;
470
468
471
- DefaultFont = bundle . LoadAsset < Font > ( "arial" ) ;
469
+ DefaultFont = ExplorerBundle . LoadAsset < Font > ( "arial" ) ;
472
470
DefaultFont . hideFlags = HideFlags . HideAndDontSave ;
473
471
UnityEngine . Object . DontDestroyOnLoad ( DefaultFont ) ;
474
472
475
- BackupShader = bundle . LoadAsset < Shader > ( "DefaultUI" ) ;
473
+ BackupShader = ExplorerBundle . LoadAsset < Shader > ( "DefaultUI" ) ;
474
+ BackupShader . hideFlags = HideFlags . HideAndDontSave ;
475
+ UnityEngine . Object . DontDestroyOnLoad ( BackupShader ) ;
476
476
// Fix for games which don't ship with 'UI/Default' shader.
477
477
if ( Graphic . defaultGraphicMaterial . shader ? . name != "UI/Default" )
478
478
{
@@ -481,7 +481,6 @@ AssetBundle LoadBundle(string id)
481
481
}
482
482
else
483
483
BackupShader = Graphic . defaultGraphicMaterial . shader ;
484
-
485
484
}
486
485
487
486
private static byte [ ] ReadFully ( Stream input )
@@ -495,5 +494,58 @@ private static byte[] ReadFully(Stream input)
495
494
return ms . ToArray ( ) ;
496
495
}
497
496
}
497
+
498
+ // AssetBundle patch
499
+
500
+ static bool donePatch ;
501
+ private static Type T_AssetBundle => ReflectionUtility . GetTypeByName ( "UnityEngine.AssetBundle" ) ;
502
+
503
+ private static void SetupAssetBundlePatches ( )
504
+ {
505
+ if ( ! donePatch )
506
+ {
507
+ try
508
+ {
509
+ if ( T_AssetBundle . GetMethod ( "UnloadAllAssetBundles" , AccessTools . all ) is MethodInfo unloadAllBundles )
510
+ {
511
+ var processor = ExplorerCore . Harmony . CreateProcessor ( unloadAllBundles ) ;
512
+ var prefix = new HarmonyMethod ( typeof ( UIManager ) . GetMethod ( nameof ( Prefix_UnloadAllAssetBundles ) , AccessTools . all ) ) ;
513
+ processor . AddPrefix ( prefix ) ;
514
+ processor . Patch ( ) ;
515
+
516
+ donePatch = true ;
517
+ }
518
+ }
519
+ catch ( Exception ex )
520
+ {
521
+ ExplorerCore . LogWarning ( $ "Exception setting up AssetBundle.UnloadAllAssetBundles patch: { ex } ") ;
522
+ }
523
+ }
524
+ }
525
+
526
+
527
+ static bool Prefix_UnloadAllAssetBundles ( bool unloadAllObjects )
528
+ {
529
+ try
530
+ {
531
+ var method = typeof ( AssetBundle ) . GetMethod ( "GetAllLoadedAssetBundles" , AccessTools . all ) ;
532
+ if ( method == null )
533
+ return true ;
534
+ var bundles = method . Invoke ( null , ArgumentUtility . EmptyArgs ) as AssetBundle [ ] ;
535
+ foreach ( var obj in bundles )
536
+ {
537
+ if ( obj . m_CachedPtr == ExplorerBundle . m_CachedPtr )
538
+ continue ;
539
+
540
+ obj . Unload ( unloadAllObjects ) ;
541
+ }
542
+ }
543
+ catch ( Exception ex )
544
+ {
545
+ ExplorerCore . LogWarning ( $ "Exception unloading AssetBundles: { ex } ") ;
546
+ }
547
+
548
+ return false ;
549
+ }
498
550
}
499
551
}
0 commit comments