Style cleanup

This commit is contained in:
2025-03-02 17:19:57 +01:00
parent c0bc905c46
commit 284ab3079c
175 changed files with 588 additions and 650 deletions

View File

@@ -47,7 +47,7 @@ public static class AbilityDataLoader
var objects = LoadAbilitiesData(stream);
if (objects == null)
throw new InvalidDataException("Ability data is empty.");
var abilities = objects.Select(x => DeserializeAbility(x.Key, x.Value));
foreach (var a in abilities)
library.Add(a);
@@ -60,7 +60,7 @@ public static class AbilityDataLoader
var parameters = serialized.Parameters.ToDictionary(x => (StringKey)x.Key, x => x.Value.ToParameter());
StringKey? effectName = effect == null ? null! : new StringKey(effect);
var flags = serialized.Flags.Select(x => new StringKey(x)).ToImmutableHashSet();
var ability = new AbilityImpl(name, effectName, parameters, flags);

View File

@@ -26,15 +26,15 @@ public static class ItemDataLoader
library.Add(i);
return library;
}
public delegate IItem ItemFactoryDelegate(SerializedItem serialized, StringKey name, ItemCategory type,
BattleItemCategory battleType, int price, ImmutableHashSet<StringKey> flags,
ISecondaryEffect? effect, ISecondaryEffect? battleTriggerEffect, byte flingPower);
BattleItemCategory battleType, int price, ImmutableHashSet<StringKey> flags, ISecondaryEffect? effect,
ISecondaryEffect? battleTriggerEffect, byte flingPower);
[PublicAPI]
public static ItemFactoryDelegate ItemConstructor { get; set; } = (_, name, type, battleType, price, flags, effect,
battleTriggerEffect, flingPower) =>
new ItemImpl(name, type, battleType, price, flags, effect, battleTriggerEffect, flingPower);
public static ItemFactoryDelegate ItemConstructor { get; set; } =
(_, name, type, battleType, price, flags, effect, battleTriggerEffect, flingPower) => new ItemImpl(name, type,
battleType, price, flags, effect, battleTriggerEffect, flingPower);
private static IItem DeserializeItem(SerializedItem serialized)
{

View File

@@ -4,7 +4,7 @@ namespace PkmnLib.Dataloader;
internal static class JsonOptions
{
public static JsonSerializerOptions DefaultOptions => new JsonSerializerOptions()
public static JsonSerializerOptions DefaultOptions => new()
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,

View File

@@ -14,7 +14,6 @@ public class SerializedItem
public byte FlingPower { get; set; }
public SerializedMoveEffect? Effect { get; set; }
public SerializedMoveEffect? BattleEffect { get; set; }
[JsonExtensionData]
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
[JsonExtensionData] public Dictionary<string, JsonElement>? ExtensionData { get; set; }
}

View File

@@ -22,9 +22,8 @@ public class SerializedMove
public string Category { get; set; } = null!;
public string[] Flags { get; set; } = null!;
public SerializedMoveEffect? Effect { get; set; }
[JsonExtensionData]
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
[JsonExtensionData] public Dictionary<string, JsonElement>? ExtensionData { get; set; }
}
public class SerializedMoveEffect

View File

@@ -20,9 +20,8 @@ public class SerializedSpecies
public string[] Flags { get; set; } = [];
public Dictionary<string, SerializedForm> Formes { get; set; } = null!;
public SerializedEvolution[] Evolutions { get; set; } = [];
[JsonExtensionData]
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
[JsonExtensionData] public Dictionary<string, JsonElement>? ExtensionData { get; set; }
}
public class SerializedForm
@@ -38,9 +37,8 @@ public class SerializedForm
public bool IsMega { get; set; }
public SerializedMoves Moves { get; set; } = null!;
public string[] Flags { get; set; } = [];
[JsonExtensionData]
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
[JsonExtensionData] public Dictionary<string, JsonElement>? ExtensionData { get; set; }
}
public class SerializedEvolution

View File

