using System;
using System.Collections.Generic;
namespace RynthCore.Plugin.RynthAi.Meta;
///
/// Items this plugin just used, or used something on (Host.ObjectUsed), whose
/// identified properties may now be out of date: uses left on a pet essence after
/// summoning or refilling, a kit's uses, a stack's size. The expression engine
/// re-identifies them on the next property read (ExpressionEngine.EnsureIdentified),
/// so metas don't act on the value from before the use. Engine-independent, so it
/// also covers engines whose property-update hook is missing or broken.
///
internal static class ObjectChangeTracker
{
private static readonly Dictionary _changedAtSec = new();
public static void MarkChanged(uint id)
{
if (id == 0) return;
lock (_changedAtSec)
{
if (_changedAtSec.Count > 500) _changedAtSec.Clear();
_changedAtSec[id] = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
}
///
/// True while changed after its last identify
/// (). A later identify clears it.
///
public static bool IsStale(uint id, long lastIdUnixSec)
{
lock (_changedAtSec)
{
if (!_changedAtSec.TryGetValue(id, out long changed)) return false;
if (lastIdUnixSec > changed) { _changedAtSec.Remove(id); return false; }
return true;
}
}
public static void Clear()
{
lock (_changedAtSec) _changedAtSec.Clear();
}
}