Nature Library, Nature wrapper and tests, cleanup to ensure no duplicate enums are visible outside the library.
This commit is contained in:
11
PkmnLibSharp/Library/Items/BattleItemCategory.cs
Normal file
11
PkmnLibSharp/Library/Items/BattleItemCategory.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace PkmnLibSharp.Library.Items
|
||||
{
|
||||
public enum BattleItemCategory
|
||||
{
|
||||
None = 0,
|
||||
Healing = 1,
|
||||
StatusHealing = 2,
|
||||
Pokeball = 3,
|
||||
MiscBattleItem = 4,
|
||||
}
|
||||
}
|
||||
44
PkmnLibSharp/Library/Items/Item.cs
Normal file
44
PkmnLibSharp/Library/Items/Item.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
PkmnLibSharp/Library/Items/ItemCategory.cs
Normal file
14
PkmnLibSharp/Library/Items/ItemCategory.cs
Normal 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,
|
||||
}
|
||||
}
|
||||
68
PkmnLibSharp/Library/Items/ItemLibrary.cs
Normal file
68
PkmnLibSharp/Library/Items/ItemLibrary.cs
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user