From 89064e9ee73e34db520d4758469a694732231149 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Wed, 28 Jun 2023 20:30:10 +0200 Subject: [PATCH] Add "Default" option to most settings, which keeps game's original value --- GraphicsSettings/GraphicsSettings.cs | 62 +++++++++++++++++++++------- GraphicsSettings/SettingEnum.cs | 36 ++++++++++++++-- 2 files changed, 79 insertions(+), 19 deletions(-) diff --git a/GraphicsSettings/GraphicsSettings.cs b/GraphicsSettings/GraphicsSettings.cs index bbffdf9..9b11f3e 100644 --- a/GraphicsSettings/GraphicsSettings.cs +++ b/GraphicsSettings/GraphicsSettings.cs @@ -12,7 +12,7 @@ namespace GraphicsSettings public class GraphicsSettings : BaseUnityPlugin { public const string GUID = "keelhauled.graphicssettings"; - public const string Version = "1.2.1"; + public const string Version = "1.3"; private const string CATEGORY_GENERAL = "General"; private const string CATEGORY_RENDER = "Rendering"; @@ -34,9 +34,9 @@ public class GraphicsSettings : BaseUnityPlugin private ConfigEntry SelectedMonitor { get; set; } private ConfigEntry VSync { get; set; } private ConfigEntry FramerateLimit { get; set; } - private ConfigEntry AntiAliasing { get; set; } - private ConfigEntry AnisotropicFiltering { get; set; } - private ConfigEntry RunInBackground { get; set; } + private ConfigEntry AntiAliasing { get; set; } + private ConfigEntry AnisotropicFiltering { get; set; } + private ConfigEntry RunInBackground { get; set; } private ConfigEntry OptimizeInBackground { get; set; } private string resolutionX = Screen.width.ToString(); @@ -51,13 +51,13 @@ public class GraphicsSettings : BaseUnityPlugin private void Awake() { Resolution = Config.AddSetting(CATEGORY_RENDER, "Resolution", "", new ConfigDescription(DESCRIPTION_RESOLUTION, null, new ConfigurationManagerAttributes { Order = 9, HideDefaultButton = true, CustomDrawer = new Action(ResolutionDrawer) })); - DisplayMode = Config.AddSetting(CATEGORY_RENDER, "Display mode", SettingEnum.DisplayMode.Windowed, new ConfigDescription("", null, new ConfigurationManagerAttributes { Order = 10 })); + DisplayMode = Config.AddSetting(CATEGORY_RENDER, "Display mode", SettingEnum.DisplayMode.Default, new ConfigDescription("", null, new ConfigurationManagerAttributes { Order = 10 })); SelectedMonitor = Config.AddSetting(CATEGORY_RENDER, "Selected monitor", 0, new ConfigDescription("", new AcceptableValueList(Enumerable.Range(0, Display.displays.Length).ToArray()), new ConfigurationManagerAttributes { Order = 8 })); - VSync = Config.AddSetting(CATEGORY_RENDER, "VSync", (SettingEnum.VSyncType)QualitySettings.vSyncCount, new ConfigDescription(DESCRIPTION_VSYNC, null, new ConfigurationManagerAttributes { Order = 7 })); - FramerateLimit = Config.AddSetting(CATEGORY_RENDER, "Framerate limit", -1, new ConfigDescription(DESCRIPTION_FRAMERATELIMIT, null, new ConfigurationManagerAttributes { Order = 6, HideDefaultButton = true, CustomDrawer = new Action(FramerateLimitDrawer) })); - AntiAliasing = Config.AddSetting(CATEGORY_RENDER, "Anti-aliasing multiplier", QualitySettings.antiAliasing, new ConfigDescription(DESCRIPTION_ANTIALIASING, new AcceptableValueRange(0, 8))); - AnisotropicFiltering = Config.AddSetting(CATEGORY_RENDER, "Anisotropic filtering", QualitySettings.anisotropicFiltering, new ConfigDescription(DESCRIPTION_ANISOFILTER)); - RunInBackground = Config.AddSetting(CATEGORY_GENERAL, "Run in background", true, new ConfigDescription(DESCRIPTION_RUNINBACKGROUND)); + VSync = Config.AddSetting(CATEGORY_RENDER, "VSync", SettingEnum.VSyncType.Default, new ConfigDescription(DESCRIPTION_VSYNC, null, new ConfigurationManagerAttributes { Order = 7 })); + FramerateLimit = Config.AddSetting(CATEGORY_RENDER, "Framerate limit", Application.targetFrameRate, new ConfigDescription(DESCRIPTION_FRAMERATELIMIT, null, new ConfigurationManagerAttributes { Order = 6, HideDefaultButton = true, CustomDrawer = new Action(FramerateLimitDrawer) })); + AntiAliasing = Config.AddSetting(CATEGORY_RENDER, "Anti-aliasing multiplier", SettingEnum.AntiAliasingMode.Default, new ConfigDescription(DESCRIPTION_ANTIALIASING)); + AnisotropicFiltering = Config.AddSetting(CATEGORY_RENDER, "Anisotropic filtering", SettingEnum.AnisotropicFilteringMode.Default, new ConfigDescription(DESCRIPTION_ANISOFILTER)); + RunInBackground = Config.AddSetting(CATEGORY_GENERAL, "Run in background", SettingEnum.RunInBackgroundMode.Default, new ConfigDescription(DESCRIPTION_RUNINBACKGROUND)); OptimizeInBackground = Config.AddSetting(CATEGORY_GENERAL, "Optimize in background", true, new ConfigDescription(DESCRIPTION_OPTIMIZEINBACKGROUND)); if(DisplayMode.Value == SettingEnum.DisplayMode.BorderlessFullscreen) @@ -67,16 +67,43 @@ private void Awake() SelectedMonitor.SettingChanged += (sender, args) => StartCoroutine(SelectMonitor()); InitSetting(FramerateLimit, SetFramerateLimit); - InitSetting(VSync, () => QualitySettings.vSyncCount = (int)VSync.Value); - InitSetting(AntiAliasing, () => QualitySettings.antiAliasing = AntiAliasing.Value); - InitSetting(AnisotropicFiltering, () => QualitySettings.anisotropicFiltering = AnisotropicFiltering.Value); - InitSetting(RunInBackground, () => Application.runInBackground = RunInBackground.Value); + InitSetting(VSync, () => + { + if(VSync.Value != SettingEnum.VSyncType.Default) + QualitySettings.vSyncCount = (int)VSync.Value; + }); + InitSetting(AntiAliasing, () => + { + if(AntiAliasing.Value != SettingEnum.AntiAliasingMode.Default) + QualitySettings.antiAliasing = (int)AntiAliasing.Value; + }); + InitSetting(AnisotropicFiltering, () => + { + if(AnisotropicFiltering.Value != SettingEnum.AnisotropicFilteringMode.Default) + QualitySettings.anisotropicFiltering = (AnisotropicFiltering)AnisotropicFiltering.Value; + }); + InitSetting(RunInBackground, () => + { + if(RunInBackground.Value != SettingEnum.RunInBackgroundMode.Default) + Application.runInBackground = RunInBackground.Value == SettingEnum.RunInBackgroundMode.Yes; + }); } + private int lastAntiAliasingValue = -1; private void OnApplicationFocus(bool hasFocus) { if(OptimizeInBackground.Value) - QualitySettings.antiAliasing = hasFocus ? AntiAliasing.Value : 0; + { + if (lastAntiAliasingValue < 0 || !hasFocus) + lastAntiAliasingValue = QualitySettings.antiAliasing; + + if (!hasFocus) + QualitySettings.antiAliasing = 0; + else if (AntiAliasing.Value != SettingEnum.AntiAliasingMode.Default) + QualitySettings.antiAliasing = (int)AntiAliasing.Value; + else + QualitySettings.antiAliasing = lastAntiAliasingValue; + } } private void ResolutionDrawer(ConfigEntryBase configEntry) @@ -123,7 +150,8 @@ private void FramerateLimitDrawer(ConfigEntryBase configEntry) var toggle = GUILayout.Toggle(framerateToggle, "Enabled", GUILayout.Width(70)); if(toggle != framerateToggle) { - if(framerateToggle = toggle) + framerateToggle = toggle; + if(toggle) { var refreshRate = Screen.currentResolution.refreshRate; FramerateLimit.Value = refreshRate; @@ -161,6 +189,8 @@ private void SetDisplayMode() case SettingEnum.DisplayMode.BorderlessFullscreen: StartCoroutine(RemoveBorder()); break; + case SettingEnum.DisplayMode.Default: + break; } } diff --git a/GraphicsSettings/SettingEnum.cs b/GraphicsSettings/SettingEnum.cs index 804829c..d484ba4 100644 --- a/GraphicsSettings/SettingEnum.cs +++ b/GraphicsSettings/SettingEnum.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using UnityEngine; namespace GraphicsSettings { @@ -6,17 +7,46 @@ internal class SettingEnum { public enum VSyncType { - Disabled, - Enabled, - Half + Default = -1, + Disabled = 0, + Enabled = 1, + Half = 2 } public enum DisplayMode { + Default, Fullscreen, [Description("Borderless fullscreen")] BorderlessFullscreen, Windowed } + + public enum AntiAliasingMode + { + Default = -1, + Disabled = 0, + [Description("2x MSAA")] + X2 = 2, + [Description("4x MSAA")] + X4 = 4, + [Description("8x MSAA")] + X8 = 8 + } + + public enum AnisotropicFilteringMode + { + Default = -1, + Disable = AnisotropicFiltering.Disable, + Enable = AnisotropicFiltering.Enable, + ForceEnable = AnisotropicFiltering.ForceEnable, + } + + public enum RunInBackgroundMode + { + Default = -1, + No = 0, + Yes = 1, + } } }