using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using Decal.Interop.Filters; namespace Decal.Adapter.Wrappers; public class Vendor : GenericDisposableWrapper, IEnumerable, IEnumerable { public class VendorEnumerator : IEnumerator, IDisposable, IEnumerator { private Vendor vendor; private WorldObject current; public WorldObject Current { get { if (current == null) { MoveNext(); } return current; } } object IEnumerator.Current { get { if (current == null) { MoveNext(); } return current; } } internal VendorEnumerator(Vendor vendor) { this.vendor = vendor; } ~VendorEnumerator() { Dispose(userCalled: false); } public void Dispose() { Dispose(userCalled: true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool userCalled) { if (vendor != null) { Reset(); vendor = null; } } public bool MoveNext() { Decal.Interop.Filters.WorldObject ppObject = null; bool num = vendor.Wrapped.Next(ref ppObject); if (num && ppObject != null) { current = vendor.GetCachedObject(ppObject); } return num; } public void Reset() { vendor.Wrapped.Reset(); } } private WorldFilter wf; private DisposableObjectDictionary objectCache; private IEnumerator oldstyleenumerator; /// /// The indentifier for this merchant /// public int MerchantId => base.Wrapped.MerchantID; /// /// The maximum value of an item this vendor will purchase /// public int MaxValue => base.Wrapped.BuyValue; /// /// The rate at which this vendor sells items /// public float SellRate => base.Wrapped.SellRate; /// /// The rate at which this vendor buys items /// public float BuyRate => base.Wrapped.BuyRate; /// /// The item categories that this vendor buys /// public int Categories => base.Wrapped.BuyCategories; /// /// Returns the number of items in the collection /// public int Count => base.Wrapped.Count; /// /// Returns the number of items represented by the collection, taking into account stacks of items. /// public int Quantity => base.Wrapped.Quantity; public WorldObject First { get { using IEnumerator enumerator = GetEnumerator(); return enumerator.Current; } } [Obsolete("Use enumerators")] [EditorBrowsable(EditorBrowsableState.Never)] public WorldObject Current { get { if (oldstyleenumerator == null) { oldstyleenumerator = GetEnumerator(); } return oldstyleenumerator.Current; } } public WorldObject this[int id] { get { WorldObject value = null; if (!objectCache.TryGetValue(id, out value)) { Decal.Interop.Filters.WorldObject byID = base.Wrapped.GetByID(id); if (byID != null) { value = new WorldObject(byID); objectCache.Add(value.Id, value); } } return value; } } public Vendor(WorldFilter wf, Decal.Interop.Filters.Vendor obj) : base(obj) { this.wf = wf; objectCache = new DisposableObjectDictionary("Id"); } protected override void Dispose(bool userCalled) { if (oldstyleenumerator != null) { oldstyleenumerator.Dispose(); oldstyleenumerator = null; } objectCache.Clear(); base.Dispose(userCalled); } internal WorldObject GetCachedObject(Decal.Interop.Filters.WorldObject iwo) { WorldObject value = null; if (!objectCache.TryGetValue(iwo.GUID, out value)) { value = new WorldObject(iwo); objectCache.Add(value.Id, value); } return value; } /// /// Apply a filter to this collection to limit what it returns /// /// filter object public void SetFilter(WorldObjectCollectionFilter filter) { filter?.ApplyFilter(base.Wrapped); } [Obsolete("Use enumerators")] [EditorBrowsable(EditorBrowsableState.Never)] public bool MoveNext() { if (oldstyleenumerator == null) { oldstyleenumerator = GetEnumerator(); } return oldstyleenumerator.MoveNext(); } [Obsolete("Use enumerators")] [EditorBrowsable(EditorBrowsableState.Never)] public void Reset() { if (oldstyleenumerator == null) { oldstyleenumerator = GetEnumerator(); } oldstyleenumerator.Reset(); } public IEnumerator GetEnumerator() { return new VendorEnumerator(this); } IEnumerator IEnumerable.GetEnumerator() { return new VendorEnumerator(this); } }