From cfd46b8077076b607d84e3f89a83b8cb2a8f3c7d Mon Sep 17 00:00:00 2001 From: Manu <52102823+Manu098vm@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:28:53 +0200 Subject: [PATCH] Properly handle TID/SID methods on both the WinForm and ConsoleApp projects --- SwitchGiftDataManager.CommandLine/Program.cs | 24 +++++++++++++++ .../Classes/BCATManager/BCATManager.cs | 16 +++++++--- .../Classes/WCManager/WC9.cs | 14 +++++++-- SwitchGiftDataManager.WinForm/MainWindow.cs | 29 ++++++++++++++----- 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/SwitchGiftDataManager.CommandLine/Program.cs b/SwitchGiftDataManager.CommandLine/Program.cs index fb55e2d..725e59e 100644 --- a/SwitchGiftDataManager.CommandLine/Program.cs +++ b/SwitchGiftDataManager.CommandLine/Program.cs @@ -51,6 +51,30 @@ public static class Program } bcat.Sort(); + + var methodSelectionInfo = $"{Environment.NewLine}The Pokémon Scarlet & Violet v2.0.1 Update, released on September 13 - 2023, introduced a new TID / SID handling method for Wondercards.{Environment.NewLine}" + + $"This Pokémon Gift could be redeemed both before and after the v2.0.1 release, meaning it can be redeemed with either the old or new TID/SID handling method, depending on the redemption date.{Environment.NewLine}" + + $"The redemption date can be changed with the homebrew 'switch-time'. If you're not going to use 'switch-time', please select 'After v2.0.1'."; + + foreach (var (i, title) in bcat.GetListNames().Select((title, i) => (i, title)).Where(x => bcat.GetRequiresMethodSelection(x.i))) + { + var method = 0; + while (method is not (1 or 2)) + { + Log($"{Environment.NewLine}[{title}] This Pokémon Gift could be redeemed both before and after the v2.0.1 release. Please select the redemption method you want to use, depending on the Date/Time you'll redeem the gift." + + $"{Environment.NewLine}1 - Before v2.0.1" + + $"{Environment.NewLine}2 - After v2.0.1" + + $"{Environment.NewLine}3 - Info"); + + method = int.Parse(Console.ReadLine()!); + + if (method is (1 or 2)) + bcat.SetIsBefore201(i, method == 1); + else + Log(methodSelectionInfo); + } + } + Log($"{Environment.NewLine}Enter the source (full) path to your dumped BCAT:"); var sourcepath = Console.ReadLine()!; if (!CheckValidBcatPath(sourcepath)) diff --git a/SwitchGiftDataManager.Core/Classes/BCATManager/BCATManager.cs b/SwitchGiftDataManager.Core/Classes/BCATManager/BCATManager.cs index a68e1f5..570371c 100644 --- a/SwitchGiftDataManager.Core/Classes/BCATManager/BCATManager.cs +++ b/SwitchGiftDataManager.Core/Classes/BCATManager/BCATManager.cs @@ -130,14 +130,22 @@ public class BCATManager return false; } - public void SetTIDSID(int index, bool after200) + public bool GetIsBefore201(int index) + { + if (WCList is not null && WCList.Count > 0 && WCList.Count >= index - 1 && index >= 0) + if (WCList.ElementAt(index) is WC9 wc9) + return wc9.IsBefore201; + return false; + } + + public void SetIsBefore201(int index, bool before201) { if (WCList is not null && WCList.Count > 0 && WCList.Count >= index - 1 && index >= 0) if (WCList.ElementAt(index) is WC9 wc9 && wc9.RequiresMethodSelection) - if (after200) - wc9.UpdateNewToOldTIDSID(); - else + if (before201) wc9.UpdateOldToNewTIDSID(); + else + wc9.UpdateNewToOldTIDSID(); } public int GetIndex(ushort wcid) diff --git a/SwitchGiftDataManager.Core/Classes/WCManager/WC9.cs b/SwitchGiftDataManager.Core/Classes/WCManager/WC9.cs index 7efbb71..8493dfc 100644 --- a/SwitchGiftDataManager.Core/Classes/WCManager/WC9.cs +++ b/SwitchGiftDataManager.Core/Classes/WCManager/WC9.cs @@ -21,10 +21,12 @@ public class WC9 : Wondercard // Entities with set TID / SID prior to the v2.0.1 game update would've had their TID / SID modified based on the Wondercard ID. private readonly HashSet OldGifts = new() { 0024, 0025, 0028, 0501, 0503, 0504, 0505, 0506, 1002, 1003, 1005 }; - // These Gifts (Birthday Flabébé, Glaseado Cetitan) could be obtained both before and after the v2.0.1 game update. - private readonly HashSet MixedGifts = new() { 0001, 1524 }; + // These Gifts (Birthday Flabébé, Glaseado Cetitan, Home Gifts) could be obtained both before and after the v2.0.1 game update. + private readonly HashSet MixedGifts = new() { 0001, 1524, 9021, 9022, 9023 }; public bool RequiresMethodSelection { get => MixedGifts.Contains(WCID); } + public bool IsBefore201 { get; set; } = false; + public WC9(ReadOnlySpan data) : base(data) { WCID = BinaryPrimitives.ReadUInt16LittleEndian(data[WondercardIDOffset..]); @@ -129,14 +131,22 @@ public class WC9 : Wondercard public void UpdateOldToNewTIDSID() { + if (IsBefore201) + return; + var FTID = BinaryPrimitives.ReadUInt32LittleEndian(Data.AsSpan(TIDOffset)) - (1000000u * WCID); BinaryPrimitives.WriteUInt32LittleEndian(Data.AsSpan(TIDOffset), FTID); + IsBefore201 = true; } public void UpdateNewToOldTIDSID() { + if (!IsBefore201) + return; + var FTID = BinaryPrimitives.ReadUInt32LittleEndian(Data.AsSpan(TIDOffset)); BinaryPrimitives.WriteUInt32LittleEndian(Data.AsSpan(TIDOffset), FTID + (1000000u * WCID)); + IsBefore201 = false; } public override void UpdateChecksum() diff --git a/SwitchGiftDataManager.WinForm/MainWindow.cs b/SwitchGiftDataManager.WinForm/MainWindow.cs index 4dc566b..d2e460a 100644 --- a/SwitchGiftDataManager.WinForm/MainWindow.cs +++ b/SwitchGiftDataManager.WinForm/MainWindow.cs @@ -204,6 +204,7 @@ public partial class MainWindow : Form var list = GetCurrentList(); var wcid = UInt16.Parse(TxtWCID.Text); var repeatable = ChkRepeatable.Checked; + var isBefore201 = cmbRedemptionMethod.SelectedIndex == 0; if (wcid != list.GetWCID(ListBoxWC.SelectedIndex)) { @@ -228,6 +229,12 @@ public partial class MainWindow : Form list.SetIsRepeatable(ListBoxWC.SelectedIndex, repeatable); BtnApply.Enabled = false; } + + if (list.GetRequiresMethodSelection(ListBoxWC.SelectedIndex)) + { + list.SetIsBefore201(ListBoxWC.SelectedIndex, isBefore201); + BtnApply.Enabled = false; + } } private void BtnRemove_Click(object sender, EventArgs e) @@ -305,19 +312,22 @@ public partial class MainWindow : Form private void CmbRedemptionMethod_IndexChanged(object sender, EventArgs e) { - int newIndex = cmbRedemptionMethod.SelectedIndex; - if (newIndex != cmbRedemptionMethod.Tag as int?) + if (CurrentGame is Games.SCVI) { - if (CurrentGame is Games.SCVI) + var list = GetCurrentList(); + if (list.GetRequiresMethodSelection(ListBoxWC.SelectedIndex)) { - var list = GetCurrentList(); - list.SetTIDSID(ListBoxWC.SelectedIndex, newIndex == 1); + var newValue = cmbRedemptionMethod.SelectedIndex == 0; + var oldValue = list.GetIsBefore201(ListBoxWC.SelectedIndex); + + if (newValue != oldValue) + BtnApply.Enabled = true; + else + BtnApply.Enabled = false; } - cmbRedemptionMethod.Tag = newIndex; } } - private void ToolTipWcid_Draw(object sender, DrawToolTipEventArgs e) { Point screenPosition = ListBox.MousePosition; @@ -387,8 +397,13 @@ public partial class MainWindow : Form TxtWCID.Text = content.ElementAt(0); ChkRepeatable.Checked = list.GetIsRepeatable(index); + cmbRedemptionMethod.Visible = cmbRedemptionMethod.Enabled = lblRedemptionMethod.Visible = lblRedemptionMethod.Enabled = list.GetRequiresMethodSelection(index); + + if (cmbRedemptionMethod.Enabled) + cmbRedemptionMethod.SelectedIndex = list.GetIsBefore201(index) ? 0 : 1; + EnableContent(); } else