Decompiled source of Enforcer Variants v1.1.2

plugins/EnforcerSurvariantsCompat.dll

Decompiled 11 hours ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
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("EnforcerSurvariantsCompat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EnforcerSurvariantsCompat")]
[assembly: AssemblyTitle("EnforcerSurvariantsCompat")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 EnforcerSurvariantsCompat
{
	[BepInPlugin("com.ple.enforcersurvariantscompat", "Enforcer Survariants Compatibility", "1.1.1")]
	public sealed class EnforcerSurvariantsCompatPlugin : BaseUnityPlugin
	{
		private const string PluginGuid = "com.ple.enforcersurvariantscompat";

		private const string PluginName = "Enforcer Survariants Compatibility";

		private const string PluginVersion = "1.1.1";

		private static ManualLogSource Log;

		private bool registrationAttempted;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(RegisterCompatibility));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded. Will register EnforcerGang's Enforcer survivors as variants of the official Enforcer after survivor catalogs load.");
		}

		private void OnDestroy()
		{
			RoR2Application.onLoad = (Action)Delegate.Remove(RoR2Application.onLoad, new Action(RegisterCompatibility));
		}

		private void RegisterCompatibility()
		{
			if (registrationAttempted)
			{
				return;
			}
			registrationAttempted = true;
			try
			{
				Type variantType = FindType("Survariants.SurvivorVariantDef");
				Type type = FindType("Survariants.SurvivorVariantCatalog");
				if (variantType == null || type == null)
				{
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Survariants API not found; compatibility is inactive.");
					return;
				}
				SurvivorDef val = SurvivorCatalog.allSurvivorDefs.FirstOrDefault((Func<SurvivorDef, bool>)IsOfficialEnforcer);
				SurvivorDef val2 = SurvivorCatalog.allSurvivorDefs.FirstOrDefault((Func<SurvivorDef, bool>)IsEnforcerGangEnforcer);
				SurvivorDef val3 = SurvivorCatalog.allSurvivorDefs.FirstOrDefault((Func<SurvivorDef, bool>)IsNemforcer);
				if ((Object)(object)val == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not identify the Hallowed Concepts Enforcer. No survivor was hidden or changed.");
					return;
				}
				MethodInfo methodInfo = type.GetMethods(BindingFlags.Static | BindingFlags.Public).FirstOrDefault((MethodInfo method) => method.Name == "AddSurvivorVariant" && method.GetParameters().Length == 1 && method.GetParameters()[0].ParameterType.IsAssignableFrom(variantType));
				if (methodInfo == null)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)"Survariants is installed, but its AddSurvivorVariant API was not found.");
					return;
				}
				bool flag = false;
				if ((Object)(object)val2 != (Object)null)
				{
					flag |= RegisterVariant(methodInfo, variantType, val, val2, "EnforcerGangEnforcerVariant", "Classic Enforcer", "The classic Enforcer joins the Hallowed Concepts Enforcer as a selectable variant.");
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not identify EnforcerGang's classic Enforcer survivor.");
				}
				if ((Object)(object)val3 != (Object)null)
				{
					flag |= RegisterVariant(methodInfo, variantType, val, val3, "EnforcerGangNemforcerVariant", "NEMFORCER_NAME", "The Nemesis Enforcer joins the Hallowed Concepts Enforcer as a selectable variant.");
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not identify EnforcerGang's Nemesis Enforcer survivor.");
				}
				if (!flag)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"No Enforcer variants were registered.");
				}
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to register Enforcer variant: {arg}");
			}
		}

		private static Type? FindType(string fullName)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i = 0; i < assemblies.Length; i++)
			{
				Type type = assemblies[i].GetType(fullName, throwOnError: false);
				if (type != null)
				{
					return type;
				}
			}
			return null;
		}

		private static bool IsOfficialEnforcer(SurvivorDef survivor)
		{
			string obj = survivor.cachedName ?? string.Empty;
			string text = (((Object)(object)survivor.bodyPrefab != (Object)null) ? ((Object)survivor.bodyPrefab).name : string.Empty);
			if (!obj.Equals("Enforcer2", StringComparison.OrdinalIgnoreCase))
			{
				return text.Equals("Enforcer2Body", StringComparison.OrdinalIgnoreCase);
			}
			return true;
		}

		private static bool IsEnforcerGangEnforcer(SurvivorDef survivor)
		{
			string text = survivor.cachedName ?? string.Empty;
			string obj = (((Object)(object)survivor.bodyPrefab != (Object)null) ? ((Object)survivor.bodyPrefab).name : string.Empty);
			string text2 = ((!((Object)(object)survivor.bodyPrefab != (Object)null)) ? string.Empty : (survivor.bodyPrefab.GetComponent<CharacterBody>()?.baseNameToken ?? string.Empty));
			if (!obj.Equals("EnforcerBody", StringComparison.OrdinalIgnoreCase))
			{
				if (text.Equals("Enforcer", StringComparison.OrdinalIgnoreCase))
				{
					return text2.Equals("ENFORCER_NAME", StringComparison.OrdinalIgnoreCase);
				}
				return false;
			}
			return true;
		}

		private static bool IsNemforcer(SurvivorDef survivor)
		{
			string text = survivor.cachedName ?? string.Empty;
			string obj = (((Object)(object)survivor.bodyPrefab != (Object)null) ? ((Object)survivor.bodyPrefab).name : string.Empty);
			string text2 = ((!((Object)(object)survivor.bodyPrefab != (Object)null)) ? string.Empty : (survivor.bodyPrefab.GetComponent<CharacterBody>()?.baseNameToken ?? string.Empty));
			if (!obj.Equals("NemesisEnforcerBody", StringComparison.OrdinalIgnoreCase) && !text.Equals("Nemforcer", StringComparison.OrdinalIgnoreCase))
			{
				return text2.Equals("NEMFORCER_NAME", StringComparison.OrdinalIgnoreCase);
			}
			return true;
		}

		private bool RegisterVariant(MethodInfo addVariant, Type variantType, SurvivorDef targetSurvivor, SurvivorDef variantSurvivor, string internalName, string displayNameToken, string description)
		{
			try
			{
				ScriptableObject val = ScriptableObject.CreateInstance(variantType);
				((Object)val).name = internalName;
				SetMember(val, "DisplayName", displayNameToken);
				SetMember(val, "VariantSurvivor", variantSurvivor);
				SetMember(val, "TargetSurvivor", targetSurvivor);
				SetMember(val, "Description", description);
				addVariant.Invoke(null, new object[1] { val });
				variantSurvivor.hidden = true;
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Registered " + displayNameToken + " as a variant of the official Enforcer."));
				return true;
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to register {displayNameToken} as an Enforcer variant: {arg}");
				return false;
			}
		}

		private static void SetMember(object target, string name, object value)
		{
			Type type = target.GetType();
			PropertyInfo property = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public);
			if (property != null && property.CanWrite)
			{
				property.SetValue(target, value);
				return;
			}
			FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public);
			if (field != null)
			{
				field.SetValue(target, value);
				return;
			}
			throw new MissingMemberException(type.FullName, name);
		}
	}
}