using JetBrains.Annotations; using System; using System.Collections.Generic; using System.ComponentModel.Design; using NEG.Utils; using NegUtils; using NegUtils.NEG.UI; namespace NEG.UI.Popup { public struct PopupDataHook { private uint version; private PopupData data; public bool IsValid => version == data.CurrentVersion; public PopupDataHook(PopupData data) { this.data = data; version = data.CurrentVersion; } public void Invalidate() { if (!IsValid) return; data.Dispose(); } } [PublicAPI] public class PopupData : IPoolable { private static readonly Dictionary> ReturnDelegates = new(); /// /// Is this data is still valid. If set to false, popup will not show. /// public bool IsValid { get; protected set; } internal uint CurrentVersion { get; private set; } internal void Dispose() { IsValid = false; IPopupsHandler.Instance.RefreshPopups(); var type = GetType(); if (!ReturnDelegates.TryGetValue(GetType(), out var returnDelegate)) ReturnDelegates[type] = CreateReturnDelegate(type); returnDelegate?.Invoke(this); } private static Action CreateReturnDelegate(Type type) { var returnMethod = typeof(NativePool<>) .MakeGenericType(type) .GetMethod(nameof(NativePool.Return), new[] { type }); return (Action)Delegate.CreateDelegate( typeof(Action), returnMethod); } public virtual void OnGet() { IsValid = true; } public virtual void OnRelease() { CurrentVersion++; } } }