@@ -26,11 +26,12 @@ public static class MoveDataLoader
return library;
}
public static Func<SerializedMove, StringKey, TypeIdentifier, MoveCategory, byte, byte, byte, MoveTarget, sbyte,
ISecondaryEffect?, IEnumerable<StringKey>, MoveDataImpl> MoveConstructor =
(serialized, name, moveType, category, basePower, accuracy, baseUsages, target, priority, secondaryEffect,
flags) => new MoveDataImpl(name, moveType, category, basePower, accuracy, baseUsages, target, priority,
secondaryEffect, flags);
public static
Func<SerializedMove, StringKey, TypeIdentifier, MoveCategory, byte, byte, byte, MoveTarget, sbyte,
ISecondaryEffect?, IEnumerable<StringKey>, MoveDataImpl> MoveConstructor =
(serialized, name, moveType, category, basePower, accuracy, baseUsages, target, priority, secondaryEffect,
flags) => new MoveDataImpl(name, moveType, category, basePower, accuracy, baseUsages, target, priority,
secondaryEffect, flags);
private static MoveDataImpl DeserializeMove(SerializedMove serialized, TypeLibrary typeLibrary)
{
@@ -55,9 +56,8 @@ public static class MoveDataLoader
throw new InvalidDataException($"Target {target} is not a valid target.");
var secondaryEffect = effect.ParseEffect();
var move = MoveConstructor(serialized, serialized.Name, typeIdentifier, categoryEnum, power, accuracy, pp, targetEnum,
priority, secondaryEffect, flags.Select(x => (StringKey)x).ToImmutableHashSet());
var move = MoveConstructor(serialized, serialized.Name, typeIdentifier, categoryEnum, power, accuracy, pp,
targetEnum, priority, secondaryEffect, flags.Select(x => (StringKey)x).ToImmutableHashSet());
return move;
}
}

View File

@@ -13,7 +13,7 @@ public static class NatureDataLoader
public static NatureLibrary LoadNatureLibrary(Stream stream)
{
var library = new NatureLibrary();
using var reader = new StreamReader(stream);
var header = reader.ReadLine();
if (header == null)
@@ -31,7 +31,7 @@ public static class NatureDataLoader
var nature = values[0];
var increasedStat = values[1];
var decreasedStat = values[2];
var increasedModifier = 1.1f;
var decreasedModifier = 0.9f;
@@ -46,7 +46,7 @@ public static class NatureDataLoader
decreasedStat = "Hp";
decreasedModifier = 1.0f;
}
if (!Enum.TryParse<Statistic>(increasedStat, out var increasedStatEnum))
throw new InvalidDataException($"Increased stat {increasedStat} is not a valid stat.");
if (!Enum.TryParse<Statistic>(decreasedStat, out var decreasedStatEnum))

View File

