From d4452ce3af1f419beefa0872e0058ba0151bb5a5 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 4 Oct 2020 14:42:48 -0700 Subject: [PATCH] SK2 coerce to load to a hint language This format won't be supported cleanly, so let's just try to allow language detection to work as best as it can without storing metadata in the SK2 bytes. --- PKHeX.Core/PKM/SK2.cs | 26 ++++++++++++++++--- PKHeX.Core/Saves/Util/SaveExtensions.cs | 2 +- PKHeX.WinForms/Controls/PKM Editor/EditPK2.cs | 20 ++++++++++++++ .../Controls/SAV Editor/SlotChangeManager.cs | 2 +- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/PKHeX.Core/PKM/SK2.cs b/PKHeX.Core/PKM/SK2.cs index 3f7751b37..0846b9d3e 100644 --- a/PKHeX.Core/PKM/SK2.cs +++ b/PKHeX.Core/PKM/SK2.cs @@ -12,7 +12,8 @@ public sealed class SK2 : GBPKM, ICaughtData2 public override int SIZE_PARTY => PokeCrypto.SIZE_2STADIUM; public override int SIZE_STORED => PokeCrypto.SIZE_2STADIUM; - public override bool Japanese { get; } + private bool _jp { get; set; } + public override bool Japanese => _jp; public override bool Korean => false; private const int StringLength = 12; @@ -22,11 +23,11 @@ public sealed class SK2 : GBPKM, ICaughtData2 public SK2(bool jp = false) : base(PokeCrypto.SIZE_2STADIUM) { - Japanese = jp; + _jp = jp; } public SK2(byte[] data) : this(data, IsJapanese(data)) { } - public SK2(byte[] data, bool jp) : base(data) => Japanese = jp; + public SK2(byte[] data, bool jp) : base(data) => _jp = jp; public override PKM Clone() => new SK2((byte[])Data.Clone(), Japanese) { @@ -175,8 +176,25 @@ public PK2 ConvertToPK2() }; } - private static bool IsJapanese(byte[] data) => StringConverter12.GetIsG1Japanese(data, 0x24, StringLength) && StringConverter12.GetIsG1Japanese(data, 0x30, StringLength); + private static bool IsJapanese(byte[] data) + { + if (!StringConverter12.GetIsG1Japanese(data, 0x24, StringLength)) + return false; + if (!StringConverter12.GetIsG1Japanese(data, 0x30, StringLength)) + return false; + + for (int i = 6; i < 0xC; i++) + { + if (data[0x24 + i] != 0 && data[0x24 + i] != StringConverter12.G1TerminatorCode) + return false; + if (data[0x30 + i] != 0 && data[0x30 + i] != StringConverter12.G1TerminatorCode) + return false; + } + return true; + } + private static bool IsEnglish(byte[] data) => StringConverter12.GetIsG1English(data, 0x24, StringLength) && StringConverter12.GetIsG1English(data, 0x30, StringLength); public bool IsPossible(bool japanese) => japanese ? IsJapanese(Data) : IsEnglish(Data); + public void SwapLanguage() => _jp ^= true; } } diff --git a/PKHeX.Core/Saves/Util/SaveExtensions.cs b/PKHeX.Core/Saves/Util/SaveExtensions.cs index ac26caf69..7c802a9f3 100644 --- a/PKHeX.Core/Saves/Util/SaveExtensions.cs +++ b/PKHeX.Core/Saves/Util/SaveExtensions.cs @@ -165,7 +165,7 @@ public static IEnumerable GetCompatible(this SaveFile sav, IEnumerable continue; } - if (sav is ILangDeviantSave il && PKMConverter.IsIncompatibleGB(pk, il.Japanese, pk.Japanese)) + if (sav is ILangDeviantSave il && PKMConverter.IsIncompatibleGB(temp, il.Japanese, pk.Japanese)) { c = PKMConverter.GetIncompatibleGBMessage(pk, il.Japanese); Debug.WriteLine(c); diff --git a/PKHeX.WinForms/Controls/PKM Editor/EditPK2.cs b/PKHeX.WinForms/Controls/PKM Editor/EditPK2.cs index d764c7508..b751f3ac7 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/EditPK2.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/EditPK2.cs @@ -10,6 +10,11 @@ private void PopulateFieldsPK2() if (!(Entity is GBPKM pk2) || !(Entity is ICaughtData2 c2)) throw new FormatException(nameof(Entity)); + if (Entity is SK2 sk2) + { + var sav = RequestSaveFile; + CoerceStadium2Language(sk2, sav); + } LoadMisc1(pk2); LoadMisc2(pk2); @@ -25,6 +30,21 @@ private void PopulateFieldsPK2() UpdateStats(); } + private static void CoerceStadium2Language(SK2 sk2, SaveFile sav) + { + if (sk2.Japanese == (sav.Language == 1)) + return; + + var la = new LegalityAnalysis(sk2); + if (la.Valid || !sk2.IsPossible(sav.Language == 1)) + return; + + sk2.SwapLanguage(); + la = new LegalityAnalysis(sk2); + if (!la.Valid) // fail + sk2.SwapLanguage(); + } + private GBPKM PreparePK2() { if (!(Entity is GBPKM pk2) || !(Entity is ICaughtData2 c2)) diff --git a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs index 8a52db779..06d7e5cba 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs @@ -286,7 +286,7 @@ private bool TryLoadFiles(IReadOnlyList files, DragEventArgs e, bool bad if (badDest && (pk.Species == 0 || pk.IsEgg)) return false; - if (sav is ILangDeviantSave il && PKMConverter.IsIncompatibleGB(pk, il.Japanese, pk.Japanese)) + if (sav is ILangDeviantSave il && PKMConverter.IsIncompatibleGB(temp, il.Japanese, pk.Japanese)) { c = PKMConverter.GetIncompatibleGBMessage(pk, il.Japanese); WinFormsUtil.Error(c);