move pkm load compat check to pkmconverter

closes #1550 , mgdb/pkmdb throw unconverted files which need conversion;
move main file load conversion to a reusable method and have pkmeditor
call it on every load.

add skip argument to ignore the conversion check (ie if the file is
loaded from an undoubtedly same type source).
This commit is contained in:
Kurt 2017-10-28 14:26:27 -07:00
parent 9d0b153eab
commit 021ecbfe47
4 changed files with 38 additions and 21 deletions

View file

@ -200,7 +200,7 @@ namespace PKHeX.Core
/// <returns>Converted PKM</returns>
public static PKM ConvertToType(PKM pk, Type PKMType, out string comment)
{
if (pk == null || pk.Species == 0)
if (pk == null)
{
comment = $"Bad {nameof(pk)} input. Aborting.";
return null;
@ -392,6 +392,35 @@ namespace PKHeX.Core
}
}
/// <summary>
/// Checks if the input <see cref="PKM"/> is compatible with the target <see cref="PKM"/>.
/// </summary>
/// <param name="pk">Input to check -> update/sanitize</param>
/// <param name="target">Target type PKM with misc properties accessible for checking.</param>
/// <param name="c">Comment output</param>
/// <param name="pkm">Output compatible PKM</param>
/// <returns>Indication if the input is (now) compatible with the target.</returns>
public static bool TryMakePKMCompatible(PKM pk, PKM target, out string c, out PKM pkm)
{
if (!IsConvertibleToFormat(pk, target.Format))
{
pkm = null;
c = $"Can't load {pk.GetType().Name}s to Gen{target.Format} saves.";
return false;
}
if (target.Format < 3 && pk.Japanese != target.Japanese)
{
pkm = null;
var strs = new[] { "International", "Japanese" };
var val = target.Japanese ? 0 : 1;
c = $"Cannot load {strs[val]} {pk.GetType().Name}s to {strs[val ^ 1]} saves.";
return false;
}
pkm = ConvertToType(pk, target.GetType(), out c);
Debug.WriteLine(c);
return pkm != null;
}
/// <summary>
/// Gets a Blank <see cref="PKM"/> object of the specified type.
/// </summary>

View file

@ -172,12 +172,16 @@ namespace PKHeX.WinForms.Controls
if (GB_ExtraBytes.Enabled)
CB_ExtraBytes.SelectedIndex = 0;
}
public void PopulateFields(PKM pk, bool focus = true)
public void PopulateFields(PKM pk, bool focus = true, bool skipConversionCheck = false) => LoadFieldsFromPKM(pk, focus, skipConversionCheck);
private void LoadFieldsFromPKM(PKM pk, bool focus = true, bool skipConversionCheck = true)
{
if (pk == null) { WinFormsUtil.Error("Attempted to load a null file."); return; }
if (focus)
Tab_Main.Focus();
if (!skipConversionCheck && !PKMConverter.TryMakePKMCompatible(pk, CurrentPKM, out string c, out pk))
{ WinFormsUtil.Alert(c); return; }
bool oldInit = fieldsInitialized;
fieldsInitialized = fieldsLoaded = false;

View file

@ -33,7 +33,7 @@ namespace PKHeX.WinForms.Controls
if ((sender as PictureBox)?.Image == null)
{ System.Media.SystemSounds.Asterisk.Play(); return; }
m.SE.PKME_Tabs.PopulateFields(m.GetPKM(info), false);
m.SE.PKME_Tabs.PopulateFields(m.GetPKM(info), false, true);
m.SetColor(info.Box, info.Slot, Resources.slotView);
}
private static void ClickSet(object sender, EventArgs e)

View file

@ -575,27 +575,11 @@ namespace PKHeX.WinForms
}
private bool TryLoadPKM(byte[] input, string path, string ext, SaveFile SAV)
{
var temp = PKMConverter.GetPKMfromBytes(input, prefer: ext.Length > 0 ? (ext.Last() - '0') & 0xF : C_SAV.SAV.Generation);
if (temp == null)
return false;
var type = PKME_Tabs.CurrentPKM.GetType();
PKM pk = PKMConverter.ConvertToType(temp, type, out string c);
var pk = PKMConverter.GetPKMfromBytes(input, prefer: ext.Length > 0 ? (ext.Last() - '0') & 0xF : C_SAV.SAV.Generation);
if (pk == null)
{
WinFormsUtil.Alert("Conversion failed.", c);
return true;
}
if (SAV.Generation < 3 && pk.Japanese != SAV.Japanese)
{
var strs = new[] { "International", "Japanese" };
var val = SAV.Japanese ? 0 : 1;
WinFormsUtil.Alert($"Cannot load {strs[val]} {pk.GetType().Name}s to {strs[val ^ 1]} saves.");
return true;
}
return false;
PKME_Tabs.PopulateFields(pk);
Debug.WriteLine(c);
return true;
}
private bool TryLoadPCBoxBin(byte[] input)