Implements dataloading
This commit is contained in:
52
PkmnLib.Dataloader/AbilityDataLoader.cs
Normal file
52
PkmnLib.Dataloader/AbilityDataLoader.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using PkmnLib.Dataloader.Models;
|
||||
using PkmnLib.Static.Libraries;
|
||||
using PkmnLib.Static.Species;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class AbilityDataLoader
|
||||
{
|
||||
public static AbilityLibrary LoadAbilities(Stream stream)
|
||||
{
|
||||
var library = new AbilityLibrary();
|
||||
var obj = JsonSerializer.Deserialize<JsonObject>(stream);
|
||||
if (obj == null)
|
||||
throw new InvalidDataException("Ability data is empty.");
|
||||
obj.Remove("$schema");
|
||||
var cleanedString = obj.ToJsonString();
|
||||
|
||||
var objects = JsonSerializer.Deserialize<Dictionary<string, SerializedAbility>>(cleanedString,
|
||||
new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
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);
|
||||
return library;
|
||||
}
|
||||
|
||||
private static AbilityImpl DeserializeAbility(string name, SerializedAbility serialized)
|
||||
{
|
||||
var effect = serialized.Effect;
|
||||
var parameters = serialized.Parameters.ToDictionary(x => (StringKey)x.Key, x => x.Value.ToParameter());
|
||||
|
||||
StringKey? effectName;
|
||||
if (effect == null)
|
||||
effectName = null;
|
||||
else
|
||||
effectName = new StringKey(effect);
|
||||
|
||||
var ability = new AbilityImpl(name, effectName, parameters);
|
||||
return ability;
|
||||
}
|
||||
}
|
||||
22
PkmnLib.Dataloader/GrowthRateDataLoader.cs
Normal file
22
PkmnLib.Dataloader/GrowthRateDataLoader.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Libraries;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class GrowthRateDataLoader
|
||||
{
|
||||
public static GrowthRateLibrary LoadGrowthRates(Stream stream)
|
||||
{
|
||||
var objects = JsonSerializer.Deserialize<Dictionary<string, uint[]>>(stream)!;
|
||||
var library = new GrowthRateLibrary();
|
||||
foreach (var (key, value) in objects)
|
||||
{
|
||||
var growthRate = new LookupGrowthRate(key, value);
|
||||
library.Add(growthRate);
|
||||
}
|
||||
return library;
|
||||
}
|
||||
}
|
||||
40
PkmnLib.Dataloader/ItemDataLoader.cs
Normal file
40
PkmnLib.Dataloader/ItemDataLoader.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using PkmnLib.Dataloader.Models;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Libraries;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class ItemDataLoader
|
||||
{
|
||||
public static ItemLibrary LoadItems(Stream stream)
|
||||
{
|
||||
var library = new ItemLibrary();
|
||||
var obj = JsonSerializer.Deserialize<SerializedItem[]>(stream, new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
if (obj == null)
|
||||
throw new InvalidDataException("Item data is empty.");
|
||||
var items = obj.Select(DeserializeItem);
|
||||
foreach (var i in items)
|
||||
library.Add(i);
|
||||
return library;
|
||||
}
|
||||
|
||||
private static IItem DeserializeItem(SerializedItem serialized)
|
||||
{
|
||||
if (!Enum.TryParse<ItemCategory>(serialized.ItemType, true, out var itemType))
|
||||
throw new InvalidDataException($"Item type {serialized.ItemType} is not valid for item {serialized.Name}.");
|
||||
BattleItemCategory battleType;
|
||||
Enum.TryParse(serialized.BattleType, true, out battleType);
|
||||
|
||||
return new ItemImpl(serialized.Name, itemType, battleType, serialized.Price,
|
||||
serialized.Flags.Select(x => (StringKey)x).ToImmutableHashSet());
|
||||
}
|
||||
}
|
||||
40
PkmnLib.Dataloader/JsonParameterLoader.cs
Normal file
40
PkmnLib.Dataloader/JsonParameterLoader.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
internal static class JsonParameterLoader
|
||||
{
|
||||
internal static object? ToParameter(this JsonNode node)
|
||||
{
|
||||
switch (node.GetValueKind())
|
||||
{
|
||||
case JsonValueKind.Undefined:
|
||||
throw new InvalidOperationException("Undefined value.");
|
||||
case JsonValueKind.Object:
|
||||
return node.AsObject().ToDictionary(x => (StringKey)x.Key, x => x.Value?.ToParameter());
|
||||
case JsonValueKind.Array:
|
||||
return node.AsArray().Select(x => x?.ToParameter()).ToList();
|
||||
case JsonValueKind.String:
|
||||
return node.GetValue<string>();
|
||||
case JsonValueKind.Number:
|
||||
var element = node.GetValue<JsonElement>();
|
||||
if (element.TryGetInt32(out var v))
|
||||
return v;
|
||||
if (element.TryGetSingle(out var f))
|
||||
return f;
|
||||
throw new InvalidOperationException("Number is not an integer or a float.");
|
||||
case JsonValueKind.True:
|
||||
return true;
|
||||
case JsonValueKind.False:
|
||||
return false;
|
||||
case JsonValueKind.Null:
|
||||
return null;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
PkmnLib.Dataloader/Models/SerializedAbility.cs
Normal file
11
PkmnLib.Dataloader/Models/SerializedAbility.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Nodes;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dataloader.Models;
|
||||
|
||||
public class SerializedAbility
|
||||
{
|
||||
public string? Effect { get; set; }
|
||||
public Dictionary<string, JsonNode> Parameters { get; set; } = new();
|
||||
}
|
||||
11
PkmnLib.Dataloader/Models/SerializedItem.cs
Normal file
11
PkmnLib.Dataloader/Models/SerializedItem.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace PkmnLib.Dataloader.Models;
|
||||
|
||||
public class SerializedItem
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string ItemType { get; set; }
|
||||
public string BattleType { get; set; }
|
||||
public string[] Flags { get; set; }
|
||||
public int Price { get; set; }
|
||||
public byte FlingPower { get; set; }
|
||||
}
|
||||
30
PkmnLib.Dataloader/Models/SerializedMove.cs
Normal file
30
PkmnLib.Dataloader/Models/SerializedMove.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PkmnLib.Dataloader.Models;
|
||||
|
||||
public class SerializedMoveDataWrapper
|
||||
{
|
||||
public SerializedMove[] Data { get; set; }
|
||||
}
|
||||
|
||||
public class SerializedMove
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Type { get; set; }
|
||||
public byte Power { get; set; }
|
||||
public byte PP { get; set; }
|
||||
public byte Accuracy { get; set; }
|
||||
public sbyte Priority { get; set; }
|
||||
public string Target { get; set; }
|
||||
public string Category { get; set; }
|
||||
public string[] Flags { get; set; }
|
||||
public SerializedMoveEffect? Effect { get; set; }
|
||||
}
|
||||
|
||||
public class SerializedMoveEffect
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public float Chance { get; set; }
|
||||
public Dictionary<string, JsonNode>? Parameters { get; set; }
|
||||
}
|
||||
68
PkmnLib.Dataloader/Models/SerializedSpecies.cs
Normal file
68
PkmnLib.Dataloader/Models/SerializedSpecies.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace PkmnLib.Dataloader.Models;
|
||||
|
||||
public class SerializedSpecies
|
||||
{
|
||||
public string Species { get; set; }
|
||||
public ushort Id { get; set; }
|
||||
public float GenderRatio { get; set; }
|
||||
public string GrowthRate { get; set; }
|
||||
public byte BaseHappiness { get; set; }
|
||||
public byte CatchRate { get; set; }
|
||||
public string Color { get; set; }
|
||||
public bool GenderDifference { get; set; }
|
||||
public string[] EggGroups { get; set; }
|
||||
public int EggCycles { get; set; }
|
||||
public string[] Flags { get; set; } = [];
|
||||
public Dictionary<string, SerializedForm> Formes { get; set; }
|
||||
public SerializedEvolution[] Evolutions { get; set; } = [];
|
||||
}
|
||||
|
||||
public class SerializedForm
|
||||
{
|
||||
public string[] Abilities { get; set; }
|
||||
public string[] HiddenAbilities { get; set; } = [];
|
||||
public SerializedStats BaseStats { get; set; }
|
||||
public SerializedStats EVReward { get; set; }
|
||||
public string[] Types { get; set; }
|
||||
public float Height { get; set; }
|
||||
public float Weight { get; set; }
|
||||
public uint BaseExp { get; set; }
|
||||
public bool IsMega { get; set; }
|
||||
public SerializedMoves Moves { get; set; }
|
||||
public string[] Flags { get; set; } = [];
|
||||
}
|
||||
|
||||
public class SerializedEvolution
|
||||
{
|
||||
public string Species { get; set; }
|
||||
public string Method { get; set; }
|
||||
public JsonNode Data { get; set; }
|
||||
}
|
||||
|
||||
public class SerializedStats
|
||||
{
|
||||
public ushort Hp { get; set; }
|
||||
public ushort Attack { get; set; }
|
||||
public ushort Defense { get; set; }
|
||||
public ushort SpecialAttack { get; set; }
|
||||
public ushort SpecialDefense { get; set; }
|
||||
public ushort Speed { get; set; }
|
||||
}
|
||||
|
||||
public class SerializedLevelMove
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public uint Level { get; set; }
|
||||
}
|
||||
|
||||
public class SerializedMoves
|
||||
{
|
||||
public SerializedLevelMove[] LevelMoves { get; set; }
|
||||
public string[] EggMoves { get; set; }
|
||||
public string[] TutorMoves { get; set; }
|
||||
public string[] Machine { get; set; }
|
||||
}
|
||||
70
PkmnLib.Dataloader/MoveDataLoader.cs
Normal file
70
PkmnLib.Dataloader/MoveDataLoader.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using PkmnLib.Dataloader.Models;
|
||||
using PkmnLib.Static.Libraries;
|
||||
using PkmnLib.Static.Moves;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class MoveDataLoader
|
||||
{
|
||||
public static MoveLibrary LoadMoves(Stream stream, TypeLibrary typeLibrary)
|
||||
{
|
||||
var library = new MoveLibrary();
|
||||
var objects = JsonSerializer.Deserialize<SerializedMoveDataWrapper>(stream,
|
||||
new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
});
|
||||
if (objects == null)
|
||||
throw new InvalidDataException("Move data is empty.");
|
||||
var moves = objects.Data.Select(x => DeserializeMove(x, typeLibrary));
|
||||
foreach (var m in moves)
|
||||
library.Add(m);
|
||||
return library;
|
||||
}
|
||||
|
||||
private static MoveDataImpl DeserializeMove(SerializedMove serialized, TypeLibrary typeLibrary)
|
||||
{
|
||||
var type = serialized.Type;
|
||||
var power = serialized.Power;
|
||||
var pp = serialized.PP;
|
||||
var accuracy = serialized.Accuracy;
|
||||
var priority = serialized.Priority;
|
||||
var target = serialized.Target;
|
||||
var category = serialized.Category;
|
||||
var flags = serialized.Flags;
|
||||
var effect = serialized.Effect;
|
||||
|
||||
if (target.Equals("self", StringComparison.InvariantCultureIgnoreCase))
|
||||
target = "selfUse";
|
||||
|
||||
if (!typeLibrary.TryGetTypeIdentifier(type, out var typeIdentifier))
|
||||
throw new InvalidDataException($"Type {type} is not a valid type.");
|
||||
if (!Enum.TryParse<MoveCategory>(category, true, out var categoryEnum))
|
||||
throw new InvalidDataException($"Category {category} is not a valid category.");
|
||||
if (!Enum.TryParse<MoveTarget>(target, true, out var targetEnum))
|
||||
throw new InvalidDataException($"Target {target} is not a valid target.");
|
||||
var secondaryEffect = ParseEffect(effect);
|
||||
|
||||
var move = new MoveDataImpl(serialized.Name, typeIdentifier, categoryEnum, power, accuracy, pp, targetEnum,
|
||||
priority, secondaryEffect, flags.Select(x => (StringKey)x).ToImmutableHashSet());
|
||||
return move;
|
||||
}
|
||||
|
||||
private static ISecondaryEffect? ParseEffect(SerializedMoveEffect? effect)
|
||||
{
|
||||
if (effect == null)
|
||||
return null;
|
||||
var name = effect.Name;
|
||||
var chance = effect.Chance;
|
||||
var parameters = effect.Parameters?.ToDictionary(x => (StringKey)x.Key, x => x.Value.ToParameter()) ??
|
||||
new Dictionary<StringKey, object?>();
|
||||
return new SecondaryEffectImpl(chance, name, parameters);
|
||||
}
|
||||
}
|
||||
60
PkmnLib.Dataloader/NatureDataLoader.cs
Normal file
60
PkmnLib.Dataloader/NatureDataLoader.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Libraries;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class NatureDataLoader
|
||||
{
|
||||
private static readonly char[] CommonCsvDelimiters = ['|', ','];
|
||||
|
||||
public static NatureLibrary LoadNatureLibrary(Stream stream)
|
||||
{
|
||||
var library = new NatureLibrary();
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
var header = reader.ReadLine();
|
||||
if (header == null)
|
||||
throw new InvalidDataException("Type data is empty.");
|
||||
var delimiter = CommonCsvDelimiters.FirstOrDefault(header.Contains);
|
||||
if (delimiter == default)
|
||||
throw new InvalidDataException("No valid delimiter found in type data.");
|
||||
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (line == null)
|
||||
break;
|
||||
var values = line.Split(delimiter)!;
|
||||
var nature = values[0];
|
||||
var increasedStat = values[1];
|
||||
var decreasedStat = values[2];
|
||||
|
||||
var increasedModifier = 1.1f;
|
||||
var decreasedModifier = 0.9f;
|
||||
|
||||
if (increasedStat == string.Empty)
|
||||
{
|
||||
increasedStat = "Hp";
|
||||
increasedModifier = 1.0f;
|
||||
}
|
||||
|
||||
if (decreasedStat == string.Empty)
|
||||
{
|
||||
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))
|
||||
throw new InvalidDataException($"Decreased stat {decreasedStat} is not a valid stat.");
|
||||
|
||||
library.Add(new Nature(nature, increasedStatEnum, decreasedStatEnum, increasedModifier, decreasedModifier));
|
||||
}
|
||||
|
||||
return library;
|
||||
}
|
||||
}
|
||||
17
PkmnLib.Dataloader/PkmnLib.Dataloader.csproj
Normal file
17
PkmnLib.Dataloader/PkmnLib.Dataloader.csproj
Normal file
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>12</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Json" Version="8.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PkmnLib.Static\PkmnLib.Static.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
206
PkmnLib.Dataloader/SpeciesDataLoader.cs
Normal file
206
PkmnLib.Dataloader/SpeciesDataLoader.cs
Normal file
@@ -0,0 +1,206 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using PkmnLib.Dataloader.Models;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Libraries;
|
||||
using PkmnLib.Static.Species;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class SpeciesDataLoader
|
||||
{
|
||||
public static SpeciesLibrary LoadSpecies(Stream stream, IReadOnlyTypeLibrary typeLibrary)
|
||||
{
|
||||
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,
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
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 = new SpeciesImpl(serialized.Id, serialized.Species, genderRate, serialized.GrowthRate,
|
||||
serialized.CatchRate, serialized.BaseHappiness, forms,
|
||||
serialized.Flags.Select(x => new StringKey(x)).ToImmutableHashSet(), evolutions);
|
||||
return species;
|
||||
}
|
||||
|
||||
private static IForm DeserializeForm(string name, SerializedForm form, IReadOnlyTypeLibrary typeLibrary)
|
||||
{
|
||||
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
|
||||
: throw new InvalidDataException($"Type {x} for form {name} is invalid.")).ToList();
|
||||
|
||||
return new FormImpl(name, form.Height, form.Weight, form.BaseExp, types, DeserializeStats(form.BaseStats),
|
||||
form.Abilities.Select(x => new StringKey(x)).ToList(),
|
||||
form.HiddenAbilities.Select(x => new StringKey(x)).ToList(), DeserializeMoves(form.Moves),
|
||||
form.Flags.Select(x => new StringKey(x)).ToImmutableHashSet());
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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 IEvolution DeserializeEvolution(SerializedEvolution evolution)
|
||||
{
|
||||
return evolution.Method.ToLowerInvariant() switch
|
||||
{
|
||||
"level" => new LevelEvolution
|
||||
{
|
||||
Level = evolution.Data.GetValue<byte>(),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"levelfemale" => new LevelGenderEvolution
|
||||
{
|
||||
Level = evolution.Data.GetValue<byte>(),
|
||||
Gender = Gender.Female,
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"levelmale" => new LevelGenderEvolution
|
||||
{
|
||||
Level = evolution.Data.GetValue<byte>(),
|
||||
Gender = Gender.Male,
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"happiness" => new HappinessEvolution
|
||||
{
|
||||
Happiness = evolution.Data.GetValue<byte>(),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"happinessday" => new HappinessDayEvolution()
|
||||
{
|
||||
Happiness = evolution.Data.GetValue<byte>(),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"happinessnight" => new HappinessNightEvolution()
|
||||
{
|
||||
Happiness = evolution.Data.GetValue<byte>(),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"item" => new ItemUseEvolution()
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"itemmale" => new ItemGenderEvolution
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"itemfemale" => new ItemGenderEvolution
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"holditem" => new HoldItemEvolution()
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"dayholditem" => new DayHoldItemEvolution()
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"nightholditem" => new NightHoldItemEvolution()
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"hasmove" => new HasMoveEvolution
|
||||
{
|
||||
MoveName = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Move is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"trade" => new TradeEvolution
|
||||
{
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"tradespecies" => new TradeSpeciesEvolution
|
||||
{
|
||||
WithSpecies = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Species is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"tradeitem" => new TradeItemEvolution
|
||||
{
|
||||
Item = evolution.Data.GetValue<string>() ?? throw new InvalidDataException("Item is null."),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"location" => new CustomEvolution
|
||||
{
|
||||
Name = "location",
|
||||
Parameters = new Dictionary<StringKey, object?>
|
||||
{
|
||||
["location"] = evolution.Data.ToString() ?? throw new InvalidDataException("Location is null.")
|
||||
},
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
"custom" => new CustomEvolution
|
||||
{
|
||||
Name = evolution.Data.AsObject()["type"]?.GetValue<string>() ??
|
||||
throw new InvalidDataException("Type is null."),
|
||||
Parameters = evolution.Data.AsObject()
|
||||
.Where(x => x.Key != "type")
|
||||
.ToDictionary(x => new StringKey(x.Key), x => x.Value!.ToParameter()),
|
||||
ToSpecies = evolution.Species,
|
||||
},
|
||||
_ => throw new InvalidDataException($"Evolution type {evolution.Method} is invalid.")
|
||||
};
|
||||
}
|
||||
}
|
||||
53
PkmnLib.Dataloader/TypeDataLoader.cs
Normal file
53
PkmnLib.Dataloader/TypeDataLoader.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using PkmnLib.Static;
|
||||
using PkmnLib.Static.Libraries;
|
||||
|
||||
namespace PkmnLib.Dataloader;
|
||||
|
||||
public static class TypeDataLoader
|
||||
{
|
||||
private static readonly char[] CommonCsvDelimiters = ['|', ','];
|
||||
|
||||
public static TypeLibrary LoadTypeLibrary(Stream stream)
|
||||
{
|
||||
var library = new TypeLibrary();
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
var header = reader.ReadLine();
|
||||
if (header == null)
|
||||
throw new InvalidDataException("Type data is empty.");
|
||||
var delimiter = CommonCsvDelimiters.FirstOrDefault(header.Contains);
|
||||
if (delimiter == default)
|
||||
throw new InvalidDataException("No valid delimiter found in type data.");
|
||||
|
||||
var types = header.Split(delimiter, StringSplitOptions.RemoveEmptyEntries)!;
|
||||
if (!types.Any())
|
||||
throw new InvalidDataException("No types found in type data.");
|
||||
|
||||
foreach (var type in types)
|
||||
library.RegisterType(type);
|
||||
|
||||
while (!reader.EndOfStream)
|
||||
{
|
||||
var line = reader.ReadLine();
|
||||
if (line == null)
|
||||
break;
|
||||
var values = line.Split(delimiter, StringSplitOptions.RemoveEmptyEntries)!;
|
||||
var type = values[0];
|
||||
if (!library.TryGetTypeIdentifier(type, out var typeId))
|
||||
throw new InvalidDataException($"Type {type} not found in type library.");
|
||||
for (var i = 1; i < values.Length; i++)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return library;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user