Decompiled source of KeepMySettings v1.4.0

KeepMySettings/KeepMySettings.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using JetBrains.Annotations;
using Microsoft.CodeAnalysis;
using On.RoR2.ConVar;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using RoR2.ConVar;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("KeepMySettings")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+5845a75ca0c252dc3acbe96d7520f050a2e386fb")]
[assembly: AssemblyProduct("KeepMySettings")]
[assembly: AssemblyTitle("KeepMySettings")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace KeepMySettings
{
	public static class ConvertExtensions
	{
		public static string ToCfgString(this Resolution resolution)
		{
			return $"{((Resolution)(ref resolution)).width}x{((Resolution)(ref resolution)).height}x{((Resolution)(ref resolution)).refreshRate}";
		}

		public static bool ToBool(this string str)
		{
			if (!(str == "0"))
			{
				if (str == "1")
				{
					return true;
				}
				return false;
			}
			return false;
		}

		public static string ToCfgString(this bool boolean)
		{
			if (!boolean)
			{
				return "0";
			}
			return "1";
		}

		public static int ToInt32(this string str)
		{
			return Convert.ToInt32(str);
		}

		public static float ToSingle(this string str)
		{
			return Convert.ToSingle(str);
		}
	}
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("InkyaRev.KeepMySettings", "KeepMySettings", "1.4.0")]
	public class KeepMySettingsPlugin : BaseUnityPlugin
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class <>c
		{
			public static readonly <>c <>9 = new <>c();

			public static Func<Resolution, string> <>9__16_0;

			public static Func<string, string, string> <>9__17_0;

			public static hook_AttemptSetString <>9__17_1;

			internal string <get_ResolutionsList>b__16_0(Resolution res)
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				return res.ToCfgString();
			}

			internal string <Awake>b__17_0(string str, string res)
			{
				return str + "\n" + res;
			}

			internal void <Awake>b__17_1(orig_AttemptSetString orig, BaseConVar self, string value)
			{
				switch (self.name)
				{
				case "enable_damage_numbers":
					if (GameplayModuleEnabled.Value)
					{
						if (self.GetString().ToBool() != PreferredDamageNumbers.Value)
						{
							self.SetString(PreferredDamageNumbers.Value.ToCfgString());
						}
						return;
					}
					break;
				case "exp_and_money_effects":
					if (GameplayModuleEnabled.Value)
					{
						if (self.GetString().ToBool() != PreferredExpAndMoneyEffects.Value)
						{
							self.SetString(PreferredExpAndMoneyEffects.Value.ToCfgString());
						}
						return;
					}
					break;
				case "volume_master":
					if (AudioModuleEnabled.Value)
					{
						if (!Mathf.Approximately(self.GetString().ToSingle(), PreferredMasterVolume.Value))
						{
							self.SetString(PreferredMasterVolume.Value.ToString(CultureInfo.InvariantCulture));
						}
						return;
					}
					break;
				case "volume_sfx":
					if (AudioModuleEnabled.Value)
					{
						if (!Mathf.Approximately(self.GetString().ToSingle(), PreferredSFXVolume.Value))
						{
							self.SetString(PreferredSFXVolume.Value.ToString(CultureInfo.InvariantCulture));
						}
						return;
					}
					break;
				case "parent_volume_music":
					if (AudioModuleEnabled.Value)
					{
						if (!Mathf.Approximately(self.GetString().ToSingle(), PreferredMusicVolume.Value))
						{
							self.SetString(PreferredMusicVolume.Value.ToString(CultureInfo.InvariantCulture));
						}
						return;
					}
					break;
				case "resolution":
					if (VideoModuleEnabled.Value)
					{
						if (!(self.GetString() == PreferredResolution.Value) && ResolutionsList.Contains(PreferredResolution.Value))
						{
							self.SetString(PreferredResolution.Value);
						}
						return;
					}
					break;
				case "fps_max":
					if (VideoModuleEnabled.Value)
					{
						if (self.GetString().ToInt32() != PreferredFPSLimit.Value)
						{
							self.SetString(PreferredFPSLimit.Value.ToString());
						}
						return;
					}
					break;
				}
				orig.Invoke(self, value);
			}
		}

		private const string PluginGUID = "InkyaRev.KeepMySettings";

		private const string PluginAuthor = "InkyaRev";

		private const string PluginName = "KeepMySettings";

		private const string PluginVersion = "1.4.0";

		public static ConfigEntry<bool> GameplayModuleEnabled;

		public static ConfigEntry<bool> PreferredDamageNumbers;

		public static ConfigEntry<bool> PreferredExpAndMoneyEffects;

		public static ConfigEntry<bool> AudioModuleEnabled;

		public static ConfigEntry<float> PreferredMasterVolume;

		public static ConfigEntry<float> PreferredSFXVolume;

		public static ConfigEntry<float> PreferredMusicVolume;

		public static ConfigEntry<bool> VideoModuleEnabled;

		public static ConfigEntry<string> PreferredResolution;

		public static ConfigEntry<int> PreferredFPSLimit;

		private static readonly IEnumerable<string> Cfg = File.ReadAllLines(Path.Combine(Paths.GameRootPath, Paths.ProcessName + "_Data", "Config", "config.cfg"));

		private static IEnumerable<string> ResolutionsList => Screen.resolutions.Select((Resolution res) => res.ToCfgString());

		public void Awake()
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Expected O, but got Unknown
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Expected O, but got Unknown
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Expected O, but got Unknown
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Expected O, but got Unknown
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Expected O, but got Unknown
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ca: Expected O, but got Unknown
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Expected O, but got Unknown
			//IL_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Expected O, but got Unknown
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Expected O, but got Unknown
			//IL_0295: Unknown result type (might be due to invalid IL or missing references)
			//IL_029f: Expected O, but got Unknown
			//IL_025d: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ff: Expected O, but got Unknown
			//IL_0304: Expected O, but got Unknown
			//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Expected O, but got Unknown
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_031d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_0328: Expected O, but got Unknown
			Log.Init(((BaseUnityPlugin)this).Logger);
			string text = ResolutionsList.Aggregate(string.Empty, (string str, string res) => str + "\n" + res);
			GameplayModuleEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "Enabled", false, "Enables gameplay module.");
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(GameplayModuleEnabled));
			string str2 = FindValueInCfg("enable_damage_numbers") ?? "1";
			PreferredDamageNumbers = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "Preferred Damage Numbers", str2.ToBool(), (ConfigDescription)null);
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(PreferredDamageNumbers));
			string str3 = FindValueInCfg("exp_and_money_effects") ?? "1";
			PreferredExpAndMoneyEffects = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay", "Preferred Exp and Money Effects", str3.ToBool(), (ConfigDescription)null);
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(PreferredExpAndMoneyEffects));
			AudioModuleEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Audio", "Enabled", false, "Enables audio module.");
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(AudioModuleEnabled));
			string str4 = FindValueInCfg("volume_master") ?? "100";
			PreferredMasterVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "Preferred Master Volume", str4.ToSingle(), (ConfigDescription)null);
			ModSettingsManager.AddOption((BaseOption)new SliderOption(PreferredMasterVolume, new SliderConfig
			{
				FormatString = "{0:N0}%"
			}));
			string str5 = FindValueInCfg("volume_sfx") ?? "100";
			PreferredSFXVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "Preferred SFX Volume", str5.ToSingle(), (ConfigDescription)null);
			ModSettingsManager.AddOption((BaseOption)new SliderOption(PreferredSFXVolume, new SliderConfig
			{
				FormatString = "{0:N0}%"
			}));
			string str6 = FindValueInCfg("parent_volume_music") ?? "100";
			PreferredMusicVolume = ((BaseUnityPlugin)this).Config.Bind<float>("Audio", "Preferred Music Volume", str6.ToSingle(), (ConfigDescription)null);
			ModSettingsManager.AddOption((BaseOption)new SliderOption(PreferredMusicVolume, new SliderConfig
			{
				FormatString = "{0:N0}%"
			}));
			VideoModuleEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Video", "Enabled", false, "Enables video module.");
			ModSettingsManager.AddOption((BaseOption)new CheckBoxOption(VideoModuleEnabled));
			string text2 = FindValueInCfg("resolution") ?? Screen.currentResolution.ToCfgString();
			PreferredResolution = ((BaseUnityPlugin)this).Config.Bind<string>("Video", "Preferred Resolution", text2, "Available resolutions: " + text);
			ModSettingsManager.AddOption((BaseOption)new StringInputFieldOption(PreferredResolution));
			string text3 = FindValueInCfg("fps_max");
			if (text3 == null)
			{
				Resolution currentResolution = Screen.currentResolution;
				text3 = (((Resolution)(ref currentResolution)).refreshRate * 2).ToString();
			}
			string str7 = text3;
			PreferredFPSLimit = ((BaseUnityPlugin)this).Config.Bind<int>("Video", "Preferred FPS Limit", str7.ToInt32(), "Can be any positive number.");
			ConfigEntry<int> preferredFPSLimit = PreferredFPSLimit;
			IntFieldConfig val = new IntFieldConfig();
			((NumericFieldConfig<int>)val).Min = 0;
			ModSettingsManager.AddOption((BaseOption)new IntFieldOption(preferredFPSLimit, val));
			object obj = <>c.<>9__17_1;
			if (obj == null)
			{
				hook_AttemptSetString val2 = delegate(orig_AttemptSetString orig, BaseConVar self, string value)
				{
					switch (self.name)
					{
					case "enable_damage_numbers":
						if (GameplayModuleEnabled.Value)
						{
							if (self.GetString().ToBool() != PreferredDamageNumbers.Value)
							{
								self.SetString(PreferredDamageNumbers.Value.ToCfgString());
							}
							return;
						}
						break;
					case "exp_and_money_effects":
						if (GameplayModuleEnabled.Value)
						{
							if (self.GetString().ToBool() != PreferredExpAndMoneyEffects.Value)
							{
								self.SetString(PreferredExpAndMoneyEffects.Value.ToCfgString());
							}
							return;
						}
						break;
					case "volume_master":
						if (AudioModuleEnabled.Value)
						{
							if (!Mathf.Approximately(self.GetString().ToSingle(), PreferredMasterVolume.Value))
							{
								self.SetString(PreferredMasterVolume.Value.ToString(CultureInfo.InvariantCulture));
							}
							return;
						}
						break;
					case "volume_sfx":
						if (AudioModuleEnabled.Value)
						{
							if (!Mathf.Approximately(self.GetString().ToSingle(), PreferredSFXVolume.Value))
							{
								self.SetString(PreferredSFXVolume.Value.ToString(CultureInfo.InvariantCulture));
							}
							return;
						}
						break;
					case "parent_volume_music":
						if (AudioModuleEnabled.Value)
						{
							if (!Mathf.Approximately(self.GetString().ToSingle(), PreferredMusicVolume.Value))
							{
								self.SetString(PreferredMusicVolume.Value.ToString(CultureInfo.InvariantCulture));
							}
							return;
						}
						break;
					case "resolution":
						if (VideoModuleEnabled.Value)
						{
							if (!(self.GetString() == PreferredResolution.Value) && ResolutionsList.Contains(PreferredResolution.Value))
							{
								self.SetString(PreferredResolution.Value);
							}
							return;
						}
						break;
					case "fps_max":
						if (VideoModuleEnabled.Value)
						{
							if (self.GetString().ToInt32() != PreferredFPSLimit.Value)
							{
								self.SetString(PreferredFPSLimit.Value.ToString());
							}
							return;
						}
						break;
					}
					orig.Invoke(self, value);
				};
				<>c.<>9__17_1 = val2;
				obj = (object)val2;
			}
			BaseConVar.AttemptSetString += (hook_AttemptSetString)obj;
		}

		private void FixedUpdate()
		{
			if (Console.instance == null)
			{
				return;
			}
			if (GameplayModuleEnabled.Value)
			{
				BaseConVar obj = Console.instance.FindConVar("enable_damage_numbers");
				if (obj != null)
				{
					obj.AttemptSetString(PreferredDamageNumbers.Value.ToCfgString());
				}
				BaseConVar obj2 = Console.instance.FindConVar("exp_and_money_effects");
				if (obj2 != null)
				{
					obj2.AttemptSetString(PreferredExpAndMoneyEffects.Value.ToCfgString());
				}
			}
			if (AudioModuleEnabled.Value)
			{
				BaseConVar obj3 = Console.instance.FindConVar("volume_master");
				if (obj3 != null)
				{
					obj3.AttemptSetString(PreferredResolution.Value);
				}
				BaseConVar obj4 = Console.instance.FindConVar("volume_sfx");
				if (obj4 != null)
				{
					obj4.AttemptSetString(PreferredResolution.Value);
				}
				BaseConVar obj5 = Console.instance.FindConVar("parent_volume_music");
				if (obj5 != null)
				{
					obj5.AttemptSetString(PreferredResolution.Value);
				}
			}
			if (VideoModuleEnabled.Value)
			{
				BaseConVar obj6 = Console.instance.FindConVar("resolution");
				if (obj6 != null)
				{
					obj6.AttemptSetString(PreferredResolution.Value);
				}
				BaseConVar obj7 = Console.instance.FindConVar("fps_max");
				if (obj7 != null)
				{
					obj7.AttemptSetString(PreferredFPSLimit.Value.ToString());
				}
			}
		}

		[CanBeNull]
		private static string FindValueInCfg(string varName)
		{
			string varNameWithSpace = varName + " ";
			return (from line in Cfg
				where line.StartsWith(varNameWithSpace)
				select line into str
				select str.Replace(varNameWithSpace, string.Empty).TrimEnd(';')).FirstOrDefault();
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
}