using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("AuroraWardDecay")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AuroraWardDecay")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("91245976-b7c2-4fcd-88a9-505fe801b8e8")]
[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 AuroraWardDecay
{
public class Class1
{
}
}
namespace AuroraWardDecayMod
{
[BepInPlugin("aurora.warddecay", "AuroraWardDecay", "1.0.5")]
public class AuroraWardDecayPlugin : BaseUnityPlugin
{
[CompilerGenerated]
private sealed class <ServerSweepLoop>d__12 : IEnumerator<object>, IDisposable, IEnumerator
{
private int <>1__state;
private object <>2__current;
public AuroraWardDecayPlugin <>4__this;
private PrivateArea[] <wards>5__1;
private long <nowTicks>5__2;
private long <maxAge>5__3;
private PrivateArea[] <>s__4;
private int <>s__5;
private PrivateArea <pa>5__6;
private ZNetView <nview>5__7;
private ZDO <zdo>5__8;
private long <lastTouched>5__9;
private Exception <ex>5__10;
object IEnumerator<object>.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
object IEnumerator.Current
{
[DebuggerHidden]
get
{
return <>2__current;
}
}
[DebuggerHidden]
public <ServerSweepLoop>d__12(int <>1__state)
{
this.<>1__state = <>1__state;
}
[DebuggerHidden]
void IDisposable.Dispose()
{
<wards>5__1 = null;
<>s__4 = null;
<pa>5__6 = null;
<nview>5__7 = null;
<zdo>5__8 = null;
<ex>5__10 = null;
<>1__state = -2;
}
private bool MoveNext()
{
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Expected O, but got Unknown
switch (<>1__state)
{
default:
return false;
case 0:
<>1__state = -1;
break;
case 1:
<>1__state = -1;
if ((Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsServer())
{
break;
}
try
{
<wards>5__1 = Object.FindObjectsOfType<PrivateArea>();
if (<wards>5__1 == null || <wards>5__1.Length == 0)
{
break;
}
<nowTicks>5__2 = NowTicks();
<maxAge>5__3 = DaysToTicks(DecayDays.Value);
<>s__4 = <wards>5__1;
for (<>s__5 = 0; <>s__5 < <>s__4.Length; <>s__5++)
{
<pa>5__6 = <>s__4[<>s__5];
if (Object.op_Implicit((Object)(object)<pa>5__6))
{
<nview>5__7 = ((Component)<pa>5__6).GetComponent<ZNetView>();
if (Object.op_Implicit((Object)(object)<nview>5__7) && <nview>5__7.IsValid())
{
<zdo>5__8 = <nview>5__7.GetZDO();
if (<zdo>5__8 != null)
{
<lastTouched>5__9 = <zdo>5__8.GetLong("aurora_lastTouched_ticks", 0L);
if (<lastTouched>5__9 <= 0)
{
<zdo>5__8.Set("aurora_lastTouched_ticks", <nowTicks>5__2);
}
else
{
if (<nowTicks>5__2 - <lastTouched>5__9 >= <maxAge>5__3)
{
Object.Destroy((Object)(object)((Component)<pa>5__6).gameObject);
}
<nview>5__7 = null;
<zdo>5__8 = null;
<pa>5__6 = null;
}
}
}
}
}
<>s__4 = null;
<wards>5__1 = null;
}
catch (Exception ex)
{
<ex>5__10 = ex;
((BaseUnityPlugin)<>4__this).Logger.LogError((object)$"Ward decay sweep failed: {<ex>5__10}");
}
break;
}
<>2__current = (object)new WaitForSeconds(CheckIntervalSeconds.Value);
<>1__state = 1;
return true;
}
bool IEnumerator.MoveNext()
{
//ILSpy generated this explicit interface implementation from .override directive in MoveNext
return this.MoveNext();
}
[DebuggerHidden]
void IEnumerator.Reset()
{
throw new NotSupportedException();
}
}
public const string PluginGuid = "aurora.warddecay";
public const string PluginName = "AuroraWardDecay";
public const string PluginVersion = "1.0.5";
internal const string ZdoKeyLastTouchedTicks = "aurora_lastTouched_ticks";
internal static ConfigEntry<float> CheckIntervalSeconds;
internal static ConfigEntry<double> DecayDays;
internal static ConfigEntry<bool> RefreshOnInteractOnly;
internal static ConfigEntry<bool> EnableHoverText;
internal static ConfigEntry<string> HoverFormat;
private Harmony _harmony;
private void Awake()
{
//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
//IL_00c1: Expected O, but got Unknown
CheckIntervalSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("General", "CheckIntervalSeconds", 30f, "How often the server checks for expired wards.");
DecayDays = ((BaseUnityPlugin)this).Config.Bind<double>("General", "DecayDays", 7.0, "Days without interaction before a ward expires.");
RefreshOnInteractOnly = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RefreshOnInteractOnly", true, "If true, only Interact() refreshes the timer (placement always seeds).");
EnableHoverText = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableHoverText", true, "Show remaining time on ward hover.");
HoverFormat = ((BaseUnityPlugin)this).Config.Bind<string>("General", "HoverFormat", "<color=yellow><b>Upkeep:</b></color> {0}", "Format for remaining time; {0}=time left.");
_harmony = new Harmony("aurora.warddecay");
_harmony.PatchAll();
((MonoBehaviour)this).StartCoroutine(ServerSweepLoop());
((BaseUnityPlugin)this).Logger.LogInfo((object)"AuroraWardDecay 1.0.5 loaded");
}
private void OnDestroy()
{
try
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
catch
{
}
}
[IteratorStateMachine(typeof(<ServerSweepLoop>d__12))]
private IEnumerator ServerSweepLoop()
{
//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
return new <ServerSweepLoop>d__12(0)
{
<>4__this = this
};
}
internal static long NowTicks()
{
return (((Object)(object)ZNet.instance != (Object)null) ? ZNet.instance.GetTime() : DateTime.UtcNow).ToUniversalTime().Ticks;
}
internal static long DaysToTicks(double days)
{
return TimeSpan.FromDays(days).Ticks;
}
internal static string FormatRemaining(long nowTicks, long lastTouchedTicks, double days)
{
long num = DaysToTicks(days);
long num2 = ((lastTouchedTicks > 0) ? Math.Max(0L, num - (nowTicks - lastTouchedTicks)) : num);
if (num2 <= 0)
{
return "expired";
}
TimeSpan timeSpan = TimeSpan.FromTicks(num2);
if (timeSpan.TotalDays >= 1.0)
{
return $"{(int)timeSpan.TotalDays}d {timeSpan.Hours}h";
}
if (timeSpan.TotalHours >= 1.0)
{
return $"{(int)timeSpan.TotalHours}h {timeSpan.Minutes}m";
}
return $"{timeSpan.Minutes}m {timeSpan.Seconds}s";
}
}
internal static class SeedHelper
{
public static void Seed(PrivateArea pa)
{
if (!Object.op_Implicit((Object)(object)pa))
{
return;
}
ZNetView component = ((Component)pa).GetComponent<ZNetView>();
if (Object.op_Implicit((Object)(object)component) && component.IsValid())
{
ZDO zDO = component.GetZDO();
if (zDO != null && zDO.GetLong("aurora_lastTouched_ticks", 0L) == 0)
{
zDO.Set("aurora_lastTouched_ticks", AuroraWardDecayPlugin.NowTicks());
}
}
}
}
[HarmonyPatch(typeof(PrivateArea), "Awake")]
public static class Patch_PrivateArea_Awake_Seed
{
private static void Postfix(PrivateArea __instance)
{
SeedHelper.Seed(__instance);
}
}
[HarmonyPatch(typeof(PrivateArea), "Interact")]
public static class Patch_PrivateArea_Interact
{
private static void Postfix(PrivateArea __instance, bool __result, Humanoid human, bool hold, bool alt)
{
if (!__result || !AuroraWardDecayPlugin.RefreshOnInteractOnly.Value)
{
return;
}
ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
if (Object.op_Implicit((Object)(object)component) && component.IsValid())
{
ZDO zDO = component.GetZDO();
if (zDO != null)
{
zDO.Set("aurora_lastTouched_ticks", AuroraWardDecayPlugin.NowTicks());
}
}
}
}
[HarmonyPatch(typeof(PrivateArea), "GetHoverText")]
public static class Patch_PrivateArea_GetHoverText
{
private static void Postfix(PrivateArea __instance, ref string __result)
{
if (!AuroraWardDecayPlugin.EnableHoverText.Value)
{
return;
}
ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
if (!Object.op_Implicit((Object)(object)component) || !component.IsValid())
{
return;
}
ZDO zDO = component.GetZDO();
if (zDO != null)
{
long @long = zDO.GetLong("aurora_lastTouched_ticks", 0L);
long nowTicks = AuroraWardDecayPlugin.NowTicks();
string arg = AuroraWardDecayPlugin.FormatRemaining(nowTicks, @long, AuroraWardDecayPlugin.DecayDays.Value);
StringBuilder stringBuilder = new StringBuilder(__result ?? string.Empty);
if (!stringBuilder.ToString().EndsWith("\n"))
{
stringBuilder.Append('\n');
}
stringBuilder.AppendFormat(AuroraWardDecayPlugin.HoverFormat.Value, arg);
__result = stringBuilder.ToString();
}
}
}
}