Decompiled source of ToTheCore v0.2.0

ToTheCore.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BelowTheStone;
using BelowTheStone.Crafting;
using BelowTheStone.InventorySystem;
using BelowTheStone.LevelSystem;
using BelowTheStone.NewDatabase;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ToTheCore")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ToTheCore")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("F35B4E0E-3A05-4E8A-B12A-735550ACBAF5")]
[assembly: AssemblyFileVersion("0.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyVersion("0.2.0.0")]
namespace ToTheCore;

public static class Mocks
{
	public static void FixMocks(ItemStack itemStack)
	{
		if (itemStack != null && Object.op_Implicit((Object)(object)itemStack.ItemType) && ((Object)itemStack.ItemType).name.StartsWith("Mock_"))
		{
			string key = ((Object)itemStack.ItemType).name.Substring(5);
			AccessTools.FieldRefAccess<Item, ItemType>(itemStack.Item, "itemType") = Patches.vanillaItems[key];
		}
	}

	public static void FixMocks(Ingredient ingredient)
	{
		if (ingredient != null && Object.op_Implicit((Object)(object)ingredient.ItemType) && ((Object)ingredient.ItemType).name.StartsWith("Mock_"))
		{
			string key = ((Object)ingredient.ItemType).name.Substring(5);
			AccessTools.FieldRefAccess<Ingredient, ItemType>(ingredient, "itemType") = Patches.vanillaItems[key];
		}
	}

	public static void FixMocks(ItemType itemType)
	{
		if ((Object)(object)itemType != (Object)null && Object.op_Implicit((Object)(object)itemType.EquipItemPrefab))
		{
			FixMocks(itemType.EquipItemPrefab);
		}
	}

	public static void FixMocks(GameObject gameObject)
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		if (!Object.op_Implicit((Object)(object)gameObject))
		{
			return;
		}
		EquippedItem equippedItem = default(EquippedItem);
		if (gameObject.TryGetComponent<EquippedItem>(ref equippedItem))
		{
			FixMocks(equippedItem);
		}
		Renderer renderer = default(Renderer);
		if (gameObject.TryGetComponent<Renderer>(ref renderer))
		{
			FixMocks(renderer);
		}
		foreach (Transform item in gameObject.transform)
		{
			Transform val = item;
			FixMocks(((Component)val).gameObject);
		}
	}

	public static void FixMocks(Renderer renderer)
	{
		if (Object.op_Implicit((Object)(object)renderer) && Object.op_Implicit((Object)(object)renderer.sharedMaterial) && ((Object)renderer.sharedMaterial).name.StartsWith("Mock_"))
		{
			string materialName = ((Object)renderer.sharedMaterial).name.Substring(5);
			if (materialName.EndsWith(" (Instance)"))
			{
				materialName = materialName.Substring(0, materialName.Length - 11);
			}
			Material val = Resources.FindObjectsOfTypeAll<Material>().First((Material mat) => ((Object)mat).name == materialName);
			AccessTools.PropertySetter(typeof(Renderer), "sharedMaterial").Invoke(renderer, new object[1] { val });
		}
	}

	public static void FixMocks(EquippedItem equippedItem)
	{
		if (Object.op_Implicit((Object)(object)equippedItem))
		{
			if (Object.op_Implicit((Object)(object)equippedItem.ItemTypeData) && ((Object)equippedItem.ItemTypeData).name.StartsWith("Mock_"))
			{
				string key = ((Object)equippedItem.ItemTypeData).name.Substring(5);
				AccessTools.FieldRefAccess<EquippedItem, ItemType>(equippedItem, "itemTypeData") = Patches.vanillaItems[key];
			}
			if (Object.op_Implicit((Object)(object)equippedItem.HandAnimations) && ((Object)equippedItem.HandAnimations).name.StartsWith("Mock_"))
			{
				string key2 = ((Object)equippedItem.HandAnimations).name.Substring(5);
				AccessTools.FieldRefAccess<EquippedItem, RuntimeAnimatorController>(equippedItem, "handAnimations") = Patches.vanillaAnimations[key2];
			}
		}
	}
}
[HarmonyPatch]
public static class Patches
{
	public static Dictionary<string, ItemType> vanillaItems = new Dictionary<string, ItemType>();

	public static Dictionary<string, RuntimeAnimatorController> vanillaAnimations = new Dictionary<string, RuntimeAnimatorController>();

	[HarmonyPatch(typeof(SODatabase), "Init", new Type[] { })]
	[HarmonyPrefix]
	public static void SODatabaseInit(SODatabase __instance)
	{
		EquippedItem val2 = default(EquippedItem);
		foreach (DatabaseElement master in __instance.MasterList)
		{
			ItemType val = (ItemType)(object)((master is ItemType) ? master : null);
			if (val != null)
			{
				vanillaItems[((DatabaseElement)val).NameID] = val;
				if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)val.EquipItemPrefab) && val.EquipItemPrefab.TryGetComponent<EquippedItem>(ref val2) && Object.op_Implicit((Object)(object)val2.HandAnimations))
				{
					vanillaAnimations[((Object)val2.HandAnimations).name] = val2.HandAnimations;
				}
			}
		}
		AssetBundle val3 = LoadAssetBundleFromResources("tothecore");
		ItemType val4 = val3.LoadAsset<ItemType>("MS_ToTheCoreRadar");
		Mocks.FixMocks(val4);
		__instance.MasterList.Add((DatabaseElement)(object)val4);
		__instance.MasterList.Add((DatabaseElement)(object)val3.LoadAsset<CraftingRecipe>("MS_ToTheCoreRadar_Recipe"));
	}

	[HarmonyPatch(typeof(CraftingRecipe), "OnEnable")]
	[HarmonyPrefix]
	public static void CraftingRecipeOnEnable(CraftingRecipe __instance)
	{
		Mocks.FixMocks(__instance.OutputItem);
		foreach (Ingredient ingredient in __instance.Ingredients)
		{
			Mocks.FixMocks(ingredient);
		}
	}

	public static AssetBundle LoadAssetBundleFromResources(string bundleName)
	{
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string name = executingAssembly.GetManifestResourceNames().Single((string str) => str.EndsWith(bundleName));
		using Stream stream = executingAssembly.GetManifestResourceStream(name);
		return AssetBundle.LoadFromStream(stream);
	}
}
[BepInPlugin("com.maxsch.BelowTheStone.ToTheCore", "To The Core", "0.2.0")]
public class Plugin : BaseUnityPlugin
{
	public const string PluginGUID = "com.maxsch.BelowTheStone.ToTheCore";

