Use fancy C# 8 Nullable Reference pattern.

This commit is contained in:
Deukhoofd 2020-08-08 14:32:27 +02:00
parent e0f4a38309
commit 01e622c22c
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
31 changed files with 159 additions and 159 deletions

View File

@ -32,7 +32,7 @@ namespace PkmnLibSharp.Battling
var ptr = Creaturelib.Generated.Battle.GetLibrary(Ptr);
if (TryResolvePointer(ptr, out _library))
{
return _library;
return _library!;
}
_library = new BattleLibrary(ptr);
return _library;
@ -47,7 +47,7 @@ namespace PkmnLibSharp.Battling
var ptr = Creaturelib.Generated.Battle.GetRandom(Ptr);
if (TryResolvePointer(ptr, out _random))
{
return _random;
return _random!;
}
_random = new BattleRandom(ptr);
return _random;
@ -85,7 +85,7 @@ namespace PkmnLibSharp.Battling
}
}
public string WeatherName => Pkmnlib.Generated.Battle.GetWeatherName(Ptr).PtrString();
public string? WeatherName => Pkmnlib.Generated.Battle.GetWeatherName(Ptr).PtrString();
public bool CanUse(BaseTurnChoice turnChoice)
@ -118,7 +118,7 @@ namespace PkmnLibSharp.Battling
{
var ptr = IntPtr.Zero;
Creaturelib.Generated.Battle.GetCreature(ref ptr, Ptr, side, index).Assert();
return TryResolvePointer(ptr, out Pokemon pokemon) ? pokemon : new Pokemon(ptr);
return TryResolvePointer(ptr, out Pokemon? pokemon) ? pokemon! : new Pokemon(ptr);
}
public void ForceRecall(byte side, byte index)
@ -146,9 +146,9 @@ namespace PkmnLibSharp.Battling
public Script GetVolatileScript(string key)
{
var ptr = Creaturelib.Generated.Battle.GetVolatileScript(Ptr, key.ToPtr());
if (TryResolvePointer(ptr, out Script script))
if (TryResolvePointer(ptr, out Script? script))
{
return script;
return script!;
}
// TODO: Handle with different script providers.
return new AngelscriptScript(ptr);
@ -196,10 +196,10 @@ namespace PkmnLibSharp.Battling
Pkmnlib.Generated.Battle.ClearWeather(Ptr);
}
private BattleLibrary _library;
private BattleRandom _random;
private ReadOnlyNativePtrArray<BattleSide> _sides;
private ReadOnlyNativePtrArray<BattleParty> _parties;
private BattleLibrary? _library;
private BattleRandom? _random;
private ReadOnlyNativePtrArray<BattleSide>? _sides;
private ReadOnlyNativePtrArray<BattleParty>? _parties;
protected override void DeletePtr()
{

View File

@ -15,7 +15,7 @@ namespace PkmnLibSharp.Battling
Initialize(ptr);
}
private PokemonParty _party;
private PokemonParty? _party;
public PokemonParty Party
{
get
@ -23,7 +23,7 @@ namespace PkmnLibSharp.Battling
if (_party != null) return _party;
var ptr = Creaturelib.Generated.BattleParty.GetParty(Ptr);
if (TryResolvePointer(ptr, out _party))
return _party;
return _party!;
_party = new PokemonParty(ptr);
return _party;
}

View File

@ -16,12 +16,12 @@ namespace PkmnLibSharp.Battling.ChoiceTurn
if (_user != null) return _user;
var ptr = Creaturelib.Generated.BaseTurnChoice.GetUser(Ptr);
if (TryResolvePointer(ptr, out _user))
return _user;
return _user!;
_user = new Pokemon(ptr);
return _user;
}
}
private Pokemon _user;
private Pokemon? _user;
}
}

View File

