Implemented MoveLibrary

This commit is contained in:
Deukhoofd 2020-05-03 23:34:28 +02:00
parent 42ec208425
commit 0a5a78bc13
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pkmn/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -46,7 +46,7 @@ namespace PkmnLibSharp.Library
return new MoveData(ptr); return new MoveData(ptr);
} }
private MoveData(IntPtr ptr) : base(ptr) internal MoveData(IntPtr ptr) : base(ptr)
{ {
} }

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using Creatureliblibrary.Generated;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library
{
public class MoveLibrary : PointerWrapper
{
private readonly Dictionary<string, MoveData> _cache = new Dictionary<string, MoveData>(StringComparer.InvariantCultureIgnoreCase);
public ulong Count => AttackLibrary.GetCount(Ptr);
public void Insert(string key, MoveData move)
{
AttackLibrary.Insert(Ptr, key.ToPtr(), move.Ptr).Assert();
_cache.Add(key, move);
}
public void Delete(string key)
{
AttackLibrary.Delete(Ptr, key.ToPtr()).Assert();
_cache.Remove(key);
}
public bool TryGet(string key, out MoveData move)
{
if (_cache.TryGetValue(key, out move))
return true;
var ptr = IntPtr.Zero;
if (AttackLibrary.TryGet(Ptr, key.ToPtr(), ref ptr) != MarshalHelper.True)
return false;
if (TryResolvePointer(ptr, out move))
{
_cache.Add(key, move);
return true;
}
move = new MoveData(ptr);
_cache.Add(key, move);
return true;
}
public MoveData Get(string key)
{
if (_cache.TryGetValue(key, out var move))
return move;
var ptr = IntPtr.Zero;
AttackLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
if (TryResolvePointer(ptr, out move))
{
_cache.Add(key, move);
return move;
}
move = new MoveData(ptr);
_cache.Add(key, move);
return move;
}
public static MoveLibrary Create(ulong defaultCapacity)
{
var ptr = IntPtr.Zero;
AttackLibrary.Construct(ref ptr, defaultCapacity).Assert();
return new MoveLibrary(ptr);
}
private MoveLibrary(IntPtr ptr) : base(ptr)
{
}
protected override void DeletePtr()
{
AttackLibrary.Destruct(Ptr);
}
}
}