2024-08-18 12:22:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.Json;
|
|
|
|
using PkmnLib.Dataloader.Models;
|
2024-10-01 08:22:53 +00:00
|
|
|
using PkmnLib.Static;
|
2024-08-18 12:22:50 +00:00
|
|
|
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();
|
2025-02-08 08:49:16 +00:00
|
|
|
var objects = JsonSerializer.Deserialize<SerializedMoveDataWrapper>(stream, JsonOptions.DefaultOptions);
|
2024-08-18 12:22:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-03-02 16:19:57 +00:00
|
|
|
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);
|
2024-10-01 08:22:53 +00:00
|
|
|
|
2024-08-18 12:22:50 +00:00
|
|
|
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.");
|
2025-01-10 10:11:50 +00:00
|
|
|
var secondaryEffect = effect.ParseEffect();
|
2024-08-18 12:22:50 +00:00
|
|
|
|
2025-03-02 16:19:57 +00:00
|
|
|
var move = MoveConstructor(serialized, serialized.Name, typeIdentifier, categoryEnum, power, accuracy, pp,
|
|
|
|
targetEnum, priority, secondaryEffect, flags.Select(x => (StringKey)x).ToImmutableHashSet());
|
2024-08-18 12:22:50 +00:00
|
|
|
return move;
|
|
|
|
}
|
|
|
|
}
|