@ -16,7 +16,7 @@ namespace PkmnLibSharp.Battling.ChoiceTurn
if (_move != null) return _move;
var ptr = Creaturelib.Generated.AttackTurnChoice.GetAttack(Ptr);
if (TryResolvePointer(ptr, out _move))
return _move;
return _move!;
_move = new LearnedMove(ptr);
return _move;
}
@ -37,7 +37,7 @@ namespace PkmnLibSharp.Battling.ChoiceTurn
// TODO: Move Script getter
private LearnedMove _move;
private LearnedMove? _move;
protected override void DeletePtr()
{

View File

@ -14,7 +14,7 @@ namespace PkmnLibSharp.Battling.Events
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable
private readonly BattleEventPtrDelegate _innerDel;
internal readonly IntPtr FunctionPtr;
private Task _currentTask;
private Task? _currentTask;
public BattleEventListener(BattleEventDelegate del)
{

View File

@ -15,7 +15,7 @@ namespace PkmnLibSharp.Battling.Events
if (_pokemon != null) return _pokemon;
var ptr = Creaturelib.Generated.DamageEvent.GetCreature(Ptr);
if (TryResolvePointer(ptr, out _pokemon))
return _pokemon;
return _pokemon!;
_pokemon = new Pokemon(ptr);
return _pokemon;
}
@ -25,7 +25,7 @@ namespace PkmnLibSharp.Battling.Events
public uint OriginalHealth => Creaturelib.Generated.DamageEvent.GetOriginalHealth(Ptr);
public uint NewHealth => Creaturelib.Generated.DamageEvent.GetNewHealth(Ptr);
private Pokemon _pokemon;
private Pokemon? _pokemon;
protected override void DeletePtr()
{

View File

@ -15,7 +15,7 @@ namespace PkmnLibSharp.Battling.Events
if (_pokemon != null) return _pokemon;
var ptr = Creaturelib.Generated.ExperienceGainEvent.GetCreature(Ptr);
if (TryResolvePointer(ptr, out _pokemon))
return _pokemon;
return _pokemon!;
_pokemon = new Pokemon(ptr);
return _pokemon;
}
@ -25,7 +25,7 @@ namespace PkmnLibSharp.Battling.Events
private uint NewExperience => Creaturelib.Generated.ExperienceGainEvent.GetNewExperience(Ptr);
private Pokemon _pokemon;
private Pokemon? _pokemon;
protected override void DeletePtr()
{

View File

@ -15,14 +15,14 @@ namespace PkmnLibSharp.Battling.Events
if (_pokemon != null) return _pokemon;
var ptr = Creaturelib.Generated.DamageEvent.GetCreature(Ptr);
if (TryResolvePointer(ptr, out _pokemon))
return _pokemon;
return _pokemon!;
_pokemon = new Pokemon(ptr);
return _pokemon;
}
}
private Pokemon _pokemon;
private Pokemon? _pokemon;
protected override void DeletePtr()
{

View File

@ -15,7 +15,7 @@ namespace PkmnLibSharp.Battling.Events
if (_pokemon != null) return _pokemon;
var ptr = Creaturelib.Generated.HealEvent.GetCreature(Ptr);
if (TryResolvePointer(ptr, out _pokemon))
return _pokemon;
return _pokemon!;
_pokemon = new Pokemon(ptr);
return _pokemon;
}
@ -24,7 +24,7 @@ namespace PkmnLibSharp.Battling.Events
public uint OriginalHealth => Creaturelib.Generated.HealEvent.GetOriginalHealth(Ptr);
public uint NewHealth => Creaturelib.Generated.HealEvent.GetNewHealth(Ptr);
private Pokemon _pokemon;
private Pokemon? _pokemon;
protected override void DeletePtr()
{

View File

@ -15,7 +15,7 @@ namespace PkmnLibSharp.Battling.Events
if (_newPokemon != null) return _newPokemon;
var ptr = Creaturelib.Generated.SwitchEvent.GetNewCreature(Ptr);
if (TryResolvePointer(ptr, out _newPokemon))
return _newPokemon;
return _newPokemon!;
_newPokemon = new Pokemon(ptr);
return _newPokemon;
}
@ -25,7 +25,7 @@ namespace PkmnLibSharp.Battling.Events
public byte Index => Creaturelib.Generated.SwitchEvent.GetIndex(Ptr);
private Pokemon _newPokemon;
private Pokemon? _newPokemon;
protected override void DeletePtr()
{

View File

@ -25,7 +25,7 @@ namespace PkmnLibSharp.Battling
if (_move != null) return _move;
var ptr = LearnedAttack.GetAttack(Ptr);
if (TryResolvePointer(ptr, out _move))
return _move;
return _move!;
_move = new MoveData(ptr);
return _move;
}
@ -36,7 +36,7 @@ namespace PkmnLibSharp.Battling
public MoveLearnMethod LearnMethod =>
(MoveLearnMethod) Creaturelib.Generated.LearnedAttack.GetLearnMethod(Ptr);
private MoveData _move;
private MoveData? _move;
protected override void DeletePtr()
{

View File

@ -6,11 +6,11 @@ namespace PkmnLibSharp.Battling
{
public class BattleLibrary : PointerWrapper
{
private PokemonLibrary _static;
private StatCalculator _statCalculator;
private DamageLibrary _damageLibrary;
private MiscLibrary _miscLibrary;
private ExperienceLibrary _experienceLibrary;
private PokemonLibrary? _static;
private StatCalculator? _statCalculator;
private DamageLibrary? _damageLibrary;
private MiscLibrary? _miscLibrary;
private ExperienceLibrary? _experienceLibrary;
public PokemonLibrary StaticLibrary
{
@ -19,7 +19,7 @@ namespace PkmnLibSharp.Battling
if (_static != null) return _static;
var ptr = Creaturelib.Generated.BattleLibrary.GetStaticLib(Ptr);
if (TryResolvePointer(ptr, out _static))
return _static;
return _static!;
_static = new PokemonLibrary(ptr);
return _static;
}
@ -32,7 +32,7 @@ namespace PkmnLibSharp.Battling
if (_statCalculator != null) return _statCalculator;
var ptr = Creaturelib.Generated.BattleLibrary.GetStatCalculator(Ptr);
if (TryResolvePointer(ptr, out _statCalculator))
return _statCalculator;
return _statCalculator!;
_statCalculator = new StatCalculator(ptr);
return _statCalculator;
}
@ -45,7 +45,7 @@ namespace PkmnLibSharp.Battling
if (_damageLibrary != null) return _damageLibrary;
var ptr = Creaturelib.Generated.BattleLibrary.GetDamageLibrary(Ptr);
if (TryResolvePointer(ptr, out _damageLibrary))
return _damageLibrary;
return _damageLibrary!;
_damageLibrary = new DamageLibrary(ptr);
return _damageLibrary;
}
@ -58,7 +58,7 @@ namespace PkmnLibSharp.Battling
if (_miscLibrary != null) return _miscLibrary;
var ptr = Creaturelib.Generated.BattleLibrary.GetMiscLibrary(Ptr);
if (TryResolvePointer(ptr, out _miscLibrary))
return _miscLibrary;
return _miscLibrary!;
_miscLibrary = new MiscLibrary(ptr);
return _miscLibrary;
}
@ -71,7 +71,7 @@ namespace PkmnLibSharp.Battling
if (_experienceLibrary != null) return _experienceLibrary;
var ptr = Creaturelib.Generated.BattleLibrary.GetExperienceLibrary(Ptr);
if (TryResolvePointer(ptr, out _experienceLibrary))
return _experienceLibrary;
return _experienceLibrary!;
_experienceLibrary = new ExperienceLibrary(ptr);
return _experienceLibrary;
}