@@ -20,10 +20,10 @@ public static class SpeciesDataLoader
var obj = JsonSerializer.Deserialize<JsonObject>(stream, JsonOptions.DefaultOptions);
if (obj == null)
throw new InvalidDataException("Species data is empty.");
return obj.Where(x => x.Key != "$schema")
.ToDictionary(x => x.Key, x => x.Value.Deserialize<SerializedSpecies>(JsonOptions.DefaultOptions));
return obj.Where(x => x.Key != "$schema").ToDictionary(x => x.Key,
x => x.Value.Deserialize<SerializedSpecies>(JsonOptions.DefaultOptions));
}
public static SpeciesLibrary LoadSpecies(Stream[] streams, IReadOnlyTypeLibrary typeLibrary)
{
var library = new SpeciesLibrary();
@@ -35,7 +35,7 @@ public static class SpeciesDataLoader
library.Add(s);
return library;
}
public static SpeciesLibrary LoadSpecies(Stream stream, IReadOnlyTypeLibrary typeLibrary)
{
var library = new SpeciesLibrary();
@@ -48,34 +48,37 @@ public static class SpeciesDataLoader
return library;
}
public static Func<SerializedSpecies, ushort, StringKey, float, StringKey, byte, byte, IReadOnlyDictionary<StringKey, IForm>,
IEnumerable<StringKey>, IReadOnlyList<IEvolution>, IEnumerable<StringKey>, SpeciesImpl> SpeciesConstructor =
(_, id, name, genderRate, growthRate, captureRate, baseHappiness, forms, flags, evolutionData,
eggGroups) =>
{
return new SpeciesImpl(id, name, genderRate, growthRate,
captureRate, baseHappiness, forms,
flags, evolutionData, eggGroups);
};
public static
Func<SerializedSpecies, ushort, StringKey, float, StringKey, byte, byte, IReadOnlyDictionary<StringKey, IForm>,
IEnumerable<StringKey>, IReadOnlyList<IEvolution>, IEnumerable<StringKey>, SpeciesImpl> SpeciesConstructor =
(_, id, name, genderRate, growthRate, captureRate, baseHappiness, forms, flags, evolutionData, eggGroups) =>
{
return new SpeciesImpl(id, name, genderRate, growthRate, captureRate, baseHappiness, forms, flags,
evolutionData, eggGroups);
};
private static SpeciesImpl DeserializeSpecies(SerializedSpecies serialized, IReadOnlyTypeLibrary typeLibrary)
{
var id = serialized.Id;
var genderRate = serialized.GenderRatio;
if (genderRate < -1.0 || genderRate > 100.0)
{
throw new InvalidDataException(
$"Gender rate for species {id} is invalid: {genderRate}. Must be between -1.0 and 100.0.");
}
if (serialized.EggCycles < 0)
{
throw new InvalidDataException(
$"Egg cycles for species {id} is invalid: {serialized.EggCycles}. Must be greater than or equal to 0.");
}
var forms = serialized.Formes.ToDictionary(x => (StringKey)x.Key,
x => DeserializeForm(x.Key, x.Value, typeLibrary));
var evolutions = serialized.Evolutions.Select(DeserializeEvolution).ToList();
var species = SpeciesConstructor(serialized, serialized.Id, serialized.Species, genderRate, serialized.GrowthRate,
serialized.CatchRate, serialized.BaseHappiness, forms,
var species = SpeciesConstructor(serialized, serialized.Id, serialized.Species, genderRate,
serialized.GrowthRate, serialized.CatchRate, serialized.BaseHappiness, forms,
serialized.Flags.Select(x => new StringKey(x)), evolutions, serialized.EggGroups.Select(x => (StringKey)x));
return species;
}
@@ -85,11 +88,15 @@ public static class SpeciesDataLoader
if (form == null)
throw new ArgumentException("Form data is null.", nameof(form));
if (form.Height < 0.0)
{
throw new InvalidDataException(
$"Height for form {name} is invalid: {form.Height}. Must be greater than or equal to 0.0.");
}
if (form.Weight < 0.0)
{
throw new InvalidDataException(
$"Weight for form {name} is invalid: {form.Weight}. Must be greater than or equal to 0.0.");
}
var types = form.Types.Select(x =>
typeLibrary.TryGetTypeIdentifier(new StringKey(x), out var t)
? t
@@ -105,25 +112,26 @@ public static class SpeciesDataLoader
{
var learnableMoves = new LearnableMovesImpl();
if (moves.LevelMoves != null)
{
foreach (var levelMove in moves.LevelMoves)
{
learnableMoves.AddLevelMove((byte)levelMove.Level, new StringKey(levelMove.Name));
}
}
if (moves.EggMoves != null)
{
foreach (var eggMove in moves.EggMoves)
{
learnableMoves.AddEggMove(new StringKey(eggMove));
}
}
return learnableMoves;
}
private static ImmutableStatisticSet<ushort> DeserializeStats(SerializedStats stats)
{
return new ImmutableStatisticSet<ushort>(stats.Hp, stats.Attack, stats.Defense, stats.SpecialAttack,
stats.SpecialDefense, stats.Speed);
}
private static ImmutableStatisticSet<ushort> DeserializeStats(SerializedStats stats) =>
new(stats.Hp, stats.Attack, stats.Defense, stats.SpecialAttack, stats.SpecialDefense, stats.Speed);
private static IEvolution DeserializeEvolution(SerializedEvolution evolution)
{
@@ -223,8 +231,7 @@ public static class SpeciesDataLoader
{
Name = evolution.Data.AsObject()["type"]?.GetValue<string>() ??
throw new InvalidDataException("Type is null."),
Parameters = evolution.Data.AsObject()
.Where(x => x.Key != "type")
Parameters = evolution.Data.AsObject().Where(x => x.Key != "type")
.ToDictionary(x => new StringKey(x.Key), x => x.Value!.ToParameter()),
ToSpecies = evolution.Species,
},

View File

@@ -42,8 +42,10 @@ public static class TypeDataLoader
{
var effectiveness = float.Parse(values[i]);
if (effectiveness < 0.0)
{
throw new InvalidDataException(
$"Effectiveness for {type} against {types[i]} is invalid: {effectiveness}. Must be greater than or equal to 0.0.");
}
library.SetEffectiveness(typeId, (TypeIdentifier)i, effectiveness);
}
}