Remove public empty constructors for pointer arrays, implements basic evolutions.

This commit is contained in:
Deukhoofd 2020-08-08 15:24:14 +02:00
parent 01e622c22c
commit cecb1e9a83
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
17 changed files with 205 additions and 37 deletions

View File

@ -5,8 +5,6 @@ namespace PkmnLibSharp.Battling
{
public class BattleParty : PointerWrapper
{
public BattleParty(){}
public BattleParty(PokemonParty party, byte[] responsibleIndices)
{
var ptr = IntPtr.Zero;

View File

@ -5,7 +5,6 @@ namespace PkmnLibSharp.Battling
{
public class BattleSide : PointerWrapper
{
public BattleSide(){}
internal BattleSide(IntPtr ptr) : base(ptr){}
protected override void DeletePtr()

View File

@ -8,7 +8,6 @@ namespace PkmnLibSharp.Battling
{
public class LearnedMove : PointerWrapper
{
public LearnedMove(){}
internal LearnedMove(IntPtr ptr) : base(ptr){}
public LearnedMove(MoveData move, byte maxUses, MoveLearnMethod learnMethod)

View File

@ -11,12 +11,6 @@ namespace PkmnLibSharp.Battling
{
public class Pokemon : PointerWrapper
{
public Pokemon()
{
// Just here so BattleLibrary can be initialized
Library = new BattleLibrary(IntPtr.Zero);
}
internal Pokemon(IntPtr ptr) : base(ptr)
{
Library = new BattleLibrary(Creaturelib.Generated.Creature.GetLibrary(ptr));

View File

@ -30,5 +30,83 @@ namespace Pkmnlib.Generated
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateLocationEvolution")]
internal static extern IntPtr CreateLocationEvolution(IntPtr location, IntPtr into);
/// <param name="time">TimeOfDay</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateTimeEvolution")]
internal static extern IntPtr CreateTimeEvolution(TimeOfDay time, IntPtr into);
/// <param name="item">const Item *</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateItemEvolution")]
internal static extern IntPtr CreateItemEvolution(IntPtr item, IntPtr into);
/// <param name="gender">Gender</param>
/// <param name="level">unsigned char</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateGenderBasedEvolution")]
internal static extern IntPtr CreateGenderBasedEvolution(Gender gender, byte level, IntPtr into);
/// <param name="item">const Item *</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateItemUseEvolution")]
internal static extern IntPtr CreateItemUseEvolution(IntPtr item, IntPtr into);
/// <param name="item">const Item *</param>
/// <param name="gender">Gender</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateItemUseWithGenderEvolution")]
internal static extern IntPtr CreateItemUseWithGenderEvolution(IntPtr item, Gender gender, IntPtr into);
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateTradeEvolution")]
internal static extern IntPtr CreateTradeEvolution(IntPtr into);
/// <param name="item">const Item *</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateTradeWithItemEvolution")]
internal static extern IntPtr CreateTradeWithItemEvolution(IntPtr item, IntPtr into);
/// <param name="traded">const PokemonSpecies *</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateTradeWithSpeciesEvolution")]
internal static extern IntPtr CreateTradeWithSpeciesEvolution(IntPtr traded, IntPtr into);
/// <param name="data">const EffectParameter * *</param>
/// <param name="dataLength">long unsigned int</param>
/// <param name="into">const PokemonSpecies *</param>
/// <returns>const EvolutionData *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_CreateCustomEvolution")]
internal static extern IntPtr CreateCustomEvolution(IntPtr data, ulong dataLength, IntPtr into);
/// <param name="data">const EvolutionData *</param>
/// <returns>EvolutionMethod</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_GetMethod")]
internal static extern EvolutionMethod GetMethod(IntPtr data);
/// <param name="data">const EvolutionData *</param>
/// <returns>const PokemonSpecies *</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_GetNewSpecies")]
internal static extern IntPtr GetNewSpecies(IntPtr data);
/// <param name="data">const EvolutionData *</param>
/// <returns>long unsigned int</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_GetDataCount")]
internal static extern ulong GetDataCount(IntPtr data);
/// <param name="data">const EvolutionData *</param>
/// <param name="index">long unsigned int</param>
/// <param name="out">const EffectParameter * &</param>
/// <returns>unsigned char</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_EvolutionData_GetData")]
internal static extern byte GetData(IntPtr data, ulong index, ref IntPtr @out);
}
}

View File

@ -46,5 +46,11 @@ namespace Pkmnlib.Generated
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_PokemonSpecies_GetEvolution")]
internal static extern byte GetEvolution(IntPtr p, ulong index, ref IntPtr @out);
/// <param name="p">const PokemonSpecies *</param>
/// <param name="out">const const EvolutionData * * &</param>
/// <returns>unsigned char</returns>
[DllImport("pkmnLib", CallingConvention = CallingConvention.Cdecl, EntryPoint= "PkmnLib_PokemonSpecies_GetEvolutions")]
internal static extern byte GetEvolutions(IntPtr p, ref IntPtr @out);
}
}

View File

@ -15,6 +15,8 @@ namespace PkmnLibSharp.Library
public class EffectParameter : PointerWrapper
{
internal EffectParameter(IntPtr ptr) : base(ptr){}
public EffectParameter(string s) : base(Creaturelib.Generated.EffectParameter.FromString(s.ToPtr()))
{
}

View File

@ -8,11 +8,7 @@ namespace PkmnLibSharp.Library
public byte MaximalLevel => Creaturelib.Generated.LibrarySettings.GetMaximalLevel(Ptr);
public byte MaximalMoves => Creaturelib.Generated.LibrarySettings.GetMaximalMoves(Ptr);
public ushort ShinyRate => Pkmnlib.Generated.LibrarySettings.GetShinyRate(Ptr);
internal LibrarySettings()
{
}
internal LibrarySettings(IntPtr ptr) : base(ptr)
{
}

View File

@ -0,0 +1,49 @@
using System;
using PkmnLibSharp.Utilities;
namespace PkmnLibSharp.Library.Evolution
{
public class EvolutionData : PointerWrapper
{
private EvolutionData(IntPtr ptr) : base(ptr)
{
}
public static EvolutionData CreateLevelEvolution(byte level, Species into)
{
return new EvolutionData(Pkmnlib.Generated.EvolutionData.CreateLevelEvolution(level, into.Ptr));
}
public EvolutionMethod Method => (EvolutionMethod) Pkmnlib.Generated.EvolutionData.GetMethod(Ptr);
public Species NewSpecies
{
get
{
if (_species != null) return _species;
var ptr = Pkmnlib.Generated.EvolutionData.GetNewSpecies(Ptr);
if (TryResolvePointer(ptr, out _species))
return _species!;
_species = new Species(ptr);
return _species;
}
}
public ulong DataLength => Pkmnlib.Generated.EvolutionData.GetDataCount(Ptr);
public EffectParameter GetData(ulong index)
{
var ptr = IntPtr.Zero;
Pkmnlib.Generated.EvolutionData.GetData(Ptr, index, ref ptr).Assert();
if (TryResolvePointer(ptr, out EffectParameter? parameter))
return parameter!;
return new EffectParameter(ptr);
}
private Species? _species;
protected override void DeletePtr()
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,19 @@
namespace PkmnLibSharp.Library.Evolution
{
public enum EvolutionMethod
{
Level = 0,
HighFriendship = 1,
KnownMove = 2,
LocationBased = 3,
TimeBased = 4,
HoldsItem = 5,
IsGenderAndLevel = 6,
EvolutionItemUse = 7,
EvolutionItemUseWithGender = 8,
Trade = 9,
TradeWithHeldItem = 10,
TradeWithSpecificPokemon = 11,
Custom = 12,
}
}

View File

@ -4,6 +4,7 @@ using Creaturelib.Generated;
using Pkmnlib;
using Pkmnlib.Generated;
using PkmnLibSharp.Utilities;
using EvolutionData = PkmnLibSharp.Library.Evolution.EvolutionData;
using Gender = PkmnLibSharp.Battling.Gender;
using Random = PkmnLibSharp.Utilities.Random;
@ -11,11 +12,19 @@ namespace PkmnLibSharp.Library
{
public class Species : PointerWrapper
{
private string? _name;
private string? _growthRate;
internal Species(IntPtr ptr) : base(ptr)
{
}
private readonly Dictionary<string, Forme> _formes =
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
public Species(ushort id, string name, Forme defaultForme, float genderRatio, string growthRate,
byte captureRate, byte baseHappiness)
{
var ptr = IntPtr.Zero;
PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio,
growthRate.ToPtr(), captureRate, baseHappiness).Assert();
_formes.Add("default", defaultForme);
Initialize(ptr);
}
public ushort Id => CreatureSpecies.GetId(Ptr);
public float GenderRate => CreatureSpecies.GetGenderRate(Ptr);
@ -23,6 +32,19 @@ namespace PkmnLibSharp.Library
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString()!;
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString()!;
public ReadOnlyNativePtrArray<EvolutionData> Evolutions
{
get
{
if (_evolutions != null) return _evolutions;
var ptr = IntPtr.Zero;
PokemonSpecies.GetEvolutions(Ptr, ref ptr);
var length = PokemonSpecies.GetEvolutionCount(Ptr);
_evolutions = new ReadOnlyNativePtrArray<EvolutionData>(ptr, (int) length);
return _evolutions;
}
}
public bool HasForme(string s)
{
return CreatureSpecies.HasVariant(Ptr, s.ToPtr()) == MarshalHelper.True;
@ -76,20 +98,18 @@ namespace PkmnLibSharp.Library
return (Gender) CreatureSpecies.GetRandomGender(Ptr, random.Ptr);
}
internal Species(IntPtr ptr) : base(ptr)
public void AddEvolution(EvolutionData evolutionData)
{
Pkmnlib.Generated.PokemonSpecies.AddEvolution(Ptr, evolutionData.Ptr);
}
private string? _name;
private string? _growthRate;
public Species(ushort id, string name, Forme defaultForme, float genderRatio, string growthRate,
byte captureRate, byte baseHappiness)
{
var ptr = IntPtr.Zero;
PokemonSpecies.Construct(ref ptr, id, name.ToPtr(), defaultForme.Ptr, genderRatio,
growthRate.ToPtr(), captureRate, baseHappiness).Assert();
_formes.Add("default", defaultForme);
Initialize(ptr);
}
private readonly Dictionary<string, Forme> _formes =
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
private ReadOnlyNativePtrArray<EvolutionData>? _evolutions;
protected internal override void MarkAsDeleted()
{

BIN
PkmnLibSharp/Native/libpkmnLib.so (Stored with Git LFS)

Binary file not shown.

View File

@ -1,4 +1,5 @@
<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/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=battling_005Cbattle/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=battling_005Cchoiceturn/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=battling_005Clibrary/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=battling_005Clibrary/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=library_005Cspecies/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -1,17 +1,21 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace PkmnLibSharp.Utilities
{
public class ReadOnlyNativePtrArray<T> : IReadOnlyList<T> where T : PointerWrapper, new()
public class ReadOnlyNativePtrArray<T> : IReadOnlyList<T> where T : PointerWrapper
{
private readonly IntPtr _ptr;
private T?[] _cache;
private Type _type = typeof(T);
internal ReadOnlyNativePtrArray(IntPtr ptr, int length)
{
_ptr = ptr;
Count = length;
_cache = new T[Count];
}
public IEnumerator<T> GetEnumerator()
@ -45,7 +49,7 @@ namespace PkmnLibSharp.Utilities
public int Count { get; }
public int IndexOf(T item)
public int IndexOf(T? item)
{
for (var i = 0; i < Count; i++)
{
@ -67,10 +71,13 @@ namespace PkmnLibSharp.Utilities
// Where's your god now?
// (We add the offset of the index to the pointer, then dereference the pointer pointer to get the actual pointer to the object we want.)
var p = new IntPtr(*(void**)IntPtr.Add(_ptr, index * IntPtr.Size).ToPointer());
if (_cache[index]?.Ptr == p)
return _cache[index]!;
if (PointerWrapper.TryResolvePointer(p, out T? t))
return t!;
t = new T();
t = (T) FormatterServices.GetUninitializedObject(_type);
t.Initialize(p);
_cache[index] = t;
return t;
}
}

File diff suppressed because one or more lines are too long