Decompiled source of Immersively Obtainable Blue Mushrooms v1.0.3

ObtainableBlueMushrooms.dll

Decompiled 7 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ObtainableBlueMushrooms")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ObtainableBlueMushrooms")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("bdd44dfe-2c25-4b5f-b37d-168252968b93")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.0")]
[module: UnverifiableCode]
namespace ObtainableBlueMushrooms;

internal sealed class ConfigurationManagerAttributes
{
	public bool? ShowRangeAsPercent;

	public Action<ConfigEntryBase> CustomDrawer;

	public bool? Browsable;

	public string Category;

	public object DefaultValue;

	public bool? HideDefaultButton;

	public bool? HideSettingName;

	public string Description;

	public string DispName;

	public int? Order;

	public bool? ReadOnly;

	public bool? IsAdvanced;

	public Func<object, string> ObjToStr;

	public Func<string, object> StrToObj;

	internal static ConfigDescription CustomHiddenDisplay(string description, AcceptableValueBase acceptableValues = null)
	{
		return HiddenDisplay(() => ObtainableBlueMushroomsPlugin.HasPlugin("advize.PlantEverything"), description, acceptableValues);
	}

	internal static ConfigDescription HiddenDisplay(Func<bool> condition, string description, AcceptableValueBase acceptableValues = null)
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Expected O, but got Unknown
		if (condition())
		{
			return new ConfigDescription(description, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = false,
					ReadOnly = true
				}
			});
		}
		return new ConfigDescription(description, acceptableValues, Array.Empty<object>());
	}

	internal static ConfigDescription CustomSeeOnlyDisplay(AcceptableValueBase acceptableValues = null)
	{
		return SeeOnlyDisplay(() => ObtainableBlueMushroomsPlugin.HasPlugin("advize.PlantEverything"), "Whether you can plant regrowing blue mushrooms in frost caves. This is automatically disabled when 'Plant Everything' is installed.", "Cave mushroom planting is disabled in favor of the mushrooms from the 'Plant Everything' mod.", "Disabled in favor of 'Plant Everything' mod", acceptableValues);
	}

	internal static ConfigDescription SeeOnlyDisplay(Func<bool> condition, string description, string overwriteDescription, string overwriteLabel, AcceptableValueBase acceptableValues = null)
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		if (condition())
		{
			return new ConfigDescription(overwriteDescription, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					ReadOnly = true,
					CustomDrawer = delegate
					{
						CustomLabelDrawer(overwriteLabel);
					}
				}
			});
		}
		return new ConfigDescription(description, acceptableValues, Array.Empty<object>());
	}

	private static void CustomLabelDrawer(string labelText)
	{
		GUILayout.Label(labelText, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
	}
}
internal class FoodAndRecipeChanges
{
	internal static void AddFoodAndRecipeChanges(ObjectDB __instance)
	{
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Expected O, but got Unknown
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Unknown result type (might be due to invalid IL or missing references)
		//IL_018b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0193: Expected O, but got Unknown
		foreach (Recipe recipe in __instance.m_recipes)
		{
			if ((Object)(object)recipe?.m_item == (Object)null)
			{
				continue;
			}
			SharedData val = recipe.m_item.m_itemData?.m_shared;
			if (val == null)
			{
				continue;
			}
			if (MushroomConfig.ReplaceMushroomsInWolfSkewer.Value && val.m_name == "$item_wolf_skewer")
			{
				Requirement[] resources = recipe.m_resources;
				foreach (Requirement val2 in resources)
				{
					if (val2.m_resItem.m_itemData.m_shared.m_name == "$item_mushroomcommon")
					{
						val2.m_resItem = PatchObjectDB.blueMushroomItemDrop;
						val2.m_amount = 1;
					}
				}
			}
			if (val.m_name == "$item_onionsoup")
			{
				if (MushroomConfig.MiniFoodRebalance.Value)
				{
					val.m_foodStamina = 65f;
					val.m_food = 21f;
					val.m_foodBurnTime = 1500f;
					val.m_foodRegen = 2f;
				}
				if (MushroomConfig.AddMushroomToOnionSoup.Value)
				{
					recipe.m_resources = (Requirement[])(object)new Requirement[2]
					{
						new Requirement
						{
							m_resItem = PatchObjectDB.blueMushroomItemDrop
						},
						new Requirement
						{
							m_resItem = __instance.GetItemPrefab("Onion").GetComponent<ItemDrop>(),
							m_amount = 3
						}
					};
				}
			}
			if (!MushroomConfig.MiniFoodRebalance.Value || !(val.m_name == "$item_eyescream"))
			{
				continue;
			}
			val.m_foodStamina = 60f;
			val.m_food = 20f;
			val.m_foodBurnTime = 1500f;
			val.m_foodRegen = 1f;
			Requirement[] resources2 = recipe.m_resources;
			foreach (Requirement val3 in resources2)
			{
				if (val3.m_resItem.m_itemData.m_shared.m_name == "$item_greydwarfeye")
				{
					val3.m_amount = 2;
				}
			}
		}
	}
}
[HarmonyPatch(typeof(CharacterDrop), "Start")]
public static class Patch_CharacterDrop_Start
{
	private static void Postfix(CharacterDrop __instance)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Expected O, but got Unknown
		if (MushroomConfig.BatMushroomDropMethod.Value == MushroomConfig.LootChange.Disabled || __instance.m_character.m_name != "$enemy_bat")
		{
			return;
		}
		if ((Object)(object)PatchObjectDB.blueMushroomItemDrop == (Object)null)
		{
			Debug.Log((object)"Failed to add blue mushroom to bat drops, because blue mushroom couldn't be found.");
			return;
		}
		if (__instance.m_drops == null)
		{
			__instance.m_drops = new List<Drop>();
		}
		Drop val = new Drop
		{
			m_prefab = ((Component)PatchObjectDB.blueMushroomItemDrop).gameObject,
			m_chance = Mathf.Clamp01(MushroomConfig.BatMushroomDropChance.Value),
			m_amountMin = 1,
			m_amountMax = 1,
			m_onePerPlayer = false,
			m_levelMultiplier = false
		};
		if (MushroomConfig.BatMushroomDropMethod.Value == MushroomConfig.LootChange.AddToExistingDrops)
		{
			__instance.m_drops.Add(val);
		}
		else if (MushroomConfig.BatMushroomDropMethod.Value == MushroomConfig.LootChange.ReplaceLeatherScraps && __instance.m_drops.Count > 0)
		{
			ReplaceLeatherDrop(__instance, val);
		}
	}

	private static void ReplaceLeatherDrop(CharacterDrop characterDrops, Drop newDrop)
	{
		for (int i = 0; i < characterDrops.m_drops.Count; i++)
		{
			Drop val = characterDrops.m_drops[i];
			if (!((Object)(object)val?.m_prefab == (Object)null))
			{
				ItemDrop component = val.m_prefab.GetComponent<ItemDrop>();
				if (!((Object)(object)component == (Object)null) && component.m_itemData?.m_shared?.m_name == "$item_leatherscraps")
				{
					characterDrops.m_drops[i] = newDrop;
				}
			}
		}
	}
}
internal class MushroomConfig
{
	public enum LootChange
	{
		Disabled,
		AddToExistingDrops,
		ReplaceLeatherScraps
	}

