Decompiled source of CorruptionPeriaptCompatibility v1.3.1

PeriaptCompatibility.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using RoR2;
using RoR2.ContentManagement;
using RoR2.UI.MainMenu;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace PeriaptCompatibility;

[BepInPlugin("binma.periaptbossfix", "Corruption Periapt Compatibility", "1.3.1")]
public class Plugin : BaseUnityPlugin
{
	internal static ConfigEntry<string> KeepFor;

	internal static ConfigEntry<bool> LarvaRecipe;

	internal static ManualLogSource Log;

	internal const string Zoea = "VoidMegaCrabItem";

	private void Awake()
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		KeepFor = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Items allowed to craft into Zoea", "BeetleGland", "Comma-separated internal item names. A recipe that makes Newly Hatched Zoea is only kept if it uses one of these (default: Queen's Gland).");
		LarvaRecipe = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Restore Pluripotent Larva with Irradiant Pearl", true, "Adds a Wandering CHEF recipe: a consumed Pluripotent Larva + Irradiant Pearl = Pluripotent Larva.");
		new Harmony("binma.periaptcompatibility").PatchAll(typeof(Plugin).Assembly);
		RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, (Action)delegate
		{
			Scrub.Live("game load");
		});
	}

	internal static HashSet<string> Allowed()
	{
		return new HashSet<string>(from s in KeepFor.Value.Split(',')
			select s.Trim() into s
			where s.Length > 0
			select s);
	}

	internal static bool RecipeAllowed(Recipe r, HashSet<string> allowed)
	{
		if (r == null || r.ingredients == null)
		{
			return true;
		}
		RecipeIngredient[] ingredients = r.ingredients;
		foreach (RecipeIngredient val in ingredients)
		{
			if (val != null && Object.op_Implicit(val.pickup) && allowed.Contains(val.pickup.name))
			{
				return true;
			}
		}
		return false;
	}
}
[HarmonyPatch(typeof(CraftableCatalog), "SetCraftableDefs")]
internal static class SetDefsPatch
{
	private static void Prefix(ref CraftableDef[] newCraftableDefs)
	{
		if (newCraftableDefs == null)
		{
			return;
		}
		try
		{
			HashSet<string> allowed = Plugin.Allowed();
			List<CraftableDef> list = new List<CraftableDef>(newCraftableDefs.Length);
			int num = 0;
			CraftableDef[] array = newCraftableDefs;
			foreach (CraftableDef val in array)
			{
				if (!Object.op_Implicit((Object)(object)val) || val.recipes == null || !Object.op_Implicit(val.pickup) || val.pickup.name != "VoidMegaCrabItem")
				{
					list.Add(val);
					continue;
				}
				Recipe[] array2 = val.recipes.Where((Recipe r) => Plugin.RecipeAllowed(r, allowed)).ToArray();
				num += val.recipes.Length - array2.Length;
				if (array2.Length != 0)
				{
					val.recipes = array2;
					list.Add(val);
				}
			}
			if (num > 0)
			{
				Plugin.Log.LogInfo((object)("Removed " + num + " recipe(s) that crafted Newly Hatched Zoea from something other than Queen's Gland."));
			}
			CraftableDef val2 = LarvaRecipe.Get();
			if (Object.op_Implicit((Object)(object)val2) && !list.Contains(val2))
			{
				list.Add(val2);
			}
			if (num > 0 || Object.op_Implicit((Object)(object)val2))
			{
				newCraftableDefs = list.ToArray();
				FieldInfo fieldInfo = AccessTools.Field(typeof(ContentManager), "_craftableDefs");
				if (fieldInfo != null)
				{
					fieldInfo.SetValue(null, newCraftableDefs);
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)ex);
		}
	}
}
[HarmonyPatch(typeof(MainMenuController), "Start")]
internal static class MainMenuPatch
{
	private static void Postfix()
	{
		Scrub.Live("main menu");
	}
}
internal static class LarvaRecipe
{
	private static CraftableDef def;

