Decompiled source of DevSettingsOverride v1.0.0

dev-settings-override.dll

Decompiled 16 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using On.RoR2;
using RoR2;
using RoR2.ConVar;

[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("dev-settings-override")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("dev-settings-override")]
[assembly: AssemblyTitle("dev-settings-override")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 DevSettingsOverride
{
	public class DevSettingsOverrideConfig
	{
		private ConfigFile _configFile;

		private const string _generalSection = "General";

		public ConfigEntry<string> OverridesCfg { get; set; }

		public DevSettingsOverrideConfig(ConfigFile configFile)
		{
			_configFile = configFile;
			InitEntries();
		}

		private void InitEntries()
		{
			OverridesCfg = _configFile.Bind<string>("General", "Overrides", "", "Console settings to override, in config.cfg format. Example: resolution 1280x720x60; window_mode Window;");
		}
	}
	[BepInPlugin("com.orsa.devsettingsoverride", "DevSettingsOverride", "1.0.0")]
	public class DevSettingsOverridePlugin : BaseUnityPlugin
	{
		private DevSettingsOverrideConfig _config;

		private readonly Dictionary<string, string> _originalValues = new Dictionary<string, string>();

		private readonly Dictionary<string, string> _overrideValues = new Dictionary<string, string>();

		public void Awake()
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Expected O, but got Unknown
			_config = new DevSettingsOverrideConfig(((BaseUnityPlugin)this).Config);
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(OnGameLoading));
			Console.SaveArchiveConVars += new hook_SaveArchiveConVars(OnSaveArchiveConVars);
			ReadOverrides();
		}

		public void OnDestroy()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			Console.SaveArchiveConVars -= new hook_SaveArchiveConVars(OnSaveArchiveConVars);
		}

		private void ReadOverrides()
		{
			string[] array = _config.OverridesCfg.Value.Split(';', StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				string[] array2 = array[i].Trim().Split(' ', 2);
				if (array2.Length == 2)
				{
					string key = array2[0].Trim();
					string value = array2[1].Trim();
					_overrideValues[key] = value;
				}
			}
		}

		private void OnGameLoading()
		{
			foreach (string key in _overrideValues.Keys)
			{
				BaseConVar val = Console.instance.FindConVar(key);
				if (val == null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Unknown convar: " + key + ". Double-check your config."));
					continue;
				}
				string value = val.GetString();
				_originalValues[key] = value;
			}
			SetConVars(_overrideValues);
		}

		private void OnSaveArchiveConVars(orig_SaveArchiveConVars orig, Console self)
		{
			SetConVars(_originalValues);
			orig.Invoke(self);
			SetConVars(_overrideValues);
		}

		private static void SetConVars(Dictionary<string, string> values)
		{
			foreach (KeyValuePair<string, string> value2 in values)
			{
				value2.Deconstruct(out var key, out var value);
				string text = key;
				string text2 = value;
				BaseConVar obj = Console.instance.FindConVar(text);
				if (obj != null)
				{
					obj.SetString(text2);
				}
			}
		}
	}
}