Skip to content

Commit 87a36d9

Browse files
authored
GraphicsSettings - Do not override by default, remember game defaults (#4)
1 parent 1dc8596 commit 87a36d9

File tree

2 files changed

+169
-57
lines changed

2 files changed

+169
-57
lines changed

GraphicsSettingsIL2CPP_net6/GraphicsSettings.cs

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using BepInEx.Configuration;
3+
using BepInEx.Logging;
34
using BepInEx.Unity.IL2CPP;
45
using UnityEngine;
6+
using UnityEngine.Events;
57
using UnityEngine.SceneManagement;
68

79
namespace BepInEx
@@ -27,57 +29,111 @@ public class GraphicsSettings : BasePlugin
2729

2830
public override void Load()
2931
{
30-
AutoApply = Config.Bind("Apply on Startup", "Apply on Startup", false, "Apply graphics settings when you start the game. May also force the resolution when the game is running");
31-
AutoApply.SettingChanged += (sender, args) => ApplySettings();
32-
33-
Width = Config.Bind("Resolution", "Width", 1280, "Set Resolution Width. Minimum is 800");
32+
AutoApply = Config.Bind("General", "Auto Apply", false, "Apply all graphics settings automatically whenever active scene changes. Otherwise settings are only applied as they are changed.");
33+
var applySettingsAction = (UnityAction<Scene, Scene>)new Action<Scene, Scene>((f, t) => ApplySettings());
34+
var applySettingsAction2 = (UnityAction<Scene, LoadSceneMode>)new Action<Scene, LoadSceneMode>((f, t) => ApplySettings());
35+
void ApplySceneHook(bool showWarning)
36+
{
37+
try
38+
{
39+
if (AutoApply.Value)
40+
SceneManager.add_activeSceneChanged(applySettingsAction);
41+
else
42+
SceneManager.remove_activeSceneChanged(applySettingsAction);
43+
}
44+
catch (NotSupportedException)
45+
{
46+
try
47+
{
48+
if (AutoApply.Value)
49+
SceneManager.add_sceneLoaded(applySettingsAction2);
50+
else
51+
SceneManager.remove_sceneLoaded(applySettingsAction2);
52+
}
53+
catch (NotSupportedException)
54+
{
55+
Log.Log(showWarning ? LogLevel.Message | LogLevel.Warning : LogLevel.Warning, "Could not hook the scene change event handler, changes will not be applied automatically");
56+
}
57+
}
58+
}
59+
AutoApply.SettingChanged += (sender, args) => { ApplySceneHook(true); };
60+
61+
Width = Config.Bind("Resolution Override", "Width", 0, "Force resolution width to this value. Minimum is 800. Set to 0 to disable this feature.");
3462
Width.SettingChanged += (sender, args) => ApplySettings();
35-
Height = Config.Bind("Resolution", "Height", 720, "Set Resolution Height. Minimum is 600");
63+
Height = Config.Bind("Resolution Override", "Height", 0, "Force resolution height to this value. Minimum is 600. Set to 0 to disable this feature.");
3664
Height.SettingChanged += (sender, args) => ApplySettings();
37-
DisplayMode = Config.Bind("Resolution", "Display Mode", DisplayModeList.Windowed);
65+
DisplayMode = Config.Bind("Resolution Override", "Display Mode", DisplayModeList.Default, "Force specified window mode.");
3866
DisplayMode.SettingChanged += (sender, args) => ApplySettings();
3967

40-
vSync = Config.Bind("Framerate", "vSync", vSyncList.On);
68+
vSync = Config.Bind("Framerate Override", "vSync", vSyncList.Default, "Force specified vsync mode.");
4169
vSync.SettingChanged += (sender, args) => ApplySettings();
42-
Framerate = Config.Bind("Framerate", "Target Framerate", -1, "Target Framerate only works if vSync is Off. Set -1 to unlimited");
70+
Framerate = Config.Bind("Framerate Override", "Target Framerate", Application.targetFrameRate, "Force specified target Framerate. Only works if vSync is Off. Set -1 for unlimited.");
4371
Framerate.SettingChanged += (sender, args) => ApplySettings();
4472

45-
SceneManager.add_sceneLoaded(new Action<Scene, LoadSceneMode>((s, lsm) =>
46-
{
47-
if (AutoApply.Value) ApplySettings();
48-
}));
73+
if (AutoApply.Value)
74+
ApplySceneHook(false);
4975
}
5076

5177
private enum DisplayModeList
5278
{
79+
Default = 0,
5380
FullScreen,
5481
Windowed,
5582
Borderless_FullScreen
5683
}
5784

5885
private enum vSyncList
5986
{
87+
Default = -1,
6088
On = 1,
6189
Off = 0,
6290
Half = 2
6391
}
6492

65-
private void ApplySettings()
66-
{
67-
if (Width.Value < 800) Width.Value = 800;
68-
if (Height.Value < 600) Height.Value = 600;
69-
70-
if (DisplayMode.Value == DisplayModeList.FullScreen)
71-
Screen.SetResolution(Width.Value, Height.Value, FullScreenMode.ExclusiveFullScreen);
93+
private static int _originalWidth, _originalHeight;
94+
private static vSyncList _originalVsync = vSyncList.Default;
95+
private static DisplayModeList _originalDisplayMode = DisplayModeList.Default;
7296

73-
if (DisplayMode.Value == DisplayModeList.Windowed)
74-
Screen.SetResolution(Width.Value, Height.Value, FullScreenMode.Windowed);
75-
76-
if (DisplayMode.Value == DisplayModeList.Borderless_FullScreen)
77-
Screen.SetResolution(Width.Value, Height.Value, FullScreenMode.FullScreenWindow);
78-
79-
QualitySettings.vSyncCount = (int)vSync.Value;
80-
Application.targetFrameRate = Framerate.Value;
97+
private static void ApplySettings()
98+
{
99+
if (_originalWidth <= 0)
100+
_originalWidth = Screen.width;
101+
if (_originalHeight <= 0)
102+
_originalHeight = Screen.height;
103+
if (_originalVsync == vSyncList.Default)
104+
_originalVsync = (vSyncList)QualitySettings.vSyncCount;
105+
if (_originalDisplayMode == DisplayModeList.Default)
106+
_originalDisplayMode = (Screen.fullScreenMode == FullScreenMode.ExclusiveFullScreen) ? DisplayModeList.FullScreen :
107+
(Screen.fullScreenMode == FullScreenMode.Windowed) ? DisplayModeList.Windowed :
108+
(Screen.fullScreenMode == FullScreenMode.FullScreenWindow) ? DisplayModeList.Borderless_FullScreen :
109+
DisplayModeList.Windowed;
110+
111+
var width = Width.Value;
112+
if (width <= 0) width = _originalWidth;
113+
else if (width < 800) width = Width.Value = 800;
114+
115+
var height = Height.Value;
116+
if (height <= 0) height = _originalHeight;
117+
else if (height < 600) height = Height.Value = 600;
118+
119+
var displayMode = DisplayMode.Value;
120+
if (displayMode == DisplayModeList.Default)
121+
displayMode = _originalDisplayMode;
122+
123+
if (displayMode == DisplayModeList.FullScreen)
124+
Screen.SetResolution(width, height, FullScreenMode.ExclusiveFullScreen);
125+
else if (displayMode == DisplayModeList.Windowed)
126+
Screen.SetResolution(width, height, FullScreenMode.Windowed);
127+
else if (displayMode == DisplayModeList.Borderless_FullScreen)
128+
Screen.SetResolution(width, height, FullScreenMode.FullScreenWindow);
129+
130+
if (vSync.Value == vSyncList.Default)
131+
QualitySettings.vSyncCount = (int)_originalVsync;
132+
else
133+
QualitySettings.vSyncCount = (int)vSync.Value;
134+
135+
if (QualitySettings.vSyncCount == 0)
136+
Application.targetFrameRate = Framerate.Value;
81137
}
82138
}
83139
}