	public enum PlantingTool
	{
		Hammer,
		Cultivator
	}

	public static ConfigEntry<LootChange> BatMushroomDropMethod;

	public static ConfigEntry<float> BatMushroomDropChance;

	public static ConfigEntry<bool> ReplaceMushroomsInWolfSkewer;

	public static ConfigEntry<bool> AddMushroomToOnionSoup;

	public static ConfigEntry<bool> MiniFoodRebalance;

	public static ConfigEntry<bool> EnablePlantingInCaves;

	public static ConfigEntry<PlantingTool> MushroomPlantingTool;

	internal static void LoadConfig(ConfigFile config)
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Expected O, but got Unknown
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Expected O, but got Unknown
		string text = "0 - Mushroom Planting";
		EnablePlantingInCaves = config.Bind<bool>(text, "EnablePlantingInCaves", true, ConfigurationManagerAttributes.CustomSeeOnlyDisplay());
		MushroomPlantingTool = config.Bind<PlantingTool>(text, "MushroomPlantingTool", PlantingTool.Hammer, ConfigurationManagerAttributes.CustomHiddenDisplay("Whether you plant blue mushrooms with the hammer or the cultivator."));
		ConfigDefinition val = new ConfigDefinition(text, "ReplaceMushroomsInWolfSkewer");
		config.Bind<bool>(val, true, (ConfigDescription)null);
		config.Remove(val);
		text = "1 - Mushroom Drop";
		BatMushroomDropMethod = config.Bind<LootChange>(text, "BatMushroomDropMethod", LootChange.ReplaceLeatherScraps, "How to add the blue mushroom drop to newly spawned bats");
		BatMushroomDropChance = config.Bind<float>(text, "BatMushroomDropChance", 0.5f, new ConfigDescription("Chance for a newly spawned bat to drop a blue mushroom, default of 0.5 means 50%.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
		text = "2 - Food Changes";
		ReplaceMushroomsInWolfSkewer = config.Bind<bool>(text, "ReplaceMushroomsInWolfSkewer", true, "Replaces the 2 normal mushrooms with 1 blue mushroom in the wolf skewer recipe.");
		AddMushroomToOnionSoup = config.Bind<bool>(text, "AddMushroomToOnionSoup", true, "Adds the blue mushroom to the onion soup recipe. Without 'MiniFoodRebalance' this is a nerf.");
		MiniFoodRebalance = config.Bind<bool>(text, "MiniFoodRebalance", true, "Swaps the food stats for onion soup and eyescream, then adds minor buffs to both. Also reduces the amount of greydwarf eyes needed to craft eyescream.");
	}
}
[BepInPlugin("goldenrevolver.ObtainableBlueMushroomsPlugin", "Immersively Obtainable Blue Mushrooms", "1.0.3")]
public class ObtainableBlueMushroomsPlugin : BaseUnityPlugin
{
	public const string NAME = "Immersively Obtainable Blue Mushrooms";

