Added ItemLibrary Wrapper
This commit is contained in:
parent
6491360dbe
commit
bf158802d7
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
using NUnit.Framework;
|
||||
using Pkmnlib;
|
||||
using PkmnLibSharp.Library;
|
||||
using PkmnLibSharp.Utilities;
|
||||
|
||||
namespace PkmnLibSharpTests.Library
|
||||
{
|
||||
public class ItemLibraryTests
|
||||
{
|
||||
[Test]
|
||||
public void ConstructDestruct()
|
||||
{
|
||||
var item = new ItemLibrary(100);
|
||||
item.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Insert()
|
||||
{
|
||||
var item = Item.Create("testItem", ItemCategory.MiscItem, BattleItemCategory.Healing, 1000,
|
||||
new[] {"testFlag"}, 50);
|
||||
var library = new ItemLibrary(100);
|
||||
library.Insert("foobar", item);
|
||||
Assert.AreEqual(1, library.Count);
|
||||
library.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Delete()
|
||||
{
|
||||
var item = Item.Create("testItem", ItemCategory.MiscItem, BattleItemCategory.Healing, 1000,
|
||||
new[] {"testFlag"}, 50);
|
||||
var library = new ItemLibrary(100);
|
||||
library.Insert("foobar", item);
|
||||
Assert.AreEqual(1, library.Count);
|
||||
library.Delete("foobar");
|
||||
Assert.AreEqual(0, library.Count);
|
||||
library.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Get()
|
||||
{
|
||||
var item = Item.Create("testItem", ItemCategory.MiscItem, BattleItemCategory.Healing, 1000,
|
||||
new[] {"testFlag"}, 50);
|
||||
var library = new ItemLibrary(100);
|
||||
library.Insert("foobar", item);
|
||||
Assert.AreEqual(1, library.Count);
|
||||
var m = library.Get("foobar");
|
||||
Assert.AreEqual(m.Name, "testItem");
|
||||
Assert.Throws<NativeException>(() =>
|
||||
{
|
||||
library.Get("barfoo");
|
||||
});
|
||||
library.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryGet()
|
||||
{
|
||||
var item = Item.Create("testItem", ItemCategory.MiscItem, BattleItemCategory.Healing, 1000,
|
||||
new[] {"testFlag"}, 50);
|
||||
var library = new ItemLibrary(100);
|
||||
library.Insert("foobar", item);
|
||||
Assert.AreEqual(1, library.Count);
|
||||
Assert.True(library.TryGet("foobar", out item));
|
||||
Assert.AreEqual(item.Name, "testItem");
|
||||
Assert.False(library.TryGet("barfoo", out item));
|
||||
library.Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue