Fixed bug with libraries returning some values twice in GetEnumerator, move more over to Constructor class.

This commit is contained in:
Deukhoofd 2020-08-22 16:42:56 +02:00
parent 59a5ddf5da
commit a2cf693bbd
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
8 changed files with 34 additions and 21 deletions

View File

@ -25,7 +25,7 @@ namespace PkmnLibSharp.Battling
var ptr = LearnedAttack.GetAttack(Ptr); var ptr = LearnedAttack.GetAttack(Ptr);
if (TryResolvePointer(ptr, out _move)) if (TryResolvePointer(ptr, out _move))
return _move!; return _move!;
_move = new MoveData(ptr); _move = Constructor.Active.ConstructMove(ptr)!;
return _move; return _move;
} }
} }

View File

@ -149,7 +149,7 @@ namespace PkmnLibSharp.Battling
if (ptr == IntPtr.Zero) return null; if (ptr == IntPtr.Zero) return null;
if (!TryResolvePointer(ptr, out Item? item)) if (!TryResolvePointer(ptr, out Item? item))
{ {
item = new Item(ptr); item = Constructor.Active.ConstructItem(ptr);
} }
return item; return item;
} }

View File

@ -8,10 +8,6 @@ namespace PkmnLibSharp.Library.Items
{ {
private string? _name; private string? _name;
internal Item(IntPtr ptr) : base(ptr)
{
}
public string Name => _name ??= Creaturelib.Generated.Item.GetName(Ptr).PtrString()!; public string Name => _name ??= Creaturelib.Generated.Item.GetName(Ptr).PtrString()!;
public ItemCategory Category => (ItemCategory) Creaturelib.Generated.Item.GetCategory(Ptr); public ItemCategory Category => (ItemCategory) Creaturelib.Generated.Item.GetCategory(Ptr);

View File

@ -45,7 +45,7 @@ namespace PkmnLibSharp.Library.Items
return true; return true;
} }
item = new Item(ptr); item = Constructor.Active.ConstructItem(ptr)!;
_cache.Add(key, item); _cache.Add(key, item);
return true; return true;
} }
@ -62,7 +62,7 @@ namespace PkmnLibSharp.Library.Items
return i!; return i!;
} }
item = new Item(ptr); item = Constructor.Active.ConstructItem(ptr)!;
_cache.Add(key, item); _cache.Add(key, item);
return item; return item;
} }
@ -76,7 +76,8 @@ namespace PkmnLibSharp.Library.Items
Creaturelib.Generated.ItemLibrary.GetAtIndex(Ptr, i, ref ptr).Assert(); Creaturelib.Generated.ItemLibrary.GetAtIndex(Ptr, i, ref ptr).Assert();
if (TryResolvePointer(ptr, out Item? item)) if (TryResolvePointer(ptr, out Item? item))
yield return item!; yield return item!;
yield return new Item(ptr); else
yield return Constructor.Active.ConstructItem(ptr)!;
} }
} }

View File

@ -11,10 +11,6 @@ namespace PkmnLibSharp.Library.Moves
private string? _name; private string? _name;
private string? _secondaryEffectName; private string? _secondaryEffectName;
internal MoveData(IntPtr ptr) : base(ptr)
{
}
public string Name => _name ??= AttackData.GetName(Ptr).PtrString()!; public string Name => _name ??= AttackData.GetName(Ptr).PtrString()!;
public byte Type => AttackData.GetType(Ptr); public byte Type => AttackData.GetType(Ptr);
public MoveCategory Category => (MoveCategory) AttackData.GetCategory(Ptr); public MoveCategory Category => (MoveCategory) AttackData.GetCategory(Ptr);

View File

@ -41,7 +41,7 @@ namespace PkmnLibSharp.Library.Moves
return true; return true;
} }
move = new MoveData(ptr); move = Constructor.Active.ConstructMove(ptr)!;
_cache.Add(key, move); _cache.Add(key, move);
return true; return true;
} }
@ -58,7 +58,7 @@ namespace PkmnLibSharp.Library.Moves
return m!; return m!;
} }
move = new MoveData(ptr); move = Constructor.Active.ConstructMove(ptr)!;
_cache.Add(key, move); _cache.Add(key, move);
return move; return move;
} }
@ -79,7 +79,8 @@ namespace PkmnLibSharp.Library.Moves
AttackLibrary.GetAtIndex(Ptr, i, ref ptr).Assert(); AttackLibrary.GetAtIndex(Ptr, i, ref ptr).Assert();
if (TryResolvePointer(ptr, out MoveData? move)) if (TryResolvePointer(ptr, out MoveData? move))
yield return move!; yield return move!;
yield return new MoveData(ptr); else
yield return Constructor.Active.ConstructMove(ptr)!;
} }
} }

