mirror of
https://github.com/kwsch/PKHeX.git
synced 2026-08-27 20:34:19 -05:00
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:
@@ -200,7 +200,7 @@ public static bool IsConvertibleToFormat(PKM pk, int format)
|
||||
/// <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 @@ public static void CheckEncrypted(ref byte[] pkm)
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
||||
@@ -172,12 +172,16 @@ public void SetPKMFormatMode(int Format)
|
||||
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;
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ private static void ClickView(object sender, EventArgs e)
|
||||
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)
|
||||
|
||||
@@ -575,27 +575,11 @@ private bool TryLoadMemoryCard(byte[] input, string path)
|
||||
}
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user