Decompiled source of TimbnChestLabels v1.1.2

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

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
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.1.2.0")]
[assembly: AssemblyInformationalVersion("1.1.2+fdf5f45bffc5d58f5352cc1bc80b38bc791d9088")]
[assembly: AssemblyProduct("Timbn Chest Labels")]
[assembly: AssemblyTitle("Timbn.GraveyardKeeperTwo.ChestLabels")]
[assembly: AssemblyVersion("1.1.2.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 const int _maxLength = 200;

		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(Wgo wgo)
		{
			if (wgo.InteractionHandler is ChestInteractionHandler)
			{
				return wgo.Id?.Contains("chest", StringComparison.OrdinalIgnoreCase) ?? false;
			}
			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 string? GetColor(WgoData chest)
		{
			if (!((TimbnData<ChestLabelData>)(object)_data).Current.Labels.TryGetValue(Key(chest.UniqueId), out ChestLabelEntry value))
			{
				return null;
			}
			return value.Color;
		}

		public int GetSize(WgoData chest)
		{
			if (!((TimbnData<ChestLabelData>)(object)_data).Current.Labels.TryGetValue(Key(chest.UniqueId), out ChestLabelEntry value))
			{
				return 0;
			}
			return value.SizeModifier;
		}

		public void SetLabel(Wgo chest, string text, string? color, int sizeModifier)
		{
			string key = Key(chest.Data.UniqueId);
			text = text.Trim();
			if (text.Length > 200)
			{
				text = text.Substring(0, 200);
			}
			if (text.Length == 0)
			{
				((TimbnData<ChestLabelData>)(object)_data).Current.Labels.Remove(key);
			}
			else
			{
				((TimbnData<ChestLabelData>)(object)_data).Current.Labels[key] = new ChestLabelEntry
				{
					Text = text,
					Color = color,
					SizeModifier = sizeModifier
				};
			}
			Rebuild();
			chest.DrawWidgets();
		}

		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_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			if (_texts.Count != 0 && chest.Data != null && !chest.Data.IsHidden && _texts.TryGetValue(chest.Data.UniqueId.Guid, out string value))
			{
				UIQualityTooltipWidgetData item = new UIQualityTooltipWidgetData("", value);
				if (PluginConfig.LabelBelow.Value)
				{
					widgets.Add((LazyWidgetDataBase)(object)item);
				}
				else
				{
					widgets.Insert(0, (LazyWidgetDataBase)(object)item);
				}
			}
		}

		public void Rebuild()
		{
			_texts.Clear();
			foreach (var (input, chestLabelEntry2) in ((TimbnData<ChestLabelData>)(object)_data).Current.Labels)
			{
				if (Guid.TryParse(input, out var result))
				{
					string text2 = PluginConfig.FontSize(chestLabelEntry2.SizeModifier).ToString("0.##", CultureInfo.InvariantCulture);
					string text3 = ((chestLabelEntry2.Color == null) ? chestLabelEntry2.Text : ("<color=" + chestLabelEntry2.Color + ">" + chestLabelEntry2.Text + "</color>"));
					_texts[result] = "<size=" + text2 + ">" + text3 + "</size>";
				}
			}
		}

		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; } = "";

		public string? Color { get; set; }

		public int SizeModifier { get; set; }
	}
	public enum ChestLabelSize
	{
		Small = -2,
		DefaultSize = 0,
		Large = 2,
		ExtraLarge = 4
	}
	[BepInPlugin("Timbn.GraveyardKeeperTwo.ChestLabels", "Timbn Chest Labels", "1.1.2")]
	[BepInDependency("Timbn.GraveyardKeeperTwo.Core", "2.2.0")]
	public class Plugin : TimbnFrameworkPlugin<Plugin>
	{
		private bool _warmed;

		internal ChestLabelBoard Board { get; private set; }

		protected override TimbnModInfo ModInfo => new TimbnModInfo
		{
			ModGroupName = "Timbn Mods",
			ModGroupId = "338834e9-f5a5-4d7b-9773-17dfe3d5a954",
			Description = "Name your chests, and the name floats over the chest.",
			Author = "Timbn",
			NexusLink = "https://www.nexusmods.com/graveyardkeeper2/mods/398",
			SteamWorkshopLink = "https://steamcommunity.com/sharedfiles/filedetails/?id=3821251395",
			ThunderstoreLink = "https://thunderstore.io/c/graveyard-keeper-2/p/Timbn/TimbnChestLabels/"
		};

		protected override void BindConfig(ConfigFile config)
		{
			PluginConfig.Bind(config);
		}

		protected override void OnAwake()
		{
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			TimbnSaveData<ChestLabelData> data = ((TimbnFrameworkPlugin)this).Saves.Register<ChestLabelData>((Action<ChestLabelData>)delegate
			{
				Board?.Rebuild();
			});
			Board = new ChestLabelBoard(data);
			((TimbnFrameworkPlugin)this).Settings.Changed<string>(PluginConfig.DefaultSize, (Action<string>)delegate
			{
				Board.Rebuild();
			});
			((TimbnFrameworkPlugin)this).Events.SaveReady((Action)WarmLabelPage);
			((TimbnFrameworkPlugin)this).Interactions.Register((Func<Wgo, bool>)ChestLabelBoard.CanLabel).AddSecondary(((TimbnFrameworkPlugin)this).Text.Key("timbn_chest_labels_label"), (Action<Wgo>)NameChest);
			((TimbnFrameworkPlugin)this).Events.WorldObjectRemoved((Action<WgoData>)delegate(WgoData val)
			{
				Board.ChestRemoved(val.UniqueId);
			});
		}

		private void NameChest(Wgo chest)
		{
			ShowLabelPage(chest, Board.GetText(chest.Data), Board.GetColor(chest.Data), (ChestLabelSize)Board.GetSize(chest.Data));
		}

		private void WarmLabelPage()
		{
			if (!_warmed && ((TimbnFrameworkPlugin)this).UI.CanShowPage)
			{
				_warmed = true;
				ShowLabelPage(null, string.Empty, null, ChestLabelSize.DefaultSize, overOthers: true).Dispose();
			}
		}

		private IDisposable ShowLabelPage(Wgo? chest, string name, string? color, ChestLabelSize labelSize, bool overOthers = false)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			TimbnPluginUI uI = ((TimbnFrameworkPlugin)this).UI;
			string text = ((TimbnFrameworkPlugin)this).Text.Get("timbn_chest_labels_title");
			Action<TimbnPageBuilder, Action> obj = delegate(TimbnPageBuilder page, Action close)
			{
				//IL_005c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0061: Unknown result type (might be due to invalid IL or missing references)
				//IL_006d: Expected O, but got Unknown
				//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
				//IL_00bc: Expected O, but got Unknown
				page.Add<TimbnTextBox>(new TimbnTextBox((Func<string>)(() => name), (Action<string>)delegate(string value)
				{
					name = value;
				}, (bool?)null)
				{
					FocusWhenShown = true
				});
				page.Add<TimbnColorPicker>(new TimbnColorPicker((IReadOnlyList<string>)PluginConfig.ColorList, (Func<string>)(() => color), (Action<string>)delegate(string? value)
				{
					color = value;
				}));
				page.Add<TimbnChoice<ChestLabelSize>>(new TimbnChoice<ChestLabelSize>((Func<ChestLabelSize>)(() => labelSize), (Action<ChestLabelSize>)delegate(ChestLabelSize value)
				{
					labelSize = value;
				}));
				page.ButtonFooter((Action<TimbnFooterBuilder>)delegate(TimbnFooterBuilder footer)
				{
					//IL_003c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0042: Unknown result type (might be due to invalid IL or missing references)
					footer.AddPrimarySpecial(((TimbnFrameworkPlugin)this).Text.Get("ui_save"), (Action)delegate
					{
						Board.SetLabel(chest, name, color, (int)labelSize);
						close();
					}, default(TimbnBinding<bool>), (TimbnGamepadPlacement)0);
				});
			};
			TimbnLength? val = TimbnLength.Pixels(180f);
			bool flag = overOthers;
			return uI.ShowPage(text, obj, val, (TimbnLength?)null, (Action)null, flag);
		}
	}
	internal static class PluginConfig
	{
		private const float _gameTextSize = 16f;

		private const float _smallestSize = 8f;

		private const float _largestSize = 64f;

		public static ConfigEntry<bool> LabelBelow { get; private set; }

		public static ConfigEntry<string> DefaultSize { get; private set; }

		public static float DefaultFontSize
		{
			get
			{
				string text = DefaultSize.Value.Trim();
				bool flag = text.EndsWith("%");
				if (!float.TryParse(flag ? text.TrimEnd('%') : text, NumberStyles.Float, CultureInfo.InvariantCulture, out var result) || result <= 0f)
				{
					return 16f;
				}
				return Mathf.Clamp(flag ? (16f * result / 100f) : result, 8f, 64f);
			}
		}

		public static ConfigEntry<string> Colors { get; private set; }

		public static string[] ColorList => (from color in Colors.Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
			select color.Trim()).ToArray();

		public static float FontSize(int sizeModifier)
		{
			return Mathf.Clamp(DefaultFontSize + (float)sizeModifier, 8f, 64f);
		}

		public static void Bind(ConfigFile config)
		{
			LabelBelow = config.Bind<bool>("Labels", "LabelBelow", true, "Show a chest's name under the Open and Label hints. Turn off to show it above them.");
			DefaultSize = config.Bind<string>("Labels", "DefaultSize", "100%", "How big a chest's name is at the Default size, as a percent of the game's text such as 150%, or a font size such as 24. The game's text is 16. Small, Large and Extra Large go from there.");
			Colors = config.Bind<string>("Labels", "Colors", "", "Colors to pick from when naming a chest, before the default ones. Separate them with commas, such as #FF8800, orange.");
		}
	}
	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.1.2";
	}
}
namespace Timbn.GraveyardKeeperTwo.ChestLabels.Patches
{
	[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);
		}
	}
}