Decompiled source of DisableHellbroth v1.0.4

Cjayride.DisableHellbroth.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("Cjayride.DisableHellbroth")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+a424fab5a3d5e0862d4b8421c0c624cc2989ef84")]
[assembly: AssemblyProduct("Cjayride.DisableHellbroth")]
[assembly: AssemblyTitle("Cjayride.DisableHellbroth")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.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 Cjayride.DisableHellbroth
{
	[BepInPlugin("cjayride.DisableHellbroth", "Disable Hellbroth", "1.0.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "cjayride.DisableHellbroth";

		public const string PluginName = "Disable Hellbroth";

		public const string PluginVersion = "1.0.4";

		internal static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "cjayride.DisableHellbroth");
			((MonoBehaviour)this).StartCoroutine(DelayedRecipePurge.PurgeRepeatedly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Hellbroth potions, Odins Alchemy Wand, and Odins Dragon Staff: recipes purged, items removed from ObjectDB, craft/use blocked.");
		}
	}
	internal static class HellbrothIds
	{
		private static readonly Dictionary<string, bool> NameCache = new Dictionary<string, bool>(64, StringComparer.OrdinalIgnoreCase);

		internal static bool IsHellbroth(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				return false;
			}
			if (NameCache.TryGetValue(name, out var value))
			{
				return value;
			}
			bool flag = Compute(name);
			NameCache[name] = flag;
			return flag;
		}

		private static bool Compute(string name)
		{
			if (name.IndexOf("Hellbroth", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return true;
			}
			if (IsAlchemyWandUncached(name) || IsDragonStaffUncached(name))
			{
				return true;
			}
			return false;
		}

		internal static bool IsAlchemyWand(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				return false;
			}
			if (NameCache.TryGetValue(name, out var value))
			{
				return value;
			}
			bool num = IsAlchemyWandUncached(name);
			if (num)
			{
				NameCache[name] = true;
			}
			return num;
		}

		private static bool IsAlchemyWandUncached(string name)
		{
			if (name.IndexOf("Alchemy", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return false;
			}
			if (name.IndexOf("OdinsAlchemyWand", StringComparison.OrdinalIgnoreCase) < 0 && name.IndexOf("Odins_Alchemy_Wand", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return name.IndexOf("Alchemy Wand", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}

		internal static bool IsDragonStaff(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				return false;
			}
			if (NameCache.TryGetValue(name, out var value))
			{
				return value;
			}
			bool num = IsDragonStaffUncached(name);
			if (num)
			{
				NameCache[name] = true;
			}
			return num;
		}

		private static bool IsDragonStaffUncached(string name)
		{
			if (name.IndexOf("DragonStaff", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return true;
			}
			if (name.IndexOf("Dragon_Staff", StringComparison.OrdinalIgnoreCase) < 0)
			{
				return name.IndexOf("Dragon Staff", StringComparison.OrdinalIgnoreCase) >= 0;
			}
			return true;
		}

		internal static bool IsRemovedPotionsPlusItem(string name)
		{
			if (!IsAlchemyWandUncached(name))
			{
				return IsDragonStaffUncached(name);
			}
			return true;
		}

		internal static bool IsHellbroth(ItemData item)
		{
			if (item?.m_shared == null)
			{
				return false;
			}
			if ((Object)(object)item.m_dropPrefab != (Object)null && IsHellbroth(((Object)item.m_dropPrefab).name))
			{
				return true;
			}
			return IsHellbroth(item.m_shared.m_name);
		}

		internal static bool IsHellbroth(Recipe recipe)
		{
			if ((Object)(object)recipe?.m_item != (Object)null)
			{
				return IsHellbroth(recipe.m_item.m_itemData);
			}
			return false;
		}

		internal static bool IsBlockedRecipe(Recipe recipe)
		{
			if ((Object)(object)recipe?.m_item == (Object)null)
			{
				return false;
			}
			if (IsHellbroth(recipe))
			{
				return true;
			}
			if (((Object)recipe.m_item).name != null && IsRemovedPotionsPlusItem(((Object)recipe.m_item).name))
			{
				return true;
			}
			ItemData itemData = recipe.m_item.m_itemData;
			if ((Object)(object)itemData?.m_dropPrefab != (Object)null && IsRemovedPotionsPlusItem(((Object)itemData.m_dropPrefab).name))
			{
				return true;
			}
			if (itemData?.m_shared != null)
			{
				return IsRemovedPotionsPlusItem(itemData.m_shared.m_name);
			}
			return false;
		}

		internal static bool IsBlockedItem(ItemData item)
		{
			if (IsHellbroth(item))
			{
				return true;
			}
			if ((Object)(object)item?.m_dropPrefab != (Object)null && IsRemovedPotionsPlusItem(((Object)item.m_dropPrefab).name))
			{
				return true;
			}
			if (item?.m_shared != null)
			{
				return IsRemovedPotionsPlusItem(item.m_shared.m_name);
			}
			return false;
		}
	}
	internal static class HellbrothRecipePurge
	{
		[ThreadStatic]
		private static bool _running;

		internal static void PurgeObjectDb(ObjectDB db)
		{
			if (_running || db?.m_recipes == null)
			{
				return;
			}
			_running = true;
			try
			{
				PurgeObjectDbUnguarded(db);
			}
			finally
			{
				_running = false;
			}
		}

		private static void PurgeObjectDbUnguarded(ObjectDB db)
		{
			int num = db.m_recipes.RemoveAll(HellbrothIds.IsBlockedRecipe);
			if (num > 0)
			{
				ManualLogSource log = Plugin.Log;
				if (log != null)
				{
					log.LogInfo((object)$"Removed {num} blocked PotionsPlus recipe(s) from ObjectDB.");
				}
			}
			if (db.m_items == null)
			{
				return;
			}
			int num2 = db.m_items.RemoveAll((GameObject go) => (Object)(object)go != (Object)null && HellbrothIds.IsRemovedPotionsPlusItem(((Object)go).name));
			if (num2 > 0)
			{
				db.UpdateRegisters();
				ManualLogSource log2 = Plugin.Log;
				if (log2 != null)
				{
					log2.LogInfo((object)$"Removed blocked PotionsPlus items from ObjectDB ({num2} prefab(s)).");
				}
			}
		}
	}
	internal static class DelayedRecipePurge
	{
		internal static IEnumerator PurgeRepeatedly()
		{
			float[] delays = new float[5] { 0.5f, 2f, 5f, 15f, 30f };
			for (int i = 0; i < delays.Length; i++)
			{
				yield return (object)new WaitForSecondsRealtime(delays[i]);
				if ((Object)(object)ObjectDB.instance != (Object)null)
				{
					HellbrothRecipePurge.PurgeObjectDb(ObjectDB.instance);
				}
			}
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "Awake")]
	internal static class ObjectDbAwakePurgeHellbroth
	{
		private static void Postfix(ObjectDB __instance)
		{
			HellbrothRecipePurge.PurgeObjectDb(__instance);
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "CopyOtherDB")]
	internal static class ObjectDbCopyPurgeHellbroth
	{
		private static void Postfix(ObjectDB __instance)
		{
			HellbrothRecipePurge.PurgeObjectDb(__instance);
		}
	}
	[HarmonyPatch(typeof(ObjectDB), "UpdateRegisters")]
	internal static class ObjectDbUpdateRegistersPurgeHellbroth
	{
		private static void Postfix(ObjectDB __instance)
		{
			HellbrothRecipePurge.PurgeObjectDb(__instance);
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "DoCrafting")]
	internal static class BlockHellbrothCrafting
	{
		private static bool Prefix(InventoryGui __instance, Player player)
		{
			if (!HellbrothIds.IsBlockedRecipe(__instance.m_craftRecipe))
			{
				return true;
			}
			if ((Object)(object)player != (Object)null)
			{
				((Character)player).Message((MessageType)2, "$msg_blocked", 0, (Sprite)null, false);
			}
			HellbrothRecipePurge.PurgeObjectDb(ObjectDB.instance);
			return false;
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "UpdateRecipeList")]
	internal static class FilterHellbrothCraftingUi
	{
		private static void Prefix(InventoryGui __instance, List<Recipe> recipes)
		{
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			if (recipes != null && recipes.Count > 0)
			{
				recipes.RemoveAll(HellbrothIds.IsBlockedRecipe);
			}
			if (HellbrothIds.IsBlockedRecipe(__instance.m_craftRecipe))
			{
				__instance.m_craftRecipe = null;
			}
			if (HellbrothIds.IsBlockedRecipe(((RecipeDataPair)(ref __instance.m_selectedRecipe)).Recipe))
			{
				__instance.m_selectedRecipe = default(RecipeDataPair);
				__instance.m_craftRecipe = null;
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "UseItem")]
	internal static class BlockHellbrothUse
	{
		private static bool Prefix(Humanoid __instance, ItemData item)
		{
			if (!HellbrothIds.IsBlockedItem(item))
			{
				return true;
			}
			Player val = (Player)(object)((__instance is Player) ? __instance : null);
			if (val != null)
			{
				((Character)val).Message((MessageType)2, "$msg_blocked", 0, (Sprite)null, false);
			}
			return false;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}