Decompiled source of ChallengePresets v1.0.0

ChallengePresets.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using RoR2;
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("ChallengePresets")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ChallengePresets")]
[assembly: AssemblyTitle("ChallengePresets")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 ChallengePresets
{
	[BepInPlugin("dileppy.challengepresets", "ChallengePresets", "1.0.0")]
	public class ChallengePresetsPlugin : BaseUnityPlugin
	{
		public enum Preset
		{
			None,
			DavidVsGoliath,
			KaijuMode,
			GremlinMode,
			BulletHellLite
		}

		public const string PluginGUID = "dileppy.challengepresets";

		public const string PluginName = "ChallengePresets";

		public const string PluginVersion = "1.0.0";

		public static ConfigEntry<Preset> SelectedPreset;

		public static ConfigEntry<bool> AnnouncePreset;

		public static ConfigEntry<bool> DebugLogging;

		private static readonly Dictionary<Preset, (string flavor, List<(string guid, string mod, string field, object value)> steps)> Presets = new Dictionary<Preset, (string, List<(string, string, string, object)>)>
		{
			{
				Preset.DavidVsGoliath,
				("You are small. They are giants. Bring them down.", new List<(string, string, string, object)>
				{
					("dileppy.shrunkensurvivor", "ShrunkenSurvivor", "Tier", "Small"),
					("dileppy.enlargedenemies", "EnlargedEnemies", "Tier", "Large")
				})
			},
			{
				Preset.KaijuMode,
				("You are the monster now. The swarm is beneath you.", new List<(string, string, string, object)>
				{
					("dileppy.enlargedsurvivor", "EnlargedSurvivor", "Tier", "ExtraLarge"),
					("dileppy.shrunkenenemies", "ShrunkenEnemies", "Tier", "Small")
				})
			},
			{
				Preset.GremlinMode,
				("They are tiny. They are fast. They are EVERYWHERE.", new List<(string, string, string, object)> { ("dileppy.shrunkenenemies", "ShrunkenEnemies", "Tier", "ExtremelySmall") })
			},
			{
				Preset.BulletHellLite,
				("Everything is bigger. Especially the danger.", new List<(string, string, string, object)>
				{
					("dileppy.biggerbullets", "BiggerBullets", "SizeMultiplier", 2.5f),
					("dileppy.enlargedenemies", "EnlargedEnemies", "Tier", "Large")
				})
			}
		};

		private string lastSummary = "";

		private void Awake()
		{
			SelectedPreset = ((BaseUnityPlugin)this).Config.Bind<Preset>("General", "SelectedPreset", Preset.None, "Pick a curated experience. The preset reconfigures your other installed Dileppy mods on game start. DavidVsGoliath: tiny you, giant enemies. KaijuMode: giant you, tiny swarm. GremlinMode: microscopic fast enemies everywhere. BulletHellLite: big bullets, big enemies. None: your own config, untouched.");
			AnnouncePreset = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AnnouncePreset", true, "Announce the active preset in chat at run start.");
			DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Advanced", "DebugLogging", false, "Log each config value the preset changes.");
			Run.onRunStartGlobal += OnRunStart;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"ChallengePresets v1.0.0 loaded. Part of The Petrichor Protocol.");
		}

		private void Start()
		{
			ApplyPreset();
		}

		private void OnDestroy()
		{
			Run.onRunStartGlobal -= OnRunStart;
		}

		private void OnRunStart(Run run)
		{
			if (AnnouncePreset.Value && SelectedPreset.Value != Preset.None && lastSummary.Length > 0)
			{
				Chat.AddMessage($"<color=#7fd4a8>Preset: {SelectedPreset.Value}</color> - {Presets[SelectedPreset.Value].flavor}");
				Chat.AddMessage(lastSummary);
			}
		}

		private void ApplyPreset()
		{
			Preset value = SelectedPreset.Value;
			if (value == Preset.None || !Presets.TryGetValue(value, out (string, List<(string, string, string, object)>) value2))
			{
				return;
			}
			List<string> list = new List<string>();
			List<string> list2 = new List<string>();
			foreach (var (guid, text, text2, obj) in value2.Item2)
			{
				if (!SetModConfig(guid, text2, obj))
				{
					list2.Add(text);
				}
				else
				{
					list.Add($"{text}.{text2}={obj}");
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0}: applied {1} - {2}", "ChallengePresets", value, string.Join(", ", list)));
			if (list2.Count > 0)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)string.Format("{0}: {1} works best with these mods you don't have installed: {2}. Find them on Thunderstore under Dileppy.", "ChallengePresets", value, string.Join(", ", list2)));
			}
			lastSummary = ((list2.Count > 0) ? string.Format("(Install {0} for the full {1} experience)", string.Join(", ", list2), value) : $"({list.Count} settings tuned)");
		}

		private bool SetModConfig(string guid, string fieldName, object value)
		{
			try
			{
				if (!Chainloader.PluginInfos.TryGetValue(guid, out var value2) || (Object)(object)value2.Instance == (Object)null)
				{
					return false;
				}
				object obj = ((object)value2.Instance).GetType().GetField(fieldName, BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
				if (obj == null)
				{
					return false;
				}
				Type type = obj.GetType();
				Type type2 = (Type)type.GetProperty("SettingType").GetValue(obj);
				object obj2 = ((value is string value3 && type2.IsEnum) ? Enum.Parse(type2, value3) : Convert.ChangeType(value, type2));
				type.GetProperty("Value").SetValue(obj, obj2);
				if (DebugLogging.Value)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0}: {1}.{2} -> {3}", "ChallengePresets", guid, fieldName, obj2));
				}
				return true;
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("ChallengePresets: could not set " + guid + "." + fieldName + ": " + ex.Message));
				return false;
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "ChallengePresets";

		public const string PLUGIN_NAME = "ChallengePresets";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}