Implements Formes iterator.
This commit is contained in:
@@ -15,6 +15,27 @@ namespace PkmnLibSharp.Library
|
||||
private LearnableMoves? _moves;
|
||||
private string? _name;
|
||||
private ReadOnlyArray<byte>? _types;
|
||||
|
||||
public Forme(string name, float height, float weight, uint baseExperience, byte[] types,
|
||||
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
|
||||
ushort baseSpecialDefense, ushort baseSpeed, IReadOnlyCollection<string> abilities,
|
||||
IReadOnlyCollection<string> hiddenAbilities,
|
||||
LearnableMoves moves)
|
||||
{
|
||||
var abilitiesConverted = abilities.Select(x => x.ToPtr()).ToArray();
|
||||
var hiddenAbilitiesConverted = hiddenAbilities.Select(x => x.ToPtr()).ToArray();
|
||||
var ab = abilitiesConverted.ArrayPtr();
|
||||
var hab = hiddenAbilitiesConverted.ArrayPtr();
|
||||
var ptr = SpeciesVariant.Construct(name.ToPtr(), height, weight, baseExperience, types.ArrayPtr(),
|
||||
(ulong) types.Length, baseHealth, baseAttack, baseDefense, baseSpecialAttack,
|
||||
baseSpecialDefense, baseSpeed, ab, (ulong) abilities.Count, hab,
|
||||
(ulong) hiddenAbilities.Count, moves.Ptr);
|
||||
foreach (var intPtr in abilitiesConverted)
|
||||
Marshal.FreeHGlobal(intPtr);
|
||||
foreach (var intPtr in hiddenAbilitiesConverted)
|
||||
Marshal.FreeHGlobal(intPtr);
|
||||
Initialize(ptr);
|
||||
}
|
||||
|
||||
internal Forme(IntPtr parent) : base(parent)
|
||||
{
|
||||
@@ -112,29 +133,7 @@ namespace PkmnLibSharp.Library
|
||||
{
|
||||
return Types[index];
|
||||
}
|
||||
|
||||
public Forme(string name, float height, float weight, uint baseExperience, byte[] types,
|
||||
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
|
||||
ushort baseSpecialDefense, ushort baseSpeed, IReadOnlyCollection<string> abilities,
|
||||
IReadOnlyCollection<string> hiddenAbilities,
|
||||
LearnableMoves moves)
|
||||
{
|
||||
var abilitiesConverted = abilities.Select(x => x.ToPtr()).ToArray();
|
||||
var hiddenAbilitiesConverted = hiddenAbilities.Select(x => x.ToPtr()).ToArray();
|
||||
var ab = abilitiesConverted.ArrayPtr();
|
||||
var hab = hiddenAbilitiesConverted.ArrayPtr();
|
||||
var ptr = SpeciesVariant.Construct(name.ToPtr(), height, weight, baseExperience, types.ArrayPtr(),
|
||||
(ulong) types.Length, baseHealth, baseAttack, baseDefense, baseSpecialAttack,
|
||||
baseSpecialDefense, baseSpeed, ab, (ulong) abilities.Count, hab,
|
||||
(ulong) hiddenAbilities.Count, moves.Ptr);
|
||||
var f = new Forme(ptr);
|
||||
foreach (var intPtr in abilitiesConverted)
|
||||
Marshal.FreeHGlobal(intPtr);
|
||||
foreach (var intPtr in hiddenAbilitiesConverted)
|
||||
Marshal.FreeHGlobal(intPtr);
|
||||
Initialize(ptr);
|
||||
}
|
||||
|
||||
|
||||
public byte GetRandomAbility(Random rand)
|
||||
{
|
||||
return SpeciesVariant.GetRandomTalent(Ptr, rand.Ptr);
|
||||
|
||||
@@ -15,12 +15,13 @@ namespace PkmnLibSharp.Library
|
||||
}
|
||||
|
||||
public Species(ushort id, string name, Forme defaultForme, float genderRatio, string growthRate,
|
||||
byte captureRate, byte baseHappiness)
|
||||
byte captureRate, byte baseHappiness, string[] eggGroups)
|
||||
{
|
||||
var ptr = IntPtr.Zero;
|
||||
var eggGroupsPtr = eggGroups.ArrayPtr();
|
||||
PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio,
|
||||
growthRate.ToPtr(), captureRate, baseHappiness).Assert();
|
||||
_formes.Add("default", defaultForme);
|
||||
growthRate.ToPtr(), captureRate, baseHappiness, eggGroupsPtr, (ulong) eggGroups.Length).Assert();
|
||||
_formeCache.Add("default", defaultForme);
|
||||
Initialize(ptr);
|
||||
}
|
||||
|
||||
@@ -30,6 +31,18 @@ namespace PkmnLibSharp.Library
|
||||
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString()!;
|
||||
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString()!;
|
||||
|
||||
public ReadOnlyNativePtrArray<Forme> Formes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_formes != null) return _formes;
|
||||
var startPtr = CreatureSpecies.GetVariants(Ptr);
|
||||
var size = CreatureSpecies.GetVariantsCount(Ptr);
|
||||
_formes = new ReadOnlyNativePtrArray<Forme>(startPtr, (int) size);
|
||||
return _formes;
|
||||
}
|
||||
}
|
||||
|
||||
public ReadOnlyNativePtrArray<EvolutionData> Evolutions
|
||||
{
|
||||
get
|
||||
@@ -50,19 +63,19 @@ namespace PkmnLibSharp.Library
|
||||
|
||||
public bool TryGetForme(string s, out Forme? forme)
|
||||
{
|
||||
if (_formes.TryGetValue(s, out forme))
|
||||
if (_formeCache.TryGetValue(s, out forme))
|
||||
return true;
|
||||
var ptr = IntPtr.Zero;
|
||||
if (CreatureSpecies.TryGetVariant(Ptr, s.ToPtr(), ref ptr) == MarshalHelper.True)
|
||||
{
|
||||
if (TryResolvePointer(ptr, out forme))
|
||||
{
|
||||
_formes.Add(s, forme!);
|
||||
_formeCache.Add(s, forme!);
|
||||
return true;
|
||||
}
|
||||
|
||||
forme = new Forme(ptr);
|
||||
_formes.Add(s, forme);
|
||||
_formeCache.Add(s, forme);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -71,23 +84,23 @@ namespace PkmnLibSharp.Library
|
||||
|
||||
public Forme GetForme(string s)
|
||||
{
|
||||
if (_formes.TryGetValue(s, out var forme))
|
||||
if (_formeCache.TryGetValue(s, out var forme))
|
||||
return forme;
|
||||
var ptr = IntPtr.Zero;
|
||||
CreatureSpecies.GetVariant(ref ptr, Ptr, s.ToPtr()).Assert();
|
||||
if (TryResolvePointer(ptr, out Forme? f))
|
||||
{
|
||||
_formes.Add(s, f!);
|
||||
_formeCache.Add(s, f!);
|
||||
return f!;
|
||||
}
|
||||
forme = new Forme(ptr);
|
||||
_formes.Add(s, forme);
|
||||
_formeCache.Add(s, forme);
|
||||
return forme;
|
||||
}
|
||||
|
||||
public void SetForme(string s, Forme forme)
|
||||
{
|
||||
_formes.Add(s, forme);
|
||||
_formeCache.Add(s, forme);
|
||||
CreatureSpecies.SetVariant(Ptr, s.ToPtr(), forme.Ptr).Assert();
|
||||
}
|
||||
|
||||
@@ -104,15 +117,16 @@ namespace PkmnLibSharp.Library
|
||||
private string? _name;
|
||||
private string? _growthRate;
|
||||
|
||||
private readonly Dictionary<string, Forme> _formes =
|
||||
private readonly Dictionary<string, Forme> _formeCache =
|
||||
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
|
||||
|
||||
private ReadOnlyNativePtrArray<EvolutionData>? _evolutions;
|
||||
private ReadOnlyNativePtrArray<Forme>? _formes;
|
||||
|
||||
protected internal override void MarkAsDeleted()
|
||||
{
|
||||
base.MarkAsDeleted();
|
||||
foreach (var forme in _formes)
|
||||
foreach (var forme in _formeCache)
|
||||
{
|
||||
forme.Value.MarkAsDeleted();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user