View File

@ -83,6 +83,7 @@ namespace PkmnLibSharp.Library
Creaturelib.Generated.SpeciesLibrary.GetAtIndex(Ptr, i, ref ptr).Assert(); Creaturelib.Generated.SpeciesLibrary.GetAtIndex(Ptr, i, ref ptr).Assert();
if (TryResolvePointer(ptr, out Species? species)) if (TryResolvePointer(ptr, out Species? species))
yield return species!; yield return species!;
else
yield return Constructor.Active.ConstructSpecies(ptr)!; yield return Constructor.Active.ConstructSpecies(ptr)!;
} }
} }

View File

@ -2,27 +2,33 @@ using System;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using PkmnLibSharp.Battling; using PkmnLibSharp.Battling;
using PkmnLibSharp.Library; using PkmnLibSharp.Library;
using PkmnLibSharp.Library.Items;
using PkmnLibSharp.Library.Moves;
namespace PkmnLibSharp.Utilities namespace PkmnLibSharp.Utilities
{ {
public abstract class Constructor public abstract class Constructor
{ {
public static Constructor Active { get; set; } = public static Constructor Active { get; set; } =
new Constructor<Species, Forme, LearnableMoves, Pokemon, Battle>(); new Constructor<Species, Forme, MoveData, Item, LearnableMoves, Pokemon, Battle>();
internal abstract Species? ConstructSpecies(IntPtr ptr); internal abstract Species? ConstructSpecies(IntPtr ptr);
internal abstract Forme? ConstructForme(IntPtr ptr); internal abstract Forme? ConstructForme(IntPtr ptr);
internal abstract MoveData? ConstructMove(IntPtr ptr);
internal abstract Item? ConstructItem(IntPtr ptr);
internal abstract LearnableMoves? ConstructLearnableMoves(IntPtr ptr); internal abstract LearnableMoves? ConstructLearnableMoves(IntPtr ptr);
internal abstract Pokemon? ConstructPokemon(IntPtr ptr); internal abstract Pokemon? ConstructPokemon(IntPtr ptr);
internal abstract Battle? ConstructBattle(IntPtr ptr); internal abstract Battle? ConstructBattle(IntPtr ptr);
} }
public class Constructor< public class Constructor<
TSpecies, TForme, TLearnableMoves, TSpecies, TForme, TMoveData, TItem, TLearnableMoves,
TPokemon, TBattle> TPokemon, TBattle>
: Constructor : Constructor
where TSpecies : Species where TSpecies : Species
where TForme : Forme where TForme : Forme
where TMoveData : MoveData
where TItem: Item
where TLearnableMoves : LearnableMoves where TLearnableMoves : LearnableMoves
where TPokemon : Pokemon where TPokemon : Pokemon
@ -30,6 +36,8 @@ namespace PkmnLibSharp.Utilities
{ {
private readonly Type _speciesType = typeof(TSpecies); private readonly Type _speciesType = typeof(TSpecies);
private readonly Type _formeType = typeof(TForme); private readonly Type _formeType = typeof(TForme);
private readonly Type _moveDataType = typeof(TMoveData);
private readonly Type _itemType = typeof(TItem);
private readonly Type _learnableMovesType = typeof(TLearnableMoves); private readonly Type _learnableMovesType = typeof(TLearnableMoves);
private readonly Type _pokemonType = typeof(TPokemon); private readonly Type _pokemonType = typeof(TPokemon);
@ -53,6 +61,16 @@ namespace PkmnLibSharp.Utilities
return Create<TForme>(ptr, _formeType); return Create<TForme>(ptr, _formeType);
} }
internal override MoveData? ConstructMove(IntPtr ptr)
{
return Create<TMoveData>(ptr, _moveDataType);
}
internal override Item? ConstructItem(IntPtr ptr)
{
return Create<TItem>(ptr, _itemType);
}
internal override LearnableMoves? ConstructLearnableMoves(IntPtr ptr) internal override LearnableMoves? ConstructLearnableMoves(IntPtr ptr)
{ {
return Create<TLearnableMoves>(ptr, _learnableMovesType); return Create<TLearnableMoves>(ptr, _learnableMovesType);