	public const string PluginName = "To The Core";

	public const string PluginVersion = "0.2.0";

	public static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		Harmony val = new Harmony("com.maxsch.BelowTheStone.ToTheCore");
		val.PatchAll();
		Log = ((BaseUnityPlugin)this).Logger;
	}
}
public class CoreRadarLogic : MonoBehaviour
{
	private BasicItemLogic itemLogic;

	private void Awake()
	{
		itemLogic = ((Component)this).GetComponent<BasicItemLogic>();
		itemLogic.RegisterCanUseItemHook((Predicate<BasicItemLogic>)CheckItemUse);
		itemLogic.RegisterUseItemHook((Func<bool>)OnUseItem);
	}

	private bool CheckItemUse(BasicItemLogic obj)
	{
		Level currentLevel = LevelManager.CurrentLevel;
		if (currentLevel == null)
		{
			UI_ShortTextPresenter.DisplayText("Can only be used in the caves");
			return false;
		}
		return true;
	}

	private bool OnUseItem()
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_0195: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_019f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		CameraShake.Shake(0.2f, 20f, 0.25f, (Easing)2);
		Level currentLevel = LevelManager.CurrentLevel;
		GameObject holderRoot = itemLogic.ItemBaseScript.GetHolderRoot();
		Vector2 itemPosition = Vector2.op_Implicit(holderRoot.transform.position);
		List<Vector2> list = new List<Vector2>();
		BossCreature val = default(BossCreature);
		LayerExitPoint val2 = default(LayerExitPoint);
		foreach (Chunk chunkDatum in currentLevel.levelGrid.ChunkData)
		{
			if (((chunkDatum != null) ? chunkDatum.EntityList : null) == null)
			{
				continue;
			}
			foreach (EntityData entity in chunkDatum.EntityList)
			{
				if (Object.op_Implicit((Object)(object)entity.EntityType) && Object.op_Implicit((Object)(object)entity.EntityType.MainEntityPrefab))
				{
					GameObject mainEntityPrefab = entity.EntityType.MainEntityPrefab;
					if (mainEntityPrefab.TryGetComponent<BossCreature>(ref val))
					{
						list.Add(entity.Position);
					}
					else if (mainEntityPrefab.TryGetComponent<LayerExitPoint>(ref val2))
					{
						list.Add(entity.Position);
					}
				}
			}
		}
		if (list.Count == 0)
		{
			UI_ShortTextPresenter.DisplayText("No exits found!");
			return true;
		}
		Vector2 val3 = list.OrderBy((Vector2 position) => Vector2.Distance(position, itemPosition)).First();
		int num = (int)Vector2.Distance(val3, itemPosition);
		UI_ShortTextPresenter.DisplayText($"Nearest exit is {num} block away!");
		return true;
	}
}