2018-07-21 04:32:33 +00:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.IO ;
using System.Linq ;
using static PKHeX . Core . MessageStrings ;
namespace PKHeX.Core
{
2019-07-14 22:06:45 +00:00
/// <summary>
/// Extension methods for <see cref="SaveFile"/> syntax sugar.
/// </summary>
2018-07-21 04:32:33 +00:00
public static class SaveExtensions
{
/// <summary>
/// Checks if the <see cref="PKM"/> is compatible with the input <see cref="SaveFile"/>, and makes any necessary modifications to force compatibility.
/// </summary>
/// <remarks>Should only be used when forcing a backwards conversion to sanitize the PKM fields to the target format.
/// If the PKM is compatible, some properties may be forced to sanitized values.</remarks>
2019-07-14 22:06:45 +00:00
/// <param name="sav">Save File target that the PKM will be injected.</param>
2018-07-21 04:32:33 +00:00
/// <param name="pk">PKM input that is to be injected into the Save File.</param>
/// <returns>Indication whether or not the PKM is compatible.</returns>
2019-07-14 22:06:45 +00:00
public static bool IsPKMCompatibleWithModifications ( this SaveFile sav , PKM pk ) = > PKMConverter . IsPKMCompatibleWithModifications ( pk , sav ) ;
2018-07-21 04:32:33 +00:00
/// <summary>
/// Sets the details of a path to a <see cref="SaveFile"/> object.
/// </summary>
/// <param name="sav">Save File to set path details to.</param>
/// <param name="path">Full Path of the file</param>
public static void SetFileInfo ( this SaveFile sav , string path )
{
if ( ! sav . Exportable ) // Blank save file
{
2019-02-15 08:50:23 +00:00
sav . FileFolder = sav . FilePath = string . Empty ;
2018-07-21 04:32:33 +00:00
sav . FileName = "Blank Save File" ;
return ;
}
sav . FilePath = path ;
sav . FileFolder = Path . GetDirectoryName ( path ) ;
2018-07-30 04:54:02 +00:00
sav . FileName = string . Empty ;
var bakName = Util . CleanFileName ( sav . BAKName ) ;
2018-07-21 04:32:33 +00:00
sav . FileName = Path . GetFileName ( path ) ;
2018-07-30 04:54:02 +00:00
if ( sav . FileName ? . EndsWith ( bakName ) = = true )
sav . FileName = sav . FileName . Substring ( 0 , sav . FileName . Length - bakName . Length ) ;
2018-07-21 04:32:33 +00:00
}
2019-02-19 05:59:57 +00:00
/// <summary>
/// Gets suggested export options for the savefile.
/// </summary>
2019-07-14 22:06:45 +00:00
/// <param name="sav">SaveFile to be exported</param>
/// <param name="ext">Selected export extension</param>
2019-02-19 05:59:57 +00:00
public static ExportFlags GetSuggestedFlags ( this SaveFile sav , string ext )
{
var flags = ExportFlags . None ;
if ( ext = = ".dsv" )
flags | = ExportFlags . IncludeFooter ;
if ( ext = = ".gci" | | ( sav is IGCSaveFile gc & & ! gc . IsMemoryCardSave ) )
flags | = ExportFlags . IncludeHeader ;
return flags ;
}
2018-07-21 04:32:33 +00:00
/// <summary>
/// Checks a <see cref="PKM"/> file for compatibility to the <see cref="SaveFile"/>.
/// </summary>
2019-07-14 22:06:45 +00:00
/// <param name="sav"><see cref="SaveFile"/> that is being checked.</param>
2018-07-21 04:32:33 +00:00
/// <param name="pkm"><see cref="PKM"/> that is being tested for compatibility.</param>
/// <returns></returns>
2019-07-14 22:06:45 +00:00
public static IReadOnlyList < string > IsPKMCompatible ( this SaveFile sav , PKM pkm )
2018-07-21 04:32:33 +00:00
{
2019-07-14 22:06:45 +00:00
return sav . GetSaveFileErrata ( pkm , GameInfo . Strings ) ;
2018-07-21 04:32:33 +00:00
}
2019-07-14 22:06:45 +00:00
private static IReadOnlyList < string > GetSaveFileErrata ( this SaveFile sav , PKM pkm , IBasicStrings strings )
2018-07-21 04:32:33 +00:00
{
var errata = new List < string > ( ) ;
2019-07-14 22:06:45 +00:00
if ( sav . Generation > 1 )
2018-07-21 04:32:33 +00:00
{
ushort held = ( ushort ) pkm . HeldItem ;
2018-08-21 14:46:55 +00:00
var itemstr = GameInfo . Strings . GetItemStrings ( pkm . Format , ( GameVersion ) pkm . Version ) ;
if ( held > itemstr . Count )
2018-07-21 04:32:33 +00:00
errata . Add ( $"{MsgIndexItemRange} {held}" ) ;
2019-07-14 22:06:45 +00:00
else if ( held > sav . MaxItemID )
2018-08-21 14:46:55 +00:00
errata . Add ( $"{MsgIndexItemGame} {itemstr[held]}" ) ;
2019-07-14 22:06:45 +00:00
else if ( ! pkm . CanHoldItem ( sav . HeldItems ) )
2018-08-21 14:46:55 +00:00
errata . Add ( $"{MsgIndexItemHeld} {itemstr[held]}" ) ;
2018-07-21 04:32:33 +00:00
}
if ( pkm . Species > strings . Species . Count )
errata . Add ( $"{MsgIndexSpeciesRange} {pkm.Species}" ) ;
2019-07-14 22:06:45 +00:00
else if ( sav . MaxSpeciesID < pkm . Species )
2018-07-21 04:32:33 +00:00
errata . Add ( $"{MsgIndexSpeciesGame} {strings.Species[pkm.Species]}" ) ;
2019-07-14 22:06:45 +00:00
if ( ! sav . Personal [ pkm . Species ] . IsFormeWithinRange ( pkm . AltForm ) & & ! FormConverter . IsValidOutOfBoundsForme ( pkm . Species , pkm . AltForm , pkm . GenNumber ) )
errata . Add ( string . Format ( LegalityCheckStrings . LFormInvalidRange , Math . Max ( 0 , sav . Personal [ pkm . Species ] . FormeCount - 1 ) , pkm . AltForm ) ) ;
2018-07-21 04:32:33 +00:00
if ( pkm . Moves . Any ( m = > m > strings . Move . Count ) )
errata . Add ( $"{MsgIndexMoveRange} {string.Join(" , ", pkm.Moves.Where(m => m > strings.Move.Count).Select(m => m.ToString()))}" ) ;
2019-07-14 22:06:45 +00:00
else if ( pkm . Moves . Any ( m = > m > sav . MaxMoveID ) )
errata . Add ( $"{MsgIndexMoveGame} {string.Join(" , ", pkm.Moves.Where(m => m > sav.MaxMoveID).Select(m => strings.Move[m]))}" ) ;
2018-07-21 04:32:33 +00:00
if ( pkm . Ability > strings . Ability . Count )
errata . Add ( $"{MsgIndexAbilityRange} {pkm.Ability}" ) ;
2019-07-14 22:06:45 +00:00
else if ( pkm . Ability > sav . MaxAbilityID )
2018-07-21 04:32:33 +00:00
errata . Add ( $"{MsgIndexAbilityGame} {strings.Ability[pkm.Ability]}" ) ;
return errata ;
}
2018-12-29 01:58:13 +00:00
/// <summary>
2019-07-14 22:06:45 +00:00
/// Imports compatible <see cref="PKM"/> data to the <see cref="sav"/>, starting at the provided box.
2018-12-29 01:58:13 +00:00
/// </summary>
2019-07-14 22:06:45 +00:00
/// <param name="sav">Save File that will receive the <see cref="compat"/> data.</param>
/// <param name="compat">Compatible <see cref="PKM"/> data that can be set to the <see cref="sav"/> without conversion.</param>
2018-12-29 01:58:13 +00:00
/// <param name="overwrite">Overwrite existing full slots. If true, will only overwrite empty slots.</param>
/// <param name="boxStart">First box to start loading to. All prior boxes are not modified.</param>
/// <param name="noSetb">Bypass option to not modify <see cref="PKM"/> properties when setting to Save File.</param>
/// <returns>Count of injected <see cref="PKM"/>.</returns>
2019-07-14 22:06:45 +00:00
public static int ImportPKMs ( this SaveFile sav , IEnumerable < PKM > compat , bool overwrite = false , int boxStart = 0 , PKMImportSetting noSetb = PKMImportSetting . UseDefault )
2018-07-21 04:32:33 +00:00
{
2019-07-14 22:06:45 +00:00
int startCount = boxStart * sav . BoxSlotCount ;
int maxCount = sav . SlotCount ;
2018-12-29 01:58:13 +00:00
int index = startCount ;
2019-05-15 16:11:48 +00:00
int nonOverwriteImport = 0 ;
2018-07-21 04:32:33 +00:00
foreach ( var pk in compat )
{
2018-12-29 01:58:13 +00:00
if ( overwrite )
{
2019-07-14 22:06:45 +00:00
while ( sav . IsSlotOverwriteProtected ( index ) )
2018-12-29 01:58:13 +00:00
+ + index ;
2019-05-15 16:11:48 +00:00
2019-07-14 22:06:45 +00:00
sav . SetBoxSlotAtIndex ( pk , index , noSetb ) ;
2018-12-29 01:58:13 +00:00
}
else
2018-07-21 04:32:33 +00:00
{
2019-07-14 22:06:45 +00:00
index = sav . NextOpenBoxSlot ( index - 1 ) ;
2018-12-29 01:58:13 +00:00
if ( index < 0 ) // Boxes full!
break ;
2018-07-21 04:32:33 +00:00
2019-07-14 22:06:45 +00:00
sav . SetBoxSlotAtIndex ( pk , index , noSetb ) ;
2019-05-15 16:11:48 +00:00
nonOverwriteImport + + ;
}
2018-07-21 04:32:33 +00:00
2018-12-29 01:58:13 +00:00
if ( + + index = = maxCount ) // Boxes full!
2018-07-21 04:32:33 +00:00
break ;
}
2019-07-14 22:06:45 +00:00
return overwrite ? index - startCount : nonOverwriteImport ; // actual imported count
2018-07-21 04:32:33 +00:00
}
2019-07-14 22:06:45 +00:00
public static IEnumerable < PKM > GetCompatible ( this SaveFile sav , IEnumerable < PKM > pks )
2018-07-21 04:32:33 +00:00
{
2019-07-14 22:06:45 +00:00
var savtype = sav . PKMType ;
2018-07-21 04:32:33 +00:00
foreach ( var temp in pks )
{
var pk = PKMConverter . ConvertToType ( temp , savtype , out string c ) ;
if ( pk = = null )
{
Debug . WriteLine ( c ) ;
continue ;
}
2019-07-14 22:06:45 +00:00
if ( sav is ILangDeviantSave il & & PKMConverter . IsIncompatibleGB ( pk . Format , il . Japanese , pk . Japanese ) )
2018-07-24 22:49:00 +00:00
{
2019-07-14 22:06:45 +00:00
c = PKMConverter . GetIncompatibleGBMessage ( pk , il . Japanese ) ;
2018-07-24 22:49:00 +00:00
Debug . WriteLine ( c ) ;
continue ;
}
2019-07-14 22:06:45 +00:00
var compat = sav . IsPKMCompatible ( pk ) ;
2018-07-21 04:32:33 +00:00
if ( compat . Count > 0 )
continue ;
yield return pk ;
}
}
2019-02-15 08:50:23 +00:00
/// <summary>
/// Gets a compatible <see cref="PKM"/> for editing with a new <see cref="SaveFile"/>.
/// </summary>
/// <param name="sav">SaveFile to receive the compatible <see cref="pk"/></param>
/// <param name="pk">Current Pokémon being edited</param>
/// <returns>Current Pokémon, assuming conversion is possible. If conversion is not possible, a blank <see cref="PKM"/> will be obtained from the <see cref="sav"/>.</returns>
public static PKM GetCompatiblePKM ( this SaveFile sav , PKM pk = null )
{
if ( pk = = null )
return sav . BlankPKM ;
if ( pk . Format < 3 & & sav . Generation < 7 )
{
// gen1-2 compatibility check
2019-07-14 22:06:45 +00:00
if ( pk . Japanese ! = ( ( ILangDeviantSave ) sav ) . Japanese )
2019-02-15 08:50:23 +00:00
return sav . BlankPKM ;
if ( sav is SAV2 s2 & & s2 . Korean ! = pk . Korean )
return sav . BlankPKM ;
}
return PKMConverter . ConvertToType ( pk , sav . PKMType , out _ ) ? ? sav . BlankPKM ;
}
/// <summary>
/// Gets a blank file for the save file. If the template path exists, a template load will be attempted.
/// </summary>
/// <param name="sav">Save File to fetch a template for</param>
/// <param name="templatePath">Path to look for a template in</param>
/// <returns>Template if it exists, or a blank <see cref="PKM"/> from the <see cref="sav"/></returns>
public static PKM LoadTemplate ( this SaveFile sav , string templatePath = null )
{
var blank = sav . BlankPKM ;
if ( ! Directory . Exists ( templatePath ) )
return blank ;
var di = new DirectoryInfo ( templatePath ) ;
string path = Path . Combine ( templatePath , $"{di.Name}.{blank.Extension}" ) ;
if ( ! File . Exists ( path ) | | ! PKX . IsPKM ( new FileInfo ( path ) . Length ) )
return blank ;
var pk = PKMConverter . GetPKMfromBytes ( File . ReadAllBytes ( path ) , prefer : blank . Format ) ;
2019-07-14 22:06:45 +00:00
return PKMConverter . ConvertToType ( pk , sav . BlankPKM . GetType ( ) , out _ ) ? ? blank ;
2019-02-15 08:50:23 +00:00
}
2018-07-21 04:32:33 +00:00
}
}