	public const string VERSION = "1.0.3";

	internal const string plantEverything = "advize.PlantEverything";

	protected void Awake()
	{
		MushroomConfig.LoadConfig(((BaseUnityPlugin)this).Config);
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
	}

	internal static bool AllowCavePlantingChanges()
	{
		return MushroomConfig.EnablePlantingInCaves.Value && !HasPlugin("advize.PlantEverything");
	}

	internal static bool HasPlugin(string guid)
	{
		return Chainloader.PluginInfos.ContainsKey(guid);
	}
}
[HarmonyPatch(typeof(ObjectDB), "Awake")]
internal static class PatchObjectDB
{
	internal const string pickableBlueMushroomPrefabName = "Pickable_Mushroom_blue";

	internal static GameObject pickableBlueMushroomPrefab;

	private const string cultivatorPrefabName = "Cultivator";

	internal static ItemDrop cultivatorPrefab;

	private const string hammerPrefabName = "Hammer";

	internal static ItemDrop hammerPrefab;

	private const string blueMushroomItemDropName = "MushroomBlue";

	internal static ItemDrop blueMushroomItemDrop;

	public static void Postfix(ObjectDB __instance)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Expected O, but got Unknown
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
		Scene activeScene = SceneManager.GetActiveScene();
		if (((Scene)(ref activeScene)).name != "main")
		{
			return;
		}
		Object[] array = Resources.FindObjectsOfTypeAll(typeof(GameObject));
		ItemDrop val2 = default(ItemDrop);
		ItemDrop val3 = default(ItemDrop);
		foreach (Object val in array)
		{
			if (val.name == "Pickable_Mushroom_blue")
			{
				pickableBlueMushroomPrefab = (GameObject)val;
			}
			if (val.name == "Cultivator" && ((GameObject)val).TryGetComponent<ItemDrop>(ref val2))
			{
				cultivatorPrefab = val2;
			}
			if (val.name == "Hammer" && ((GameObject)val).TryGetComponent<ItemDrop>(ref val3))
			{
				hammerPrefab = val3;
			}
		}
		blueMushroomItemDrop = __instance.GetItemPrefab("MushroomBlue").GetComponent<ItemDrop>();
		if (ObtainableBlueMushroomsPlugin.AllowCavePlantingChanges())
		{
			PlantableBlueMushroom.InitPieceAndAddToBuildPieces();
		}
		FoodAndRecipeChanges.AddFoodAndRecipeChanges(__instance);
	}
}
internal class PlantableBlueMushroom
{
	internal static int pieceNonSolidLayerMask;

