Decompiled source of TimbnChestLabels v1.0.0

Timbn.GraveyardKeeperTwo.ChestLabels/Timbn.GraveyardKeeperTwo.ChestLabels.dll

Decompiled 18 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using LazyBearTechnology;
using Microsoft.CodeAnalysis;
using Timbn.GraveyardKeeperTwo.Core.Framework;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Timbn.GraveyardKeeperTwo.ChestLabels")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Name your chests. Press the Work key at a chest and type a label, and it shows over the chest in the same style as grave quality.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+a943a49303e04a57778912bf03d3f5b80cf744b8")]
[assembly: AssemblyProduct("Timbn Chest Labels")]
[assembly: AssemblyTitle("Timbn.GraveyardKeeperTwo.ChestLabels")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 Timbn.GraveyardKeeperTwo.ChestLabels
{
	internal sealed class ChestLabelBoard
	{
		private readonly TimbnSaveData<ChestLabelData> _data;

		private readonly Dictionary<Guid, string> _texts = new Dictionary<Guid, string>();

		public ChestLabelBoard(TimbnSaveData<ChestLabelData> data)
		{
			_data = data;
			Rebuild();
		}

		public static bool CanLabel(WGOInteractionHandlerBase handler, Wgo? wgo)
		{
			if (handler is ChestInteractionHandler)
			{
				if (wgo == null)
				{
					return false;
				}
				return wgo.Id?.Contains("chest", StringComparison.OrdinalIgnoreCase) == true;
			}
			return false;
		}

		public string GetText(WgoData chest)
		{
			if (!((TimbnData<ChestLabelData>)(object)_data).Current.Labels.TryGetValue(Key(chest.UniqueId), out ChestLabelEntry value))
			{
				return "";
			}
			return value.Text;
		}

		public void SetText(Wgo chest, string text)
		{
			string key = Key(chest.Data.UniqueId);
			text = text.Trim();
			if (text.Length == 0)
			{
				((TimbnData<ChestLabelData>)(object)_data).Current.Labels.Remove(key);
			}
			else
			{
				((TimbnData<ChestLabelData>)(object)_data).Current.Labels[key] = new ChestLabelEntry
				{
					Text = text
				};
			}
			Rebuild();
			UIObjectBubbleManager instance = UIObjectBubbleManager.Instance;
			if (instance != null)
			{
				instance.Display((IBubbleDrawable)(object)chest);
			}
		}

		public void ChestRemoved(SGuid id)
		{
			if (((TimbnData<ChestLabelData>)(object)_data).Current.Labels.Remove(Key(id)))
			{
				Rebuild();
			}
		}

		public void AddLabel(Wgo chest, List<LazyWidgetDataBase> widgets)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Expected O, but got Unknown
			if (_texts.Count != 0 && chest.Data != null && !chest.Data.IsHidden && _texts.TryGetValue(chest.Data.UniqueId.Guid, out string value))
			{
				widgets.Insert(0, (LazyWidgetDataBase)new UIQualityTooltipWidgetData("", value));
			}
		}

		public void Rebuild()
		{
			_texts.Clear();
			foreach (var (input, chestLabelEntry2) in ((TimbnData<ChestLabelData>)(object)_data).Current.Labels)
			{
				if (Guid.TryParse(input, out var result))
				{
					_texts[result] = chestLabelEntry2.Text;
				}
			}
		}

		private static string Key(SGuid id)
		{
			return ((object)id).ToString();
		}
	}
	public sealed class ChestLabelData
	{
		public Dictionary<string, ChestLabelEntry> Labels { get; set; } = new Dictionary<string, ChestLabelEntry>();
	}
	public sealed class ChestLabelEntry
	{
		public string Text { get; set; } = "";
	}
	[BepInPlugin("Timbn.GraveyardKeeperTwo.ChestLabels", "Timbn Chest Labels", "1.0.0")]
	[BepInDependency("Timbn.GraveyardKeeperTwo.Core", "2.1.0")]
	public class Plugin : TimbnFrameworkPlugin<Plugin>
	{
		internal ChestLabelBoard Board { get; private set; }

		protected override void OnAwake()
		{
			TimbnSaveData<ChestLabelData> data = ((TimbnFrameworkPlugin)this).Saves.Register<ChestLabelData>((Action<ChestLabelData>)delegate
			{
				Board?.Rebuild();
			});
			Board = new ChestLabelBoard(data);
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "Timbn.GraveyardKeeperTwo.ChestLabels";

		public const string PLUGIN_NAME = "Timbn Chest Labels";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace Timbn.GraveyardKeeperTwo.ChestLabels.Patches
{
	[HarmonyPatch(typeof(ChestInteractionHandler))]
	internal static class ChestInteractionHandlerPatch
	{
		[HarmonyPatch("FormInteractionInfo")]
		[HarmonyPostfix]
		private static void FormInteractionInfoPostFix(ChestInteractionHandler __instance, Wgo ___assignedWgo, ref InteractionInfos __result)
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			if (ChestLabelBoard.CanLabel((WGOInteractionHandlerBase)(object)__instance, ___assignedWgo))
			{
				__result.Add(new InteractionInfo(ControllerIconLibrary.GetIconId(GameKey.Action, (GameKeyIconType)null, true) + LLBase.L("timbn_chest_labels_label")));
			}
		}
	}
	[HarmonyPatch(typeof(UIQualityTooltipWidget))]
	internal static class UIQualityTooltipWidgetPatch
	{
		[HarmonyPatch("Draw", new Type[] { typeof(UIQualityTooltipWidgetData) })]
		[HarmonyPostfix]
		private static void DrawPostFix(UIQualityTooltipWidgetData data, Component ___iconLabel)
		{
			___iconLabel.gameObject.SetActive(!string.IsNullOrEmpty(data.IconName));
		}
	}
	[HarmonyPatch(typeof(WGOInteractionHandlerBase))]
	internal static class WGOInteractionHandlerBasePatch
	{
		[HarmonyPatch("HasInteraction2")]
		[HarmonyPostfix]
		private static void HasInteraction2PostFix(WGOInteractionHandlerBase __instance, Wgo ___assignedWgo, ref bool __result)
		{
			if (ChestLabelBoard.CanLabel(__instance, ___assignedWgo))
			{
				__result = true;
			}
		}

		[HarmonyPatch("Interact2")]
		[HarmonyPrefix]
		private static bool Interact2PreFix(WGOInteractionHandlerBase __instance, Wgo ___assignedWgo, ref bool __result)
		{
			if (ChestLabelBoard.CanLabel(__instance, ___assignedWgo))
			{
				Plugin plugin = TimbnFrameworkPlugin<Plugin>.Instance;
				if (plugin != null)
				{
					WgoData data = ___assignedWgo.Data;
					((TimbnFrameworkPlugin)plugin).UI.ShowPrompt(LLBase.L("timbn_chest_labels_title"), plugin.Board.GetText(data), (Action<string>)delegate(string text)
					{
						plugin.Board.SetText(___assignedWgo, text);
					}, LLBase.L("timbn_chest_labels_placeholder"), (Action)null);
					__result = true;
					return false;
				}
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Wgo))]
	internal static class WgoPatch
	{
		[HarmonyPatch("GetWidgetData")]
		[HarmonyPostfix]
		private static void GetWidgetDataPostFix(Wgo __instance, List<LazyWidgetDataBase> __result)
		{
			TimbnFrameworkPlugin<Plugin>.Instance?.Board.AddLabel(__instance, __result);
		}
	}
	[HarmonyPatch(typeof(WorldData))]
	internal static class WorldDataPatch
	{
		[HarmonyPatch("RemoveWgoDataFromGameScene", new Type[]
		{
			typeof(WgoData),
			typeof(bool)
		})]
		[HarmonyPostfix]
		private static void RemoveWgoDataFromGameScenePostFix(WgoData wgoData, bool clearCraftComponent)
		{
			if (clearCraftComponent)
			{
				TimbnFrameworkPlugin<Plugin>.Instance?.Board.ChestRemoved(wgoData.UniqueId);
			}
		}

		[HarmonyPatch("RemoveWgoDataFromGameScene", new Type[] { typeof(SGuid) })]
		[HarmonyPostfix]
		private static void RemoveWgoDataFromGameScenePostFix(SGuid sGuid)
		{
			TimbnFrameworkPlugin<Plugin>.Instance?.Board.ChestRemoved(sGuid);
		}
	}
}