Decompiled source of TavernTweaks v1.1.0

TavernTweaks/TavernTweaks.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("TavernTweaks")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TavernTweaks")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("d70a7faa-92df-4e7e-a95b-6c8f84261dac")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace TavernTweaks
{
	[BepInPlugin("chemified.TavernTweaks", "TavernTweaks", "1.1.0")]
	internal class TavernTweaks : BaseUnityPlugin
	{
		private const string modGUID = "chemified.TavernTweaks";

		private const string modName = "TavernTweaks";

		private const string modVersion = "1.1.0";

		internal readonly Harmony harmony = new Harmony("chemified.TavernTweaks");

		public static TavernTweaks Instance { get; private set; }

		internal static ManualLogSource mls { get; private set; }

		private void Awake()
		{
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			mls = Logger.CreateLogSource("chemified.TavernTweaks");
			TavernTweaksConfig.InitializeConfig();
			foreach (Type item in from t in Assembly.GetExecutingAssembly().GetTypes()
				where t.Namespace == "TavernTweaks.Patches" && t.IsClass && t.GetCustomAttributes(typeof(HarmonyPatch), inherit: false).Any()
				select t)
			{
				try
				{
					harmony.PatchAll(item);
					mls.LogInfo((object)("Patched " + item.Name));
				}
				catch (Exception ex)
				{
					mls.LogError((object)("Failed to patch " + item.Name + ": " + ex.Message));
				}
			}
		}

		public void BindConfig<T>(ref ConfigEntry<T> config, string section, string key, T defaultValue, string description = "")
		{
			config = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, defaultValue, description);
		}
	}
	internal class TavernTweaksConfig
	{
		internal static ConfigEntry<bool> configToggleCustomerSpawnTime;

		internal static ConfigEntry<bool> configToggleUseHoldTime;

		internal static ConfigEntry<bool> configToggleAutofill;

		internal static ConfigEntry<float> customerMultiplierRange;

		internal static ConfigEntry<float> useTimeRange;

		internal static void InitializeConfig()
		{
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Expected O, but got Unknown
			TavernTweaks.Instance.BindConfig(ref configToggleUseHoldTime, "Toggles", "Enable Use Hold Time", defaultValue: true, "Adjusts how long it takes to interact with an object (e.g filling water bucket, harvesting crops, picking up furniture).");
			TavernTweaks.Instance.BindConfig(ref configToggleAutofill, "Toggles", "Enable Autofill", defaultValue: true, "Adds a button to the cooking UI that automatically grabs items from your inventory.");
			TavernTweaks.Instance.BindConfig(ref configToggleCustomerSpawnTime, "Toggles", "Enable Customer Spawn Multiplier", defaultValue: false, "Adjusts the spawn rate of customers. This adjusts the base value.\nNote customers also increase their spawn rate with more players.");
			AcceptableValueRange<float> val = new AcceptableValueRange<float>(0.25f, 2f);
			customerMultiplierRange = ((BaseUnityPlugin)TavernTweaks.Instance).Config.Bind<float>("Values", "Customer Spawn Multiplier", 0.75f, new ConfigDescription("Value used as a multiplier. 0.5 = customers spawn twice as fast.", (AcceptableValueBase)(object)val, Array.Empty<object>()));
			AcceptableValueRange<float> val2 = new AcceptableValueRange<float>(0.3f, 60f);
			useTimeRange = ((BaseUnityPlugin)TavernTweaks.Instance).Config.Bind<float>("Values", "Use Hold Time", 0.5f, new ConfigDescription("Value is displayed in seconds. Game default is 2 seconds.", (AcceptableValueBase)(object)val2, Array.Empty<object>()));
		}
	}
}
namespace TavernTweaks.Patches
{
	[HarmonyPatch(typeof(CookUI), "Awake")]
	internal class CookingPatch
	{
		[HarmonyPostfix]
		private static void AddAutofill(CookUI __instance)
		{
			//IL_0138: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Expected O, but got Unknown
			if (!TavernTweaksConfig.configToggleAutofill.Value)
			{
				return;
			}
			GameObject obj = GameObject.Find("Cook Screen/cookingDevice/CookPanel");
			Transform transform = obj.transform;
			if ((Object)(object)obj == (Object)null)
			{
				TavernTweaks.mls.LogError((object)"Failed to get CookPanel");
				return;
			}
			Transform val = transform.Find("ButtonWaste");
			if ((Object)(object)val == (Object)null)
			{
				TavernTweaks.mls.LogError((object)"Failed to get ButtonWaste");
				return;
			}
			Transform val2 = transform.Find("ButtonCraft");
			if ((Object)(object)val2 == (Object)null)
			{
				TavernTweaks.mls.LogError((object)"Failed to get ButtonClear");
				return;
			}
			InputHint component = ((Component)transform).GetComponent<InputHint>();
			if ((Object)(object)component == (Object)null)
			{
				TavernTweaks.mls.LogError((object)"Failed to get InputHint");
				return;
			}
			GameObject gameObject = ((Component)val).gameObject;
			GameObject gameObject2 = ((Component)val2).gameObject;
			RectTransform val3 = default(RectTransform);
			if (((Component)((Component)transform).transform.Find("IngredientsBG")).TryGetComponent<RectTransform>(ref val3))
			{
				GameObject val4 = Object.Instantiate<GameObject>(gameObject, gameObject.transform.parent, false);
				((Object)val4).name = "ButtonAutofill";
				((TMP_Text)val4.GetComponentInChildren<TextMeshProUGUI>()).text = "Autofill";
				RectTransform[] array = (RectTransform[])(object)new RectTransform[3]
				{
					gameObject.GetComponent<RectTransform>(),
					val4.GetComponent<RectTransform>(),
					gameObject2.GetComponent<RectTransform>()
				};
				float x = array[2].anchoredPosition.x;
				float x2 = array[0].anchoredPosition.x;
				Rect rect = array[0].rect;
				float width = ((Rect)(ref rect)).width;
				float num = x - x2 - width * 2f;
				float num2 = width + num;
				float x3 = val3.anchoredPosition.x;
				RectTransform[] array2 = array;
				foreach (RectTransform val5 in array2)
				{
					val5.pivot = new Vector2(0.5f, val5.pivot.y);
				}
				for (int j = -1; j <= 1; j++)
				{
					array[j + 1].anchoredPosition = new Vector2(x3 + num2 * (float)j, array[j + 1].anchoredPosition.y);
				}
				FieldInfo fieldInfo = AccessTools.Field(typeof(InputHint), "kmObjects");
				GameObject[] array3 = (GameObject[])fieldInfo.GetValue(component);
				Array.Resize(ref array3, array3.Length + 1);
				array3[^1] = val4;
				fieldInfo.SetValue(component, array3);
				Button component2 = val4.GetComponent<Button>();
				((UnityEventBase)component2.onClick).RemoveAllListeners();
				((UnityEvent)component2.onClick).AddListener((UnityAction)delegate
				{
					DoAutofill(__instance);
				});
			}
			else
			{
				TavernTweaks.mls.LogError((object)"Failed to get IngredientsBG");
			}
		}

