2024-08-18 12:22:50 +00:00
|
|
|
using System;
|
2024-09-30 17:23:20 +00:00
|
|
|
using System.Collections.Generic;
|
2024-08-18 12:22:50 +00:00
|
|
|
using System.Collections.Immutable;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text.Json;
|
2025-03-02 13:03:51 +00:00
|
|
|
using JetBrains.Annotations;
|
2024-08-18 12:22:50 +00:00
|
|
|
using PkmnLib.Dataloader.Models;
|
|
|
|
using PkmnLib.Static;
|
|
|
|
using PkmnLib.Static.Libraries;
|
2025-01-10 10:11:50 +00:00
|
|
|
using PkmnLib.Static.Moves;
|
2024-08-18 12:22:50 +00:00
|
|
|
using PkmnLib.Static.Utils;
|
|
|
|
|
|
|
|
namespace PkmnLib.Dataloader;
|
|
|
|
|
|
|
|
public static class ItemDataLoader
|
|
|
|
{
|
|
|
|
public static ItemLibrary LoadItems(Stream stream)
|
|
|
|
{
|
|
|
|
var library = new ItemLibrary();
|
2025-02-08 08:49:16 +00:00
|
|
|
var obj = JsonSerializer.Deserialize<SerializedItem[]>(stream, JsonOptions.DefaultOptions);
|
2024-08-18 12:22:50 +00:00
|
|
|
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;
|
|
|
|
}
|
2025-03-02 13:03:51 +00:00
|
|
|
|
|
|
|
public delegate IItem ItemFactoryDelegate(SerializedItem serialized, StringKey name, ItemCategory type,
|
|
|
|
BattleItemCategory battleType, int price, ImmutableHashSet<StringKey> flags,
|
|
|
|
ISecondaryEffect? effect, ISecondaryEffect? battleTriggerEffect, byte flingPower);
|
2025-01-10 10:11:50 +00:00
|
|
|
|
2025-03-02 13:03:51 +00:00
|
|
|
[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);
|
2024-08-18 12:22:50 +00:00
|
|
|
|
|
|
|
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}.");
|
2024-09-03 07:31:32 +00:00
|
|
|
Enum.TryParse(serialized.BattleType, true, out BattleItemCategory battleType);
|
2025-01-10 10:11:50 +00:00
|
|
|
var effect = serialized.Effect?.ParseEffect();
|
|
|
|
var battleTriggerEffect = serialized.BattleEffect?.ParseEffect();
|
2024-08-18 12:22:50 +00:00
|
|
|
|
2024-09-30 17:23:20 +00:00
|
|
|
return ItemConstructor(serialized, serialized.Name, itemType, battleType, serialized.Price,
|
2025-03-02 13:03:51 +00:00
|
|
|
serialized.Flags.Select(x => (StringKey)x).ToImmutableHashSet(), effect, battleTriggerEffect,
|
|
|
|
serialized.FlingPower);
|
2024-08-18 12:22:50 +00:00
|
|
|
}
|
|
|
|
}
|