using System; using System.Collections.Generic; using System.Linq; using PkmnLibSharp.Utilities; namespace PkmnLibSharp.Library { public class SpeciesLibrary : PointerWrapper { private readonly Dictionary _cache = new Dictionary(StringComparer.InvariantCultureIgnoreCase); private readonly Dictionary _idCache = new Dictionary(); public ulong Count => Creaturelib.Generated.SpeciesLibrary.GetCount(Ptr); public void Insert(string key, Species species) { Creaturelib.Generated.SpeciesLibrary.Insert(Ptr, key.ToPtr(), species.Ptr).Assert(); _cache.Add(key, species); _idCache.Add(species.Id, species); } public void Delete(string key) { Creaturelib.Generated.SpeciesLibrary.Delete(Ptr, key.ToPtr()).Assert(); if (_cache.TryGetValue(key, out var s)) { _cache.Remove(key); if (s != null) _idCache.Remove(s.Id); } } public bool TryGet(string key, out Species? species) { if (_cache.TryGetValue(key, out species)) return true; var ptr = IntPtr.Zero; if (Creaturelib.Generated.SpeciesLibrary.TryGet(Ptr, key.ToPtr(), ref ptr) != MarshalHelper.True) return false; if (TryResolvePointer(ptr, out species)) { _cache.Add(key, species!); return true; } species = Constructor.Active.ConstructSpecies(ptr)!; _cache.Add(key, species); return true; } public Species Get(string key) { if (_cache.TryGetValue(key, out var species)) return species; var ptr = IntPtr.Zero; Creaturelib.Generated.SpeciesLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert(); if (TryResolvePointer(ptr, out Species? s)) { _cache.Add(key, s!); _idCache.Add(s!.Id, s); return s!; } species = Constructor.Active.ConstructSpecies(ptr)!; _cache.Add(key, species); return species; } public Species GetById(ushort id) { if (_idCache.TryGetValue(id, out var species)) return species; var ptr = Creaturelib.Generated.SpeciesLibrary.GetById(Ptr, id); if (TryResolvePointer(ptr, out Species? s)) { _idCache.Add(id, s!); return s!; } s = Constructor.Active.ConstructSpecies(ptr)!; _idCache.Add(id, s!); return s; } public Species? FindPreEvolution(Species species) { var outPtr = IntPtr.Zero; Pkmnlib.Generated.SpeciesLibrary.FindPreEvolution(ref outPtr, Ptr, species.Ptr).Assert(); if (outPtr == IntPtr.Zero) return null; return TryResolvePointer(outPtr, out Species? prevoSpecies) ? prevoSpecies : Constructor.Active.ConstructSpecies(outPtr); } public IEnumerable GetEnumerator() { var count = Creaturelib.Generated.SpeciesLibrary.GetCount(Ptr); var ptr = IntPtr.Zero; for (ulong i = 0; i < count; i++) { Creaturelib.Generated.SpeciesLibrary.GetAtIndex(Ptr, i, ref ptr).Assert(); if (TryResolvePointer(ptr, out Species? species)) yield return species!; else yield return Constructor.Active.ConstructSpecies(ptr)!; } } public IEnumerable GetCachedIdEnumerator() { return _idCache.Keys; } internal SpeciesLibrary(IntPtr ptr) : base(ptr) { } public SpeciesLibrary(ulong initialCapacity) : base( Pkmnlib.Generated.SpeciesLibrary.Construct(initialCapacity)) { } protected override void DeletePtr() { Creaturelib.Generated.SpeciesLibrary.Destruct(Ptr); } protected internal override void MarkAsDeleted() { base.MarkAsDeleted(); foreach (var species in _cache) { species.Value.MarkAsDeleted(); } } } }