11using System . Reflection ;
2+ using System . Runtime . InteropServices ;
23using BepInEx ;
34using BepInEx . Configuration ;
45using BepInEx . Unity . IL2CPP ;
56using HarmonyLib ;
7+ using Il2CppInterop . Runtime . Injection ;
68using UnityEngine ;
79
810namespace GraphicsPlus ;
@@ -11,48 +13,84 @@ namespace GraphicsPlus;
1113public partial class GraphicsPlugin : BasePlugin
1214{
1315 private static GraphicsPlugin Instance { get ; set ; }
14-
16+
1517 private ConfigEntry < int > TargetFrameRate { get ; set ; }
1618
1719 private ConfigEntry < bool > FullResolution { get ; set ; }
20+
21+ private static ResolutionManager _resolutionManager ;
1822
23+ [ LibraryImport ( "libstarlight.so" ) ]
24+ private static unsafe partial int get_width ( ) ;
25+
26+ [ LibraryImport ( "libstarlight.so" ) ]
27+ private static unsafe partial int get_height ( ) ;
28+
1929 public GraphicsPlugin ( )
2030 {
2131 Instance = this ;
2232 TargetFrameRate = Config . Bind ( "General" , "Target Frame Rate" , 60 , "The target frame rate of the game" ) ;
23- FullResolution = Config . Bind ( "General" , "Increase Resolution" , false , "Set the game to use the display resolution." ) ;
33+ FullResolution = Config . Bind ( "General" , "Increase Resolution" , false ,
34+ "Set the game to use the display resolution." ) ;
2435 }
2536
2637 public override void Load ( )
2738 {
2839 Harmony . CreateAndPatchAll ( Assembly . GetExecutingAssembly ( ) , Id ) ;
40+ ClassInjector . RegisterTypeInIl2Cpp < ResolutionManager > ( ) ;
2941 Log . LogInfo ( "GraphicsPlus loaded!" ) ;
3042 }
3143
3244 [ HarmonyPatch ( typeof ( AmongUsClient ) , nameof ( AmongUsClient . Awake ) ) ]
3345 public static class FrameRatePatch
3446 {
3547 public static void Postfix ( )
36- {
48+ {
3749 Instance . Log . LogInfo ( $ "Setting target frame rate to { Instance . TargetFrameRate . Value } ") ;
3850 Application . targetFrameRate = Instance . TargetFrameRate . Value ;
3951
40- if ( ! Instance . FullResolution . Value ) return ;
52+ if ( Instance . FullResolution . Value )
53+ {
54+ _resolutionManager = Instance . AddComponent < ResolutionManager > ( ) ;
55+ }
56+ }
57+ }
4158
42- Instance . Log . LogInfo ( $ "Setting fullscreen resolution to { Display . main . systemWidth } x{ Display . main . systemHeight } ") ;
43-
44- Screen . autorotateToPortrait = false ;
45- Screen . autorotateToPortraitUpsideDown = false ;
46- Screen . autorotateToLandscapeLeft = false ;
47- Screen . autorotateToLandscapeRight = false ;
59+ public class ResolutionManager : MonoBehaviour
60+ {
61+ public ResolutionManager ( nint ptr ) : base ( ptr ) { }
62+
63+ public ResolutionManager ( ) : base ( ClassInjector . DerivedConstructorPointer < ResolutionManager > ( ) )
64+ {
65+ ClassInjector . DerivedConstructorBody ( this ) ;
66+ }
67+
68+ private ScreenOrientation _lastOrientation = ScreenOrientation . Landscape ;
4869
49- Screen . orientation = ScreenOrientation . Portrait ;
70+ public void Start ( )
71+ {
72+ SetNativeResolution ( ) ;
73+ }
74+
75+ public void Update ( )
76+ {
77+ if ( _lastOrientation != Screen . orientation )
78+ {
79+ SetNativeResolution ( ) ;
80+ }
81+ }
5082
51- Screen . SetResolution (
52- Display . main . systemWidth ,
53- Display . main . systemHeight ,
54- true
55- ) ;
83+ public void SetNativeResolution ( )
84+ {
85+ ScalableBufferManager . ResizeBuffers ( 1f , 1f ) ;
86+ var width = get_width ( ) ;
87+ 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 ;
5694 }
5795 }
5896}
0 commit comments