	public static void InitPieceAndAddToBuildPieces()
	{
		Piece val = InitBlueMushroomPiece();
		if (MushroomConfig.MushroomPlantingTool.Value == MushroomConfig.PlantingTool.Hammer)
		{
			if (!PatchObjectDB.hammerPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.Contains(((Component)val).gameObject))
			{
				PatchObjectDB.hammerPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.Add(((Component)val).gameObject);
			}
			PatchObjectDB.cultivatorPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.Remove(((Component)val).gameObject);
		}
		else
		{
			if (!PatchObjectDB.cultivatorPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.Contains(((Component)val).gameObject))
			{
				PatchObjectDB.cultivatorPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.Add(((Component)val).gameObject);
			}
			PatchObjectDB.cultivatorPrefab.m_itemData.m_shared.m_buildPieces.m_canRemovePieces = true;
			PatchObjectDB.hammerPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.Remove(((Component)val).gameObject);
		}
		GameObject val2 = PatchObjectDB.cultivatorPrefab.m_itemData.m_shared.m_buildPieces.m_pieces.First((GameObject p) => ((Object)p).name == "sapling_onion");
		SphereCollider componentInChildren = ((Component)val).GetComponentInChildren<SphereCollider>();
		componentInChildren.radius /= 2f;
		((Component)componentInChildren).gameObject.layer = ((Component)val2.GetComponentInChildren<Collider>()).gameObject.layer;
		string text = LayerMask.LayerToName(((Component)componentInChildren).gameObject.layer);
		pieceNonSolidLayerMask = LayerMask.GetMask(new string[1] { text });
		val.m_placeEffect.m_effectPrefabs = val2.GetComponent<Piece>().m_placeEffect.m_effectPrefabs;
	}

	private static Piece InitBlueMushroomPiece()
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		Piece val = PatchObjectDB.pickableBlueMushroomPrefab.GetComponent<Piece>() ?? PatchObjectDB.pickableBlueMushroomPrefab.AddComponent<Piece>();
		val.m_resources = (Requirement[])(object)new Requirement[1]
		{
			new Requirement
			{
				m_resItem = PatchObjectDB.blueMushroomItemDrop,
				m_amount = 2,
				m_recover = false
			}
		};
		val.m_craftingStation = null;
		val.m_name = PatchObjectDB.blueMushroomItemDrop.m_itemData.m_shared.m_name;
		val.m_description = string.Empty;
		val.m_blockingPieces = new List<Piece>();
		val.m_blockRadius = 10f;
		val.m_category = (PieceCategory)0;
		val.m_noInWater = true;
		val.m_canBeRemoved = true;
		((StaticTarget)val).m_primaryTarget = true;
		val.m_allowedInDungeons = true;
		val.m_onlyInBiome = (Biome)4;
		val.m_icon = PatchObjectDB.blueMushroomItemDrop.m_itemData.GetIcon();
		return val;
	}
}
[HarmonyPatch(typeof(Player), "UpdatePlacementGhost")]
internal static class Player_UpdatePlacementGhost_Patch
{
	private static void Postfix(Player __instance, GameObject ___m_placementGhost)
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Invalid comparison between Unknown and I4
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0124: Unknown result type (might be due to invalid IL or missing references)
		if (!ObtainableBlueMushroomsPlugin.AllowCavePlantingChanges() || (Object)(object)___m_placementGhost == (Object)null || ((Object)___m_placementGhost).name != "Pickable_Mushroom_blue")
		{
			return;
		}
		__instance.m_placementMarkerInstance.SetActive(false);
		if ((int)__instance.m_placementStatus > 0)
		{
			return;
		}
		if (!((Character)__instance).InInterior() && !EnvMan.instance.CheckInteriorBuildingOverride())
		{
			__instance.m_placementStatus = (PlacementStatus)8;
			__instance.SetPlacementGhostValid(false);
			return;
		}
		Piece component = __instance.m_placementGhost.GetComponent<Piece>();
		Vector3 val = default(Vector3);
		Vector3 val2 = default(Vector3);
		Piece val3 = default(Piece);
		Heightmap val4 = default(Heightmap);
		Collider val5 = default(Collider);
		if (!__instance.PieceRayTest(ref val, ref val2, ref val3, ref val4, ref val5, false))
		{
			return;
		}
		Collider[] array = Physics.OverlapSphere(val, component.m_blockRadius, PlantableBlueMushroom.pieceNonSolidLayerMask);
		for (int i = 0; i < array.Length; i++)
		{
			Piece componentInParent = ((Component)array[i]).gameObject.GetComponentInParent<Piece>();
			if (!((Object)(object)componentInParent == (Object)null) && !((Object)(object)componentInParent == (Object)(object)component) && componentInParent.m_name == component.m_name)
			{
				__instance.m_placementStatus = (PlacementStatus)5;
				__instance.SetPlacementGhostValid(false);
				break;
			}
		}
	}
}