using System.Collections.Generic;
using System.IO;
using System.Linq;
using static PKHeX.Core.MessageStrings;
namespace PKHeX.Core
{
///
/// Utility logic for dealing with objects.
///
public static class MysteryUtil
{
///
/// Gets objects from a folder.
///
/// Folder path
/// Consumable list of gifts.
public static IEnumerable GetGiftsFromFolder(string folder)
{
foreach (var file in Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories))
{
var fi = new FileInfo(file);
if (!MysteryGift.IsMysteryGift(fi.Length))
continue;
var gift = MysteryGift.GetMysteryGift(File.ReadAllBytes(file), fi.Extension);
if (gift != null)
yield return gift;
}
}
///
/// Gets a description of the using the current default string data.
///
/// Gift data to parse
/// List of lines
public static IEnumerable GetDescription(this MysteryGift gift) => gift.GetDescription(GameInfo.Strings);
///
/// Gets a description of the using provided string data.
///
/// Gift data to parse
/// String data to use
/// List of lines
public static IEnumerable GetDescription(this MysteryGift gift, IBasicStrings strings)
{
if (gift.Empty)
return new[] { MsgMysteryGiftSlotEmpty };
var result = new List { gift.CardHeader };
if (gift.IsItem)
{
AddLinesItem(gift, strings, result);
}
else if (gift.IsPokémon)
{
try
{
AddLinesPKM(gift, strings, result);
}
catch { result.Add(MsgMysteryGiftParseFail); }
}
else
{
switch (gift)
{
case WC7 { IsBP: true } w7bp:
result.Add($"BP: {w7bp.BP}");
break;
case WC7 { IsBean: true } w7bean:
result.Add($"Bean ID: {w7bean.Bean}");
result.Add($"Quantity: {w7bean.Quantity}");
break;
default:
result.Add(MsgMysteryGiftParseTypeUnknown);
break;
}
}
switch (gift)
{
case WC7 w7:
result.Add($"Repeatable: {w7.GiftRepeatable}");
result.Add($"Collected: {w7.GiftUsed}");
result.Add($"Once Per Day: {w7.GiftOncePerDay}");
break;
}
return result;
}
private static void AddLinesItem(MysteryGift gift, IBasicStrings strings, ICollection result)
{
result.Add($"Item: {strings.Item[gift.ItemID]} (Quantity: {gift.Quantity})");
if (gift is not WC7 wc7)
return;
for (var ind = 1; wc7.GetItem(ind) != 0; ind++)
{
result.Add($"Item: {strings.Item[wc7.GetItem(ind)]} (Quantity: {wc7.GetQuantity(ind)})");
}
}
private static void AddLinesPKM(MysteryGift gift, IBasicStrings strings, ICollection result)
{
var id = gift.Generation < 7 ? $"{gift.TID:D5}/{gift.SID:D5}" : $"[{gift.TrainerSID7:D4}]{gift.TrainerID7:D6}";
var first =
$"{strings.Species[gift.Species]} @ {strings.Item[gift.HeldItem >= 0 ? gift.HeldItem : 0]} --- "
+ (gift.IsEgg ? strings.EggName : $"{gift.OT_Name} - {id}");
result.Add(first);
result.Add(string.Join(" / ", gift.Moves.Select(z => strings.Move[z])));
if (gift is WC7 wc7)
{
var addItem = wc7.AdditionalItem;
if (addItem != 0)
result.Add($"+ {strings.Item[addItem]}");
}
}
///
/// Checks if the data is compatible with the . Sets appropriate data to the save file in order to receive the gift.
///
/// Gift data to potentially insert to the save file.
/// Save file receiving the gift data.
/// Error message if incompatible.
/// True if compatible, false if incompatible.
public static bool IsCardCompatible(this MysteryGift g, SaveFile sav, out string message)
{
if (g.Generation != sav.Generation)
{
message = MsgMysteryGiftSlotSpecialReject;
return false;
}
if (!sav.CanReceiveGift(g))
{
message = MsgMysteryGiftTypeDetails;
return false;
}
if (g is WC6 && g.CardID == 2048 && g.ItemID == 726) // Eon Ticket (OR/AS)
{
if (sav is not SAV6AO)
{
message = MsgMysteryGiftSlotSpecialReject;
return false;
}
}
message = string.Empty;
return true;
}
///
/// Checks if the gift values are receivable by the game.
///
/// Save file receiving the gift data.
/// Gift data to potentially insert to the save file.
/// True if compatible, false if incompatible.
public static bool CanReceiveGift(this SaveFile sav, MysteryGift gift)
{
if (gift.Species > sav.MaxSpeciesID)
return false;
if (gift.Moves.Any(move => move > sav.MaxMoveID))
return false;
if (gift.HeldItem > sav.MaxItemID)
return false;
return true;
}
}
}