Decompiled source of HearthStone Revived v1.0.7

Hearthstone.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
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.Configuration;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Valheim HearthStone Mod")]
[assembly: AssemblyDescription("Adds a ConsumableItem that will bring you home")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HearthStone")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("97d58a4d-8886-44f9-ba81-73da0e389949")]
[assembly: AssemblyFileVersion("1.0.6.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.6.0")]
[module: UnverifiableCode]
namespace Hearthstone;

[BepInPlugin("mennowar.mods.Hearthstone_revived", "Hearthstone Revived", "1.0.7")]
[BepInDependency("com.jotunn.jotunn", "2.23.2")]
public class Hearthstone : BaseUnityPlugin
{
	public const string sharedName = "Hearthstone";

	public const string PluginGUID = "mennowar.mods.Hearthstone_revived";

	private Harmony harmony = new Harmony("mennowar.mods.Hearthstone_revived");

	public static ConfigEntry<string> item1;

	public static ConfigEntry<string> item2;

	public static ConfigEntry<string> item3;

	public static ConfigEntry<int> itemCost1;

	public static ConfigEntry<int> itemCost2;

	public static ConfigEntry<int> itemCost3;

	public static ConfigEntry<bool> allowTeleportWithoutRestriction;

	public static ConfigEntry<bool> writeDebugOutput;

	private static CustomItem m_hearthStoneItem;

	private static string m_lastPositionString;

	public static bool IsOwner { get; set; }

	private static string PositionFile
	{
		get
		{
			string customItemDataFolder = Paths.CustomItemDataFolder;
			if (!Directory.Exists(customItemDataFolder))
			{
				Directory.CreateDirectory(customItemDataFolder);
			}
			string text = Path.Combine(customItemDataFolder, "HSPosition.txt");
			Debug("using Position File: " + text);
			return text;
		}
	}

	public static void Debug(string value)
	{
		if (writeDebugOutput.Value)
		{
			Logger.LogMessage((object)value);
		}
	}

	private void Awake()
	{
		//IL_0046: 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_0053: Expected O, but got Unknown
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Expected O, but got Unknown
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Expected O, but got Unknown
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Expected O, but got Unknown
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Expected O, but got Unknown
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Expected O, but got Unknown
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Expected O, but got Unknown
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_0113: Expected O, but got Unknown
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Expected O, but got Unknown
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Expected O, but got Unknown
		//IL_0176: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Expected O, but got Unknown
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Expected O, but got Unknown
		//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01be: Expected O, but got Unknown
		//IL_01be: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Expected O, but got Unknown
		//IL_01ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f9: Expected O, but got Unknown
		//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0203: Expected O, but got Unknown
		((BaseUnityPlugin)this).Config.SaveOnConfigSet = true;
		((BaseUnityPlugin)this).Config.SettingChanged += delegate
		{
			GenerateRecipe();
		};
		item1 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "RecipeItem1", "Stone", new ConfigDescription("Recipe item 1 - leave blank for none", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		itemCost1 = ((BaseUnityPlugin)this).Config.Bind<int>("General", "itemCost1", 2, new ConfigDescription("Amount of item 1 required to craft the Hearthstone", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		item2 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "RecipeItem2", "Coins", new ConfigDescription("Recipe item 2 - leave blank for none", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		itemCost2 = ((BaseUnityPlugin)this).Config.Bind<int>("General", "itemCost2", 10, new ConfigDescription("Amount of item 2 required to craft the Hearthstone", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		item3 = ((BaseUnityPlugin)this).Config.Bind<string>("General", "RecipeItem3", "Carrot", new ConfigDescription("Recipe item 3 - leave blank for none", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		itemCost3 = ((BaseUnityPlugin)this).Config.Bind<int>("General", "itemCost3", 5, new ConfigDescription("Amount of item 3 required to craft the Hearthstone", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		writeDebugOutput = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "writeDebugOutput", true, new ConfigDescription("Write debug output?", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		allowTeleportWithoutRestriction = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "allowTeleportWithoutRestriction", false, new ConfigDescription("Allow teleport without restriction", (AcceptableValueBase)null, new object[1] { (object)new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		} }));
		harmony.PatchAll();
		GetHearthStonePositionString();
		PrefabManager.OnVanillaPrefabsAvailable += AddOrUpdateHearthStoneItem;
		ItemManager.OnItemsRegistered += ItemManager_OnItemsRegistered;
		SynchronizationManager.OnConfigurationSynchronized += SynchronizationManager_OnConfigurationSynchronized;
		SynchronizationManager.OnAdminStatusChanged += SynchronizationManager_OnAdminStatusChanged;
	}

	private void SynchronizationManager_OnAdminStatusChanged()
	{
		Debug(SynchronizationManager.Instance.PlayerIsAdmin ? "No Admin anymore" : "You are admin");
	}

	private void SynchronizationManager_OnConfigurationSynchronized(object sender, ConfigurationSynchronizationEventArgs e)
	{
		Debug(e.InitialSynchronization ? "Recieved Initial Config" : "Updated Config");
		GenerateRecipe();
	}

	private void Update()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return;
		}
		GameObject hoverObject = ((Humanoid)localPlayer).GetHoverObject();
		if (!((Object)(object)hoverObject == (Object)null))
		{
			Interactable componentInParent = hoverObject.GetComponentInParent<Interactable>();
			if (IsOwner && componentInParent is Bed && Input.GetKeyDown((KeyCode)112))
			{
				SetHearthStonePosition();
				((Character)Player.m_localPlayer).Message((MessageType)2, "You have updated your HearthStone location", 0, (Sprite)null);
			}
		}
	}

	private void ItemManager_OnItemsRegistered()
	{
		ItemManager.OnItemsRegistered -= ItemManager_OnItemsRegistered;
		GenerateRecipe();
	}

	private void GenerateRecipe()
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Expected O, but got Unknown
		List<Requirement> rqs;
		if (m_hearthStoneItem != null && ObjectDB.instance.m_items.Any())
		{
			if (m_hearthStoneItem.Recipe == null)
			{
				Recipe obj = ScriptableObject.CreateInstance<Recipe>();
				((Object)obj).name = "Recipe_Hearthstone";
				obj.m_item = m_hearthStoneItem.ItemDrop;
				CustomRecipe recipe = new CustomRecipe(obj, true, true)
				{
					FixRequirementReferences = true
				};
				m_hearthStoneItem.Recipe = recipe;
			}
			rqs = new List<Requirement>();
			AddRq(item1, itemCost1);
			AddRq(item2, itemCost2);
			AddRq(item3, itemCost3);
			if (m_hearthStoneItem.Recipe != null && (Object)(object)m_hearthStoneItem.Recipe.Recipe != (Object)null)
			{
				m_hearthStoneItem.Recipe.Recipe.m_resources = rqs.ToArray();
			}
		}
		void AddRq(ConfigEntry<string> item, ConfigEntry<int> amount)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			try
			{
				if (item != null && !string.IsNullOrEmpty(item.Value) && amount.Value > 0)
				{
					ItemDrop component = ObjectDB.instance.GetItemPrefab(item.Value).GetComponent<ItemDrop>();
					if ((Object)(object)component != (Object)null)
					{
						rqs.Add(new Requirement
						{
							m_amount = amount.Value,
							m_resItem = component
						});
					}
				}
			}
			catch (Exception ex)
			{
				Debug(ex.Message);
			}
		}
	}

	private void AddOrUpdateHearthStoneItem()
	{
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: 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_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: Expected O, but got Unknown
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Expected O, but got Unknown
		if (m_hearthStoneItem != null)
		{
			return;
		}
		AssetBundle val = AssetUtils.LoadAssetBundleFromResources("hs", typeof(Hearthstone).Assembly);
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)"Bundle could not be loaded!");
			return;
		}
		GameObject val2 = val.LoadAsset<GameObject>("Assets/_Custom/Hearthstone.prefab");
		if ((Object)(object)val2 == (Object)null)
		{
			Logger.LogWarning((object)"Prefab could not be loaded!");
			return;
		}
		SharedData shared = val2.GetComponent<ItemDrop>().m_itemData.m_shared;
		if (shared == null)
		{
			Logger.LogWarning((object)"Shared ItemData could not be loaded!");
			return;
		}
		ItemConfig val3 = new ItemConfig
		{
			Name = shared.m_name,
			Requirements = BuildRequirementConfigs(),
			Icons = shared.m_icons,
			Description = shared.m_description
		};
		m_hearthStoneItem = new CustomItem(val, "Assets/_Custom/Hearthstone.prefab", true, val3);
		ItemManager.Instance.AddItem(m_hearthStoneItem);
		GenerateRecipe();
	}

	private static RequirementConfig[] BuildRequirementConfigs()
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: 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_004f: Expected O, but got Unknown
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Expected O, but got Unknown
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Expected O, but got Unknown
		List<RequirementConfig> list = new List<RequirementConfig>();
		if (itemCost1.Value > 0 && !string.IsNullOrEmpty(item1.Value))
		{
			list.Add(new RequirementConfig
			{
				Amount = itemCost1.Value,
				Item = item1.Value
			});
		}
		if (itemCost2.Value > 0 && !string.IsNullOrEmpty(item2.Value))
		{
			list.Add(new RequirementConfig
			{
				Amount = itemCost2.Value,
				Item = item2.Value
			});
		}
		if (itemCost3.Value > 0 && !string.IsNullOrEmpty(item3.Value))
		{
			list.Add(new RequirementConfig
			{
				Amount = itemCost3.Value,
				Item = item3.Value
			});
		}
		return list.ToArray();
	}

	public static Vector3 GetHearthStonePosition()
	{
		//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_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005c: 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_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		CultureInfo provider = new CultureInfo("en-US");
		Vector3 zero = Vector3.zero;
		string hearthStonePositionString = GetHearthStonePositionString();
		if (string.IsNullOrEmpty(hearthStonePositionString))
		{
			return zero;
		}
		string[] array = hearthStonePositionString.Split(new char[1] { '|' });
		try
		{
			return new Vector3(float.Parse(array[0], provider), float.Parse(array[1], provider), float.Parse(array[2], provider));
		}
		catch
		{
			return Vector3.zero;
		}
	}

	private static string GetHearthStonePositionString()
	{
		if (string.IsNullOrEmpty(m_lastPositionString) && File.Exists(PositionFile))
		{
			m_lastPositionString = File.ReadAllText(PositionFile);
		}
		return m_lastPositionString;
	}

	private static void SetHearthStonePosition()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: 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_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		CultureInfo cultureInfo = new CultureInfo("en-US");
		string text = ((Component)Player.m_localPlayer).transform.position.x.ToString(cultureInfo) + "|" + ((Component)Player.m_localPlayer).transform.position.y.ToString(cultureInfo) + "|" + ((Component)Player.m_localPlayer).transform.position.z.ToString(cultureInfo);
		File.WriteAllText(PositionFile, text);
		m_lastPositionString = text;
	}
}
internal class Patches
{
	[HarmonyPatch(typeof(Player), "ConsumeItem")]
	public static class ConsumePatch
	{
		private static bool Prefix(ItemData item)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			string name = item.m_shared.m_name;
			if (string.IsNullOrEmpty(name))
			{
				return false;
			}
			if (name == "Hearthstone")
			{
				if (!((Humanoid)Player.m_localPlayer).IsTeleportable() && !Hearthstone.allowTeleportWithoutRestriction.Value)
				{
					((Character)Player.m_localPlayer).Message((MessageType)2, "You can't teleport carrying those items", 0, (Sprite)null);
					return false;
				}
				Vector3 hearthStonePosition = Hearthstone.GetHearthStonePosition();
				if (hearthStonePosition == Vector3.zero)
				{
					((Character)Player.m_localPlayer).Message((MessageType)2, "You need to set hearthstone spawn point", 0, (Sprite)null);
					return false;
				}
				((Character)Player.m_localPlayer).TeleportTo(hearthStonePosition, ((Component)Player.m_localPlayer).transform.rotation, true);
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Bed), "GetHoverText")]
	private static class Bed_GetHoverText_Patch
	{
		private static void Postfix(Bed __instance, ref string __result, ZNetView ___m_nview)
		{
			if ((Object)(object)Player.m_localPlayer == (Object)null)
			{
				Hearthstone.IsOwner = false;
			}
			else if (___m_nview.GetZDO().GetLong("owner", 0L) == Player.m_localPlayer.GetPlayerID() || __instance.IsCurrent())
			{
				Hearthstone.IsOwner = true;
				__result += "\n[<color=yellow><b>P</b></color>] Set hearthstone";
			}
			else
			{
				Hearthstone.IsOwner = false;
			}
		}
	}

	[HarmonyPatch(typeof(ItemStand), "CanAttach")]
	public static class AttachPatch
	{
		[HarmonyPostfix]
		private static void Postfix(ItemStand __instance, ItemData item, ref bool __result)
		{
			if (!__result)
			{
				string text = item?.m_shared?.m_name;
				if (!string.IsNullOrEmpty(text))
				{
					__result = text == "Hearthstone";
				}
			}
		}
	}
}