View File

@ -11,19 +11,23 @@ namespace PkmnLibSharp.Battling
{
public class Pokemon : PointerWrapper
{
public Pokemon(){}
public Pokemon()
{
// Just here so BattleLibrary can be initialized
Library = new BattleLibrary(IntPtr.Zero);
}
internal Pokemon(IntPtr ptr) : base(ptr)
{
Initialize(ptr);
Library = new BattleLibrary(Creaturelib.Generated.Creature.GetLibrary(ptr));
}
public Pokemon([NotNull] BattleLibrary library, [NotNull] Species species, [NotNull] Forme forme,
public Pokemon(BattleLibrary library, Species species, Forme forme,
byte level, uint experience, uint uid,
Gender gender, byte coloring, [MaybeNull] Item heldItem, [MaybeNull] string nickname, bool hiddenAbility,
Gender gender, byte coloring, Item? heldItem, string? nickname, bool hiddenAbility,
byte abilityIndex,
[NotNull] IReadOnlyCollection<LearnedMove> moves, StatisticSet<byte> ivs, StatisticSet<byte> evs,
[NotNull] Nature nature)
IReadOnlyCollection<LearnedMove> moves, StatisticSet<byte> ivs, StatisticSet<byte> evs,
Nature nature)
: base(Pkmnlib.Generated.Pokemon.Construct(
library.Ptr, species.Ptr, forme.Ptr, level, experience,
uid, (Pkmnlib.Gender) gender, coloring, heldItem?.Ptr ?? IntPtr.Zero, nickname.ToPtr(),
@ -36,13 +40,7 @@ namespace PkmnLibSharp.Battling
{
Library = library;
}
protected internal override void Initialize(IntPtr ptr)
{
base.Initialize(ptr);
Library = new BattleLibrary(Creaturelib.Generated.Creature.GetLibrary(Ptr));
}
public BattleLibrary Library { get; private set; }
public Species Species
{
@ -51,7 +49,7 @@ namespace PkmnLibSharp.Battling
if (_species != null) return _species;
var ptr = Creaturelib.Generated.Creature.GetSpecies(Ptr);
if (TryResolvePointer(ptr, out _species))
return _species;
return _species!;
_species = new Species(ptr);
return _species;
}
@ -63,12 +61,12 @@ namespace PkmnLibSharp.Battling
if (_forme != null) return _forme;
var ptr = Creaturelib.Generated.Creature.GetVariant(Ptr);
if (TryResolvePointer(ptr, out _forme))
return _forme;
return _forme!;
_forme = new Forme(ptr);
return _forme;
}
}
public Species DisplaySpecies
public Species? DisplaySpecies
{
get
{
@ -79,9 +77,9 @@ namespace PkmnLibSharp.Battling
_displaySpecies = new Species(ptr);
return _displaySpecies;
}
set => Creaturelib.Generated.Creature.SetDisplaySpecies(Ptr, value.Ptr);
set => Creaturelib.Generated.Creature.SetDisplaySpecies(Ptr, value?.Ptr ?? IntPtr.Zero);
}
public Forme DisplayForme
public Forme? DisplayForme
{
get
{
@ -92,7 +90,7 @@ namespace PkmnLibSharp.Battling
_displayForme = new Forme(ptr);
return _displayForme;
}
set => Creaturelib.Generated.Creature.SetDisplayVariant(Ptr, value.Ptr);
set => Creaturelib.Generated.Creature.SetDisplayVariant(Ptr, value?.Ptr ?? IntPtr.Zero);
}
public byte Level => Creaturelib.Generated.Creature.GetLevel(Ptr);
public uint Experience => Creaturelib.Generated.Creature.GetExperience(Ptr);
@ -102,7 +100,7 @@ namespace PkmnLibSharp.Battling
public uint CurrentHealth => Creaturelib.Generated.Creature.GetCurrentHealth(Ptr);
public uint MaxHealth => Creaturelib.Generated.Creature.GetMaxHealth(Ptr);
public string ActiveAbility
public string? ActiveAbility
{
get
{
@ -111,7 +109,7 @@ namespace PkmnLibSharp.Battling
return ptr.PtrString();
}
}
public Battle Battle
public Battle? Battle
{
get
{
@ -123,7 +121,7 @@ namespace PkmnLibSharp.Battling
return _battle;
}
}
public BattleSide BattleSide
public BattleSide? BattleSide
{
get
{
@ -136,13 +134,13 @@ namespace PkmnLibSharp.Battling
}
}
public bool IsOnBattleField => Creaturelib.Generated.Creature.IsOnBattleField(Ptr) == 1;
public Item HeldItem
public Item? HeldItem
{
get
{
var ptr = Creaturelib.Generated.Creature.GetHeldItem(Ptr);
if (ptr == IntPtr.Zero) return null;
if (!TryResolvePointer(ptr, out Item item))
if (!TryResolvePointer(ptr, out Item? item))
{
item = new Item(ptr);
}
@ -152,7 +150,7 @@ namespace PkmnLibSharp.Battling
Creaturelib.Generated.Creature.SetHeldItemFromItem(Ptr,
value?.Ptr ?? IntPtr.Zero);
}
public string Nickname
public string? Nickname
{
get
{
@ -222,13 +220,13 @@ namespace PkmnLibSharp.Battling
if (_nature != null) return _nature;
var ptr = Pkmnlib.Generated.Pokemon.GetNature(Ptr);
if (TryResolvePointer(ptr, out _nature))
return _nature;
return _nature!;
_nature = new Nature(ptr);
return _nature;
}
}
public string StatusName => Pkmnlib.Generated.Pokemon.GetStatusName(Ptr).PtrString();
public string? StatusName => Pkmnlib.Generated.Pokemon.GetStatusName(Ptr).PtrString();
public void ChangeForme(Forme forme)
{
@ -339,15 +337,15 @@ namespace PkmnLibSharp.Battling
Pkmnlib.Generated.Pokemon.ClearStatus(Ptr);
}
private Species _displaySpecies;
private Forme _displayForme;
private Species _species;
private Forme _forme;
private string _nickname;
private ReadOnlyNativePtrArray<LearnedMove> _moves;
private Nature _nature;
private Battle _battle;
private BattleSide _battleSide;
private Species? _displaySpecies;
private Forme? _displayForme;
private Species? _species;
private Forme? _forme;
private string? _nickname;
private ReadOnlyNativePtrArray<LearnedMove>? _moves;
private Nature? _nature;
private Battle? _battle;
private BattleSide? _battleSide;
protected override void DeletePtr()
{

View File

@ -22,7 +22,7 @@ namespace PkmnLibSharp.Battling
public StatisticSet<byte> EVs;
public bool? IsForceShiny { get; private set; }
public string HeldItem { get; private set; }
public string? HeldItem { get; private set; }
public List<(string moveName, MoveLearnMethod learnMethod)> LearnedMoves { get; } =
new List<(string moveName, MoveLearnMethod learnMethod)>();
@ -81,7 +81,7 @@ namespace PkmnLibSharp.Battling
throw new Exception($"Species '{Species}' was not found.");
}
if (!species.TryGetForme(Forme, out var forme))
if (!species!.TryGetForme(Forme, out var forme))
{
throw new Exception($"Forme '{Forme}' was not found on species '{Species}'");
}
@ -104,7 +104,7 @@ namespace PkmnLibSharp.Battling
coloring = 1;
}
Item heldItem = null;
Item? heldItem = null;
if (HeldItem != null)
{
if (!_library.StaticLibrary.ItemLibrary.TryGet(HeldItem, out heldItem))
@ -117,11 +117,11 @@ namespace PkmnLibSharp.Battling
var isHiddenAbility = false;
if (string.IsNullOrEmpty(Ability))
{
abilityIndex = forme.GetRandomAbility(random);
abilityIndex = forme!.GetRandomAbility(random);
}
else
{
abilityIndex = forme.Abilities.IndexOf(Ability);
abilityIndex = forme!.Abilities.IndexOf(Ability);
if (abilityIndex == -1)
{
abilityIndex = forme.HiddenAbilities.IndexOf(Ability);
@ -143,7 +143,7 @@ namespace PkmnLibSharp.Battling
throw new Exception($"Move '{LearnedMoves[i].moveName}' was not found.");
}
moves[i] = new LearnedMove(move, move.BaseUsages, LearnedMoves[i].learnMethod);
moves[i] = new LearnedMove(move!, move!.BaseUsages, LearnedMoves[i].learnMethod);
}
if (string.IsNullOrEmpty(Nature))

View File

@ -5,7 +5,7 @@ namespace PkmnLibSharp.Battling
{
public class PokemonParty : PointerWrapper
{
private ReadOnlyNativePtrArray<Pokemon> _party;
private ReadOnlyNativePtrArray<Pokemon>? _party;
internal PokemonParty(IntPtr ptr) : base(ptr){}
@ -20,7 +20,7 @@ namespace PkmnLibSharp.Battling
{
var ptr = IntPtr.Zero;
Creaturelib.Generated.CreatureParty.GetAtIndex(ref ptr, Ptr, index).Assert();
return TryResolvePointer(ptr, out Pokemon pkmn) ? pkmn : new Pokemon(ptr);
return TryResolvePointer(ptr, out Pokemon? pkmn) ? pkmn! : new Pokemon(ptr);
}
public void Switch(ulong indexA, ulong indexB)
@ -31,10 +31,8 @@ namespace PkmnLibSharp.Battling
public Pokemon SwapInto(ulong indexA, Pokemon pokemon)
{
var ptr = Creaturelib.Generated.CreatureParty.SwapInto(Ptr, indexA, pokemon.Ptr);
if (TryResolvePointer(ptr, out Pokemon newPokemon))
{
return newPokemon;
}
if (TryResolvePointer(ptr, out Pokemon? newPokemon))
return newPokemon!;
return new Pokemon(ptr);
}

View File

@ -59,7 +59,7 @@ namespace PkmnLibSharp.Library
{
var p = IntPtr.Zero;
Creaturelib.Generated.EffectParameter.AsString(Ptr, ref p).Assert();
return p.PtrString();
return p.PtrString()!;
}

View File

@ -10,17 +10,17 @@ namespace PkmnLibSharp.Library
{
public class Forme : PointerWrapper
{
private ReadOnlyArray<string> _abilities;
private ReadOnlyArray<string> _hiddenAbilities;
private LearnableMoves _moves;
private string _name;
private ReadOnlyArray<byte> _types;
private ReadOnlyArray<string>? _abilities;
private ReadOnlyArray<string>? _hiddenAbilities;
private LearnableMoves? _moves;
private string? _name;
private ReadOnlyArray<byte>? _types;
internal Forme(IntPtr parent) : base(parent)
{
}
public string Name => _name ??= SpeciesVariant.GetName(Ptr).PtrString();
public string Name => _name ??= SpeciesVariant.GetName(Ptr).PtrString()!;
public float Height => SpeciesVariant.GetHeight(Ptr);
public float Weight => SpeciesVariant.GetWeight(Ptr);
public float BaseExperience => SpeciesVariant.GetBaseExperience(Ptr);
@ -66,7 +66,7 @@ namespace PkmnLibSharp.Library
{
var s = IntPtr.Zero;
SpeciesVariant.GetTalent(Ptr, MarshalHelper.False, i, ref s).Assert();
abilities[i] = s.PtrString();
abilities[i] = s.PtrString()!;
}
_abilities = abilities.ToReadOnly();
@ -87,7 +87,7 @@ namespace PkmnLibSharp.Library
{
var s = IntPtr.Zero;
SpeciesVariant.GetTalent(Ptr, MarshalHelper.True, i, ref s).Assert();
abilities[i] = s.PtrString();
abilities[i] = s.PtrString()!;
}
_hiddenAbilities = abilities.ToReadOnly();
@ -101,9 +101,10 @@ namespace PkmnLibSharp.Library
{
if (_moves != null) return _moves;
var movesPtr = SpeciesVariant.GetLearnableAttacks(Ptr);
if (!TryResolvePointer(movesPtr, out _moves)) _moves = new LearnableMoves(movesPtr);
if (!TryResolvePointer(movesPtr, out _moves))
_moves = new LearnableMoves(movesPtr);
return _moves;
return _moves!;
}
}
@ -114,7 +115,8 @@ namespace PkmnLibSharp.Library
public Forme(string name, float height, float weight, uint baseExperience, byte[] types,
ushort baseHealth, ushort baseAttack, ushort baseDefense, ushort baseSpecialAttack,
ushort baseSpecialDefense, ushort baseSpeed, string[] abilities, string[] hiddenAbilities,
ushort baseSpecialDefense, ushort baseSpeed, IReadOnlyCollection<string> abilities,
IReadOnlyCollection<string> hiddenAbilities,
LearnableMoves moves)
{
var abilitiesConverted = abilities.Select(x => x.ToPtr()).ToArray();
@ -123,8 +125,8 @@ namespace PkmnLibSharp.Library
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.Length, hab,
(ulong) hiddenAbilities.Length, moves.Ptr);
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);

View File

@ -6,13 +6,13 @@ namespace PkmnLibSharp.Library.Items
{
public class Item : PointerWrapper
{
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 BattleItemCategory BattleCategory =>

View File

@ -32,7 +32,7 @@ namespace PkmnLibSharp.Library.Items
_cache.Remove(key);
}
public bool TryGet(string key, out Item item)
public bool TryGet(string key, out Item? item)
{
if (_cache.TryGetValue(key, out item))
return true;
@ -41,7 +41,7 @@ namespace PkmnLibSharp.Library.Items
return false;
if (TryResolvePointer(ptr, out item))
{
_cache.Add(key, item);
_cache.Add(key, item!);
return true;
}
@ -56,10 +56,10 @@ namespace PkmnLibSharp.Library.Items
return item;
var ptr = IntPtr.Zero;
Creaturelib.Generated.ItemLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
if (TryResolvePointer(ptr, out item))
if (TryResolvePointer(ptr, out Item? i))
{
_cache.Add(key, item);
return item;
_cache.Add(key, i!);
return i!;
}
item = new Item(ptr);

View File

@ -8,14 +8,14 @@ namespace PkmnLibSharp.Library.Moves
{
public class MoveData : PointerWrapper
{
private string _name;
private string _secondaryEffectName;
private string? _name;
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 MoveCategory Category => (MoveCategory) AttackData.GetCategory(Ptr);
public byte BasePower => AttackData.GetBasePower(Ptr);
@ -27,7 +27,7 @@ namespace PkmnLibSharp.Library.Moves
public float SecondaryEffectChance => AttackData.GetSecondaryEffectChance(Ptr);
public string SecondaryEffectName =>
_secondaryEffectName ??= AttackData.GetSecondaryEffectName(Ptr).PtrString();
_secondaryEffectName ??= AttackData.GetSecondaryEffectName(Ptr).PtrString()!;
public bool HasFlag(string s)
{

View File

@ -28,7 +28,7 @@ namespace PkmnLibSharp.Library.Moves
_cache.Remove(key);
}
public bool TryGet(string key, out MoveData move)
public bool TryGet(string key, out MoveData? move)
{
if (_cache.TryGetValue(key, out move))
return true;
@ -37,7 +37,7 @@ namespace PkmnLibSharp.Library.Moves
return false;
if (TryResolvePointer(ptr, out move))
{
_cache.Add(key, move);
_cache.Add(key, move!);
return true;
}
@ -52,10 +52,10 @@ namespace PkmnLibSharp.Library.Moves
return move;
var ptr = IntPtr.Zero;
AttackLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
if (TryResolvePointer(ptr, out move))
if (TryResolvePointer(ptr, out MoveData? m))
{
_cache.Add(key, move);
return move;
_cache.Add(key, m!);
return m!;
}
move = new MoveData(ptr);

View File

@ -33,11 +33,11 @@ namespace PkmnLibSharp.Library
return nature;
var ptr = IntPtr.Zero;
Pkmnlib.Generated.NatureLibrary.GetNatureByName(Ptr, name.ToPtr(), ref ptr).Assert();
if (TryResolvePointer(ptr, out nature))
if (TryResolvePointer(ptr, out Nature? n))
{
_cache.Add(name, nature);
_cache.Add(name, n!);
_natureNames.Add(ptr, name);
return nature;
return n!;
}
nature = new Nature(ptr);
@ -52,14 +52,14 @@ namespace PkmnLibSharp.Library
return s;
var ptr = IntPtr.Zero;
Pkmnlib.Generated.NatureLibrary.GetNatureName(Ptr, nature.Ptr, ref ptr).Assert();
return ptr.PtrString();
return ptr.PtrString()!;
}
public string GetRandomNatureName(Random random)
{
IntPtr val = IntPtr.Zero;
Pkmnlib.Generated.NatureLibrary.GetRandomNatureName(Ptr, random.Ptr, ref val).Assert();
return val.PtrString();
return val.PtrString()!;
}
protected override void DeletePtr()

View File

@ -9,7 +9,7 @@ namespace PkmnLibSharp.Library
{
public class PokemonLibrary : PointerWrapper
{
private LibrarySettings _settings;
private LibrarySettings? _settings;
public LibrarySettings Settings
{
@ -18,13 +18,13 @@ namespace PkmnLibSharp.Library
if (_settings != null) return _settings;
var ptr = DataLibrary.GetSettings(Ptr);
if (TryResolvePointer(ptr, out _settings))
return _settings;
return _settings!;
_settings = new LibrarySettings(ptr);
return _settings;
}
}
private SpeciesLibrary _species;
private SpeciesLibrary? _species;
public SpeciesLibrary SpeciesLibrary
{
@ -33,13 +33,13 @@ namespace PkmnLibSharp.Library
if (_species != null) return _species;
var ptr = DataLibrary.GetSpeciesLibrary(Ptr);
if (TryResolvePointer(ptr, out _species))
return _species;
return _species!;
_species = new SpeciesLibrary(ptr);
return _species;
}
}
private MoveLibrary _moves;
private MoveLibrary? _moves;
public MoveLibrary MoveLibrary
{
@ -48,13 +48,13 @@ namespace PkmnLibSharp.Library
if (_moves != null) return _moves;
var ptr = DataLibrary.GetAttackLibrary(Ptr);
if (TryResolvePointer(ptr, out _moves))
return _moves;
return _moves!;
_moves = new MoveLibrary(ptr);
return _moves;
}
}
private ItemLibrary _items;
private ItemLibrary? _items;
public ItemLibrary ItemLibrary
{
@ -63,13 +63,13 @@ namespace PkmnLibSharp.Library
if (_items != null) return _items;
var ptr = DataLibrary.GetItemLibrary(Ptr);
if (TryResolvePointer(ptr, out _items))
return _items;
return _items!;
_items = new ItemLibrary(ptr);
return _items;
}
}
private GrowthRateLibrary _growthRateLibrary;
private GrowthRateLibrary? _growthRateLibrary;
public GrowthRateLibrary GrowthRateLibrary
{
@ -78,13 +78,13 @@ namespace PkmnLibSharp.Library
if (_growthRateLibrary != null) return _growthRateLibrary;
var ptr = DataLibrary.GetGrowthRates(Ptr);
if (TryResolvePointer(ptr, out _growthRateLibrary))
return _growthRateLibrary;
return _growthRateLibrary!;
_growthRateLibrary = new GrowthRateLibrary(ptr);
return _growthRateLibrary;
}
}
private TypeLibrary _typeLibrary;
private TypeLibrary? _typeLibrary;
public TypeLibrary TypeLibrary
{
@ -93,13 +93,13 @@ namespace PkmnLibSharp.Library
if (_typeLibrary != null) return _typeLibrary;
var ptr = DataLibrary.GetTypeLibrary(Ptr);
if (TryResolvePointer(ptr, out _typeLibrary))
return _typeLibrary;
return _typeLibrary!;
_typeLibrary = new TypeLibrary(ptr);
return _typeLibrary;
}
}
private NatureLibrary _natureLibrary;
private NatureLibrary? _natureLibrary;
public NatureLibrary NatureLibrary
{
@ -108,7 +108,7 @@ namespace PkmnLibSharp.Library
if (_natureLibrary != null) return _natureLibrary;
var ptr = Pkmnlib.Generated.PokemonLibrary.GetNatureLibrary(Ptr);
if (TryResolvePointer(ptr, out _natureLibrary))
return _natureLibrary;
return _natureLibrary!;
_natureLibrary = new NatureLibrary(ptr);
return _natureLibrary;
}

View File

@ -11,8 +11,8 @@ namespace PkmnLibSharp.Library
{
public class Species : PointerWrapper
{
private string _name;
private string _growthRate;
private string? _name;
private string? _growthRate;
private readonly Dictionary<string, Forme> _formes =
new Dictionary<string, Forme>(StringComparer.InvariantCultureIgnoreCase);
@ -20,15 +20,15 @@ namespace PkmnLibSharp.Library
public ushort Id => CreatureSpecies.GetId(Ptr);
public float GenderRate => CreatureSpecies.GetGenderRate(Ptr);
public byte CaptureRate => CreatureSpecies.GetCaptureRate(Ptr);
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString();
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString();
public string Name => _name ??= CreatureSpecies.GetName(Ptr).PtrString()!;
public string GrowthRate => _growthRate ??= CreatureSpecies.GetGrowthRate(Ptr).PtrString()!;
public bool HasForme(string s)
{
return CreatureSpecies.HasVariant(Ptr, s.ToPtr()) == MarshalHelper.True;
}
public bool TryGetForme(string s, out Forme forme)
public bool TryGetForme(string s, out Forme? forme)
{
if (_formes.TryGetValue(s, out forme))
return true;
@ -37,7 +37,7 @@ namespace PkmnLibSharp.Library
{
if (TryResolvePointer(ptr, out forme))
{
_formes.Add(s, forme);
_formes.Add(s, forme!);
return true;
}
@ -55,10 +55,10 @@ namespace PkmnLibSharp.Library
return forme;
var ptr = IntPtr.Zero;
CreatureSpecies.GetVariant(ref ptr, Ptr, s.ToPtr()).Assert();
if (TryResolvePointer(ptr, out forme))
if (TryResolvePointer(ptr, out Forme? f))
{
_formes.Add(s, forme);
return forme;
_formes.Add(s, f!);
return f!;
}
forme = new Forme(ptr);
_formes.Add(s, forme);

View File

@ -21,7 +21,7 @@ namespace PkmnLibSharp.Library
_cache.Remove(key);
}
public bool TryGet(string key, out Species species)
public bool TryGet(string key, out Species? species)
{
if (_cache.TryGetValue(key, out species))
return true;
@ -30,7 +30,7 @@ namespace PkmnLibSharp.Library
return false;
if (TryResolvePointer(ptr, out species))
{
_cache.Add(key, species);
_cache.Add(key, species!);
return true;
}
species = new Species(ptr);
@ -44,10 +44,10 @@ namespace PkmnLibSharp.Library
return species;
var ptr = IntPtr.Zero;
Creaturelib.Generated.SpeciesLibrary.Get(Ptr, key.ToPtr(), ref ptr).Assert();
if (TryResolvePointer(ptr, out species))
if (TryResolvePointer(ptr, out Species? s))
{
_cache.Add(key, species);
return species;
_cache.Add(key, s!);
return s!;
}
species = new Species(ptr);
_cache.Add(key, species);

View File

@ -66,7 +66,7 @@ namespace PkmnLibSharp.Library
var ptr = IntPtr.Zero;
Creaturelib.Generated.TypeLibrary.GetTypeName(ref ptr, Ptr, typeId).Assert();
var s = ptr.PtrString();
var s = ptr.PtrString()!;
_cache[s] = typeId;
return s;
}

View File

@ -4,6 +4,8 @@
<TargetFramework>netstandard2.1</TargetFramework>
<Configurations>Debug</Configurations>
<Platforms>AnyCPU</Platforms>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8601;CS8602;CS8603;CS8604;CS8618</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">

View File

@ -6,13 +6,13 @@ namespace PkmnLibSharp.Utilities
{
internal static class MarshalHelper
{
internal static IntPtr ToPtr(this string s)
internal static IntPtr ToPtr(this string? s)
{
if (s == null) return IntPtr.Zero;
return Marshal.StringToHGlobalAnsi(s);
}
internal static string PtrString(this IntPtr i)
internal static string? PtrString(this IntPtr i)
{
if (i == IntPtr.Zero) return null;
return Marshal.PtrToStringAnsi(i);

View File

@ -48,7 +48,7 @@ namespace PkmnLibSharp.Utilities
Cached.TryRemove(Ptr, out _);
}
public static bool TryResolvePointer<T>(IntPtr p, out T result) where T : PointerWrapper
public static bool TryResolvePointer<T>(IntPtr p, out T? result) where T : PointerWrapper
{
if (p == IntPtr.Zero)
{

View File

@ -67,8 +67,8 @@ 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 (PointerWrapper.TryResolvePointer(p, out T t))
return t;
if (PointerWrapper.TryResolvePointer(p, out T? t))
return t!;
t = new T();
t.Initialize(p);
return t;

View File

@ -10,13 +10,13 @@ namespace PkmnLibSharp.Utilities
{
case 0: return;
case 1:
throw new NativeException("Arbutils", C.GetLastException().PtrString());
throw new NativeException("Arbutils", C.GetLastException().PtrString()!);
case 2:
throw new NativeException("CreatureLibLibrary",
Creaturelib.Generated.C.GetLastException().PtrString());
Creaturelib.Generated.C.GetLastException().PtrString()!);
case 4:
throw new NativeException("PkmnLib",
Pkmnlib.Generated.C.GetLastException().PtrString());
Pkmnlib.Generated.C.GetLastException().PtrString()!);
}
}
}