1- using System . Reflection ;
1+ using System ;
2+ using System . Collections ;
3+ using System . Reflection ;
24using System . Runtime . InteropServices ;
35using BepInEx ;
46using BepInEx . Configuration ;
57using BepInEx . Unity . IL2CPP ;
8+ using BepInEx . Unity . IL2CPP . Utils . Collections ;
69using HarmonyLib ;
710using Il2CppInterop . Runtime . Injection ;
811using UnityEngine ;
12+ using UnityEngine . SceneManagement ;
913
1014namespace GraphicsPlus ;
1115
1216[ BepInAutoPlugin ( "dev.xtracube.graphicsplus" ) ]
1317public partial class GraphicsPlugin : BasePlugin
1418{
15- private static GraphicsPlugin Instance { get ; set ; }
19+ private static GraphicsPlugin Instance { get ; set ; } = null ! ;
1620
1721 private ConfigEntry < int > TargetFrameRate { get ; set ; }
1822
1923 private ConfigEntry < bool > FullResolution { get ; set ; }
2024
21- private static ResolutionManager _resolutionManager ;
25+ private static CustomResolutionManager ? _customResolutionManager ;
2226
2327 [ LibraryImport ( "libstarlight.so" ) ]
2428 private static unsafe partial int get_width ( ) ;
@@ -37,7 +41,13 @@ public GraphicsPlugin()
3741 public override void Load ( )
3842 {
3943 Harmony . CreateAndPatchAll ( Assembly . GetExecutingAssembly ( ) , Id ) ;
40- ClassInjector . RegisterTypeInIl2Cpp < ResolutionManager > ( ) ;
44+ ClassInjector . RegisterTypeInIl2Cpp < CustomResolutionManager > ( ) ;
45+
46+ SceneManager . add_sceneLoaded ( ( Action < Scene , LoadSceneMode > ) ( ( scene , _ ) =>
47+ {
48+ _customResolutionManager ? . SetNativeResolution ( ) ;
49+ } ) ) ;
50+
4151 Log . LogInfo ( "GraphicsPlus loaded!" ) ;
4252 }
4353
@@ -51,46 +61,63 @@ public static void Postfix()
5161
5262 if ( Instance . FullResolution . Value )
5363 {
54- _resolutionManager = Instance . AddComponent < ResolutionManager > ( ) ;
64+ _customResolutionManager = Instance . AddComponent < CustomResolutionManager > ( ) ;
5565 }
5666 }
5767 }
5868
59- public class ResolutionManager : MonoBehaviour
69+ public class CustomResolutionManager : MonoBehaviour
6070 {
61- public ResolutionManager ( nint ptr ) : base ( ptr ) { }
71+ public CustomResolutionManager ( nint ptr ) : base ( ptr ) { }
6272
63- public ResolutionManager ( ) : base ( ClassInjector . DerivedConstructorPointer < ResolutionManager > ( ) )
73+ public CustomResolutionManager ( ) : base ( ClassInjector . DerivedConstructorPointer < CustomResolutionManager > ( ) )
6474 {
6575 ClassInjector . DerivedConstructorBody ( this ) ;
6676 }
6777
68- private ScreenOrientation _lastOrientation = ScreenOrientation . Landscape ;
78+ private record struct ScreenSize ( int Width , int Height ) ;
79+
80+ private ScreenSize _lastSize = new ( 0 , 0 ) ;
81+ private ScreenOrientation _lastOrientation = ScreenOrientation . Unknown ;
82+
83+ private Coroutine ? _activeCoroutine ;
6984
7085 public void Start ( )
7186 {
72- SetNativeResolution ( ) ;
87+ _activeCoroutine = StartCoroutine ( CoSetNativeResolution ( ) . WrapToIl2Cpp ( ) ) ;
88+ }
89+
90+ public void SetNativeResolution ( )
91+ {
92+ _activeCoroutine ??= StartCoroutine ( CoSetNativeResolution ( ) . WrapToIl2Cpp ( ) ) ;
7393 }
7494
7595 public void Update ( )
7696 {
77- if ( _lastOrientation != Screen . orientation )
97+ var screenSize = new ScreenSize ( Display . main . renderingWidth , Display . main . renderingHeight ) ;
98+ var orientation = Screen . orientation ;
99+ if ( _lastSize != screenSize || _lastOrientation != orientation )
78100 {
79- SetNativeResolution ( ) ;
101+ _lastSize = screenSize ;
102+ _lastOrientation = orientation ;
103+ _activeCoroutine ??= StartCoroutine ( CoSetNativeResolution ( ) . WrapToIl2Cpp ( ) ) ;
80104 }
81105 }
82106
83- public void SetNativeResolution ( )
107+ public IEnumerator CoSetNativeResolution ( )
84108 {
85109 ScalableBufferManager . ResizeBuffers ( 1f , 1f ) ;
86110 var width = get_width ( ) ;
87111 var height = get_height ( ) ;
88- if ( Screen . orientation is ScreenOrientation . Landscape or ScreenOrientation . LandscapeRight )
89- {
90- Screen . SetResolution ( width , height , FullScreenMode . FullScreenWindow ) ;
91- Instance . Log . LogInfo ( $ "Set resolution to { width } x{ height } ") ;
92- }
93- _lastOrientation = Screen . orientation ;
112+ var aspectRatio = width / ( float ) height ;
113+
114+ Screen . SetResolution ( width , height , FullScreenMode . FullScreenWindow ) ;
115+ Instance . Log . LogInfo ( $ "Set resolution to { width } x{ height } (Orientation: { Screen . orientation } )") ;
116+
117+ yield return null ;
118+ ResolutionManager . ResolutionChanged . Invoke ( aspectRatio , width , height , true ) ;
119+
120+ _activeCoroutine = null ;
94121 }
95122 }
96123}
0 commit comments