Improvements to dataloaders for dealing with inheritance

This commit is contained in:
Deukhoofd 2024-09-30 19:23:20 +02:00
parent b77f0122d7
commit 4c34910a43
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 71 additions and 24 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
@ -26,6 +27,12 @@ public static class ItemDataLoader
library.Add(i);
return library;
}
public static Func<SerializedItem, StringKey, ItemCategory, BattleItemCategory, int,
IEnumerable<StringKey>, IItem> ItemConstructor = (_, name, type, battleType, price, flags) =>
{
return new ItemImpl(name, type, battleType, price, flags);
};
private static IItem DeserializeItem(SerializedItem serialized)
{
@ -33,7 +40,7 @@ public static class ItemDataLoader
throw new InvalidDataException($"Item type {serialized.ItemType} is not valid for item {serialized.Name}.");
Enum.TryParse(serialized.BattleType, true, out BattleItemCategory battleType);
return new ItemImpl(serialized.Name, itemType, battleType, serialized.Price,
return ItemConstructor(serialized, serialized.Name, itemType, battleType, serialized.Price,
serialized.Flags.Select(x => (StringKey)x).ToImmutableHashSet());
}
}

View File

@ -1,3 +1,7 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace PkmnLib.Dataloader.Models;
public class SerializedItem
@ -8,4 +12,7 @@ public class SerializedItem
public string[] Flags { get; set; } = null!;
public int Price { get; set; }
public byte FlingPower { get; set; }
[JsonExtensionData]
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
}

View File

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace PkmnLib.Dataloader.Models;
@ -18,6 +20,9 @@ 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; }
}
public class SerializedForm
@ -60,8 +65,8 @@ public class SerializedLevelMove
public class SerializedMoves
{
public SerializedLevelMove[] LevelMoves { get; set; } = null!;
public string[] EggMoves { get; set; } = null!;
public string[] TutorMoves { get; set; } = null!;
public string[] Machine { get; set; } = null!;
public SerializedLevelMove[]? LevelMoves { get; set; } = null!;
public string[]? EggMoves { get; set; } = null!;
public string[]? TutorMoves { get; set; } = null!;
public string[]? Machine { get; set; } = null!;
}

View File

@ -15,20 +15,23 @@ namespace PkmnLib.Dataloader;
public static class SpeciesDataLoader
{
public static SpeciesLibrary LoadSpecies(Stream stream, IReadOnlyTypeLibrary typeLibrary)
private static Dictionary<string, SerializedSpecies> LoadSpeciesData(Stream stream)
{
var library = new SpeciesLibrary();
var obj = JsonSerializer.Deserialize<JsonObject>(stream);
if (obj == null)
throw new InvalidDataException("Species data is empty.");
obj.Remove("$schema");
var cleanedString = obj.ToJsonString();
var objects = JsonSerializer.Deserialize<Dictionary<string, SerializedSpecies>>(cleanedString,
new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
});
var jsonConfig = new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = true,
};
return obj.Where(x => x.Key != "$schema")
.ToDictionary(x => x.Key, x => x.Value.Deserialize<SerializedSpecies>(jsonConfig));
}
public static SpeciesLibrary LoadSpecies(Stream[] streams, IReadOnlyTypeLibrary typeLibrary)
{
var library = new SpeciesLibrary();
var objects = streams.SelectMany(LoadSpeciesData);
if (objects == null)
throw new InvalidDataException("Species data is empty.");
var species = objects.Select(x => DeserializeSpecies(x.Value, typeLibrary));
@ -36,6 +39,28 @@ public static class SpeciesDataLoader
library.Add(s);
return library;
}
public static SpeciesLibrary LoadSpecies(Stream stream, IReadOnlyTypeLibrary typeLibrary)
{
var library = new SpeciesLibrary();
var objects = LoadSpeciesData(stream);
if (objects == null)
throw new InvalidDataException("Species data is empty.");
var species = objects.Select(x => DeserializeSpecies(x.Value, typeLibrary));
foreach (var s in species)
library.Add(s);
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);
};
private static SpeciesImpl DeserializeSpecies(SerializedSpecies serialized, IReadOnlyTypeLibrary typeLibrary)
{
@ -53,7 +78,7 @@ public static class SpeciesDataLoader
x => DeserializeForm(x.Key, x.Value, typeLibrary));
var evolutions = serialized.Evolutions.Select(DeserializeEvolution).ToList();
var species = new SpeciesImpl(serialized.Id, serialized.Species, genderRate, serialized.GrowthRate,
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;
@ -83,14 +108,17 @@ public static class SpeciesDataLoader
private static ILearnableMoves DeserializeMoves(SerializedMoves moves)
{
var learnableMoves = new LearnableMovesImpl();
foreach (var levelMove in moves.LevelMoves)
{
learnableMoves.AddLevelMove((byte)levelMove.Level, new StringKey(levelMove.Name));
}
foreach (var eggMove in moves.EggMoves)
{
learnableMoves.AddEggMove(new StringKey(eggMove));
}
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;
}