2024-05-31 00:42:03 -04:00
using System ;
2022-08-21 17:34:32 -07:00
using System.Collections.Generic ;
2019-09-23 16:56:47 -07:00
using System.IO ;
2018-04-21 09:55:27 -07:00
using static PKHeX . Core . MessageStrings ;
2022-06-18 11:04:24 -07:00
namespace PKHeX.Core ;
/// <summary>
/// Utility logic for dealing with <see cref="MysteryGift"/> objects.
/// </summary>
public static class MysteryUtil
2018-04-21 09:55:27 -07:00
{
2019-09-10 00:21:51 -07:00
/// <summary>
2022-06-18 11:04:24 -07:00
/// Gets <see cref="MysteryGift"/> objects from a folder.
2019-09-10 00:21:51 -07:00
/// </summary>
2022-06-18 11:04:24 -07:00
/// <param name="folder">Folder path</param>
/// <returns>Consumable list of gifts.</returns>
public static IEnumerable < MysteryGift > GetGiftsFromFolder ( string folder )
2018-04-21 09:55:27 -07:00
{
2022-06-18 11:04:24 -07:00
foreach ( var file in Directory . EnumerateFiles ( folder , "*" , SearchOption . AllDirectories ) )
2019-09-23 16:56:47 -07:00
{
2022-06-18 11:04:24 -07:00
var fi = new FileInfo ( file ) ;
if ( ! MysteryGift . IsMysteryGift ( fi . Length ) )
continue ;
2019-09-23 16:56:47 -07:00
2023-11-12 21:11:11 -08:00
var data = File . ReadAllBytes ( file ) ;
var gift = MysteryGift . GetMysteryGift ( data , fi . Extension ) ;
2022-06-18 11:04:24 -07:00
if ( gift ! = null )
yield return gift ;
2019-09-23 16:56:47 -07:00
}
2022-06-18 11:04:24 -07:00
}
2019-09-23 16:56:47 -07:00
2022-06-18 11:04:24 -07:00
/// <summary>
/// Gets a description of the <see cref="MysteryGift"/> using the current default string data.
/// </summary>
/// <param name="gift">Gift data to parse</param>
/// <returns>List of lines</returns>
public static IEnumerable < string > GetDescription ( this MysteryGift gift ) = > gift . GetDescription ( GameInfo . Strings ) ;
2018-04-21 09:55:27 -07:00
2022-06-18 11:04:24 -07:00
/// <summary>
/// Gets a description of the <see cref="MysteryGift"/> using provided string data.
/// </summary>
/// <param name="gift">Gift data to parse</param>
/// <param name="strings">String data to use</param>
/// <returns>List of lines</returns>
public static IEnumerable < string > GetDescription ( this MysteryGift gift , IBasicStrings strings )
{
if ( gift . Empty )
2023-12-17 20:38:53 -04:00
return [ MsgMysteryGiftSlotEmpty ] ;
2022-06-18 11:04:24 -07:00
var result = new List < string > { gift . CardHeader } ;
if ( gift . IsItem )
{
AddLinesItem ( gift , strings , result ) ;
}
else if ( gift . IsEntity )
{
try
2018-04-21 09:55:27 -07:00
{
2022-06-18 11:04:24 -07:00
AddLinesPKM ( gift , strings , result ) ;
2018-04-21 09:55:27 -07:00
}
2022-06-18 11:04:24 -07:00
catch { result . Add ( MsgMysteryGiftParseFail ) ; }
}
else
{
2021-03-14 16:16:55 -07:00
switch ( gift )
2018-04-21 09:55:27 -07:00
{
2022-06-18 11:04:24 -07:00
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 ;
2024-03-14 02:32:20 -05:00
case PCD pcd :
2024-05-31 00:42:03 -04:00
AddLinesPGT ( pcd . Gift , result ) ;
2024-03-14 02:32:20 -05:00
result . Add ( $"Collected: {pcd.GiftUsed}" ) ;
break ;
case PGT pgt :
2024-05-31 00:42:03 -04:00
AddLinesPGT ( pgt , result ) ;
2024-03-14 02:32:20 -05:00
break ;
2022-06-18 11:04:24 -07:00
default :
result . Add ( MsgMysteryGiftParseTypeUnknown ) ;
2021-03-14 16:16:55 -07:00
break ;
2018-04-21 09:55:27 -07:00
}
}
2022-06-18 11:04:24 -07:00
switch ( gift )
2018-04-21 09:55:27 -07:00
{
2022-06-18 11:04:24 -07:00
case WC7 w7 :
result . Add ( $"Repeatable: {w7.GiftRepeatable}" ) ;
result . Add ( $"Collected: {w7.GiftUsed}" ) ;
result . Add ( $"Once Per Day: {w7.GiftOncePerDay}" ) ;
break ;
}
return result ;
}
2019-10-07 18:40:09 -07:00
2023-12-03 20:13:20 -08:00
private static void AddLinesItem ( MysteryGift gift , IBasicStrings strings , List < string > result )
2022-06-18 11:04:24 -07:00
{
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)})" ) ;
2018-04-21 09:55:27 -07:00
}
2022-06-18 11:04:24 -07:00
}
2018-07-26 19:34:27 -07:00
2023-12-03 20:13:20 -08:00
private static void AddLinesPKM ( MysteryGift gift , IBasicStrings strings , List < string > result )
2022-06-18 11:04:24 -07:00
{
2023-01-21 20:02:33 -08:00
var id = gift . Generation < 7 ? $"{gift.TID16:D5}/{gift.SID16:D5}" : $"[{gift.TrainerSID7:D4}]{gift.TrainerTID7:D6}" ;
2022-06-18 11:04:24 -07:00
var first =
$"{strings.Species[gift.Species]} @ {strings.Item[gift.HeldItem >= 0 ? gift.HeldItem : 0]} --- "
2024-02-22 21:20:54 -06:00
+ ( gift . IsEgg ? strings . EggName : $"{gift.OriginalTrainerName} - {id}" ) ;
2022-06-18 11:04:24 -07:00
result . Add ( first ) ;
2022-08-21 17:34:32 -07:00
result . Add ( gift . Moves . GetMovesetLine ( strings . Move ) ) ;
2022-06-18 11:04:24 -07:00
if ( gift is WC7 wc7 )
2018-04-21 09:55:27 -07:00
{
2022-06-18 11:04:24 -07:00
var addItem = wc7 . AdditionalItem ;
if ( addItem ! = 0 )
result . Add ( $"+ {strings.Item[addItem]}" ) ;
}
}
2018-04-21 09:55:27 -07:00
2024-05-31 00:42:03 -04:00
private static void AddLinesPGT ( PGT gift , List < string > result )
{
static string Get ( ReadOnlySpan < string > list , int index )
{
if ( ( uint ) index > = list . Length )
return $"Unknown ({index})" ;
return list [ index ] ;
}
try
{
switch ( gift . GiftType )
{
case GiftType4 . Goods :
result . Add ( $"Goods: {Get(GameInfo.Strings.uggoods, gift.ItemID)}" ) ;
break ;
case GiftType4 . HasSubType :
switch ( gift . GiftSubType ) {
case GiftSubType4 . Seal :
result . Add ( $"Seal: {Get(GameInfo.Strings.seals, (int)gift.Seal)}" ) ;
break ;
case GiftSubType4 . Accessory :
result . Add ( $"Accessory: {Get(GameInfo.Strings.accessories, (int)gift.Accessory)}" ) ;
break ;
case GiftSubType4 . Backdrop :
result . Add ( $"Backdrop: {Get(GameInfo.Strings.backdrops, (int)gift.Backdrop)}" ) ;
break ;
}
break ;
case GiftType4 . PokétchApp :
result . Add ( $"Pokétch App: {Get(GameInfo.Strings.poketchapps, (int)gift.PoketchApp)}" ) ;
break ;
case GiftType4 . PokéwalkerCourse :
result . Add ( $"Route Map: {Get(GameInfo.Strings.walkercourses, gift.PokewalkerCourseID)}" ) ;
break ;
default :
result . Add ( $"{gift.GiftType}" ) ;
break ;
}
}
catch { result . Add ( MsgMysteryGiftParseFail ) ; }
}
2022-06-18 11:04:24 -07:00
/// <summary>
/// Checks if the <see cref="MysteryGift"/> data is compatible with the <see cref="SaveFile"/>. Sets appropriate data to the save file in order to receive the gift.
/// </summary>
/// <param name="g">Gift data to potentially insert to the save file.</param>
/// <param name="sav">Save file receiving the gift data.</param>
/// <param name="message">Error message if incompatible.</param>
/// <returns>True if compatible, false if incompatible.</returns>
public static bool IsCardCompatible ( this MysteryGift g , SaveFile sav , out string message )
{
if ( g . Generation ! = sav . Generation )
{
message = MsgMysteryGiftSlotSpecialReject ;
return false ;
}
2018-04-21 09:55:27 -07:00
2022-06-18 11:04:24 -07:00
if ( ! sav . CanReceiveGift ( g ) )
{
message = MsgMysteryGiftTypeDetails ;
return false ;
2018-04-21 09:55:27 -07:00
}
2024-02-22 21:20:54 -06:00
if ( g is WC6 { CardID : 2048 , ItemID : 726 } & & sav is not SAV6AO )
2018-04-21 09:55:27 -07:00
{
2024-02-22 21:20:54 -06:00
// Eon Ticket (OR/AS)
message = MsgMysteryGiftSlotSpecialReject ;
return false ;
2018-04-21 09:55:27 -07:00
}
2018-07-29 16:39:15 -07:00
2022-06-18 11:04:24 -07:00
message = string . Empty ;
return true ;
}
/// <summary>
/// Checks if the gift values are receivable by the game.
/// </summary>
/// <param name="sav">Save file receiving the gift data.</param>
/// <param name="gift">Gift data to potentially insert to the save file.</param>
/// <returns>True if compatible, false if incompatible.</returns>
public static bool CanReceiveGift ( this SaveFile sav , MysteryGift gift )
{
if ( gift . Species > sav . MaxSpeciesID )
return false ;
2022-08-21 17:34:32 -07:00
if ( gift . Moves . AnyAbove ( sav . MaxMoveID ) )
2022-06-18 11:04:24 -07:00
return false ;
if ( gift . HeldItem > sav . MaxItemID )
return false ;
return true ;
2018-04-21 09:55:27 -07:00
}
}