Decompiled source of RareDevotionEggs v0.1.2

plugins/RareDevotionEggs.dll

Decompiled a day 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 BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using On.RoR2;
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("RareDevotionEggs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RareDevotionEggs")]
[assembly: AssemblyTitle("RareDevotionEggs")]
[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 RareDevotionEggs
{
	[BepInPlugin("com.xedamog.raredevotioneggs", "Rare Devotion Eggs", "0.1.2")]
	[BepInProcess("Risk of Rain 2.exe")]
	public sealed class RareDevotionEggsPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.xedamog.raredevotioneggs";

		public const string PluginName = "Rare Devotion Eggs";

		public const string PluginVersion = "0.1.2";

		private ConfigEntry<float> eggChance;

		private ConfigEntry<bool> enableInEclipse;

		private ConfigEntry<bool> enableWhenDevotionArtifactActive;

		private InteractableSpawnCard lemurianEggSpawnCard;

		private ArtifactDef devotionArtifactDef;

		private bool warnedEggMissing;

		private void Awake()
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Expected O, but got Unknown
			eggChance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "EggChance", 0.05f, "Chance that a classic purchasable broken drone spawn is replaced by a Lemurian Egg. 0.05 = 5%. Values are clamped between 0 and 1.");
			enableInEclipse = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableInEclipse", true, "Allow rare Lemurian Eggs to appear in Eclipse runs.");
			enableWhenDevotionArtifactActive = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableWhenDevotionArtifactActive", false, "Let this mod also roll replacements while the Artifact of Devotion is already active. Keep false to avoid duplicate or conflicting replacement logic.");
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, new Action(ResolveGameContent));
			DirectorCore.TrySpawnObject += new hook_TrySpawnObject(DirectorCore_TrySpawnObject);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Rare Devotion Eggs 0.1.2 loaded. Classic broken drones have a configurable chance to become Lemurian Eggs.");
		}

		private void OnDestroy()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			RoR2Application.onLoad = (Action)Delegate.Remove(RoR2Application.onLoad, new Action(ResolveGameContent));
			DirectorCore.TrySpawnObject -= new hook_TrySpawnObject(DirectorCore_TrySpawnObject);
		}

		private void ResolveGameContent()
		{
			lemurianEggSpawnCard = FindLemurianEggSpawnCard();
			devotionArtifactDef = FindDevotionArtifactDef();
			if ((Object)(object)lemurianEggSpawnCard != (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Resolved Lemurian Egg spawn card: " + ((Object)lemurianEggSpawnCard).name));
			}
			else
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not resolve the Lemurian Egg spawn card during initial load. The mod will retry when a drone is about to spawn.");
			}
			if ((Object)(object)devotionArtifactDef != (Object)null)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Resolved Artifact of Devotion: " + devotionArtifactDef.cachedName));
			}
		}

		private GameObject DirectorCore_TrySpawnObject(orig_TrySpawnObject orig, DirectorCore self, DirectorSpawnRequest request)
		{
			try
			{
				SpawnCard obj = request?.spawnCard;
				InteractableSpawnCard val = (InteractableSpawnCard)(object)((obj is InteractableSpawnCard) ? obj : null);
				if (val != null && ShouldTryReplacement(val))
				{
					if ((Object)(object)lemurianEggSpawnCard == (Object)null)
					{
						lemurianEggSpawnCard = FindLemurianEggSpawnCard();
					}
					if ((Object)(object)lemurianEggSpawnCard != (Object)null && RollEggChance(request))
					{
						request.spawnCard = (SpawnCard)(object)lemurianEggSpawnCard;
						((BaseUnityPlugin)this).Logger.LogDebug((object)("Replaced drone spawn '" + ((Object)val).name + "' with Lemurian Egg."));
					}
					else if ((Object)(object)lemurianEggSpawnCard == (Object)null && !warnedEggMissing)
					{
						warnedEggMissing = true;
						((BaseUnityPlugin)this).Logger.LogWarning((object)"A drone spawn was detected, but the Lemurian Egg spawn card is still unavailable. No replacement was performed.");
					}
				}
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Rare Devotion Eggs failed while checking a spawn. The original spawn will be used.\n{arg}");
			}
			return orig.Invoke(self, request);
		}

		private bool ShouldTryReplacement(InteractableSpawnCard card)
		{
			if ((Object)(object)Run.instance == (Object)null || (Object)(object)card == (Object)null)
			{
				return false;
			}
			if (!enableInEclipse.Value && IsEclipseRun())
			{
				return false;
			}
			if (!enableWhenDevotionArtifactActive.Value && IsDevotionEnabled())
			{
				return false;
			}
			return IsClassicPurchasableBrokenDrone(card);
		}

		private bool IsClassicPurchasableBrokenDrone(InteractableSpawnCard card)
		{
			string text = (((Object)card).name ?? string.Empty).ToLowerInvariant();
			if (!text.Contains("drone") || (!text.Contains("broken") && !text.StartsWith("iscbroken", StringComparison.Ordinal)))
			{
				return false;
			}
			if (text.Contains("equipment") || text.Contains("turret") || text.Contains("duplicator") || text.Contains("dronecombiner"))
			{
				return false;
			}
			GameObject prefab = ((SpawnCard)card).prefab;
			if ((Object)(object)prefab != (Object)null)
			{
				return (Object)(object)prefab.GetComponent<PurchaseInteraction>() != (Object)null;
			}
			return false;
		}

		private bool RollEggChance(DirectorSpawnRequest request)
		{
			float num = Mathf.Clamp01(eggChance.Value);
			if (num <= 0f)
			{
				return false;
			}
			if (num >= 1f)
			{
				return true;
			}
			return Random.value < num;
		}

		private bool IsEclipseRun()
		{
			return (((object)Run.instance)?.GetType().Name ?? string.Empty).IndexOf("Eclipse", StringComparison.OrdinalIgnoreCase) >= 0;
		}

		private bool IsDevotionEnabled()
		{
			if ((Object)(object)RunArtifactManager.instance == (Object)null)
			{
				return false;
			}
			if ((Object)(object)devotionArtifactDef == (Object)null)
			{
				devotionArtifactDef = FindDevotionArtifactDef();
			}
			if ((Object)(object)devotionArtifactDef != (Object)null)
			{
				return RunArtifactManager.instance.IsArtifactEnabled(devotionArtifactDef);
			}
			return false;
		}

		private InteractableSpawnCard FindLemurianEggSpawnCard()
		{
			return ((IEnumerable<InteractableSpawnCard>)Resources.FindObjectsOfTypeAll<InteractableSpawnCard>()).FirstOrDefault((Func<InteractableSpawnCard, bool>)LooksLikeLemurianEgg);
		}

		private static bool LooksLikeLemurianEgg(InteractableSpawnCard card)
		{
			if ((Object)(object)card == (Object)null)
			{
				return false;
			}
			string text = ((((Object)card).name ?? string.Empty) + " " + (((Object)(object)((SpawnCard)card).prefab != (Object)null) ? ((Object)((SpawnCard)card).prefab).name : string.Empty)).ToLowerInvariant();
			if (!text.Contains("lemurian") || !text.Contains("egg"))
			{
				if (text.Contains("devotion"))
				{
					return text.Contains("egg");
				}
				return false;
			}
			return true;
		}

		private static ArtifactDef FindDevotionArtifactDef()
		{
			return ((IEnumerable<ArtifactDef>)Resources.FindObjectsOfTypeAll<ArtifactDef>()).FirstOrDefault((Func<ArtifactDef, bool>)((ArtifactDef def) => !((Object)(object)def == (Object)null) && ((def.cachedName ?? string.Empty) + " " + (def.nameToken ?? string.Empty)).ToLowerInvariant().Contains("devotion")));
		}
	}
}