From 01e622c22c6796819c0ae7dd54cda0e9a0692410 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 8 Aug 2020 14:32:27 +0200 Subject: [PATCH] Use fancy C# 8 Nullable Reference pattern. --- PkmnLibSharp/Battling/Battle/Battle.cs | 20 +++--- PkmnLibSharp/Battling/Battle/BattleParty.cs | 4 +- .../Battling/ChoiceTurn/BaseTurnChoice.cs | 4 +- .../Battling/ChoiceTurn/MoveTurnChoice.cs | 4 +- .../Battling/Events/BattleEventListener.cs | 2 +- PkmnLibSharp/Battling/Events/DamageEvent.cs | 4 +- .../Battling/Events/ExperienceGainEvent.cs | 4 +- PkmnLibSharp/Battling/Events/FaintEvent.cs | 4 +- PkmnLibSharp/Battling/Events/HealEvent.cs | 4 +- PkmnLibSharp/Battling/Events/SwitchEvent.cs | 4 +- PkmnLibSharp/Battling/LearnedMove.cs | 4 +- .../Battling/Library/BattleLibrary.cs | 20 +++--- PkmnLibSharp/Battling/Pokemon.cs | 70 +++++++++---------- PkmnLibSharp/Battling/PokemonBuilder.cs | 12 ++-- PkmnLibSharp/Battling/PokemonParty.cs | 10 ++- PkmnLibSharp/Library/EffectParameter.cs | 2 +- PkmnLibSharp/Library/Forme.cs | 28 ++++---- PkmnLibSharp/Library/Items/Item.cs | 4 +- PkmnLibSharp/Library/Items/ItemLibrary.cs | 10 +-- PkmnLibSharp/Library/Moves/MoveData.cs | 8 +-- PkmnLibSharp/Library/Moves/MoveLibrary.cs | 10 +-- PkmnLibSharp/Library/NatureLibrary.cs | 10 +-- PkmnLibSharp/Library/PokemonLibrary.cs | 28 ++++---- PkmnLibSharp/Library/Species.cs | 18 ++--- PkmnLibSharp/Library/SpeciesLibrary.cs | 10 +-- PkmnLibSharp/Library/TypeLibrary.cs | 2 +- PkmnLibSharp/PkmnLibSharp.csproj | 2 + PkmnLibSharp/Utilities/MarshalHelper.cs | 4 +- PkmnLibSharp/Utilities/PointerWrapper.cs | 2 +- .../Utilities/ReadOnlyNativePtrArray.cs | 4 +- PkmnLibSharp/Utilities/ResultChecker.cs | 6 +- 31 files changed, 159 insertions(+), 159 deletions(-) diff --git a/PkmnLibSharp/Battling/Battle/Battle.cs b/PkmnLibSharp/Battling/Battle/Battle.cs index 0d28f9e..66b0301 100644 --- a/PkmnLibSharp/Battling/Battle/Battle.cs +++ b/PkmnLibSharp/Battling/Battle/Battle.cs @@ -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 _sides; - private ReadOnlyNativePtrArray _parties; + private BattleLibrary? _library; + private BattleRandom? _random; + private ReadOnlyNativePtrArray? _sides; + private ReadOnlyNativePtrArray? _parties; protected override void DeletePtr() { diff --git a/PkmnLibSharp/Battling/Battle/BattleParty.cs b/PkmnLibSharp/Battling/Battle/BattleParty.cs index 3e5a52f..219555a 100644 --- a/PkmnLibSharp/Battling/Battle/BattleParty.cs +++ b/PkmnLibSharp/Battling/Battle/BattleParty.cs @@ -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; } diff --git a/PkmnLibSharp/Battling/ChoiceTurn/BaseTurnChoice.cs b/PkmnLibSharp/Battling/ChoiceTurn/BaseTurnChoice.cs index 237353c..604add9 100644 --- a/PkmnLibSharp/Battling/ChoiceTurn/BaseTurnChoice.cs +++ b/PkmnLibSharp/Battling/ChoiceTurn/BaseTurnChoice.cs @@ -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; } } \ No newline at end of file diff --git a/PkmnLibSharp/Battling/ChoiceTurn/MoveTurnChoice.cs b/PkmnLibSharp/Battling/ChoiceTurn/MoveTurnChoice.cs index 1a55eab..12161d8 100644 --- a/PkmnLibSharp/Battling/ChoiceTurn/MoveTurnChoice.cs +++ b/PkmnLibSharp/Battling/ChoiceTurn/MoveTurnChoice.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/Events/BattleEventListener.cs b/PkmnLibSharp/Battling/Events/BattleEventListener.cs index 2ef9286..0f7226b 100644 --- a/PkmnLibSharp/Battling/Events/BattleEventListener.cs +++ b/PkmnLibSharp/Battling/Events/BattleEventListener.cs @@ -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) { diff --git a/PkmnLibSharp/Battling/Events/DamageEvent.cs b/PkmnLibSharp/Battling/Events/DamageEvent.cs index efdddb8..12ea86d 100644 --- a/PkmnLibSharp/Battling/Events/DamageEvent.cs +++ b/PkmnLibSharp/Battling/Events/DamageEvent.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/Events/ExperienceGainEvent.cs b/PkmnLibSharp/Battling/Events/ExperienceGainEvent.cs index 5919a4a..7cf61ea 100644 --- a/PkmnLibSharp/Battling/Events/ExperienceGainEvent.cs +++ b/PkmnLibSharp/Battling/Events/ExperienceGainEvent.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/Events/FaintEvent.cs b/PkmnLibSharp/Battling/Events/FaintEvent.cs index 5f87a9a..628fdee 100644 --- a/PkmnLibSharp/Battling/Events/FaintEvent.cs +++ b/PkmnLibSharp/Battling/Events/FaintEvent.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/Events/HealEvent.cs b/PkmnLibSharp/Battling/Events/HealEvent.cs index 8eab835..d8d1f05 100644 --- a/PkmnLibSharp/Battling/Events/HealEvent.cs +++ b/PkmnLibSharp/Battling/Events/HealEvent.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/Events/SwitchEvent.cs b/PkmnLibSharp/Battling/Events/SwitchEvent.cs index 0e0240e..a13b84f 100644 --- a/PkmnLibSharp/Battling/Events/SwitchEvent.cs +++ b/PkmnLibSharp/Battling/Events/SwitchEvent.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/LearnedMove.cs b/PkmnLibSharp/Battling/LearnedMove.cs index 76363a8..2761883 100644 --- a/PkmnLibSharp/Battling/LearnedMove.cs +++ b/PkmnLibSharp/Battling/LearnedMove.cs @@ -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() { diff --git a/PkmnLibSharp/Battling/Library/BattleLibrary.cs b/PkmnLibSharp/Battling/Library/BattleLibrary.cs index 1c83a0c..be0e770 100644 --- a/PkmnLibSharp/Battling/Library/BattleLibrary.cs +++ b/PkmnLibSharp/Battling/Library/BattleLibrary.cs @@ -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; } diff --git a/PkmnLibSharp/Battling/Pokemon.cs b/PkmnLibSharp/Battling/Pokemon.cs index f19ddeb..4b16c91 100644 --- a/PkmnLibSharp/Battling/Pokemon.cs +++ b/PkmnLibSharp/Battling/Pokemon.cs @@ -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 moves, StatisticSet ivs, StatisticSet evs, - [NotNull] Nature nature) + IReadOnlyCollection moves, StatisticSet ivs, StatisticSet 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 _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? _moves; + private Nature? _nature; + private Battle? _battle; + private BattleSide? _battleSide; protected override void DeletePtr() { diff --git a/PkmnLibSharp/Battling/PokemonBuilder.cs b/PkmnLibSharp/Battling/PokemonBuilder.cs index 10fee9d..bf0ce9b 100644 --- a/PkmnLibSharp/Battling/PokemonBuilder.cs +++ b/PkmnLibSharp/Battling/PokemonBuilder.cs @@ -22,7 +22,7 @@ namespace PkmnLibSharp.Battling public StatisticSet 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)) diff --git a/PkmnLibSharp/Battling/PokemonParty.cs b/PkmnLibSharp/Battling/PokemonParty.cs index 51252b4..d46fedf 100644 --- a/PkmnLibSharp/Battling/PokemonParty.cs +++ b/PkmnLibSharp/Battling/PokemonParty.cs @@ -5,7 +5,7 @@ namespace PkmnLibSharp.Battling { public class PokemonParty : PointerWrapper { - private ReadOnlyNativePtrArray _party; + private ReadOnlyNativePtrArray? _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); } diff --git a/PkmnLibSharp/Library/EffectParameter.cs b/PkmnLibSharp/Library/EffectParameter.cs index a1f872e..22963fa 100644 --- a/PkmnLibSharp/Library/EffectParameter.cs +++ b/PkmnLibSharp/Library/EffectParameter.cs @@ -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()!; } diff --git a/PkmnLibSharp/Library/Forme.cs b/PkmnLibSharp/Library/Forme.cs index dd85f72..655d9e2 100644 --- a/PkmnLibSharp/Library/Forme.cs +++ b/PkmnLibSharp/Library/Forme.cs @@ -10,17 +10,17 @@ namespace PkmnLibSharp.Library { public class Forme : PointerWrapper { - private ReadOnlyArray _abilities; - private ReadOnlyArray _hiddenAbilities; - private LearnableMoves _moves; - private string _name; - private ReadOnlyArray _types; + private ReadOnlyArray? _abilities; + private ReadOnlyArray? _hiddenAbilities; + private LearnableMoves? _moves; + private string? _name; + private ReadOnlyArray? _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 abilities, + IReadOnlyCollection 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); diff --git a/PkmnLibSharp/Library/Items/Item.cs b/PkmnLibSharp/Library/Items/Item.cs index 5a1df70..884c416 100644 --- a/PkmnLibSharp/Library/Items/Item.cs +++ b/PkmnLibSharp/Library/Items/Item.cs @@ -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 => diff --git a/PkmnLibSharp/Library/Items/ItemLibrary.cs b/PkmnLibSharp/Library/Items/ItemLibrary.cs index 1cd9070..dab356d 100644 --- a/PkmnLibSharp/Library/Items/ItemLibrary.cs +++ b/PkmnLibSharp/Library/Items/ItemLibrary.cs @@ -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); diff --git a/PkmnLibSharp/Library/Moves/MoveData.cs b/PkmnLibSharp/Library/Moves/MoveData.cs index ac4583a..4e18d28 100644 --- a/PkmnLibSharp/Library/Moves/MoveData.cs +++ b/PkmnLibSharp/Library/Moves/MoveData.cs @@ -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) { diff --git a/PkmnLibSharp/Library/Moves/MoveLibrary.cs b/PkmnLibSharp/Library/Moves/MoveLibrary.cs index 6b35e41..e7874a0 100644 --- a/PkmnLibSharp/Library/Moves/MoveLibrary.cs +++ b/PkmnLibSharp/Library/Moves/MoveLibrary.cs @@ -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); diff --git a/PkmnLibSharp/Library/NatureLibrary.cs b/PkmnLibSharp/Library/NatureLibrary.cs index d02f53d..e00092d 100644 --- a/PkmnLibSharp/Library/NatureLibrary.cs +++ b/PkmnLibSharp/Library/NatureLibrary.cs @@ -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() diff --git a/PkmnLibSharp/Library/PokemonLibrary.cs b/PkmnLibSharp/Library/PokemonLibrary.cs index 045f00e..e262020 100644 --- a/PkmnLibSharp/Library/PokemonLibrary.cs +++ b/PkmnLibSharp/Library/PokemonLibrary.cs @@ -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; } diff --git a/PkmnLibSharp/Library/Species.cs b/PkmnLibSharp/Library/Species.cs index 9bd1146..b85e32a 100644 --- a/PkmnLibSharp/Library/Species.cs +++ b/PkmnLibSharp/Library/Species.cs @@ -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 _formes = new Dictionary(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); diff --git a/PkmnLibSharp/Library/SpeciesLibrary.cs b/PkmnLibSharp/Library/SpeciesLibrary.cs index 6a116e3..2a6c299 100644 --- a/PkmnLibSharp/Library/SpeciesLibrary.cs +++ b/PkmnLibSharp/Library/SpeciesLibrary.cs @@ -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); diff --git a/PkmnLibSharp/Library/TypeLibrary.cs b/PkmnLibSharp/Library/TypeLibrary.cs index 165897c..a01618a 100644 --- a/PkmnLibSharp/Library/TypeLibrary.cs +++ b/PkmnLibSharp/Library/TypeLibrary.cs @@ -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; } diff --git a/PkmnLibSharp/PkmnLibSharp.csproj b/PkmnLibSharp/PkmnLibSharp.csproj index 5e7f32a..f8dbb8c 100644 --- a/PkmnLibSharp/PkmnLibSharp.csproj +++ b/PkmnLibSharp/PkmnLibSharp.csproj @@ -4,6 +4,8 @@ netstandard2.1 Debug AnyCPU + enable + CS8600;CS8601;CS8602;CS8603;CS8604;CS8618 diff --git a/PkmnLibSharp/Utilities/MarshalHelper.cs b/PkmnLibSharp/Utilities/MarshalHelper.cs index 3c111fa..fa5ee3a 100644 --- a/PkmnLibSharp/Utilities/MarshalHelper.cs +++ b/PkmnLibSharp/Utilities/MarshalHelper.cs @@ -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); diff --git a/PkmnLibSharp/Utilities/PointerWrapper.cs b/PkmnLibSharp/Utilities/PointerWrapper.cs index ff1844b..537d55c 100644 --- a/PkmnLibSharp/Utilities/PointerWrapper.cs +++ b/PkmnLibSharp/Utilities/PointerWrapper.cs @@ -48,7 +48,7 @@ namespace PkmnLibSharp.Utilities Cached.TryRemove(Ptr, out _); } - public static bool TryResolvePointer(IntPtr p, out T result) where T : PointerWrapper + public static bool TryResolvePointer(IntPtr p, out T? result) where T : PointerWrapper { if (p == IntPtr.Zero) { diff --git a/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs b/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs index c59e1c6..f208c89 100644 --- a/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs +++ b/PkmnLibSharp/Utilities/ReadOnlyNativePtrArray.cs @@ -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; diff --git a/PkmnLibSharp/Utilities/ResultChecker.cs b/PkmnLibSharp/Utilities/ResultChecker.cs index 9d0bbc9..b520842 100644 --- a/PkmnLibSharp/Utilities/ResultChecker.cs +++ b/PkmnLibSharp/Utilities/ResultChecker.cs @@ -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()!); } } }