2019-06-09 02:56:11 +00:00
using System.Collections.Generic ;
2019-09-23 23:56:47 +00:00
using System.IO ;
2018-04-21 16:55:27 +00:00
using System.Linq ;
using static PKHeX . Core . MessageStrings ;
namespace PKHeX.Core
{
2019-09-10 07:21:51 +00:00
/// <summary>
/// Utility logic for dealing with <see cref="MysteryGift"/> objects.
/// </summary>
2018-04-21 16:55:27 +00:00
public static class MysteryUtil
{
2019-09-23 23:56:47 +00:00
/// <summary>
/// Gets <see cref="MysteryGift"/> objects from a folder.
/// </summary>
/// <param name="folder">Folder path</param>
/// <returns>Consumable list of gifts.</returns>
public static IEnumerable < MysteryGift > GetGiftsFromFolder ( string folder )
{
foreach ( var file in Directory . EnumerateFiles ( folder , "*" , SearchOption . AllDirectories ) )
{
var fi = new FileInfo ( file ) ;
if ( ! MysteryGift . IsMysteryGift ( fi . Length ) )
continue ;
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
var gift = MysteryGift . GetMysteryGift ( File . ReadAllBytes ( file ) , fi . Extension ) ;
if ( gift ! = null )
yield return gift ;
2019-09-23 23:56:47 +00:00
}
}
2018-04-21 16:55:27 +00: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 ) ;
/// <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 )
return new [ ] { MsgMysteryGiftSlotEmpty } ;
var result = new List < string > { gift . CardHeader } ;
if ( gift . IsItem )
{
AddLinesItem ( gift , strings , result ) ;
}
else if ( gift . IsPokémon )
{
try
{
AddLinesPKM ( gift , strings , result ) ;
}
catch { result . Add ( MsgMysteryGiftParseFail ) ; }
}
else if ( gift . IsBP )
{
result . Add ( $"BP: {gift.BP}" ) ;
}
else if ( gift . IsBean )
{
result . Add ( $"Bean ID: {gift.Bean}" ) ;
result . Add ( $"Quantity: {gift.Quantity}" ) ;
}
else { result . Add ( MsgMysteryGiftParseTypeUnknown ) ; }
if ( gift is WC7 w7 )
{
result . Add ( $"Repeatable: {w7.GiftRepeatable}" ) ;
result . Add ( $"Collected: {w7.GiftUsed}" ) ;
result . Add ( $"Once Per Day: {w7.GiftOncePerDay}" ) ;
}
return result ;
}
private static void AddLinesItem ( MysteryGift gift , IBasicStrings strings , ICollection < string > result )
{
result . Add ( $"Item: {strings.Item[gift.ItemID]} (Quantity: {gift.Quantity})" ) ;
2019-10-08 01:40:09 +00:00
if ( ! ( gift is WC7 wc7 ) )
return ;
for ( var ind = 1 ; wc7 . GetItem ( ind ) ! = 0 ; ind + + )
2018-04-21 16:55:27 +00:00
{
2019-10-08 01:40:09 +00:00
result . Add ( $"Item: {strings.Item[wc7.GetItem(ind)]} (Quantity: {wc7.GetQuantity(ind)})" ) ;
2018-04-21 16:55:27 +00:00
}
}
2018-07-27 02:34:27 +00:00
2018-04-21 16:55:27 +00:00
private static void AddLinesPKM ( MysteryGift gift , IBasicStrings strings , ICollection < string > result )
{
2019-05-11 07:59:07 +00:00
var id = gift . Format < 7 ? $"{gift.TID:D5}/{gift.SID:D5}" : $"[{gift.TrainerSID7:D4}]{gift.TrainerID7:D6}" ;
2018-04-21 16:55:27 +00:00
var first =
$"{strings.Species[gift.Species]} @ {strings.Item[gift.HeldItem]} --- "
+ ( 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]}" ) ;
}
}
2018-08-10 04:53:39 +00: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>
2018-04-21 16:55:27 +00:00
public static bool IsCardCompatible ( this MysteryGift g , SaveFile SAV , out string message )
{
if ( g . Format ! = SAV . Generation )
{
message = MsgMysteryGiftSlotSpecialReject ;
return false ;
}
2018-08-10 04:53:39 +00:00
if ( ! SAV . CanRecieveGift ( g ) )
{
message = MsgMysteryGiftTypeDetails ;
return false ;
}
2018-04-21 16:55:27 +00:00
if ( g is WC6 & & g . CardID = = 2048 & & g . ItemID = = 726 ) // Eon Ticket (OR/AS)
{
2019-06-09 02:56:11 +00:00
if ( ! ( SAV is SAV6AO ) )
2018-04-21 16:55:27 +00:00
{
message = MsgMysteryGiftSlotSpecialReject ;
return false ;
}
}
PKHeX.Core Nullable cleanup (#2401)
* Handle some nullable cases
Refactor MysteryGift into a second abstract class (backed by a byte array, or fake data)
Make some classes have explicit constructors instead of { } initialization
* Handle bits more obviously without null
* Make SaveFile.BAK explicitly readonly again
* merge constructor methods to have readonly fields
* Inline some properties
* More nullable handling
* Rearrange box actions
define straightforward classes to not have any null properties
* Make extrabyte reference array immutable
* Move tooltip creation to designer
* Rearrange some logic to reduce nesting
* Cache generated fonts
* Split mystery gift album purpose
* Handle more tooltips
* Disallow null setters
* Don't capture RNG object, only type enum
* Unify learnset objects
Now have readonly properties which are never null
don't new() empty learnsets (>800 Learnset objects no longer created,
total of 2400 objects since we also new() a move & level array)
optimize g1/2 reader for early abort case
* Access rewrite
Initialize blocks in a separate object, and get via that object
removes a couple hundred "might be null" warnings since blocks are now readonly getters
some block references have been relocated, but interfaces should expose all that's needed
put HoF6 controls in a groupbox, and disable
* Readonly personal data
* IVs non nullable for mystery gift
* Explicitly initialize forced encounter moves
* Make shadow objects readonly & non-null
Put murkrow fix in binary data resource, instead of on startup
* Assign dex form fetch on constructor
Fixes legality parsing edge cases
also handle cxd parse for valid; exit before exception is thrown in FrameGenerator
* Remove unnecessary null checks
* Keep empty value until init
SetPouch sets the value to an actual one during load, but whatever
* Readonly team lock data
* Readonly locks
Put locked encounters at bottom (favor unlocked)
* Mail readonly data / offset
Rearrange some call flow and pass defaults
Add fake classes for SaveDataEditor mocking
Always party size, no need to check twice in stat editor
use a fake save file as initial data for savedata editor, and for
gamedata (wow i found a usage)
constrain eventwork editor to struct variable types (uint, int, etc),
thus preventing null assignment errors
2019-10-17 01:47:31 +00:00
message = string . Empty ;
2018-04-21 16:55:27 +00:00
return true ;
}
2018-07-29 23:39:15 +00:00
2018-08-10 04:53:39 +00:00
/// <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>
2018-07-29 23:39:15 +00:00
public static bool CanRecieveGift ( 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 ;
}
2018-04-21 16:55:27 +00:00
}
}