Nature Library, Nature wrapper and tests, cleanup to ensure no duplicate enums are visible outside the library.

This commit is contained in:
2020-05-06 14:40:29 +02:00
parent 8845f33d04
commit 95a2270ca3
78 changed files with 366 additions and 109 deletions

View File

@@ -0,0 +1,11 @@
namespace PkmnLibSharp.Library.Items
{
public enum BattleItemCategory
{
None = 0,
Healing = 1,
StatusHealing = 2,
Pokeball = 3,
MiscBattleItem = 4,
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Linq;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library.Items
{
public class Item : PointerWrapper
{
private string _name;
public string Name => _name ??= Creatureliblibrary.Generated.Item.GetName(Ptr).PtrString();
public ItemCategory Category => (ItemCategory) Creatureliblibrary.Generated.Item.GetCategory(Ptr);
public BattleItemCategory BattleCategory =>
(BattleItemCategory) Creatureliblibrary.Generated.Item.GetBattleCategory(Ptr);
public int Price => Creatureliblibrary.Generated.Item.GetPrice(Ptr);
public byte FlingPower => Pkmnlib.Generated.Item.GetFlingPower(Ptr);
public bool HasFlag(string s)
{
return Creatureliblibrary.Generated.Item.HasFlag(Ptr, s.ToPtr()) == MarshalHelper.True;
}
internal Item(IntPtr ptr) : base(ptr)
{
}
public static Item Create(string name, ItemCategory category, BattleItemCategory battleCategory,
int price,
string[] flags, byte flingPower)
{
var convertedFlags = flags.Select(x => x.ToPtr()).ToArray().ArrayPtr();
var p = Pkmnlib.Generated.Item.Construct(name.ToPtr(), (Pkmnlib.ItemCategory) category,
(Pkmnlib.BattleItemCategory) battleCategory, price, convertedFlags, (ulong) flags.Length, flingPower);
return new Item(p);
}
protected override void DeletePtr()
{
Pkmnlib.Generated.Item.Destruct(Ptr);
}
}
}

View File

@@ -0,0 +1,14 @@
namespace PkmnLibSharp.Library
{
public enum ItemCategory
{
MiscItem = 0,
Pokeball = 1,
Medicine = 2,
Berry = 3,
TechnicalMachine = 4,
FormeChanger = 5,
KeyItem = 6,
Mail = 7,
}
}

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library.Items
{
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);
}
}
}