Decompiled source of PartialMetamorphosis v1.3.1

plugins/PartialMetamorphosis/PartialMetamorphosis.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using InLobbyConfig;
using InLobbyConfig.Fields;
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour.HookGen;
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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.3.1.0")]
namespace PartialMetamorphosis;

public static class InLobbyConfigIntegration
{
	public const string GUID = "com.KingEnderBrine.InLobbyConfig";

	private static bool Enabled => Chainloader.PluginInfos.ContainsKey("com.KingEnderBrine.InLobbyConfig");

	private static object ModConfig { get; set; }

	public static void OnStart()
	{
		if (Enabled)
		{
			OnStartInternal();
		}
	}

	[MethodImpl(MethodImplOptions.NoInlining)]
	private static void OnStartInternal()
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Expected O, but got Unknown
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Expected O, but got Unknown
		//IL_008e: Expected O, but got Unknown
		ModConfigEntry val = new ModConfigEntry
		{
			DisplayName = "Partial Metamorphosis",
			EnableField = new BooleanConfigField("", (Func<bool>)(() => PartialMetamorphosisPlugin.IsEnabled.Value), (Action<bool>)delegate(bool newValue)
			{
				PartialMetamorphosisPlugin.IsEnabled.Value = newValue;
			})
		};
		val.SectionFields["Main"] = new List<IConfigField> { ConfigFieldUtilities.CreateFromBepInExConfigEntry<int>(PartialMetamorphosisPlugin.StagesBetweenMetamorphosis) };
		ModConfigCatalog.Add(val);
		ModConfig = (object)val;
	}

	public static void OnDestroy()
	{
		if (Enabled)
		{
			OnDestroyInternal();
		}
	}

	[MethodImpl(MethodImplOptions.NoInlining)]
	private static void OnDestroyInternal()
	{
		object modConfig = ModConfig;
		ModConfigCatalog.Remove((ModConfigEntry)((modConfig is ModConfigEntry) ? modConfig : null));
		ModConfig = null;
	}
}
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("com.KingEnderBrine.PartialMetamorphosis", "Partial Metamorphosis", "1.3.1")]
public class PartialMetamorphosisPlugin : BaseUnityPlugin
{
	public const string GUID = "com.KingEnderBrine.PartialMetamorphosis";

	public const string Name = "Partial Metamorphosis";

	public const string Version = "1.3.1";

	private static readonly MethodInfo startRun = typeof(PreGameController).GetMethod("StartRun", BindingFlags.Instance | BindingFlags.NonPublic);

	private static readonly MethodInfo respawn = typeof(CharacterMaster).GetMethod("Respawn", new Type[3]
	{
		typeof(Vector3),
		typeof(Quaternion),
		typeof(bool)
	});

	private static readonly HashSet<NetworkUser> votedForMetamorphosis = new HashSet<NetworkUser>();

	internal static PartialMetamorphosisPlugin Instance { get; private set; }

	internal static ManualLogSource InstanceLogger => ((BaseUnityPlugin)Instance).Logger;

	internal static ConfigEntry<bool> IsEnabled { get; set; }

	internal static ConfigEntry<int> StagesBetweenMetamorphosis { get; set; }

	public void Start()
	{
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Expected O, but got Unknown
		IsEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Main", "enabled", true, "Is mod enabled");
		StagesBetweenMetamorphosis = ((BaseUnityPlugin)this).Config.Bind<int>("Main", "StagesBetweenMetamorphosis", 0, "How much stages should pass until metamorphosis will be applied next time. 0 - metamorphosis will be applied every stage");
		HookEndpointManager.Add((MethodBase)startRun, (Delegate)new Action<Action<PreGameController>, PreGameController>(PreGameControllerStartRun));
		HookEndpointManager.Modify((MethodBase)respawn, (Delegate)new Manipulator(CharacterMasterRespawn));
		InLobbyConfigIntegration.OnStart();
	}

	public void Destroy()
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		HookEndpointManager.Remove((MethodBase)startRun, (Delegate)new Action<Action<PreGameController>, PreGameController>(PreGameControllerStartRun));
		HookEndpointManager.Unmodify((MethodBase)respawn, (Delegate)new Manipulator(CharacterMasterRespawn));
		InLobbyConfigIntegration.OnDestroy();
	}

	private static void CharacterMasterRespawn(ILContext il)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Expected O, but got Unknown
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		ILCursor val = new ILCursor(il);
		val.GotoNext(new Func<Instruction, bool>[1]
		{
			(Instruction x) => ILPatternMatchingExt.MatchCall(x, typeof(Artifacts), "get_randomSurvivorOnRespawnArtifactDef")
		});
		val.Index += 3;
		object operand = val.Previous.Operand;
		val.Emit(OpCodes.Ldarg_0);
		val.EmitDelegate<Func<CharacterMaster, bool>>((Func<CharacterMaster, bool>)ShouldChangeCharacter);
		val.Emit(OpCodes.Brfalse_S, operand);
	}

	public static bool ShouldChangeCharacter(CharacterMaster master)
	{
		if (IsEnabled.Value)
		{
			if (Run.instance.stageClearCount % (StagesBetweenMetamorphosis.Value + 1) == 0)
			{
				return votedForMetamorphosis.Any((NetworkUser el) => (Object)(object)el.master == (Object)(object)master);
			}
			return false;
		}
		return true;
	}

	private static void PreGameControllerStartRun(Action<PreGameController> orig, PreGameController self)
	{
		try
		{
			votedForMetamorphosis.Clear();
			RuleChoiceDef val = RuleCatalog.FindChoiceDef("Artifacts.RandomSurvivorOnRespawn.On");
			foreach (NetworkUser readOnlyInstances in NetworkUser.readOnlyInstancesList)
			{
				PreGameRuleVoteController val2 = PreGameRuleVoteController.FindForUser(readOnlyInstances);
				if (Object.op_Implicit((Object)(object)val2) && val2.IsChoiceVoted(val))
				{
					votedForMetamorphosis.Add(readOnlyInstances);
				}
			}
		}
		catch (Exception ex)
		{
			InstanceLogger.LogError((object)ex.ToString());
		}
		orig(self);
	}
}