Added ItemLibrary Wrapper
This commit is contained in:
68
PkmnLibSharp/Library/ItemLibrary.cs
Normal file
68
PkmnLibSharp/Library/ItemLibrary.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using PkmnLibSharp.Utilities;
|
||||
|
||||
namespace PkmnLibSharp.Library
|
||||
{
|
||||
public class ItemLibrary : PointerWrapper
|
||||
{
|
||||
private readonly Dictionary<string, Item> _cache = new Dictionary<string, Item>(StringComparer.InvariantCultureIgnoreCase);
|
||||
public ulong Count => Creatureliblibrary.Generated.ItemLibrary.GetCount(Ptr);
|
||||
|
||||
public void Insert(string key, Item item)
|
||||
{
|
||||
Creatureliblibrary.Generated.ItemLibrary.Insert(Ptr, key.ToPtr(), item.Ptr).Assert();
|
||||
_cache.Add(key, item);
|
||||
}
|
||||
|
||||
public void Delete(string key)
|
||||
{
|
||||
Creatureliblibrary.Generated.ItemLibrary.Delete(Ptr, key.ToPtr()).Assert();
|
||||
_cache.Remove(key);
|
||||
}
|
||||
|
||||
public bool TryGet(string key, out Item item)
|
||||
{
|
||||
if (_cache.TryGetValue(key, out item))
|
||||
return true;
|
||||
var ptr = IntPtr.Zero;
|
||||
if (Creatureliblibrary.Generated.ItemLibrary.TryGet(Ptr, key.ToPtr(), ref ptr) != MarshalHelper.True)
|
||||
return false;
|
||||
if (TryResolvePointer(ptr, out item))
|
||||
{
|
||||
_cache.Add(key, item);
|
||||
return true;
|
||||
}
|
||||
item = new Item(ptr);
|
||||
_cache.Add(key, item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Item Get(string key)
|
||||
{
|
||||
if (_cache.TryGetValue(key, out var item))
|
||||
return item;
|
||||
var ptr = IntPtr.Zero;
|
||||
Creatureliblibrary.Generated.ItemLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
|
||||
if (TryResolvePointer(ptr, out item))
|
||||
{
|
||||
_cache.Add(key, item);
|
||||
return item;
|
||||
}
|
||||
item = new Item(ptr);
|
||||
_cache.Add(key, item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public ItemLibrary(ulong initialCapacity) : base(
|
||||
Creatureliblibrary.Generated.ItemLibrary.Construct(initialCapacity))
|
||||
{
|
||||
}
|
||||
|
||||
protected override void DeletePtr()
|
||||
{
|
||||
Creatureliblibrary.Generated.ItemLibrary.Destruct(Ptr);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user