2020-05-05 19:40:04 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using PkmnLibSharp.Utilities;
|
|
|
|
|
2020-05-06 12:40:29 +00:00
|
|
|
namespace PkmnLibSharp.Library.Items
|
2020-05-05 19:40:04 +00:00
|
|
|
{
|
|
|
|
public class ItemLibrary : PointerWrapper
|
|
|
|
{
|
2020-07-18 14:49:11 +00:00
|
|
|
private readonly Dictionary<string, Item> _cache =
|
|
|
|
new Dictionary<string, Item>(StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
|
|
|
|
internal ItemLibrary(IntPtr ptr) : base(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemLibrary(ulong initialCapacity) : base(
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.ItemLibrary.Construct(initialCapacity))
|
2020-07-18 14:49:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-31 12:19:21 +00:00
|
|
|
public ulong Count => Creaturelib.Generated.ItemLibrary.GetCount(Ptr);
|
2020-05-05 19:40:04 +00:00
|
|
|
|
|
|
|
public void Insert(string key, Item item)
|
|
|
|
{
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.ItemLibrary.Insert(Ptr, key.ToPtr(), item.Ptr).Assert();
|
2020-05-05 19:40:04 +00:00
|
|
|
_cache.Add(key, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(string key)
|
|
|
|
{
|
|
|
|
_cache.Remove(key);
|
2022-03-12 09:32:31 +00:00
|
|
|
Creaturelib.Generated.ItemLibrary.Delete(Ptr, key.ToPtr()).Assert();
|
2020-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 12:32:27 +00:00
|
|
|
public bool TryGet(string key, out Item? item)
|
2020-05-05 19:40:04 +00:00
|
|
|
{
|
|
|
|
if (_cache.TryGetValue(key, out item))
|
|
|
|
return true;
|
|
|
|
var ptr = IntPtr.Zero;
|
2020-07-31 12:19:21 +00:00
|
|
|
if (Creaturelib.Generated.ItemLibrary.TryGet(Ptr, key.ToPtr(), ref ptr) != MarshalHelper.True)
|
2020-05-05 19:40:04 +00:00
|
|
|
return false;
|
|
|
|
if (TryResolvePointer(ptr, out item))
|
|
|
|
{
|
2020-08-08 12:32:27 +00:00
|
|
|
_cache.Add(key, item!);
|
2020-05-05 19:40:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-18 14:49:11 +00:00
|
|
|
|
2020-08-22 14:42:56 +00:00
|
|
|
item = Constructor.Active.ConstructItem(ptr)!;
|
2020-05-05 19:40:04 +00:00
|
|
|
_cache.Add(key, item);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Item Get(string key)
|
|
|
|
{
|
|
|
|
if (_cache.TryGetValue(key, out var item))
|
|
|
|
return item;
|
|
|
|
var ptr = IntPtr.Zero;
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.ItemLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
|
2020-08-08 12:32:27 +00:00
|
|
|
if (TryResolvePointer(ptr, out Item? i))
|
2020-05-05 19:40:04 +00:00
|
|
|
{
|
2020-08-08 12:32:27 +00:00
|
|
|
_cache.Add(key, i!);
|
|
|
|
return i!;
|
2020-05-05 19:40:04 +00:00
|
|
|
}
|
2020-07-18 14:49:11 +00:00
|
|
|
|
2020-08-22 14:42:56 +00:00
|
|
|
item = Constructor.Active.ConstructItem(ptr)!;
|
2020-05-05 19:40:04 +00:00
|
|
|
_cache.Add(key, item);
|
|
|
|
return item;
|
|
|
|
}
|
2020-08-10 17:13:17 +00:00
|
|
|
|
|
|
|
public IEnumerable<Item> GetEnumerator()
|
|
|
|
{
|
|
|
|
var count = Creaturelib.Generated.ItemLibrary.GetCount(Ptr);
|
|
|
|
var ptr = IntPtr.Zero;
|
|
|
|
for (ulong i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
Creaturelib.Generated.ItemLibrary.GetAtIndex(Ptr, i, ref ptr).Assert();
|
|
|
|
if (TryResolvePointer(ptr, out Item? item))
|
|
|
|
yield return item!;
|
2020-08-22 14:42:56 +00:00
|
|
|
else
|
|
|
|
yield return Constructor.Active.ConstructItem(ptr)!;
|
2020-08-10 17:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:40:04 +00:00
|
|
|
|
2020-05-20 12:47:17 +00:00
|
|
|
protected internal override void MarkAsDeleted()
|
|
|
|
{
|
|
|
|
base.MarkAsDeleted();
|
2020-07-18 14:49:11 +00:00
|
|
|
foreach (var item in _cache) item.Value.MarkAsDeleted();
|
2020-05-20 12:47:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 19:40:04 +00:00
|
|
|
protected override void DeletePtr()
|
|
|
|
{
|
2020-07-31 12:19:21 +00:00
|
|
|
Creaturelib.Generated.ItemLibrary.Destruct(Ptr);
|
2020-05-05 19:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|