Decompiled source of CustomChestName v1.1.2

ValheimChestName.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GemHunter1;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ValheimChestTag")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValheimChestTag")]
[assembly: AssemblyCopyright("Copyright ©  2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ce19f7d1-6e59-439e-9e1a-4ca8724db361")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace GemHunter1
{
	public abstract class BaseMethodHelper
	{
		public MethodInfo methodInfo;

		public BaseMethodHelper(Type type, string name)
		{
			methodInfo = type.GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic);
		}
	}
	public class MethodHelper : BaseMethodHelper
	{
		public MethodHelper(Type type, string name)
			: base(type, name)
		{
		}

		public void Invoke(object instance)
		{
			methodInfo.Invoke(instance, Array.Empty<object>());
		}
	}
	public class MethodHelper<Return> : BaseMethodHelper
	{
		public MethodHelper(Type type, string name)
			: base(type, name)
		{
		}

		public Return Invoke(object instance)
		{
			return (Return)methodInfo.Invoke(instance, Array.Empty<object>());
		}
	}
	public class MethodHelper<Return, T1> : BaseMethodHelper
	{
		public MethodHelper(Type type, string name)
			: base(type, name)
		{
		}

		public Return Invoke(object instance, T1 t1)
		{
			return (Return)methodInfo.Invoke(instance, new object[1] { t1 });
		}
	}
	public class MethodHelper<Return, T1, T2> : BaseMethodHelper
	{
		public MethodHelper(Type type, string name)
			: base(type, name)
		{
		}

		public Return Invoke(object instance, T1 t1, T2 t2)
		{
			return (Return)methodInfo.Invoke(instance, new object[2] { t1, t2 });
		}
	}
	public class MethodHelper<Return, T1, T2, T3> : BaseMethodHelper
	{
		public MethodHelper(Type type, string name)
			: base(type, name)
		{
		}

		public Return Invoke(object instance, T1 t1, T2 t2, T3 t3)
		{
			return (Return)methodInfo.Invoke(instance, new object[3] { t1, t2, t3 });
		}
	}
	public class MethodHelper<Return, T1, T2, T3, T4> : BaseMethodHelper
	{
		public MethodHelper(Type type, string name)
			: base(type, name)
		{
		}

		public Return Invoke(object instance, T1 t1, T2 t2, T3 t3, T4 t4)
		{
			return (Return)methodInfo.Invoke(instance, new object[4] { t1, t2, t3, t4 });
		}
	}
}
namespace ValheimChestName
{
	[BepInProcess("valheim.exe")]
	[BepInPlugin("GemHunter1.ChestName", "ChestName", "1.1.2")]
	public class ChestName : BaseUnityPlugin
	{
		public class ContainerTextReceiver : TextReceiver
		{
			private Container container;

			private ZNetView netView;

			private ZDO zdo;

			public ContainerTextReceiver(Container container)
			{
				this.container = container;
				netView = ((Component)container).GetComponent<ZNetView>();
				zdo = netView.GetZDO();
				netView.Register<string>("SetName", (Action<long, string>)RPC_SetName);
			}

			public string GetText()
			{
				return zdo.GetString("ChestName", "");
			}

			public void SetText(string text)
			{
				if (netView.IsValid())
				{
					logger.LogMessage((object)"Invoking SetName");
					netView.InvokeRPC("SetName", new object[1] { text });
				}
			}

			public void RPC_SetName(long sender, string text)
			{
				logger.LogMessage((object)"RPC SetName");
				if (netView.IsValid() && netView.IsOwner())
				{
					logger.LogMessage((object)"Setting name");
					netView.GetZDO().Set("ChestName", text);
				}
			}
		}

		[HarmonyPatch(typeof(Container), "GetHoverText")]
		private class ContainerHoverTextPatch
		{
			[HarmonyPriority(200)]
			public static void Postfix(ref string __result, ref Container __instance)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				if (PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false) && !((Object)(object)((Component)__instance).GetComponent<Piece>() == (Object)null))
				{
					bool playerHasToolOnBelt = GetPlayerHasToolOnBelt();
					string text = GetTextReceiver(__instance).GetText();
					if (!Utility.IsNullOrWhiteSpace(text))
					{
						__result = __result.Replace(Localization.instance.Localize(__instance.m_name), text);
					}
					if (playerHasToolOnBelt)
					{
						__result += Localization.instance.Localize("\n[<color=yellow><b>$KEY_AltPlace + $KEY_Use</b></color>] $hud_rename");
					}
				}
			}
		}

		[HarmonyPatch(typeof(Container), "Interact")]
		private class ContainerInteractPatch
		{
			public static void Prefix(ref bool alt, ref Container __instance, ref bool __runOriginal, ref bool __result)
			{
				if (alt && GetPlayerHasToolOnBelt())
				{
					logger.LogMessage((object)"Requesting rename");
					TextInput.instance.RequestText((TextReceiver)(object)GetTextReceiver(__instance), "$hud_rename", 32);
					__result = true;
					__runOriginal = false;
				}
			}
		}

		[HarmonyPatch(typeof(Humanoid), "HideHandItems")]
		private class HumanoidHideHandItemsPatch
		{
			public static void Postfix(ref ItemData ___m_hiddenRightItem)
			{
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0012: Invalid comparison between Unknown and I4
				playerHasToolOnBelt = ___m_hiddenRightItem != null && (int)___m_hiddenRightItem.m_shared.m_itemType == 19;
			}
		}

		private readonly Harmony harmony = new Harmony("GemHunter1.ChestName");

		public static ManualLogSource logger;

		public static Dictionary<Container, ContainerTextReceiver> textRecievers = new Dictionary<Container, ContainerTextReceiver>();

		public static ConfigEntry<bool> needHammer;

		public static bool playerHasToolOnBelt = false;

		private static MethodHelper<ItemData> Player_GetRightItem;

		private void Awake()
		{
			logger = ((BaseUnityPlugin)this).Logger;
			harmony.PatchAll();
			needHammer = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "NeedHammer", true, "Need hammer hidden on toolbelt to be able to rename a chest");
			Player_GetRightItem = new MethodHelper<ItemData>(typeof(Player), "GetRightItem");
		}

		private void OnDestroy()
		{
			harmony.UnpatchSelf();
		}

		public static ContainerTextReceiver GetTextReceiver(Container container)
		{
			if (textRecievers.TryGetValue(container, out var value))
			{
				return value;
			}
			value = new ContainerTextReceiver(container);
			textRecievers.Add(container, value);
			return value;
		}

		public static bool GetPlayerHasToolOnBelt()
		{
			if (!needHammer.Value)
			{
				return true;
			}
			if ((Object)(object)Player.m_localPlayer == (Object)null || Player_GetRightItem.Invoke(Player.m_localPlayer) != null)
			{
				return false;
			}
			return playerHasToolOnBelt;
		}
	}
}