	internal static CraftableDef Get()
	{
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Expected O, but got Unknown
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Expected O, but got Unknown
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Expected O, but got Unknown
		if (!Plugin.LarvaRecipe.Value)
		{
			return null;
		}
		if (Object.op_Implicit((Object)(object)def))
		{
			return def;
		}
		ItemDef itemDef = ItemCatalog.GetItemDef(ItemCatalog.FindItemIndex("ExtraLifeVoid"));
		ItemDef itemDef2 = ItemCatalog.GetItemDef(ItemCatalog.FindItemIndex("ExtraLifeVoidConsumed"));
		ItemDef itemDef3 = ItemCatalog.GetItemDef(ItemCatalog.FindItemIndex("ShinyPearl"));
		if (!Object.op_Implicit((Object)(object)itemDef) || !Object.op_Implicit((Object)(object)itemDef2) || !Object.op_Implicit((Object)(object)itemDef3))
		{
			Plugin.Log.LogWarning((object)"Couldn't find the items for the Pluripotent Larva recipe.");
			return null;
		}
		def = ScriptableObject.CreateInstance<CraftableDef>();
		((Object)def).name = "binmaRestoreLarva";
		def.pickup = (Object)(object)itemDef;
		Recipe val = new Recipe();
		val.amountToDrop = 1;
		RecipeIngredient val2 = new RecipeIngredient();
		val2.pickup = (Object)(object)itemDef2;
		RecipeIngredient val3 = new RecipeIngredient();
		val3.pickup = (Object)(object)itemDef3;
		val.ingredients = (RecipeIngredient[])(object)new RecipeIngredient[2] { val2, val3 };
		def.recipes = (Recipe[])(object)new Recipe[1] { val };
		Plugin.Log.LogInfo((object)"Added recipe: consumed Pluripotent Larva + Irradiant Pearl = Pluripotent Larva.");
		return def;
	}
}
[HarmonyPatch(typeof(CraftingController), "GetGeneratedOptionsFromInteractor")]
internal static class LarvaOptionPatch
{
	private static void Prefix(out bool __state)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		__state = true;
		try
		{
			if (Plugin.LarvaRecipe.Value)
			{
				ItemDef itemDef = ItemCatalog.GetItemDef(ItemCatalog.FindItemIndex("ExtraLifeVoidConsumed"));
				if (Object.op_Implicit((Object)(object)itemDef))
				{
					__state = itemDef.canRemove;
					itemDef.canRemove = true;
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)ex);
		}
	}

	private static void Postfix(bool __state)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			ItemDef itemDef = ItemCatalog.GetItemDef(ItemCatalog.FindItemIndex("ExtraLifeVoidConsumed"));
			if (Object.op_Implicit((Object)(object)itemDef))
			{
				itemDef.canRemove = __state;
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)ex);
		}
	}
}
internal static class Scrub
{
	private static readonly FieldInfo allRecipesF = AccessTools.Field(typeof(CraftableCatalog), "allRecipes");

	private static readonly FieldInfo byResultF = AccessTools.Field(typeof(CraftableCatalog), "resultToRecipeSearchTable");

	private static readonly FieldInfo byIngredientF = AccessTools.Field(typeof(CraftableCatalog), "pickupToIngredientSearchTable");

	internal static void Live(string when)
	{
		//IL_000b: 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_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Invalid comparison between Unknown and I4
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			ItemIndex val = ItemCatalog.FindItemIndex("VoidMegaCrabItem");
			if ((int)val != -1)
			{
				PickupIndex zoea = PickupCatalog.FindPickupIndex(val);
				HashSet<string> allowed = Plugin.Allowed();
				Func<object, bool> bad = delegate(object entry)
				{
					//IL_000e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0014: Unknown result type (might be due to invalid IL or missing references)
					RecipeEntry val2 = (RecipeEntry)((entry is RecipeEntry) ? entry : null);
					return val2 != null && val2.result == zoea && !Plugin.RecipeAllowed(val2.recipe, allowed);
				};
				int num = Prune(allRecipesF.GetValue(null) as IList, bad);
				PruneTable(byResultF.GetValue(null) as IDictionary, bad);
				PruneTable(byIngredientF.GetValue(null) as IDictionary, bad);
				if (num > 0)
				{
					Plugin.Log.LogInfo((object)("Removed " + num + " late-added Newly Hatched Zoea recipe(s) at " + when + "."));
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)ex);
		}
	}

	private static int Prune(IList list, Func<object, bool> bad)
	{
		if (list == null)
		{
			return 0;
		}
		int num = 0;
		for (int num2 = list.Count - 1; num2 >= 0; num2--)
		{
			if (bad(list[num2]))
			{
				list.RemoveAt(num2);
				num++;
			}
		}
		return num;
	}

	private static void PruneTable(IDictionary table, Func<object, bool> bad)
	{
		if (table == null)
		{
			return;
		}
		foreach (object item in table.Values.Cast<object>().ToList())
		{
			Prune(item as IList, bad);
		}
	}
}