Decompiled source of BreedingUpgradesChickenEggFix v1.0.0

BreedingUpgradesChickenEggFix.dll

Decompiled 12 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyVersion("0.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 BreedingUpgradesChickenEggFix
{
	[BepInPlugin("local.valheim.breedingupgradeschickeneggfix", "BreedingUpgrades Chicken Egg Fix", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInIncompatibility("local.valheim.compatibilityfixes")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private static class BreedingIsEggItemPatch
		{
			public static void Postfix(ItemDrop __0, ref bool __result)
			{
				if (__result && ProtectedEggs.IsProtected(__0))
				{
					__result = false;
				}
			}
		}

		public const string PluginGuid = "local.valheim.breedingupgradeschickeneggfix";

		public const string PluginName = "BreedingUpgrades Chicken Egg Fix";

		public const string PluginVersion = "1.0.0";

		public const string BreedingUpgradesGuid = "dumba.breedingupgrades";

		public const string CombinedPluginGuid = "local.valheim.compatibilityfixes";

		internal static ConfigEntry<bool> FixEnabled;

		internal static ConfigEntry<bool> RepairExistingChickenEggs;

		internal static ConfigEntry<string> AdditionalProtectedEggPrefabs;

		internal static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			FixEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Fix Enabled", true, "Prevent BreedingUpgrades from creating starred vanilla ChickenEgg items. Asksvin eggs and other eggs are unchanged unless listed below.");
			RepairExistingChickenEggs = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Repair", "Repair Existing Chicken Eggs", true, "Change already-starred protected eggs back to quality 1 when world drops or inventories load/save.");
			AdditionalProtectedEggPrefabs = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Repair", "Additional Protected Egg Prefabs", string.Empty, "Optional comma- or semicolon-separated prefab names to keep at quality 1 in addition to ChickenEgg. Leave blank for vanilla chickens only.");
			ProtectedEggs.RefreshAdditionalPrefabs(AdditionalProtectedEggPrefabs.Value);
			AdditionalProtectedEggPrefabs.SettingChanged += delegate
			{
				ProtectedEggs.RefreshAdditionalPrefabs(AdditionalProtectedEggPrefabs.Value);
			};
			_harmony = new Harmony("local.valheim.breedingupgradeschickeneggfix");
			_harmony.PatchAll(typeof(ItemDropSetQualityPatch));
			_harmony.PatchAll(typeof(ItemDropLoadPatch));
			_harmony.PatchAll(typeof(InventoryLoadPatch));
			_harmony.PatchAll(typeof(InventorySavePatch));
			InstallBreedingUpgradesEggGate();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded BreedingUpgrades Chicken Egg Fix 1.0.0.");
		}

		private void InstallBreedingUpgradesEggGate()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Expected O, but got Unknown
			MethodInfo methodInfo = AccessTools.Method("BreedingUpgrades.Patches.ItemDropSetQualityPatch:IsEggItem", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"BreedingUpgrades IsEggItem was not found. The SetQuality failsafe remains active, but BreedingUpgrades may print a cosmetic upgrade message for protected eggs.");
				return;
			}
			HarmonyMethod val = new HarmonyMethod(typeof(BreedingIsEggItemPatch), "Postfix", (Type[])null)
			{
				priority = 0
			};
			_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Installed the BreedingUpgrades vanilla-chicken egg selection gate.");
		}
	}
	internal static class ProtectedEggs
	{
		private static readonly HashSet<string> ProtectedPrefabNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "ChickenEgg" };

		private static readonly HashSet<string> ChickenEggSharedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "$item_chickenegg", "$item_chicken_egg" };

		internal static void RefreshAdditionalPrefabs(string value)
		{
			ProtectedPrefabNames.Clear();
			ProtectedPrefabNames.Add("ChickenEgg");
			if (!string.IsNullOrWhiteSpace(value))
			{
				string[] array = value.Split(new char[2] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
				string[] array2 = array;
				foreach (string name in array2)
				{
					string text = NormalizePrefabName(name);
					if (!string.IsNullOrEmpty(text))
					{
						ProtectedPrefabNames.Add(text);
					}
				}
			}
			if (Plugin.Log != null)
			{
				Plugin.Log.LogInfo((object)("Protected egg prefabs: " + string.Join(", ", ProtectedPrefabNames)));
			}
		}

		private static string NormalizePrefabName(string name)
		{
			if (string.IsNullOrWhiteSpace(name))
			{
				return string.Empty;
			}
			string text = name.Trim();
			int num = text.IndexOf("(Clone)", StringComparison.OrdinalIgnoreCase);
			if (num >= 0)
			{
				text = text.Substring(0, num).Trim();
			}
			return text;
		}

		private static bool IsProtectedPrefabName(string name)
		{
			return ProtectedPrefabNames.Contains(NormalizePrefabName(name));
		}

		internal static bool IsProtected(ItemDrop itemDrop)
		{
			if (!FeatureEnabled() || (Object)(object)itemDrop == (Object)null)
			{
				return false;
			}
			if (IsProtected(itemDrop.m_itemData))
			{
				return true;
			}
			return (Object)(object)((Component)itemDrop).gameObject != (Object)null && IsProtectedPrefabName(((Object)((Component)itemDrop).gameObject).name);
		}

		internal static bool IsProtected(ItemData item)
		{
			if (!FeatureEnabled() || item == null || item.m_shared == null)
			{
				return false;
			}
			if (ChickenEggSharedNames.Contains(item.m_shared.m_name))
			{
				return true;
			}
			return (Object)(object)item.m_dropPrefab != (Object)null && IsProtectedPrefabName(((Object)item.m_dropPrefab).name);
		}

		private static bool FeatureEnabled()
		{
			return Plugin.FixEnabled != null && Plugin.FixEnabled.Value;
		}

		internal static bool Normalize(ItemData item)
		{
			if (Plugin.RepairExistingChickenEggs != null && Plugin.RepairExistingChickenEggs.Value && IsProtected(item) && item.m_quality != 1)
			{
				item.m_quality = 1;
				return true;
			}
			return false;
		}

		internal static void NormalizeInventory(Inventory inventory)
		{
			if (inventory == null || Plugin.RepairExistingChickenEggs == null || !Plugin.RepairExistingChickenEggs.Value)
			{
				return;
			}
			int num = 0;
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (Normalize(allItem))
				{
					num++;
				}
			}
			if (num > 0)
			{
				Plugin.Log.LogInfo((object)("Repaired " + num + " starred protected egg(s) in " + inventory.GetName() + "."));
			}
		}
	}
	[HarmonyPatch(typeof(ItemDrop), "SetQuality")]
	internal static class ItemDropSetQualityPatch
	{
		[HarmonyPrefix]
		[HarmonyPriority(0)]
		[HarmonyAfter(new string[] { "dumba.breedingupgrades" })]
		private static void Prefix(ItemDrop __instance, ref int quality)
		{
			if (ProtectedEggs.IsProtected(__instance))
			{
				quality = 1;
			}
		}

		[HarmonyPostfix]
		[HarmonyAfter(new string[] { "dumba.breedingupgrades" })]
		private static void Postfix(ItemDrop __instance)
		{
			if ((Object)(object)__instance != (Object)null && ProtectedEggs.IsProtected(__instance) && __instance.m_itemData != null && __instance.m_itemData.m_quality != 1)
			{
				__instance.m_itemData.m_quality = 1;
			}
		}
	}
	[HarmonyPatch(typeof(ItemDrop), "Load")]
	internal static class ItemDropLoadPatch
	{
		[HarmonyPostfix]
		private static void Postfix(ItemDrop __instance)
		{
			if ((Object)(object)__instance != (Object)null && ProtectedEggs.Normalize(__instance.m_itemData))
			{
				__instance.SetQuality(1);
			}
		}
	}
	[HarmonyPatch(typeof(Inventory), "Load")]
	internal static class InventoryLoadPatch
	{
		[HarmonyPostfix]
		private static void Postfix(Inventory __instance)
		{
			ProtectedEggs.NormalizeInventory(__instance);
		}
	}
	[HarmonyPatch(typeof(Inventory), "Save")]
	internal static class InventorySavePatch
	{
		[HarmonyPrefix]
		private static void Prefix(Inventory __instance)
		{
			ProtectedEggs.NormalizeInventory(__instance);
		}
	}
}