Improvements to dataloaders for dealing with inheritance
This commit is contained in:
parent
b77f0122d7
commit
4c34910a43
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -27,13 +28,19 @@ public static class ItemDataLoader
|
|||||||
return library;
|
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)
|
private static IItem DeserializeItem(SerializedItem serialized)
|
||||||
{
|
{
|
||||||
if (!Enum.TryParse<ItemCategory>(serialized.ItemType, true, out var itemType))
|
if (!Enum.TryParse<ItemCategory>(serialized.ItemType, true, out var itemType))
|
||||||
throw new InvalidDataException($"Item type {serialized.ItemType} is not valid for item {serialized.Name}.");
|
throw new InvalidDataException($"Item type {serialized.ItemType} is not valid for item {serialized.Name}.");
|
||||||
Enum.TryParse(serialized.BattleType, true, out BattleItemCategory battleType);
|
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());
|
serialized.Flags.Select(x => (StringKey)x).ToImmutableHashSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,7 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace PkmnLib.Dataloader.Models;
|
namespace PkmnLib.Dataloader.Models;
|
||||||
|
|
||||||
public class SerializedItem
|
public class SerializedItem
|
||||||
@ -8,4 +12,7 @@ public class SerializedItem
|
|||||||
public string[] Flags { get; set; } = null!;
|
public string[] Flags { get; set; } = null!;
|
||||||
public int Price { get; set; }
|
public int Price { get; set; }
|
||||||
public byte FlingPower { get; set; }
|
public byte FlingPower { get; set; }
|
||||||
|
|
||||||
|
[JsonExtensionData]
|
||||||
|
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace PkmnLib.Dataloader.Models;
|
namespace PkmnLib.Dataloader.Models;
|
||||||
|
|
||||||
@ -18,6 +20,9 @@ public class SerializedSpecies
|
|||||||
public string[] Flags { get; set; } = [];
|
public string[] Flags { get; set; } = [];
|
||||||
public Dictionary<string, SerializedForm> Formes { get; set; } = null!;
|
public Dictionary<string, SerializedForm> Formes { get; set; } = null!;
|
||||||
public SerializedEvolution[] Evolutions { get; set; } = [];
|
public SerializedEvolution[] Evolutions { get; set; } = [];
|
||||||
|
|
||||||
|
[JsonExtensionData]
|
||||||
|
public Dictionary<string, JsonElement>? ExtensionData { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SerializedForm
|
public class SerializedForm
|
||||||
@ -60,8 +65,8 @@ public class SerializedLevelMove
|
|||||||
|
|
||||||
public class SerializedMoves
|
public class SerializedMoves
|
||||||
{
|
{
|
||||||
public SerializedLevelMove[] LevelMoves { get; set; } = null!;
|
public SerializedLevelMove[]? LevelMoves { get; set; } = null!;
|
||||||
public string[] EggMoves { get; set; } = null!;
|
public string[]? EggMoves { get; set; } = null!;
|
||||||
public string[] TutorMoves { get; set; } = null!;
|
public string[]? TutorMoves { get; set; } = null!;
|
||||||
public string[] Machine { get; set; } = null!;
|
public string[]? Machine { get; set; } = null!;
|
||||||
}
|
}
|
@ -15,20 +15,23 @@ namespace PkmnLib.Dataloader;
|
|||||||
|
|
||||||
public static class SpeciesDataLoader
|
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);
|
var obj = JsonSerializer.Deserialize<JsonObject>(stream);
|
||||||
if (obj == null)
|
if (obj == null)
|
||||||
throw new InvalidDataException("Species data is empty.");
|
throw new InvalidDataException("Species data is empty.");
|
||||||
obj.Remove("$schema");
|
var jsonConfig = new JsonSerializerOptions()
|
||||||
var cleanedString = obj.ToJsonString();
|
|
||||||
|
|
||||||
var objects = JsonSerializer.Deserialize<Dictionary<string, SerializedSpecies>>(cleanedString,
|
|
||||||
new JsonSerializerOptions()
|
|
||||||
{
|
{
|
||||||
PropertyNameCaseInsensitive = true,
|
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)
|
if (objects == null)
|
||||||
throw new InvalidDataException("Species data is empty.");
|
throw new InvalidDataException("Species data is empty.");
|
||||||
var species = objects.Select(x => DeserializeSpecies(x.Value, typeLibrary));
|
var species = objects.Select(x => DeserializeSpecies(x.Value, typeLibrary));
|
||||||
@ -37,6 +40,28 @@ public static class SpeciesDataLoader
|
|||||||
return library;
|
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)
|
private static SpeciesImpl DeserializeSpecies(SerializedSpecies serialized, IReadOnlyTypeLibrary typeLibrary)
|
||||||
{
|
{
|
||||||
var id = serialized.Id;
|
var id = serialized.Id;
|
||||||
@ -53,7 +78,7 @@ public static class SpeciesDataLoader
|
|||||||
x => DeserializeForm(x.Key, x.Value, typeLibrary));
|
x => DeserializeForm(x.Key, x.Value, typeLibrary));
|
||||||
var evolutions = serialized.Evolutions.Select(DeserializeEvolution).ToList();
|
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.CatchRate, serialized.BaseHappiness, forms,
|
||||||
serialized.Flags.Select(x => new StringKey(x)), evolutions, serialized.EggGroups.Select(x => (StringKey)x));
|
serialized.Flags.Select(x => new StringKey(x)), evolutions, serialized.EggGroups.Select(x => (StringKey)x));
|
||||||
return species;
|
return species;
|
||||||
@ -83,10 +108,13 @@ public static class SpeciesDataLoader
|
|||||||
private static ILearnableMoves DeserializeMoves(SerializedMoves moves)
|
private static ILearnableMoves DeserializeMoves(SerializedMoves moves)
|
||||||
{
|
{
|
||||||
var learnableMoves = new LearnableMovesImpl();
|
var learnableMoves = new LearnableMovesImpl();
|
||||||
|
if (moves.LevelMoves != null)
|
||||||
foreach (var levelMove in moves.LevelMoves)
|
foreach (var levelMove in moves.LevelMoves)
|
||||||
{
|
{
|
||||||
learnableMoves.AddLevelMove((byte)levelMove.Level, new StringKey(levelMove.Name));
|
learnableMoves.AddLevelMove((byte)levelMove.Level, new StringKey(levelMove.Name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (moves.EggMoves != null)
|
||||||
foreach (var eggMove in moves.EggMoves)
|
foreach (var eggMove in moves.EggMoves)
|
||||||
{
|
{
|
||||||
learnableMoves.AddEggMove(new StringKey(eggMove));
|
learnableMoves.AddEggMove(new StringKey(eggMove));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user