using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Settings;
using UnityEngine.Localization.Tables;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("OldMarket.FarmingSupport")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("OldMarket.FarmingSupport")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cd64c961-d01e-412c-8668-c4beaef94969")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace OldMarket.FarmingSupport;
[HarmonyPatch(typeof(PlayerInteraction), "InteractionRay")]
internal static class SeedPickupLayoutPatch
{
private static readonly FieldInfo LastOutlineField = AccessTools.Field(typeof(PlayerInteraction), "lastOutline");
[HarmonyPostfix]
[HarmonyPriority(0)]
private static void Postfix(PlayerInteraction __instance)
{
//IL_0117: Unknown result type (might be due to invalid IL or missing references)
//IL_0121: Unknown result type (might be due to invalid IL or missing references)
//IL_015f: Unknown result type (might be due to invalid IL or missing references)
//IL_0164: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.interactionUI == (Object)null || !__instance.interactionUI.activeSelf || LastOutlineField == null)
{
return;
}
object? value = LastOutlineField.GetValue(__instance);
Outline val = (Outline)((value is Outline) ? value : null);
if ((Object)(object)val == (Object)null)
{
return;
}
Interactable componentInParent = ((Component)val).GetComponentInParent<Interactable>();
if ((Object)(object)componentInParent == (Object)null)
{
return;
}
Item val2 = (Item)(object)((componentInParent is Item) ? componentInParent : null);
if ((Object)(object)val2 == (Object)null)
{
return;
}
ItemSO itemSO = val2.itemSO;
SeedSO val3 = (SeedSO)(object)((itemSO is SeedSO) ? itemSO : null);
if ((Object)(object)val3 == (Object)null)
{
return;
}
try
{
if ((Object)(object)__instance.textPrimaryInteraction != (Object)null)
{
((TMP_Text)__instance.textPrimaryInteraction).text = componentInParent.GetDescription();
}
}
catch
{
}
int num = val3.daysToFullGrowth;
if (num < 0)
{
num = 0;
}
string text;
try
{
text = LocalizationSettings.StringDatabase.GetLocalizedString(TableReference.op_Implicit("Translations"), TableEntryReference.op_Implicit("days"), (Locale)null, (FallbackBehavior)0, Array.Empty<object>());
}
catch
{
text = "DAYS";
}
string text2 = null;
try
{
Locale selectedLocale = LocalizationSettings.SelectedLocale;
if ((Object)(object)selectedLocale != (Object)null)
{
LocaleIdentifier identifier = selectedLocale.Identifier;
text2 = ((LocaleIdentifier)(ref identifier)).Code;
}
}
catch
{
}
string text3 = ((string.IsNullOrEmpty(text2) || !text2.StartsWith("ja", StringComparison.OrdinalIgnoreCase)) ? $"{num} {text.ToUpperInvariant()}" : $"{text} {num}");
if ((Object)(object)__instance.textDetail != (Object)null)
{
((TMP_Text)__instance.textDetail).text = text3;
}
if ((Object)(object)__instance.textDescription != (Object)null && (Object)(object)val3.productSO != (Object)null && (Object)(object)val3.productSO.season != (Object)null)
{
string text4 = val3.productSO.season.GetLocalizedName();
if (text4 == null)
{
text4 = string.Empty;
}
((TMP_Text)__instance.textDescription).text = text4;
}
}
}
[BepInPlugin("oldmarket.farminfo", "Old Market Farm Info", "1.0.0")]
public class FarmInfoPlugin : BaseUnityPlugin
{
public const string PluginGuid = "oldmarket.farminfo";
public const string PluginName = "Old Market Farm Info";
public const string PluginVersion = "1.0.0";
internal static ManualLogSource Log;
private void Awake()
{
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Harmony val = new Harmony("oldmarket.farminfo");
val.PatchAll();
}
internal static string FormatRemainingDays(int remainingDays)
{
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_002e: Unknown result type (might be due to invalid IL or missing references)
if (remainingDays <= 0)
{
return string.Empty;
}
string text = null;
try
{
text = LocalizationSettings.StringDatabase.GetLocalizedString(TableReference.op_Implicit("Translations"), TableEntryReference.op_Implicit("days_left"), (Locale)null, (FallbackBehavior)0, Array.Empty<object>());
}
catch (Exception ex)
{
if (Log != null)
{
Log.LogWarning((object)("Failed to get localized 'days_left': " + ex));
}
}
if (!string.IsNullOrEmpty(text))
{
try
{
if (text.IndexOf("{0", StringComparison.Ordinal) >= 0)
{
return string.Format(text, remainingDays);
}
return remainingDays + " " + text;
}
catch (FormatException)
{
return remainingDays + " " + text;
}
}
if (remainingDays == 1)
{
return "1 day left";
}
return remainingDays + " days left";
}
internal static string AppendDaysSuffix(string baseText, int remainingDays)
{
string text = FormatRemainingDays(remainingDays);
if (string.IsNullOrEmpty(text))
{
return baseText ?? string.Empty;
}
if (string.IsNullOrEmpty(baseText))
{
return text;
}
text = text.Replace("\r", "").Replace("\n", " ");
return baseText + "\n" + text;
}
}
[HarmonyPatch(typeof(BlockDirt))]
public static class BlockDirt_GetDescription_Patch
{
[HarmonyPostfix]
[HarmonyPatch("GetDescription")]
public static void Postfix(BlockDirt __instance, ref string __result)
{
if (string.IsNullOrEmpty(__result) || (Object)(object)GameManager.Instance == (Object)null)
{
return;
}
FieldInfo fieldInfo = AccessTools.Field(typeof(BlockDirt), "currentSeed");
if (fieldInfo == null || !(fieldInfo.GetValue(__instance) is NetworkVariable<long> val) || val.Value == -1)
{
return;
}
ItemSO itemById = GameManager.Instance.GetItemById(val.Value);
SeedSO val2 = (SeedSO)(object)((itemById is SeedSO) ? itemById : null);
if ((Object)(object)val2 == (Object)null)
{
return;
}
int value = __instance.dayCounter.Value;
int daysToFullGrowth = val2.daysToFullGrowth;
if (daysToFullGrowth > 0 && value < daysToFullGrowth)
{
int remainingDays = daysToFullGrowth - value;
__result = FarmInfoPlugin.AppendDaysSuffix(__result, remainingDays);
}
if ((Object)(object)val2.productSO != (Object)null && (Object)(object)val2.productSO.season != (Object)null)
{
string text = val2.productSO.season.GetLocalizedName() ?? string.Empty;
if (!string.IsNullOrEmpty(text) && __result.IndexOf(text, StringComparison.Ordinal) < 0)
{
__result = __result + "\n" + text;
}
}
}
}
[HarmonyPatch(typeof(BlockTree))]
public static class BlockTree_GetDescription_Patch
{
[HarmonyPostfix]
[HarmonyPatch("GetDescription")]
public static void Postfix(BlockTree __instance, ref string __result)
{
if (string.IsNullOrEmpty(__result))
{
return;
}
FieldInfo fieldInfo = AccessTools.Field(typeof(BlockTree), "treeSO");
TreeSO val = (TreeSO)((fieldInfo != null) ? /*isinst with value type is only supported in some contexts*/: null);
if ((Object)(object)val == (Object)null)
{
return;
}
int value = __instance.dayCounter.Value;
if (value < val.daysToHarvestAfterGrowth)
{
int num = ((value >= val.daysToFullGrowth) ? (val.daysToHarvestAfterGrowth - value) : (val.daysToFullGrowth - value));
if (num > 0)
{
__result = FarmInfoPlugin.AppendDaysSuffix(__result, num);
}
}
if ((Object)(object)val.productSO != (Object)null && (Object)(object)val.productSO.season != (Object)null)
{
string text = val.productSO.season.GetLocalizedName() ?? string.Empty;
if (!string.IsNullOrEmpty(text) && __result.IndexOf(text, StringComparison.Ordinal) < 0)
{
__result = __result + "\n" + text;
}
}
}
}
[HarmonyPatch(typeof(BlockAnimal))]
public static class BlockAnimal_GetDescription_Patch
{
[HarmonyPostfix]
[HarmonyPatch("GetDescription")]
public static void Postfix(BlockAnimal __instance, ref string __result)
{
if (string.IsNullOrEmpty(__result))
{
return;
}
FieldInfo fieldInfo = AccessTools.Field(typeof(BlockAnimal), "animalSO");
AnimalSO val = (AnimalSO)((fieldInfo != null) ? /*isinst with value type is only supported in some contexts*/: null);
if ((Object)(object)val == (Object)null)
{
return;
}
int value = __instance.dayCounter.Value;
if (value >= val.daysToHarvestAfterGrowth)
{
return;
}
int num = ((value >= val.daysToGrowth) ? (val.daysToHarvestAfterGrowth - value) : (val.daysToGrowth - value));
if (num > 0)
{
string text = FarmInfoPlugin.FormatRemainingDays(num);
if (!string.IsNullOrEmpty(text))
{
__result = __result + " (" + text + ")";
}
}
}
}
[HarmonyPatch(typeof(PlayerInteraction))]
public static class PlayerInteraction_SeedInfoPatch
{
[HarmonyPostfix]
[HarmonyPatch("InteractionRay")]
public static void Postfix(PlayerInteraction __instance)
{
//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
try
{
if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.interactionUI == (Object)null || !__instance.interactionUI.activeSelf)
{
return;
}
TextMeshProUGUI textPrimaryInteraction = __instance.textPrimaryInteraction;
if ((Object)(object)textPrimaryInteraction == (Object)null || string.IsNullOrEmpty(((TMP_Text)textPrimaryInteraction).text))
{
return;
}
Traverse val = Traverse.Create((object)__instance);
Camera value = val.Field("mainCamera").GetValue<Camera>();
if (!val.Field("isInteractionEnabled").GetValue<bool>() || (Object)(object)value == (Object)null)
{
return;
}
float interactionDistance = __instance.interactionDistance;
LayerMask layerMask = __instance.layerMask;
Ray val2 = value.ViewportPointToRay(Vector3.one / 2f);
RaycastHit val3 = default(RaycastHit);
if (!Physics.Raycast(val2, ref val3, interactionDistance, LayerMask.op_Implicit(layerMask), (QueryTriggerInteraction)1))
{
return;
}
Interactable componentInParent = ((Component)((RaycastHit)(ref val3)).collider).GetComponentInParent<Interactable>();
if ((Object)(object)componentInParent == (Object)null)
{
return;
}
Item val4 = (Item)(object)((componentInParent is Item) ? componentInParent : null);
if ((Object)(object)val4 == (Object)null)
{
return;
}
ItemSO itemSO = val4.itemSO;
SeedSO val5 = (SeedSO)(object)((itemSO is SeedSO) ? itemSO : null);
if ((Object)(object)val5 == (Object)null)
{
return;
}
int num = Mathf.Max(val5.daysToFullGrowth, 0);
if (num > 0 && ((TMP_Text)textPrimaryInteraction).text.IndexOf("Harvest in", StringComparison.Ordinal) < 0)
{
string text = ((num == 1) ? "Harvest in 1 day" : ("Harvest in " + num + " days"));
((TMP_Text)textPrimaryInteraction).text = ((TMP_Text)textPrimaryInteraction).text + "\n" + text;
}
TextMeshProUGUI textDescription = __instance.textDescription;
if ((Object)(object)textDescription != (Object)null && (Object)(object)val5.productSO != (Object)null && (Object)(object)val5.productSO.season != (Object)null)
{
string text2 = val5.productSO.season.GetLocalizedName() ?? string.Empty;
if (!string.IsNullOrEmpty(text2))
{
((TMP_Text)textDescription).text = text2;
}
}
}
catch (Exception ex)
{
FarmInfoPlugin.Log.LogError((object)("Seed info patch error: " + ex));
}
}
}
[HarmonyPatch(typeof(BlockBeeHive))]
public static class BlockBeeHive_GetDescription_Patch
{
[HarmonyPostfix]
[HarmonyPatch("GetDescription")]
public static void Postfix(BlockBeeHive __instance, ref string __result)
{
if (string.IsNullOrEmpty(__result) || __instance.dayCounter.Value >= __instance.requiredDays)
{
return;
}
FieldInfo fieldInfo = AccessTools.Field(typeof(BlockBeeHive), "hasBee");
NetworkVariable<bool> val = ((fieldInfo != null) ? (fieldInfo.GetValue(__instance) as NetworkVariable<bool>) : null);
if (val == null || !val.Value)
{
return;
}
int requiredDays = __instance.requiredDays;
int value = __instance.dayCounter.Value;
if (value >= requiredDays)
{
return;
}
int num = requiredDays - value;
if (num > 0)
{
string text = FarmInfoPlugin.FormatRemainingDays(num);
if (!string.IsNullOrEmpty(text))
{
__result = __result + " (" + text + ")";
}
}
}
}