		private static void DoAutofill(CookUI instance)
		{
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: 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_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0207: Unknown result type (might be due to invalid IL or missing references)
			PlayerInventory instance2 = PlayerInventory.Instance;
			ContainerNet inventory = instance2.inventory;
			CookingDevice cookingDevice = instance2.cookingDevice;
			RecipeData currentRecipe = cookingDevice.currentRecipe;
			NetworkList<Item> items = cookingDevice.items;
			ContainerUI invBP = instance.invBP;
			byte value = cookingDevice.cookingStage.Value;
			int num = (currentRecipe.isFixedAmount ? 1 : cookingDevice.itemAmount.Value);
			Dictionary<ItemData, byte> dictionary = default(Dictionary<ItemData, byte>);
			if (!currentRecipe.GetStageIngredients(ref dictionary, value))
			{
				return;
			}
			foreach (KeyValuePair<ItemData, byte> item in dictionary)
			{
				int num2 = 0;
				foreach (Item item2 in items)
				{
					if (item2.dataId == item.Key.id)
					{
						num2 = item2.amount;
						break;
					}
				}
				int num3 = item.Value * num - num2;
				if (num3 <= 0)
				{
					TavernTweaks.mls.LogInfo((object)"Enough in pot");
					continue;
				}
				if (((item.Key.maxCharge > 0) ? inventory.GetItemCharge(item.Key.id) : inventory.GetItemAmount(item.Key.id)) == 0)
				{
					TavernTweaks.mls.LogInfo((object)(item.Key.name + " not found in inventory"));
					continue;
				}
				int num4 = num3;
				if (!(from x in StringExtension.ToList<Item>(invBP.container.items)
					group x by x.dataId).ToDictionary((IGrouping<ushort, Item> g) => g.Key, (IGrouping<ushort, Item> g) => g.ToList()).TryGetValue(item.Key.id, out var value2))
				{
					continue;
				}
				foreach (Item item3 in value2)
				{
					ushort num5 = ((item.Key.maxCharge > 0) ? item3.charge : item3.amount);
					invBP.OnItemDoubleClick(item3);
					num4 -= num5;
					if (num4 <= 0)
					{
						break;
					}
				}
			}
			((MonoBehaviour)instance).Invoke("UpdItems", 0.1f);
		}
	}
	[HarmonyPatch(typeof(CustomersManager), "Awake")]
	internal class CustomersPatch
	{
		[HarmonyPostfix]
		private static void SetCustomerSpawnDelays(CustomersManager __instance, ref int ___minDelayBetweenSpawn, ref int ___maxDelayBetweenSpawn)
		{
			if (TavernTweaksConfig.configToggleCustomerSpawnTime.Value)
			{
				float num = Mathf.Max(0.25f, TavernTweaksConfig.customerMultiplierRange.Value);
				int num2 = Mathf.FloorToInt((float)___minDelayBetweenSpawn * num);
				int num3 = Mathf.FloorToInt((float)___maxDelayBetweenSpawn * num);
				___minDelayBetweenSpawn = num2;
				___maxDelayBetweenSpawn = num3;
			}
		}
	}
	[HarmonyPatch(typeof(Interactive), "Awake")]
	internal class UseTimePatch
	{
		[HarmonyPostfix]
		private static void holdTimePatch(Interactive __instance)
		{
			if (TavernTweaksConfig.configToggleUseHoldTime.Value)
			{
				__instance.holdTime = Mathf.Max(0.3f, TavernTweaksConfig.useTimeRange.Value);
				__instance.removeHoldTime = Mathf.Max(0.3f, TavernTweaksConfig.useTimeRange.Value);
			}
		}
	}
}