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;
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
}
|
2025-01-10 10:11:50 +00:00
|
|
|
|
|
|
|
// ReSharper disable once MemberCanBePrivate.Global
|
2024-09-30 17:23:20 +00:00
|
|
|
public static Func<SerializedItem, StringKey, ItemCategory, BattleItemCategory, int,
|
2025-01-10 10:11:50 +00:00
|
|
|
IEnumerable<StringKey>, ISecondaryEffect?, ISecondaryEffect?,
|
|
|
|
// ReSharper disable once FieldCanBeMadeReadOnly.Global
|
|
|
|
IItem> ItemConstructor = (_, name, type, battleType, price, flags, effect, battleTriggerEffect) =>
|
|
|
|
new ItemImpl(name, type, battleType, price, flags, effect, battleTriggerEffect);
|
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-01-10 10:11:50 +00:00
|
|
|
serialized.Flags.Select(x => (StringKey)x).ToImmutableHashSet(), effect, battleTriggerEffect);
|
2024-08-18 12:22:50 +00:00
|
|
|
}
|
|
|
|
}
|