GraphicsSettingsIL2CPP_netFm/GraphicsSettings.cs

Lines changed: 86 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System;
1+
using BepInEx.Configuration;
22
using BepInEx.IL2CPP;
3-
using BepInEx.Configuration;
3+
using BepInEx.Logging;
4+
using System;
45
using UnityEngine;
6+
using UnityEngine.Events;
57
using UnityEngine.SceneManagement;
68

79
namespace BepInEx
@@ -12,7 +14,7 @@ namespace BepInEx
1214
[BepInPlugin(GUID, PluginName, PluginVersion)]
1315
public class GraphicsSettings : BasePlugin
1416
{
15-
internal const string GUID = "BepInEx.GraphicsSettingsIL2CPP_NetFm";
17+
internal const string GUID = "BepInEx.GraphicsSettingsIL2CPP_net6";
1618
internal const string PluginName = "Graphics Settings";
1719
internal const string PluginVersion = "0.7";
1820

@@ -27,57 +29,111 @@ public class GraphicsSettings : BasePlugin
2729

2830
public override void Load()
2931
{
30-
AutoApply = Config.Bind("Apply on Startup", "Apply on Startup", false, "Apply graphics settings when you start the game. May also force the resolution when the game is running");
31-
AutoApply.SettingChanged += (sender, args) => ApplySettings();
32-
33-
Width = Config.Bind("Resolution", "Width", 1280, "Set Resolution Width. Minimum is 800");
32+
AutoApply = Config.Bind("General", "Auto Apply", false, "Apply all graphics settings automatically whenever active scene changes. Otherwise settings are only applied as they are changed.");
33+
var applySettingsAction = (UnityAction<Scene, Scene>)new Action<Scene, Scene>((f, t) => ApplySettings());
34+
var applySettingsAction2 = (UnityAction<Scene, LoadSceneMode>)new Action<Scene, LoadSceneMode>((f, t) => ApplySettings());
35+
void ApplySceneHook(bool showWarning)
36+
{
37+
try
38+
{
39+
if (AutoApply.Value)
40+
SceneManager.add_activeSceneChanged(applySettingsAction);
41+
else
42+
SceneManager.remove_activeSceneChanged(applySettingsAction);
43+
}
44+
catch (NotSupportedException)
45+
{
46+
try
47+
{
48+
if (AutoApply.Value)
49+
SceneManager.add_sceneLoaded(applySettingsAction2);
50+
else
51+
SceneManager.remove_sceneLoaded(applySettingsAction2);
52+
}
53+
catch (NotSupportedException)
54+
{
55+
Log.Log(showWarning ? LogLevel.Message | LogLevel.Warning : LogLevel.Warning, "Could not hook the scene change event handler, changes will not be applied automatically");
56+
}
57+
}
58+
}
59+
AutoApply.SettingChanged += (sender, args) => { ApplySceneHook(true); };
60+
61+
Width = Config.Bind("Resolution Override", "Width", 0, "Force resolution width to this value. Minimum is 800. Set to 0 to disable this feature.");
3462
Width.SettingChanged += (sender, args) => ApplySettings();
35-
Height = Config.Bind("Resolution", "Height", 720, "Set Resolution Height. Minimum is 600");
63+
Height = Config.Bind("Resolution Override", "Height", 0, "Force resolution height to this value. Minimum is 600. Set to 0 to disable this feature.");
3664
Height.SettingChanged += (sender, args) => ApplySettings();
37-
DisplayMode = Config.Bind("Resolution", "Display Mode", DisplayModeList.Windowed);
65+
DisplayMode = Config.Bind("Resolution Override", "Display Mode", DisplayModeList.Default, "Force specified window mode.");
3866
DisplayMode.SettingChanged += (sender, args) => ApplySettings();
3967

40-
vSync = Config.Bind("Framerate", "vSync", vSyncList.On);
68+
vSync = Config.Bind("Framerate Override", "vSync", vSyncList.Default, "Force specified vsync mode.");
4169
vSync.SettingChanged += (sender, args) => ApplySettings();
42-
Framerate = Config.Bind("Framerate", "Target Framerate", -1, "Target Framerate only works if vSync is Off. Set -1 to unlimited");
70+
Framerate = Config.Bind("Framerate Override", "Target Framerate", Application.targetFrameRate, "Force specified target Framerate. Only works if vSync is Off. Set -1 for unlimited.");
4371
Framerate.SettingChanged += (sender, args) => ApplySettings();
4472

45-
SceneManager.add_sceneLoaded(new Action<Scene, LoadSceneMode>((s, lsm) =>
46-
{
47-
if (AutoApply.Value) ApplySettings();
48-
}));
73+
if (AutoApply.Value)
74+
ApplySceneHook(false);
4975
}
5076

5177
private enum DisplayModeList
5278
{
79+
Default = 0,
5380
FullScreen,
5481
Windowed,
5582
Borderless_FullScreen
5683
}
5784

5885
private enum vSyncList
5986
{
87+
Default = -1,
6088
On = 1,
6189
Off = 0,
6290
Half = 2
6391
}
6492

65-
private void ApplySettings()
66-
{
67-
if (Width.Value < 800) Width.Value = 800;
68-
if (Height.Value < 600) Height.Value = 600;
69-
70-
if (DisplayMode.Value == DisplayModeList.FullScreen)
71-
Screen.SetResolution(Width.Value, Height.Value, FullScreenMode.ExclusiveFullScreen);
93+
private static int _originalWidth, _originalHeight;
94+
private static vSyncList _originalVsync = vSyncList.Default;
95+
private static DisplayModeList _originalDisplayMode = DisplayModeList.Default;
7296

73-
if (DisplayMode.Value == DisplayModeList.Windowed)
74-
Screen.SetResolution(Width.Value, Height.Value, FullScreenMode.Windowed);
75-
76-
if (DisplayMode.Value == DisplayModeList.Borderless_FullScreen)
77-
Screen.SetResolution(Width.Value, Height.Value, FullScreenMode.FullScreenWindow);
78-
79-
QualitySettings.vSyncCount = (int)vSync.Value;
80-
Application.targetFrameRate = Framerate.Value;
97+
private static void ApplySettings()
98+
{
99+
if (_originalWidth <= 0)
100+
_originalWidth = Screen.width;
101+
if (_originalHeight <= 0)
102+
_originalHeight = Screen.height;
103+
if (_originalVsync == vSyncList.Default)
104+
_originalVsync = (vSyncList)QualitySettings.vSyncCount;
105+
if (_originalDisplayMode == DisplayModeList.Default)
106+
_originalDisplayMode = (Screen.fullScreenMode == FullScreenMode.ExclusiveFullScreen) ? DisplayModeList.FullScreen :
107+
(Screen.fullScreenMode == FullScreenMode.Windowed) ? DisplayModeList.Windowed :
108+
(Screen.fullScreenMode == FullScreenMode.FullScreenWindow) ? DisplayModeList.Borderless_FullScreen :
109+
DisplayModeList.Windowed;
110+
111+
var width = Width.Value;
112+
if (width <= 0) width = _originalWidth;
113+
else if (width < 800) width = Width.Value = 800;
114+
115+
var height = Height.Value;
116+
if (height <= 0) height = _originalHeight;
117+
else if (height < 600) height = Height.Value = 600;
118+
119+
var displayMode = DisplayMode.Value;
120+
if (displayMode == DisplayModeList.Default)
121+
displayMode = _originalDisplayMode;
122+
123+
if (displayMode == DisplayModeList.FullScreen)
124+
Screen.SetResolution(width, height, FullScreenMode.ExclusiveFullScreen);
125+
else if (displayMode == DisplayModeList.Windowed)
126+
Screen.SetResolution(width, height, FullScreenMode.Windowed);
127+
else if (displayMode == DisplayModeList.Borderless_FullScreen)
128+
Screen.SetResolution(width, height, FullScreenMode.FullScreenWindow);
129+
130+
if (vSync.Value == vSyncList.Default)
131+
QualitySettings.vSyncCount = (int)_originalVsync;
132+
else
133+
QualitySettings.vSyncCount = (int)vSync.Value;
134+
135+
if (QualitySettings.vSyncCount == 0)
136+
Application.targetFrameRate = Framerate.Value;
81137
}
82138
}
83139
}

0 commit comments

Comments
 (0)