From 33b0dc587dd77499bca0f3b4a3978c18ca128014 Mon Sep 17 00:00:00 2001 From: SciresM Date: Mon, 26 Sep 2016 16:14:11 -0700 Subject: [PATCH] PBR support (#290) * Initial (still buggy) PBR commit * Basic save slot support --- PKHeX/MainWindow/Main.Designer.cs | 37 +- PKHeX/MainWindow/Main.cs | 58 ++- PKHeX/MainWindow/Main.resx | 456 ++++++++++-------- PKHeX/MainWindow/MainPK4.cs | 248 ++++++++++ PKHeX/PKHeX.csproj | 2 + PKHeX/PKM/BK4.cs | 461 +++++++++++++++++++ PKHeX/PKM/PK4.cs | 24 + PKHeX/PKM/PKM.cs | 4 +- PKHeX/PKM/PKMConverter.cs | 151 ++++-- PKHeX/PKM/PKX.cs | 74 ++- PKHeX/Saves/SAV1.cs | 2 +- PKHeX/Saves/SAV2.cs | 2 +- PKHeX/Saves/SAV3.cs | 2 +- PKHeX/Saves/SAV3RSBox.cs | 2 +- PKHeX/Saves/SAV4.cs | 2 +- PKHeX/Saves/SAV4BR.cs | 306 ++++++++++++ PKHeX/Saves/SAV5.cs | 2 +- PKHeX/Saves/SAV6.cs | 2 +- PKHeX/Saves/SaveFile.cs | 4 +- PKHeX/Saves/SaveUtil.cs | 24 + PKHeX/Subforms/Save Editors/SAV_BoxViewer.cs | 2 +- 21 files changed, 1598 insertions(+), 267 deletions(-) create mode 100644 PKHeX/PKM/BK4.cs create mode 100644 PKHeX/Saves/SAV4BR.cs diff --git a/PKHeX/MainWindow/Main.Designer.cs b/PKHeX/MainWindow/Main.Designer.cs index 0bddf33c2..aae41e99a 100644 --- a/PKHeX/MainWindow/Main.Designer.cs +++ b/PKHeX/MainWindow/Main.Designer.cs @@ -427,6 +427,8 @@ public void InitializeComponent() this.mnuLSave = new System.Windows.Forms.ToolStripMenuItem(); this.PB_Legal = new System.Windows.Forms.PictureBox(); this.L_UpdateAvailable = new System.Windows.Forms.LinkLabel(); + this.L_SaveSlot = new System.Windows.Forms.Label(); + this.CB_SaveSlot = new System.Windows.Forms.ComboBox(); this.tabMain.SuspendLayout(); this.Tab_Main.SuspendLayout(); this.FLP_Main.SuspendLayout(); @@ -3543,7 +3545,7 @@ public void InitializeComponent() this.Menu_Open.Name = "Menu_Open"; this.Menu_Open.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); this.Menu_Open.ShowShortcutKeys = false; - this.Menu_Open.Size = new System.Drawing.Size(139, 22); + this.Menu_Open.Size = new System.Drawing.Size(152, 22); this.Menu_Open.Text = "&Open..."; this.Menu_Open.Click += new System.EventHandler(this.mainMenuOpen); // @@ -3553,7 +3555,7 @@ public void InitializeComponent() this.Menu_Save.Name = "Menu_Save"; this.Menu_Save.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S))); this.Menu_Save.ShowShortcutKeys = false; - this.Menu_Save.Size = new System.Drawing.Size(139, 22); + this.Menu_Save.Size = new System.Drawing.Size(152, 22); this.Menu_Save.Text = "&Save PK6..."; this.Menu_Save.Click += new System.EventHandler(this.mainMenuSave); // @@ -3565,7 +3567,7 @@ public void InitializeComponent() this.Menu_ExportSAV.Enabled = false; this.Menu_ExportSAV.Image = global::PKHeX.Properties.Resources.saveSAV; this.Menu_ExportSAV.Name = "Menu_ExportSAV"; - this.Menu_ExportSAV.Size = new System.Drawing.Size(139, 22); + this.Menu_ExportSAV.Size = new System.Drawing.Size(152, 22); this.Menu_ExportSAV.Text = "&Export SAV..."; // // Menu_ExportMAIN @@ -3574,7 +3576,7 @@ public void InitializeComponent() this.Menu_ExportMAIN.Name = "Menu_ExportMAIN"; this.Menu_ExportMAIN.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.E))); this.Menu_ExportMAIN.ShowShortcutKeys = false; - this.Menu_ExportMAIN.Size = new System.Drawing.Size(142, 22); + this.Menu_ExportMAIN.Size = new System.Drawing.Size(152, 22); this.Menu_ExportMAIN.Text = "&Export main"; this.Menu_ExportMAIN.Click += new System.EventHandler(this.clickExportSAV); // @@ -3584,7 +3586,7 @@ public void InitializeComponent() this.Menu_ExportBAK.Name = "Menu_ExportBAK"; this.Menu_ExportBAK.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.B))); this.Menu_ExportBAK.ShowShortcutKeys = false; - this.Menu_ExportBAK.Size = new System.Drawing.Size(142, 22); + this.Menu_ExportBAK.Size = new System.Drawing.Size(152, 22); this.Menu_ExportBAK.Text = "Export &Backup"; this.Menu_ExportBAK.Click += new System.EventHandler(this.clickExportSAVBAK); // @@ -3594,7 +3596,7 @@ public void InitializeComponent() this.Menu_Exit.Name = "Menu_Exit"; this.Menu_Exit.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Q))); this.Menu_Exit.ShowShortcutKeys = false; - this.Menu_Exit.Size = new System.Drawing.Size(139, 22); + this.Menu_Exit.Size = new System.Drawing.Size(152, 22); this.Menu_Exit.Text = "&Quit"; this.Menu_Exit.Click += new System.EventHandler(this.mainMenuExit); // @@ -5101,6 +5103,8 @@ public void InitializeComponent() // // Tab_SAV // + this.Tab_SAV.Controls.Add(this.CB_SaveSlot); + this.Tab_SAV.Controls.Add(this.L_SaveSlot); this.Tab_SAV.Controls.Add(this.L_Secure2); this.Tab_SAV.Controls.Add(this.TB_Secure2); this.Tab_SAV.Controls.Add(this.L_Secure1); @@ -5478,6 +5482,25 @@ public void InitializeComponent() this.L_UpdateAvailable.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.L_UpdateAvailable.Visible = false; // + // L_SaveSlot + // + this.L_SaveSlot.AutoSize = true; + this.L_SaveSlot.Location = new System.Drawing.Point(92, 151); + this.L_SaveSlot.Name = "L_SaveSlot"; + this.L_SaveSlot.Size = new System.Drawing.Size(56, 13); + this.L_SaveSlot.TabIndex = 19; + this.L_SaveSlot.Text = "Save Slot:"; + // + // CB_SaveSlot + // + this.CB_SaveSlot.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.CB_SaveSlot.FormattingEnabled = true; + this.CB_SaveSlot.Location = new System.Drawing.Point(150, 148); + this.CB_SaveSlot.Name = "CB_SaveSlot"; + this.CB_SaveSlot.Size = new System.Drawing.Size(121, 21); + this.CB_SaveSlot.TabIndex = 20; + this.CB_SaveSlot.SelectedValueChanged += new System.EventHandler(this.updateSaveSlot); + // // Main // this.AllowDrop = true; @@ -6099,6 +6122,8 @@ public void InitializeComponent() private System.Windows.Forms.MaskedTextBox TB_Tough; private System.Windows.Forms.MaskedTextBox TB_Sheen; private System.Windows.Forms.Label Label_IVs; + private System.Windows.Forms.ComboBox CB_SaveSlot; + private System.Windows.Forms.Label L_SaveSlot; } } diff --git a/PKHeX/MainWindow/Main.cs b/PKHeX/MainWindow/Main.cs index 46fd32305..bfec72aa0 100644 --- a/PKHeX/MainWindow/Main.cs +++ b/PKHeX/MainWindow/Main.cs @@ -455,7 +455,7 @@ private void clickShowdownImportPK6(object sender, EventArgs e) // Set Form string[] formStrings = PKX.getFormList(Set.Species, Util.getStringList("types", "en"), - Util.getStringList("forms", "en"), gendersymbols); + Util.getStringList("forms", "en"), gendersymbols, SAV.Generation); int form = 0; for (int i = 0; i < formStrings.Length; i++) if (formStrings[i].Contains(Set.Form ?? "")) @@ -589,7 +589,7 @@ private void openQuick(string path, bool force = false) string ext = Path.GetExtension(path); FileInfo fi = new FileInfo(path); - if (fi.Length > 0x10009C) + if (fi.Length > 0x10009C && fi.Length != 0x380000) Util.Error("Input file is too large.", path); else { @@ -642,7 +642,7 @@ private void openFile(byte[] input, string path, string ext) } else if ((temp = PKMConverter.getPKMfromBytes(input)) != null) { - PKM pk = PKMConverter.convertToFormat(temp, SAV.Generation, out c); + PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk == null) Util.Alert("Conversion failed.", c); else if (SAV.Generation == 1 && ((PK1) pk).Japanese != SAV.GetJapanese) @@ -702,7 +702,7 @@ private void openFile(byte[] input, string path, string ext) B_OpenWondercards_Click(tg, null); return; } - PKM pk = PKMConverter.convertToFormat(temp, SAV.Generation, out c); + PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk == null) Util.Alert("Conversion failed.", c); else @@ -803,9 +803,9 @@ private void openSAV(byte[] input, string path) } private void loadSAV(SaveFile sav, string path) { - PKM pk = preparePKM(); // clean fields populateFields(SAV.BlankPKM); + PKM pk = preparePKM(); SAV = sav; if (path != null) // Actual save file @@ -954,6 +954,21 @@ private void loadSAV(SaveFile sav, string path) if (SAV.Generation == 1) Label_IsShiny.Visible = false; + if (SAV.Version == GameVersion.BATREV) + { + L_SaveSlot.Visible = CB_SaveSlot.Visible = true; + CB_SaveSlot.Items.Clear(); + CB_SaveSlot.DisplayMember = "Text"; CB_SaveSlot.ValueMember = "Value"; + CB_SaveSlot.DataSource = new BindingSource(((SAV4BR) SAV).SaveSlots.Select(i => new ComboItem + { + Text = ((SAV4BR) SAV).SaveNames[i], + Value = i + }).ToList(), null); + CB_SaveSlot.SelectedValue = ((SAV4BR)SAV).CurrentSlot; + } + else + L_SaveSlot.Visible = CB_SaveSlot.Visible = false; + // HaX override, needs to be after DEV_Ability enabled assignment. TB_AbilityNumber.Visible = SAV.Generation >= 6 && DEV_Ability.Enabled; @@ -1036,8 +1051,16 @@ private void loadSAV(SaveFile sav, string path) extraBytes = PK3.ExtraBytes; break; case 4: - getFieldsfromPKM = populateFieldsPK4; - getPKMfromFields = preparePK4; + if (SAV.Version == GameVersion.BATREV) + { + getFieldsfromPKM = populateFieldsBK4; + getPKMfromFields = prepareBK4; + } + else + { + getFieldsfromPKM = populateFieldsPK4; + getPKMfromFields = preparePK4; + } extraBytes = PK4.ExtraBytes; break; case 5: @@ -1433,7 +1456,7 @@ public void populateFields(PKM pk, bool focus = true) if (pkm.Format != SAV.Generation) // past gen format { string c; - pkm = PKMConverter.convertToFormat(pkm, SAV.Generation, out c); + pkm = PKMConverter.convertToFormat(pkm, SAV.PKMType, out c); if (pk.Format != pkm.Format && focus) // converted Util.Alert("Converted File."); } @@ -1504,7 +1527,7 @@ private void setForms() if (!hasForms) return; - CB_Form.DataSource = PKX.getFormList(species, types, forms, gendersymbols).ToList(); + CB_Form.DataSource = PKX.getFormList(species, types, forms, gendersymbols, SAV.Generation).ToList(); } private void setAbilityList() { @@ -2817,7 +2840,7 @@ private void dragout_MouseDown(object sender, MouseEventArgs e) // Create Temp File to Drag PKM pkx = preparePKM(); bool encrypt = ModifierKeys == Keys.Control; - string filename = $"{Path.GetFileNameWithoutExtension(pkx.FileName)}{(encrypt ? ".ek" : ".pk") + pkx.Format}"; + string filename = $"{Path.GetFileNameWithoutExtension(pkx.FileName)}{(encrypt ? ".ek" + pkx.Format : "."+pkx.Extension) }"; byte[] dragdata = encrypt ? pkx.EncryptedBoxData : pkx.DecryptedBoxData; // Make file string newfile = Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename)); @@ -3023,7 +3046,6 @@ private void clickView(object sender, EventArgs e) Util.Error($"Slot read error @ slot {slot}."); return; } - // Load the PKX file PKM pk = 30 <= slot && slot < 36 ? SAV.getPartySlot(offset) : SAV.getStoredSlot(offset); if (pk.Valid && pk.Species != 0) @@ -3073,6 +3095,16 @@ private void clickSet(object sender, EventArgs e) updateBoxViewers(); } + + private void updateSaveSlot(object sender, EventArgs e) + { + if (SAV.Version == GameVersion.BATREV) + { + ((SAV4BR) SAV).CurrentSlot = Util.getIndex(CB_SaveSlot); + setPKXBoxes(); + } + } + private void clickDelete(object sender, EventArgs e) { int slot = getSlot(sender); @@ -3508,7 +3540,7 @@ private void loadBoxesFromDB(string path) { string c; PKM temp = PKMConverter.getPKMfromBytes(data); - PKM pk = PKMConverter.convertToFormat(temp, SAV.Generation, out c); + PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk != null) // Write to save { @@ -3857,7 +3889,7 @@ private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) PKM temp = mg != null ? mg.convertToPKM(SAV) : PKMConverter.getPKMfromBytes(data); string c; - PKM pk = PKMConverter.convertToFormat(temp, SAV.Generation, out c); + PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk == null) { Util.Error(c); Console.WriteLine(c); return; } diff --git a/PKHeX/MainWindow/Main.resx b/PKHeX/MainWindow/Main.resx index afb521d4b..77586a336 100644 --- a/PKHeX/MainWindow/Main.resx +++ b/PKHeX/MainWindow/Main.resx @@ -120,108 +120,6 @@ True - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - True @@ -281,6 +179,15 @@ True + + True + + + True + + + True + True @@ -293,6 +200,9 @@ True + + True + True @@ -323,6 +233,18 @@ True + + True + + + True + + + True + + + True + True @@ -368,6 +290,9 @@ True + + True + True @@ -392,6 +317,33 @@ True + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + True @@ -419,6 +371,18 @@ True + + True + + + True + + + True + + + True + True @@ -431,6 +395,18 @@ True + + True + + + True + + + True + + + True + True @@ -539,6 +515,12 @@ True + + True + + + True + True @@ -842,6 +824,24 @@ True + + True + + + True + + + True + + + True + + + True + + + True + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAeZJREFUOE+1 @@ -1104,6 +1104,15 @@ True + + True + + + True + + + True + True @@ -1354,6 +1363,165 @@ True + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + True @@ -1459,63 +1627,6 @@ True - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - True @@ -1834,33 +1945,6 @@ True - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - - - True - True diff --git a/PKHeX/MainWindow/MainPK4.cs b/PKHeX/MainWindow/MainPK4.cs index ca7c299ad..aa4ed4e0b 100644 --- a/PKHeX/MainWindow/MainPK4.cs +++ b/PKHeX/MainWindow/MainPK4.cs @@ -250,5 +250,253 @@ private PKM preparePK4() pk4.RefreshChecksum(); return pk4; } + + private void populateFieldsBK4() + { + BK4 pk4 = pkm as BK4; + if (pk4 == null) + return; + + // Do first + pk4.Stat_Level = PKX.getLevel(pk4.Species, pk4.EXP); + if (pk4.Stat_Level == 100) + pk4.EXP = PKX.getEXP(pk4.Stat_Level, pk4.Species); + + CB_Species.SelectedValue = pk4.Species; + TB_Level.Text = pk4.Stat_Level.ToString(); + TB_EXP.Text = pk4.EXP.ToString(); + + // Load rest + CHK_Fateful.Checked = pk4.FatefulEncounter; + CHK_IsEgg.Checked = pk4.IsEgg; + CHK_Nicknamed.Checked = pk4.IsNicknamed; + Label_OTGender.Text = gendersymbols[pk4.OT_Gender]; + Label_OTGender.ForeColor = pk4.OT_Gender == 1 ? Color.Red : Color.Blue; + TB_PID.Text = pk4.PID.ToString("X8"); + CB_HeldItem.SelectedValue = pk4.HeldItem; + setAbilityList(); + CB_Nature.SelectedValue = pk4.Nature; + TB_TID.Text = pk4.TID.ToString("00000"); + TB_SID.Text = pk4.SID.ToString("00000"); + TB_Nickname.Text = pk4.Nickname; + TB_OT.Text = pk4.OT_Name; + TB_Friendship.Text = pk4.CurrentFriendship.ToString(); + GB_OT.BackgroundImage = null; + CB_Language.SelectedValue = pk4.Language; + CB_GameOrigin.SelectedValue = pk4.Version; + CB_EncounterType.SelectedValue = pk4.Gen4 ? pk4.EncounterType : 0; + CB_Ball.SelectedValue = pk4.Ball; + + if (pk4.MetDate.HasValue) + { + CAL_MetDate.Value = pk4.MetDate.Value; + } + else + { + CAL_MetDate.Value = new DateTime(2000, 1, 1); + } + + if (pk4.Egg_Location != 0) + { + // Was obtained initially as an egg. + CHK_AsEgg.Checked = true; + GB_EggConditions.Enabled = true; + + CB_EggLocation.SelectedValue = pk4.Egg_Location; + if (pk4.EggMetDate.HasValue) + { + CAL_EggDate.Value = pk4.EggMetDate.Value; + } + else + { + CAL_EggDate.Value = new DateTime(2000, 1, 1); + } + } + else { CAL_EggDate.Value = new DateTime(2000, 01, 01); CHK_AsEgg.Checked = GB_EggConditions.Enabled = false; CB_EggLocation.SelectedValue = 0; } + + CB_MetLocation.SelectedValue = pk4.Met_Location; + + TB_MetLevel.Text = pk4.Met_Level.ToString(); + + // Reset Label and ComboBox visibility, as well as non-data checked status. + Label_PKRS.Visible = CB_PKRSStrain.Visible = CHK_Infected.Checked = pk4.PKRS_Strain != 0; + Label_PKRSdays.Visible = CB_PKRSDays.Visible = pk4.PKRS_Days != 0; + + // Set SelectedIndexes for PKRS + CB_PKRSStrain.SelectedIndex = pk4.PKRS_Strain; + CHK_Cured.Checked = pk4.PKRS_Strain > 0 && pk4.PKRS_Days == 0; + CB_PKRSDays.SelectedIndex = Math.Min(CB_PKRSDays.Items.Count - 1, pk4.PKRS_Days); // to strip out bad hacked 'rus + + TB_Cool.Text = pk4.CNT_Cool.ToString(); + TB_Beauty.Text = pk4.CNT_Beauty.ToString(); + TB_Cute.Text = pk4.CNT_Cute.ToString(); + TB_Smart.Text = pk4.CNT_Smart.ToString(); + TB_Tough.Text = pk4.CNT_Tough.ToString(); + TB_Sheen.Text = pk4.CNT_Sheen.ToString(); + + TB_HPIV.Text = pk4.IV_HP.ToString(); + TB_ATKIV.Text = pk4.IV_ATK.ToString(); + TB_DEFIV.Text = pk4.IV_DEF.ToString(); + TB_SPEIV.Text = pk4.IV_SPE.ToString(); + TB_SPAIV.Text = pk4.IV_SPA.ToString(); + TB_SPDIV.Text = pk4.IV_SPD.ToString(); + CB_HPType.SelectedValue = pk4.HPType; + + TB_HPEV.Text = pk4.EV_HP.ToString(); + TB_ATKEV.Text = pk4.EV_ATK.ToString(); + TB_DEFEV.Text = pk4.EV_DEF.ToString(); + TB_SPEEV.Text = pk4.EV_SPE.ToString(); + TB_SPAEV.Text = pk4.EV_SPA.ToString(); + TB_SPDEV.Text = pk4.EV_SPD.ToString(); + + CB_Move1.SelectedValue = pk4.Move1; + CB_Move2.SelectedValue = pk4.Move2; + CB_Move3.SelectedValue = pk4.Move3; + CB_Move4.SelectedValue = pk4.Move4; + CB_PPu1.SelectedIndex = pk4.Move1_PPUps; + CB_PPu2.SelectedIndex = pk4.Move2_PPUps; + CB_PPu3.SelectedIndex = pk4.Move3_PPUps; + CB_PPu4.SelectedIndex = pk4.Move4_PPUps; + TB_PP1.Text = pk4.Move1_PP.ToString(); + TB_PP2.Text = pk4.Move2_PP.ToString(); + TB_PP3.Text = pk4.Move3_PP.ToString(); + TB_PP4.Text = pk4.Move4_PP.ToString(); + + // Set Form if count is enough, else cap. + CB_Form.SelectedIndex = CB_Form.Items.Count > pk4.AltForm ? pk4.AltForm : CB_Form.Items.Count - 1; + + // Load Extrabyte Value + TB_ExtraByte.Text = pk4.Data[Convert.ToInt32(CB_ExtraBytes.Text, 16)].ToString(); + + updateStats(); + + TB_EXP.Text = pk4.EXP.ToString(); + Label_Gender.Text = gendersymbols[pk4.Gender]; + Label_Gender.ForeColor = pk4.Gender == 2 ? Label_Species.ForeColor : (pk4.Gender == 1 ? Color.Red : Color.Blue); + + if (HaX) + DEV_Ability.SelectedValue = pk4.Ability; + else + { + int[] abils = SAV.Personal.getAbilities(pk4.Species, pk4.AltForm); + int abil = Array.IndexOf(abils, pk4.Ability); + + if (abil < 0) + CB_Ability.SelectedIndex = 0; + else if (abils[0] == abils[1] || abils[1] == 0) + CB_Ability.SelectedIndex = pk4.PIDAbility; + else + CB_Ability.SelectedIndex = abil < 0 || abil >= CB_Ability.Items.Count ? 0 : abil; + } + } + private PKM prepareBK4() + { + BK4 pk4 = pkm as BK4; + if (pk4 == null) + return null; + + pk4.Species = Util.getIndex(CB_Species); + pk4.HeldItem = Util.getIndex(CB_HeldItem); + pk4.TID = Util.ToInt32(TB_TID.Text); + pk4.SID = Util.ToInt32(TB_SID.Text); + pk4.EXP = Util.ToUInt32(TB_EXP.Text); + pk4.PID = Util.getHEXval(TB_PID.Text); + pk4.Ability = (byte)Array.IndexOf(abilitylist, CB_Ability.Text.Remove(CB_Ability.Text.Length - 4)); + + pk4.FatefulEncounter = CHK_Fateful.Checked; + pk4.Gender = PKX.getGender(Label_Gender.Text); + pk4.AltForm = (MT_Form.Enabled ? Convert.ToInt32(MT_Form.Text) : CB_Form.Enabled ? CB_Form.SelectedIndex : 0) & 0x1F; + pk4.EV_HP = Util.ToInt32(TB_HPEV.Text); + pk4.EV_ATK = Util.ToInt32(TB_ATKEV.Text); + pk4.EV_DEF = Util.ToInt32(TB_DEFEV.Text); + pk4.EV_SPE = Util.ToInt32(TB_SPEEV.Text); + pk4.EV_SPA = Util.ToInt32(TB_SPAEV.Text); + pk4.EV_SPD = Util.ToInt32(TB_SPDEV.Text); + + pk4.CNT_Cool = Util.ToInt32(TB_Cool.Text); + pk4.CNT_Beauty = Util.ToInt32(TB_Beauty.Text); + pk4.CNT_Cute = Util.ToInt32(TB_Cute.Text); + pk4.CNT_Smart = Util.ToInt32(TB_Smart.Text); + pk4.CNT_Tough = Util.ToInt32(TB_Tough.Text); + pk4.CNT_Sheen = Util.ToInt32(TB_Sheen.Text); + + pk4.PKRS_Days = CB_PKRSDays.SelectedIndex; + pk4.PKRS_Strain = CB_PKRSStrain.SelectedIndex; + pk4.Nickname = TB_Nickname.Text; + pk4.Move1 = Util.getIndex(CB_Move1); + pk4.Move2 = Util.getIndex(CB_Move2); + pk4.Move3 = Util.getIndex(CB_Move3); + pk4.Move4 = Util.getIndex(CB_Move4); + pk4.Move1_PP = Util.getIndex(CB_Move1) > 0 ? Util.ToInt32(TB_PP1.Text) : 0; + pk4.Move2_PP = Util.getIndex(CB_Move2) > 0 ? Util.ToInt32(TB_PP2.Text) : 0; + pk4.Move3_PP = Util.getIndex(CB_Move3) > 0 ? Util.ToInt32(TB_PP3.Text) : 0; + pk4.Move4_PP = Util.getIndex(CB_Move4) > 0 ? Util.ToInt32(TB_PP4.Text) : 0; + pk4.Move1_PPUps = Util.getIndex(CB_Move1) > 0 ? CB_PPu1.SelectedIndex : 0; + pk4.Move2_PPUps = Util.getIndex(CB_Move2) > 0 ? CB_PPu2.SelectedIndex : 0; + pk4.Move3_PPUps = Util.getIndex(CB_Move3) > 0 ? CB_PPu3.SelectedIndex : 0; + pk4.Move4_PPUps = Util.getIndex(CB_Move4) > 0 ? CB_PPu4.SelectedIndex : 0; + + pk4.IV_HP = Util.ToInt32(TB_HPIV.Text); + pk4.IV_ATK = Util.ToInt32(TB_ATKIV.Text); + pk4.IV_DEF = Util.ToInt32(TB_DEFIV.Text); + pk4.IV_SPE = Util.ToInt32(TB_SPEIV.Text); + pk4.IV_SPA = Util.ToInt32(TB_SPAIV.Text); + pk4.IV_SPD = Util.ToInt32(TB_SPDIV.Text); + pk4.IsEgg = CHK_IsEgg.Checked; + pk4.IsNicknamed = CHK_Nicknamed.Checked; + + pk4.OT_Name = TB_OT.Text; + pk4.CurrentFriendship = Util.ToInt32(TB_Friendship.Text); + + pk4.Ball = Util.getIndex(CB_Ball); + pk4.Met_Level = Util.ToInt32(TB_MetLevel.Text); + pk4.OT_Gender = PKX.getGender(Label_OTGender.Text); + pk4.EncounterType = Util.getIndex(CB_EncounterType); + pk4.Version = Util.getIndex(CB_GameOrigin); + pk4.Language = Util.getIndex(CB_Language); + + // Default Dates + DateTime? egg_date = null; + int egg_location = 0; + if (CHK_AsEgg.Checked) // If encountered as an egg, load the Egg Met data from fields. + { + egg_date = CAL_EggDate.Value; + egg_location = Util.getIndex(CB_EggLocation); + } + // Egg Met Data + pk4.EggMetDate = egg_date; + pk4.Egg_Location = egg_location; + // Met Data + pk4.MetDate = CAL_MetDate.Value; + pk4.Met_Location = Util.getIndex(CB_MetLocation); + + if (pk4.IsEgg && pk4.Met_Location == 0) // If still an egg, it has no hatch location/date. Zero it! + pk4.MetDate = null; + + // Toss in Party Stats + Array.Resize(ref pk4.Data, pk4.SIZE_PARTY); + pk4.Stat_Level = Util.ToInt32(TB_Level.Text); + pk4.Stat_HPCurrent = Util.ToInt32(Stat_HP.Text); + pk4.Stat_HPMax = Util.ToInt32(Stat_HP.Text); + pk4.Stat_ATK = Util.ToInt32(Stat_ATK.Text); + pk4.Stat_DEF = Util.ToInt32(Stat_DEF.Text); + pk4.Stat_SPE = Util.ToInt32(Stat_SPE.Text); + pk4.Stat_SPA = Util.ToInt32(Stat_SPA.Text); + pk4.Stat_SPD = Util.ToInt32(Stat_SPD.Text); + + if (HaX) + { + pk4.Ability = (byte)Util.getIndex(DEV_Ability); + pk4.Stat_Level = (byte)Math.Min(Convert.ToInt32(MT_Level.Text), byte.MaxValue); + } + + // Fix Moves if a slot is empty + pk4.FixMoves(); + + pk4.RefreshChecksum(); + return pk4; + } } + + } diff --git a/PKHeX/PKHeX.csproj b/PKHeX/PKHeX.csproj index d32512b52..958ecc600 100644 --- a/PKHeX/PKHeX.csproj +++ b/PKHeX/PKHeX.csproj @@ -143,6 +143,7 @@ + @@ -167,6 +168,7 @@ + diff --git a/PKHeX/PKM/BK4.cs b/PKHeX/PKM/BK4.cs new file mode 100644 index 000000000..3f6080e48 --- /dev/null +++ b/PKHeX/PKM/BK4.cs @@ -0,0 +1,461 @@ +using System; +using System.Linq; + +namespace PKHeX +{ + public class BK4 : PKM // Big Endian 4th Generation PKM File + { + internal static readonly byte[] ExtraBytes = + { + 0x42, 0x43, 0x5E, 0x63, 0x64, 0x65, 0x66, 0x67, 0x87 + }; + public sealed override int SIZE_PARTY => PKX.SIZE_4PARTY; + public override int SIZE_STORED => PKX.SIZE_4STORED; + public override int Format => 4; + + public override byte[] DecryptedBoxData => EncryptedBoxData; + + public override string Extension => "bk4"; + + public override bool Valid => ChecksumValid || (Sanity != 0 && Species <= 493); // What does Sanity do? + + public BK4(byte[] decryptedData = null, string ident = null) + { + Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); + uint sv = ((PID & 0x3E000) >> 0xD) % 24; + Data = PKX.shuffleArray45(Data, sv); + Identifier = ident; + if (Sanity != 0 && Species <= 493 && !ChecksumValid) // We can only hope + { + RefreshChecksum(); + } + if (Valid && Sanity == 0) + { + Sanity = 0x4000; + } + if (Data.Length != SIZE_PARTY) + Array.Resize(ref Data, SIZE_PARTY); + } + public override PKM Clone() { return new BK4(Encrypt()); } + + // Future Attributes + public override uint EncryptionConstant { get { return PID; } set { } } + public override int Nature { get { return (int)(PID%25); } set { } } + public override int CurrentFriendship { get { return OT_Friendship; } set { OT_Friendship = value; } } + public override int CurrentHandler { get { return 0; } set { } } + + // Structure + public override uint PID { get { return Util.SwapEndianness(BitConverter.ToUInt32(Data, 0x00)); } set { BitConverter.GetBytes(Util.SwapEndianness(value)).CopyTo(Data, 0x00); } } + public override ushort Sanity { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x04)); } set { BitConverter.GetBytes(Util.SwapEndianness(value)).CopyTo(Data, 0x04); } } + public override ushort Checksum { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x06)); } set { BitConverter.GetBytes(Util.SwapEndianness(value)).CopyTo(Data, 0x06); } } + + #region Block A + public override int Species { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x08)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x08); } } + public override int HeldItem { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x0A)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x0A); } } + public override int SID { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x0C)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x0C); } } + public override int TID { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x0E)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x0E); } } + public override uint EXP + { + get { return Util.SwapEndianness(BitConverter.ToUInt32(Data, 0x10)); } + set { BitConverter.GetBytes(Util.SwapEndianness(value)).CopyTo(Data, 0x10); } + } + public override int OT_Friendship { get { return Data[0x14]; } set { Data[0x14] = (byte)value; } } + public override int Ability { get { return Data[0x15]; } set { Data[0x15] = (byte)value; } } + public override byte MarkByte { get { return Data[0x16]; } protected set { Data[0x16] = value; } } + public override int Language { get { return Data[0x17]; } set { Data[0x17] = (byte)value; } } + public override int EV_HP { get { return Data[0x18]; } set { Data[0x18] = (byte)value; } } + public override int EV_ATK { get { return Data[0x19]; } set { Data[0x19] = (byte)value; } } + public override int EV_DEF { get { return Data[0x1A]; } set { Data[0x1A] = (byte)value; } } + public override int EV_SPE { get { return Data[0x1B]; } set { Data[0x1B] = (byte)value; } } + public override int EV_SPA { get { return Data[0x1C]; } set { Data[0x1C] = (byte)value; } } + public override int EV_SPD { get { return Data[0x1D]; } set { Data[0x1D] = (byte)value; } } + public override int CNT_Cool { get { return Data[0x1E]; } set { Data[0x1E] = (byte)value; } } + public override int CNT_Beauty { get { return Data[0x1F]; } set { Data[0x1F] = (byte)value; } } + public override int CNT_Cute { get { return Data[0x20]; } set { Data[0x20] = (byte)value; } } + public override int CNT_Smart { get { return Data[0x21]; } set { Data[0x21] = (byte)value; } } + public override int CNT_Tough { get { return Data[0x22]; } set { Data[0x22] = (byte)value; } } + public override int CNT_Sheen { get { return Data[0x23]; } set { Data[0x23] = (byte)value; } } + + private byte RIB0 { get { return Data[0x24]; } set { Data[0x24] = value; } } // Sinnoh 1 + private byte RIB1 { get { return Data[0x25]; } set { Data[0x25] = value; } } // Sinnoh 2 + private byte RIB2 { get { return Data[0x26]; } set { Data[0x26] = value; } } // Unova 1 + private byte RIB3 { get { return Data[0x27]; } set { Data[0x27] = value; } } // Unova 2 + public bool RibbonChampionSinnoh { get { return (RIB0 & (1 << 0)) == 1 << 0; } set { RIB0 = (byte)(RIB0 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonAbility { get { return (RIB0 & (1 << 1)) == 1 << 1; } set { RIB0 = (byte)(RIB0 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonAbilityGreat { get { return (RIB0 & (1 << 2)) == 1 << 2; } set { RIB0 = (byte)(RIB0 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonAbilityDouble { get { return (RIB0 & (1 << 3)) == 1 << 3; } set { RIB0 = (byte)(RIB0 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonAbilityMulti { get { return (RIB0 & (1 << 4)) == 1 << 4; } set { RIB0 = (byte)(RIB0 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonAbilityPair { get { return (RIB0 & (1 << 5)) == 1 << 5; } set { RIB0 = (byte)(RIB0 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonAbilityWorld { get { return (RIB0 & (1 << 6)) == 1 << 6; } set { RIB0 = (byte)(RIB0 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonAlert { get { return (RIB0 & (1 << 7)) == 1 << 7; } set { RIB0 = (byte)(RIB0 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonShock { get { return (RIB1 & (1 << 0)) == 1 << 0; } set { RIB1 = (byte)(RIB1 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonDowncast { get { return (RIB1 & (1 << 1)) == 1 << 1; } set { RIB1 = (byte)(RIB1 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonCareless { get { return (RIB1 & (1 << 2)) == 1 << 2; } set { RIB1 = (byte)(RIB1 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonRelax { get { return (RIB1 & (1 << 3)) == 1 << 3; } set { RIB1 = (byte)(RIB1 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonSnooze { get { return (RIB1 & (1 << 4)) == 1 << 4; } set { RIB1 = (byte)(RIB1 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonSmile { get { return (RIB1 & (1 << 5)) == 1 << 5; } set { RIB1 = (byte)(RIB1 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonGorgeous { get { return (RIB1 & (1 << 6)) == 1 << 6; } set { RIB1 = (byte)(RIB1 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonRoyal { get { return (RIB1 & (1 << 7)) == 1 << 7; } set { RIB1 = (byte)(RIB1 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonGorgeousRoyal { get { return (RIB2 & (1 << 0)) == 1 << 0; } set { RIB2 = (byte)(RIB2 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonFootprint { get { return (RIB2 & (1 << 1)) == 1 << 1; } set { RIB2 = (byte)(RIB2 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonRecord { get { return (RIB2 & (1 << 2)) == 1 << 2; } set { RIB2 = (byte)(RIB2 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonEvent { get { return (RIB2 & (1 << 3)) == 1 << 3; } set { RIB2 = (byte)(RIB2 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonLegend { get { return (RIB2 & (1 << 4)) == 1 << 4; } set { RIB2 = (byte)(RIB2 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonChampionWorld { get { return (RIB2 & (1 << 5)) == 1 << 5; } set { RIB2 = (byte)(RIB2 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonBirthday { get { return (RIB2 & (1 << 6)) == 1 << 6; } set { RIB2 = (byte)(RIB2 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonSpecial { get { return (RIB2 & (1 << 7)) == 1 << 7; } set { RIB2 = (byte)(RIB2 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonSouvenir { get { return (RIB3 & (1 << 0)) == 1 << 0; } set { RIB3 = (byte)(RIB3 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonWishing { get { return (RIB3 & (1 << 1)) == 1 << 1; } set { RIB3 = (byte)(RIB3 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonClassic { get { return (RIB3 & (1 << 2)) == 1 << 2; } set { RIB3 = (byte)(RIB3 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonPremier { get { return (RIB3 & (1 << 3)) == 1 << 3; } set { RIB3 = (byte)(RIB3 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB3_4 { get { return (RIB3 & (1 << 4)) == 1 << 4; } set { RIB3 = (byte)(RIB3 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } // Unused + public bool RIB3_5 { get { return (RIB3 & (1 << 5)) == 1 << 5; } set { RIB3 = (byte)(RIB3 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } // Unused + public bool RIB3_6 { get { return (RIB3 & (1 << 6)) == 1 << 6; } set { RIB3 = (byte)(RIB3 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } // Unused + public bool RIB3_7 { get { return (RIB3 & (1 << 7)) == 1 << 7; } set { RIB3 = (byte)(RIB3 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } // Unused + #endregion + + #region Block B + public override int Move1 { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x28)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x28); } } + public override int Move2 { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x2A)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x2A); } } + public override int Move3 { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x2C)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x2C); } } + public override int Move4 { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x2E)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x2E); } } + public override int Move1_PP { get { return Data[0x30]; } set { Data[0x30] = (byte)value; } } + public override int Move2_PP { get { return Data[0x31]; } set { Data[0x31] = (byte)value; } } + public override int Move3_PP { get { return Data[0x32]; } set { Data[0x32] = (byte)value; } } + public override int Move4_PP { get { return Data[0x33]; } set { Data[0x33] = (byte)value; } } + public override int Move1_PPUps { get { return Data[0x34]; } set { Data[0x34] = (byte)value; } } + public override int Move2_PPUps { get { return Data[0x35]; } set { Data[0x35] = (byte)value; } } + public override int Move3_PPUps { get { return Data[0x36]; } set { Data[0x36] = (byte)value; } } + public override int Move4_PPUps { get { return Data[0x37]; } set { Data[0x37] = (byte)value; } } + public uint IV32 { get { return Util.SwapEndianness(BitConverter.ToUInt32(Data, 0x38)); } set { BitConverter.GetBytes(Util.SwapEndianness(value)).CopyTo(Data, 0x38); } } + public override int IV_HP { get { return (int)(IV32 >> 02) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 02)) | (uint)((value > 31 ? 31 : value) << 02)); } } + public override int IV_ATK { get { return (int)(IV32 >> 07) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 07)) | (uint)((value > 31 ? 31 : value) << 07)); } } + public override int IV_DEF { get { return (int)(IV32 >> 12) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 12)) | (uint)((value > 31 ? 31 : value) << 12)); } } + public override int IV_SPE { get { return (int)(IV32 >> 17) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 17)) | (uint)((value > 31 ? 31 : value) << 17)); } } + public override int IV_SPA { get { return (int)(IV32 >> 22) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 22)) | (uint)((value > 31 ? 31 : value) << 22)); } } + public override int IV_SPD { get { return (int)(IV32 >> 27) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 27)) | (uint)((value > 31 ? 31 : value) << 27)); } } + public override bool IsNicknamed { get { return ((IV32 >> 0) & 1) == 1; } set { IV32 = (uint)((IV32 & ~0x00000001) | (uint)(value ? 0x00000001 : 0)); } } + public override bool IsEgg { get { return ((IV32 >> 1) & 1) == 1; } set { IV32 = (uint)((IV32 & ~0x00000002) | (uint)(value ? 0x00000002 : 0)); } } + + private byte RIB4 { get { return Data[0x3C]; } set { Data[0x3C] = value; } } // Hoenn 1a + private byte RIB5 { get { return Data[0x3D]; } set { Data[0x3D] = value; } } // Hoenn 1b + private byte RIB6 { get { return Data[0x3E]; } set { Data[0x3E] = value; } } // Hoenn 2a + private byte RIB7 { get { return Data[0x3F]; } set { Data[0x3F] = value; } } // Hoenn 2b + public bool RibbonG3Cool { get { return (RIB4 & (1 << 0)) == 1 << 0; } set { RIB4 = (byte)(RIB4 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonG3CoolSuper { get { return (RIB4 & (1 << 1)) == 1 << 1; } set { RIB4 = (byte)(RIB4 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonG3CoolHyper { get { return (RIB4 & (1 << 2)) == 1 << 2; } set { RIB4 = (byte)(RIB4 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonG3CoolMaster { get { return (RIB4 & (1 << 3)) == 1 << 3; } set { RIB4 = (byte)(RIB4 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonG3Beauty { get { return (RIB4 & (1 << 4)) == 1 << 4; } set { RIB4 = (byte)(RIB4 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonG3BeautySuper { get { return (RIB4 & (1 << 5)) == 1 << 5; } set { RIB4 = (byte)(RIB4 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonG3BeautyHyper { get { return (RIB4 & (1 << 6)) == 1 << 6; } set { RIB4 = (byte)(RIB4 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonG3BeautyMaster { get { return (RIB4 & (1 << 7)) == 1 << 7; } set { RIB4 = (byte)(RIB4 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonG3Cute { get { return (RIB5 & (1 << 0)) == 1 << 0; } set { RIB5 = (byte)(RIB5 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonG3CuteSuper { get { return (RIB5 & (1 << 1)) == 1 << 1; } set { RIB5 = (byte)(RIB5 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonG3CuteHyper { get { return (RIB5 & (1 << 2)) == 1 << 2; } set { RIB5 = (byte)(RIB5 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonG3CuteMaster { get { return (RIB5 & (1 << 3)) == 1 << 3; } set { RIB5 = (byte)(RIB5 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonG3Smart { get { return (RIB5 & (1 << 4)) == 1 << 4; } set { RIB5 = (byte)(RIB5 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonG3SmartSuper { get { return (RIB5 & (1 << 5)) == 1 << 5; } set { RIB5 = (byte)(RIB5 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonG3SmartHyper { get { return (RIB5 & (1 << 6)) == 1 << 6; } set { RIB5 = (byte)(RIB5 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonG3SmartMaster { get { return (RIB5 & (1 << 7)) == 1 << 7; } set { RIB5 = (byte)(RIB5 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonG3Tough { get { return (RIB6 & (1 << 0)) == 1 << 0; } set { RIB6 = (byte)(RIB6 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonG3ToughSuper { get { return (RIB6 & (1 << 1)) == 1 << 1; } set { RIB6 = (byte)(RIB6 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonG3ToughHyper { get { return (RIB6 & (1 << 2)) == 1 << 2; } set { RIB6 = (byte)(RIB6 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonG3ToughMaster { get { return (RIB6 & (1 << 3)) == 1 << 3; } set { RIB6 = (byte)(RIB6 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonChampionG3Hoenn { get { return (RIB6 & (1 << 4)) == 1 << 4; } set { RIB6 = (byte)(RIB6 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonWinning { get { return (RIB6 & (1 << 5)) == 1 << 5; } set { RIB6 = (byte)(RIB6 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonVictory { get { return (RIB6 & (1 << 6)) == 1 << 6; } set { RIB6 = (byte)(RIB6 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonArtist { get { return (RIB6 & (1 << 7)) == 1 << 7; } set { RIB6 = (byte)(RIB6 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonEffort { get { return (RIB7 & (1 << 0)) == 1 << 0; } set { RIB7 = (byte)(RIB7 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonChampionBattle { get { return (RIB7 & (1 << 1)) == 1 << 1; } set { RIB7 = (byte)(RIB7 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonChampionRegional{ get { return (RIB7 & (1 << 2)) == 1 << 2; } set { RIB7 = (byte)(RIB7 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonChampionNational{ get { return (RIB7 & (1 << 3)) == 1 << 3; } set { RIB7 = (byte)(RIB7 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonCountry { get { return (RIB7 & (1 << 4)) == 1 << 4; } set { RIB7 = (byte)(RIB7 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonNational { get { return (RIB7 & (1 << 5)) == 1 << 5; } set { RIB7 = (byte)(RIB7 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonEarth { get { return (RIB7 & (1 << 6)) == 1 << 6; } set { RIB7 = (byte)(RIB7 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonWorld { get { return (RIB7 & (1 << 7)) == 1 << 7; } set { RIB7 = (byte)(RIB7 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + + public override bool FatefulEncounter { get { return (Data[0x40] & 0x80) == 0x80; } set { Data[0x40] = (byte)(Data[0x40] & ~0x80 | (value ? 0x80 : 0)); } } + public override int Gender { get { return (Data[0x40] >> 5) & 0x3; } set { Data[0x40] = (byte)(Data[0x40] & ~0x60 | ((value & 3) << 5)); } } + public override int AltForm { get { return Data[0x40] & 0x1F; } set { Data[0x40] = (byte)(Data[0x40] & ~0x1F | (value & 0x1F)); } } + // 0x43-0x47 Unused + #endregion + + #region Block C + public override string Nickname + { + get + { + return PKX.array2strG4BE(Data.Skip(0x48).Take(22).ToArray()) + .Replace("\uE08F", "\u2640") // nidoran + .Replace("\uE08E", "\u2642") // nidoran + .Replace("\u2019", "\u0027"); // farfetch'd + } + set + { + if (value.Length > 11) + value = value.Substring(0, 11); // Hard cap + string TempNick = value // Replace Special Characters and add Terminator + .Replace("\u2640", "\uE08F") // nidoran + .Replace("\u2642", "\uE08E") // nidoran + .Replace("\u0027", "\u2019"); // farfetch'd + PKX.str2arrayG4BE(TempNick).CopyTo(Data, 0x48); + } + } + // 0x5E unused + public override int Version { get { return Data[0x5F]; } set { Data[0x5F] = (byte)value; } } + private byte RIB8 { get { return Data[0x60]; } set { Data[0x60] = value; } } // Sinnoh 3 + private byte RIB9 { get { return Data[0x61]; } set { Data[0x61] = value; } } // Sinnoh 4 + private byte RIBA { get { return Data[0x62]; } set { Data[0x62] = value; } } // Sinnoh 5 + private byte RIBB { get { return Data[0x63]; } set { Data[0x63] = value; } } // Sinnoh 6 + public bool RibbonG4Cool { get { return (RIB8 & (1 << 0)) == 1 << 0; } set { RIB8 = (byte)(RIB8 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonG4CoolGreat { get { return (RIB8 & (1 << 1)) == 1 << 1; } set { RIB8 = (byte)(RIB8 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonG4CoolUltra { get { return (RIB8 & (1 << 2)) == 1 << 2; } set { RIB8 = (byte)(RIB8 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonG4CoolMaster { get { return (RIB8 & (1 << 3)) == 1 << 3; } set { RIB8 = (byte)(RIB8 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonG4Beauty { get { return (RIB8 & (1 << 4)) == 1 << 4; } set { RIB8 = (byte)(RIB8 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonG4BeautyGreat { get { return (RIB8 & (1 << 5)) == 1 << 5; } set { RIB8 = (byte)(RIB8 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonG4BeautyUltra { get { return (RIB8 & (1 << 6)) == 1 << 6; } set { RIB8 = (byte)(RIB8 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonG4BeautyMaster { get { return (RIB8 & (1 << 7)) == 1 << 7; } set { RIB8 = (byte)(RIB8 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonG4Cute { get { return (RIB9 & (1 << 0)) == 1 << 0; } set { RIB9 = (byte)(RIB9 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonG4CuteGreat { get { return (RIB9 & (1 << 1)) == 1 << 1; } set { RIB9 = (byte)(RIB9 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonG4CuteUltra { get { return (RIB9 & (1 << 2)) == 1 << 2; } set { RIB9 = (byte)(RIB9 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonG4CuteMaster { get { return (RIB9 & (1 << 3)) == 1 << 3; } set { RIB9 = (byte)(RIB9 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RibbonG4Smart { get { return (RIB9 & (1 << 4)) == 1 << 4; } set { RIB9 = (byte)(RIB9 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RibbonG4SmartGreat { get { return (RIB9 & (1 << 5)) == 1 << 5; } set { RIB9 = (byte)(RIB9 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RibbonG4SmartUltra { get { return (RIB9 & (1 << 6)) == 1 << 6; } set { RIB9 = (byte)(RIB9 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RibbonG4SmartMaster { get { return (RIB9 & (1 << 7)) == 1 << 7; } set { RIB9 = (byte)(RIB9 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + public bool RibbonG4Tough { get { return (RIBA & (1 << 0)) == 1 << 0; } set { RIBA = (byte)(RIBA & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RibbonG4ToughGreat { get { return (RIBA & (1 << 1)) == 1 << 1; } set { RIBA = (byte)(RIBA & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RibbonG4ToughUltra { get { return (RIBA & (1 << 2)) == 1 << 2; } set { RIBA = (byte)(RIBA & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RibbonG4ToughMaster { get { return (RIBA & (1 << 3)) == 1 << 3; } set { RIBA = (byte)(RIBA & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIBA_4 { get { return (RIBA & (1 << 4)) == 1 << 4; } set { RIBA = (byte)(RIBA & ~(1 << 4) | (value ? 1 << 4 : 0)); } } // Unused + public bool RIBA_5 { get { return (RIBA & (1 << 5)) == 1 << 5; } set { RIBA = (byte)(RIBA & ~(1 << 5) | (value ? 1 << 5 : 0)); } } // Unused + public bool RIBA_6 { get { return (RIBA & (1 << 6)) == 1 << 6; } set { RIBA = (byte)(RIBA & ~(1 << 6) | (value ? 1 << 6 : 0)); } } // Unused + public bool RIBA_7 { get { return (RIBA & (1 << 7)) == 1 << 7; } set { RIBA = (byte)(RIBA & ~(1 << 7) | (value ? 1 << 7 : 0)); } } // Unused + public bool RIBB_0 { get { return (RIBB & (1 << 0)) == 1 << 0; } set { RIBB = (byte)(RIBB & ~(1 << 0) | (value ? 1 << 0 : 0)); } } // Unused + public bool RIBB_1 { get { return (RIBB & (1 << 1)) == 1 << 1; } set { RIBB = (byte)(RIBB & ~(1 << 1) | (value ? 1 << 1 : 0)); } } // Unused + public bool RIBB_2 { get { return (RIBB & (1 << 2)) == 1 << 2; } set { RIBB = (byte)(RIBB & ~(1 << 2) | (value ? 1 << 2 : 0)); } } // Unused + public bool RIBB_3 { get { return (RIBB & (1 << 3)) == 1 << 3; } set { RIBB = (byte)(RIBB & ~(1 << 3) | (value ? 1 << 3 : 0)); } } // Unused + public bool RIBB_4 { get { return (RIBB & (1 << 4)) == 1 << 4; } set { RIBB = (byte)(RIBB & ~(1 << 4) | (value ? 1 << 4 : 0)); } } // Unused + public bool RIBB_5 { get { return (RIBB & (1 << 5)) == 1 << 5; } set { RIBB = (byte)(RIBB & ~(1 << 5) | (value ? 1 << 5 : 0)); } } // Unused + public bool RIBB_6 { get { return (RIBB & (1 << 6)) == 1 << 6; } set { RIBB = (byte)(RIBB & ~(1 << 6) | (value ? 1 << 6 : 0)); } } // Unused + public bool RIBB_7 { get { return (RIBB & (1 << 7)) == 1 << 7; } set { RIBB = (byte)(RIBB & ~(1 << 7) | (value ? 1 << 7 : 0)); } } // Unused + // 0x64-0x67 Unused + #endregion + + #region Block D + public override string OT_Name + { + get + { + return PKX.array2strG4BE(Data.Skip(0x68).Take(16).ToArray()) + .Replace("\uE08F", "\u2640") // Nidoran ♂ + .Replace("\uE08E", "\u2642") // Nidoran ♀ + .Replace("\u2019", "\u0027"); // Farfetch'd + } + set + { + if (value.Length > 7) + value = value.Substring(0, 7); // Hard cap + string TempNick = value // Replace Special Characters and add Terminator + .Replace("\u2640", "\uE08F") // Nidoran ♂ + .Replace("\u2642", "\uE08E") // Nidoran ♀ + .Replace("\u0027", "\u2019"); // Farfetch'd + PKX.str2arrayG4BE(TempNick).CopyTo(Data, 0x68); + } + } + protected override int Egg_Year { get { return Data[0x78]; } set { Data[0x78] = (byte)value; } } + protected override int Egg_Month { get { return Data[0x79]; } set { Data[0x79] = (byte)value; } } + protected override int Egg_Day { get { return Data[0x7A]; } set { Data[0x7A] = (byte)value; } } + protected override int Met_Year { get { return Data[0x7B]; } set { Data[0x7B] = (byte)value; } } + protected override int Met_Month { get { return Data[0x7C]; } set { Data[0x7C] = (byte)value; } } + protected override int Met_Day { get { return Data[0x7D]; } set { Data[0x7D] = (byte)value; } } + + public override int Egg_Location + { + get + { + ushort hgssloc = Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x44)); + if (hgssloc != 0) + return hgssloc; + return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x7E)); + } + set + { + if (value == 0) + { + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x44); + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x7E); + } + else if (PtHGSS) + { + BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x44); + BitConverter.GetBytes(Util.SwapEndianness((ushort)0xBBA)).CopyTo(Data, 0x7E); // Faraway Place (for DP display) + } + else if ((value < 2000 && value > 111) || (value < 3000 && value > 2010)) + { + // Met location not in DP, set to Mystery Zone (0, illegal) as opposed to Faraway Place + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x44); + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x7E); + } + else + { + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x44); + BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x7E); + } + } + } + public override int Met_Location + { + get + { + ushort hgssloc = Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x46)); + if (hgssloc != 0) + return hgssloc; + return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x80)); + } + set + { + if (value == 0) + { + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x46); + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x80); + } + else if (PtHGSS) + { + BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x46); + BitConverter.GetBytes(Util.SwapEndianness((ushort)0xBBA)).CopyTo(Data, 0x80); // Faraway Place (for DP display) + } + else if ((value < 2000 && value > 111) || (value < 3000 && value > 2010)) + { + // Met location not in DP, set to Mystery Zone (0, illegal) as opposed to Faraway Place + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x46); + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x80); + } + else + { + BitConverter.GetBytes(Util.SwapEndianness((ushort)0)).CopyTo(Data, 0x46); + BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x80); + } + } + } + private byte PKRS { get { return Data[0x82]; } set { Data[0x82] = value; } } + public override int PKRS_Days { get { return PKRS & 0xF; } set { PKRS = (byte)(PKRS & ~0xF | value); } } + public override int PKRS_Strain { get { return PKRS >> 4; } set { PKRS = (byte)(PKRS & 0xF | (value << 4)); } } + public override int Ball + { + get + { + // Pokemon obtained in HGSS have the HGSS ball set (@0x86) + // However, this info is not set when receiving a wondercard! + // The PGT contains a preformatted PK4 file, which is slightly modified. + // No HGSS balls were used, and no HGSS ball info is set. + + // Sneaky way = return the higher of the two values. + return Math.Max(Data[0x86], Data[0x83]); + } + set + { + // Ball to display in DPPt + Data[0x83] = (byte)(value <= 0x10 ? value : 4); // Cap at Cherish Ball + + // HGSS Exclusive Balls -- If the user wants to screw things up, let them. Any legality checking could catch hax. + if (value > 0x10 || (HGSS && !FatefulEncounter)) + Data[0x86] = (byte)(value <= 0x18 ? value : 4); // Cap at Comp Ball + else + Data[0x86] = 0; // Unused + } + } + public override int Met_Level { get { return Data[0x84] >> 1; } set { Data[0x84] = (byte)((Data[0x84] & 0x1) | value << 1); } } + public override int OT_Gender { get { return Data[0x84] & 1; } set { Data[0x84] = (byte)((Data[0x84] & ~0x1) | value & 1); } } + public override int EncounterType { get { return Data[0x85]; } set { Data[0x85] = (byte)value; } } + // Unused 0x87 + #endregion + + public override int Stat_Level { get { return Data[0x8C]; } set { Data[0x8C] = (byte)value; } } + public override int Stat_HPCurrent { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x8E)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x8E); } } + public override int Stat_HPMax { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x90)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x90); } } + public override int Stat_ATK { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x92)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x92); } } + public override int Stat_DEF { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x94)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x94); } } + public override int Stat_SPE { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x96)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x96); } } + public override int Stat_SPA { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x98)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x98); } } + public override int Stat_SPD { get { return Util.SwapEndianness(BitConverter.ToUInt16(Data, 0x9A)); } set { BitConverter.GetBytes(Util.SwapEndianness((ushort)value)).CopyTo(Data, 0x9A); } } + + public override int PSV => (int)((PID >> 16 ^ PID & 0xFFFF) >> 3); + public override int TSV => (TID ^ SID) >> 3; + public override int Characteristic + { + get + { + // Characteristic with PID%6 + int pm6 = (int)(PID % 6); // PID MOD 6 + int maxIV = IVs.Max(); + int pm6stat = 0; + + for (int i = 0; i < 6; i++) + { + pm6stat = (pm6 + i) % 6; + if (IVs[pm6stat] == maxIV) + break; // P%6 is this stat + } + return pm6stat * 5 + maxIV % 5; + } + } + + // Methods + public override bool getGenderIsValid() + { + int gv = PersonalTable.HGSS[Species].Gender; + + if (gv == 255) + return Gender == 2; + if (gv == 254) + return Gender == 0; + if (gv == 0) + return Gender == 1; + if (gv <= (PID & 0xFF)) + return Gender == 0; + if ((PID & 0xFF) < gv) + return Gender == 1; + + return false; + } + + protected override ushort CalculateChecksum() + { + ushort chk = 0; + for (int i = 8; i < SIZE_STORED; i += 2) + chk += Util.SwapEndianness(BitConverter.ToUInt16(Data, i)); + return chk; + } + + public override byte[] Encrypt() + { + RefreshChecksum(); + return PKX.shuffleArray45(Data, PKX.blockPositionInvert[((PID & 0x3E000) >> 0xD)%24]); + } + + public PK4 convertToPK4() + { + PK4 pk4 = new PK4(); + foreach (string property in ReflectUtil.getPropertiesCanWritePublic(typeof(PK4)).Intersect(ReflectUtil.getPropertiesCanWritePublic(typeof(BK4)))) + { + var prop = ReflectUtil.GetValue(this, property); + if (prop != null) + ReflectUtil.SetValue(pk4, property, prop); + } + // Fix Non-Reflectable properties + Array.Copy(Data, 0x78, pk4.Data, 0x78, 6); // Met Info + for (int i = 0; i < 11; i++) // Nickname + { + Data.Skip(0x48 + 2 * i).Take(2).Reverse().ToArray().CopyTo(pk4.Data, 0x48 + 2 * i); + } + for (int i = 0; i < 8; i++) // OT_Name + { + Data.Skip(0x68 + 2 * i).Take(2).Reverse().ToArray().CopyTo(pk4.Data, 0x68 + 2 * i); + } + pk4.Sanity = 0; + pk4.RefreshChecksum(); + return pk4; + } + } +} diff --git a/PKHeX/PKM/PK4.cs b/PKHeX/PKM/PK4.cs index a9d669876..86d562049 100644 --- a/PKHeX/PKM/PK4.cs +++ b/PKHeX/PKM/PK4.cs @@ -405,6 +405,30 @@ public override byte[] Encrypt() return PKX.encryptArray45(Data); } + public BK4 convertToBK4() + { + BK4 bk4 = new BK4(); + foreach (string property in ReflectUtil.getPropertiesCanWritePublic(typeof(PK4)).Intersect(ReflectUtil.getPropertiesCanWritePublic(typeof(BK4)))) + { + var prop = ReflectUtil.GetValue(this, property); + if (prop != null) + ReflectUtil.SetValue(bk4, property, prop); + } + // Fix Non-Reflectable properties + Array.Copy(Data, 0x78, bk4.Data, 0x78, 6); // Met Info + for (int i = 0; i < 11; i++) // Nickname + { + Data.Skip(0x48 + 2*i).Take(2).Reverse().ToArray().CopyTo(bk4.Data, 0x48 + 2*i); + } + for (int i = 0; i < 8; i++) // OT_Name + { + Data.Skip(0x68 + 2 * i).Take(2).Reverse().ToArray().CopyTo(bk4.Data, 0x68 + 2 * i); + } + bk4.Sanity = 0x4000; + bk4.RefreshChecksum(); + return bk4; + } + public PK5 convertToPK5() { // Double Check Location Data to see if we're already a PK5 diff --git a/PKHeX/PKM/PKM.cs b/PKHeX/PKM/PKM.cs index b3afaaa3b..ae490f973 100644 --- a/PKHeX/PKM/PKM.cs +++ b/PKHeX/PKM/PKM.cs @@ -8,7 +8,7 @@ public abstract class PKM { public abstract int SIZE_PARTY { get; } public abstract int SIZE_STORED { get; } - public string Extension => "pk" + Format; + public virtual string Extension => "pk" + Format; // Internal Attributes set on creation public byte[] Data; // Raw Storage @@ -22,7 +22,7 @@ public abstract class PKM public virtual byte[] DecryptedBoxData => Write().Take(SIZE_STORED).ToArray(); public virtual bool Valid => ChecksumValid && Sanity == 0; - protected ushort CalculateChecksum() + protected virtual ushort CalculateChecksum() { ushort chk = 0; switch (Format) diff --git a/PKHeX/PKM/PKMConverter.cs b/PKHeX/PKM/PKMConverter.cs index 7ab96784a..d346a7a67 100644 --- a/PKHeX/PKM/PKMConverter.cs +++ b/PKHeX/PKM/PKMConverter.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Linq; namespace PKHeX { @@ -43,7 +45,7 @@ public static int getPKMDataFormat(byte[] data) case PKX.SIZE_4PARTY: case PKX.SIZE_4STORED: case PKX.SIZE_5PARTY: - if ((BitConverter.ToUInt16(data, 0x80) >= 0x3333 || data[0x5F] >= 0x10) && BitConverter.ToUInt16(data, 0x46) == 0) // PK5 + if (((BitConverter.ToUInt16(data, 0x4) == 0) && (BitConverter.ToUInt16(data, 0x80) >= 0x3333 || data[0x5F] >= 0x10)) && BitConverter.ToUInt16(data, 0x46) == 0) // PK5 return 5; return 4; case PKX.SIZE_6STORED: @@ -89,7 +91,14 @@ public static PKM getPKMfromBytes(byte[] data, string ident = null) case 3: return new PK3(data, ident); case 4: - return new PK4(data, ident); + var pk = new PK4(data, ident); + if (!pk.Valid || pk.Sanity != 0) + { + var bk = new BK4(data, ident); + if (bk.Valid) + return bk; + } + return pk; case 5: return new PK5(data, ident); case 6: @@ -98,7 +107,7 @@ public static PKM getPKMfromBytes(byte[] data, string ident = null) return null; } } - internal static PKM convertToFormat(PKM pk, int Format, out string comment) + internal static PKM convertToFormat(PKM pk, Type PKMType, out string comment) { if (pk == null || pk.Species == 0) { @@ -106,67 +115,109 @@ internal static PKM convertToFormat(PKM pk, int Format, out string comment) return null; } - string currentFormat = pk.Format.ToString(); - PKM pkm = pk.Clone(); + Type fromType = pk.GetType(); + int fromFormat = int.Parse(fromType.Name.Last().ToString()); + int toFormat = int.Parse(PKMType.Name.Last().ToString()); + Console.WriteLine($"Trying to convert {fromType.Name} to {PKMType.Name}."); - if (pk.Format == Format) + PKM pkm = null; + + if (fromType == PKMType) { comment = "No need to convert, current format matches requested format."; return pk; } - if (pk.Format <= 2 && Format <= 2) // 1<->2, already checked not equal + if (fromFormat <= toFormat) { - switch (Format) + pkm = pk.Clone(); + if (pkm.IsEgg) // force hatch { - case 1: - if (pk.Species > 151) - { comment = $"Cannot convert a {PKX.getSpeciesName(pk.Species, ((PK2)pk).Japanese ? 1 : 2)} to pk{Format}"; return null; } - pkm = ((PK2)pk).convertToPK1(); + pkm.IsEgg = false; + if (pkm.AO) + pkm.Met_Location = 318; // Battle Resort + else if (pkm.XY) + pkm.Met_Location = 38; // Route 7 + else if (pkm.Gen5) + pkm.Met_Location = 16; // Route 16 + else + pkm.Met_Location = 30001; // Pokétransfer + } + switch (fromType.Name) + { + case "PK1": + if (PKMType == typeof(PK2)) + { + if (pk.Species > 151) + { + comment = $"Cannot convert a {PKX.getSpeciesName(pk.Species, ((PK2)pk).Japanese ? 1 : 2)} to {PKMType.Name}"; + return null; + } + pkm = ((PK2)pk).convertToPK1(); + } + else + pkm = null; break; - case 2: - pkm = ((PK1)pk).convertToPK2(); + case "PK2": + if (PKMType == typeof(PK1)) + { + pkm = ((PK1)pk).convertToPK2(); + } + else + pkm = null; + break; + case "PK3": + if (toFormat == 3) + { + // Colo/XD stuff here! + } + pkm = ((PK3)pkm).convertToPK4(); + if (toFormat == 4) + { + if (PKMType == typeof (BK4)) + pkm = ((PK4) pkm).convertToBK4(); + break; + } + pkm = ((PK4)pkm).convertToPK5(); + if (toFormat == 5) + break; + pkm = ((PK5)pkm).convertToPK6(); + break; + case "PK4": + if (PKMType == typeof(BK4)) + { + pkm = ((PK4)pkm).convertToBK4(); + break; + } + pkm = ((PK4)pkm).convertToPK5(); + if (toFormat == 5) + break; + pkm = ((PK5)pkm).convertToPK6(); + break; + case "BK4": + pkm = ((BK4)pkm).convertToPK4(); + if (toFormat == 4) + break; + pkm = ((PK4)pkm).convertToPK5(); + if (toFormat == 5) + break; + pkm = ((PK5)pkm).convertToPK6(); + break; + case "PK5": + pkm = ((PK5)pkm).convertToPK6(); + break; + case "PK6": + // This shouldn't happen before Sun/Moon. break; } - comment = $"Converted from pk{pk.Format} to pk{Format}"; - return pkm; } - if (pk.Format > Format) + if (pkm == null) { - comment = "Cannot convert a PKM backwards." + Environment.NewLine - + "Current Format: " + pk.Format + Environment.NewLine - + "Desired Format: " + Format; - return null; + comment = $"Cannot convert a {fromType.Name} to a {PKMType.Name}."; } - if ((pk.Format == 1 || pk.Format == 2) && 2 < Format && Format < 7) + else { - comment = $"Cannot convert a PK{pk.Format} to a PK{Format}."; - return null; + comment = $"Converted from {fromType.Name} to {PKMType.Name}."; } - if (pk.Format == 1 && Format == 7) - { - comment = "PK1 to PK7 conversion is not yet supported." + Environment.NewLine - + "Please wait for Sun/Moon to release and documentation to occur."; - return null; - } - if (pkm.IsEgg) // force hatch - { - pkm.IsEgg = false; - if (pkm.AO) - pkm.Met_Location = 318; // Battle Resort - else if (pkm.XY) - pkm.Met_Location = 38; // Route 7 - else if (pkm.Gen5) - pkm.Met_Location = 16; // Route 16 - else - pkm.Met_Location = 30001; // Pokétransfer - } - if (pkm.Format == 3 && Format > 3) - pkm = ((PK3) pkm).convertToPK4(); - if (pkm.Format == 4 && Format > 4) - pkm = ((PK4) pkm).convertToPK5(); - if (pkm.Format == 5 && Format > 5) - pkm = ((PK5) pkm).convertToPK6(); - comment = $"Converted from pk{currentFormat} to pk{Format}"; return pkm; } internal static void checkEncrypted(ref byte[] pkm) @@ -180,6 +231,8 @@ internal static void checkEncrypted(ref byte[] pkm) return; case 4: case 5: + if (BitConverter.ToUInt16(pkm, 4) != 0) // BK4 + return; for (int i = 8; i < PKX.SIZE_4STORED; i += 2) chk += BitConverter.ToUInt16(pkm, i); if (chk != BitConverter.ToUInt16(pkm, 0x06)) diff --git a/PKHeX/PKM/PKX.cs b/PKHeX/PKM/PKX.cs index 8787f9572..d34d1a13a 100644 --- a/PKHeX/PKM/PKX.cs +++ b/PKHeX/PKM/PKX.cs @@ -578,7 +578,7 @@ private static void setPKXFont() } // Personal.dat - public static string[] getFormList(int species, string[] t, string[] f, string[] g) + public static string[] getFormList(int species, string[] t, string[] f, string[] g, int generation = 6) { // Mega List if (Array.IndexOf(new[] @@ -705,6 +705,49 @@ public static string[] getFormList(int species, string[] t, string[] f, string[] }; case 493: + if (generation == 4) + return new[] + { + t[00], // Normal + t[01], // Fighting + t[02], // Flying + t[03], // Poison + t[04], // etc + t[05], + t[06], + t[07], + t[08], + "???", // ???-type arceus + t[09], + t[10], + t[11], + t[12], + t[13], + t[14], + t[15], + t[16] // No Fairy Type + }; + if (generation == 5) + return new[] + { + t[00], // Normal + t[01], // Fighting + t[02], // Flying + t[03], // Poison + t[04], // etc + t[05], + t[06], + t[07], + t[08], + t[09], + t[10], + t[11], + t[12], + t[13], + t[14], + t[15], + t[16] // No Fairy type + }; return new[] { t[00], // Normal @@ -1051,6 +1094,35 @@ public static byte[] str2arrayG4(string str) return strdata; } + public static string array2strG4BE(byte[] strdata) + { + string s = ""; + for (int i = 0; i < strdata.Length; i += 2) + { + ushort val = Util.SwapEndianness(BitConverter.ToUInt16(strdata, i)); + if (val == 0xFFFF) break; + ushort chr = val2charG4(val); + if (chr == 0xFFFF) break; + s += (char)chr; + } + return s; + } + + public static byte[] str2arrayG4BE(string str) + { + byte[] strdata = new byte[str.Length * 2 + 2]; // +2 for 0xFFFF + for (int i = 0; i < str.Length; i++) + { + ushort chr = str[i]; + ushort val = char2valG4(chr); + if (val == 0xFFFF || chr == 0xFFFF) + { Array.Resize(ref strdata, i * 2); break; } + BitConverter.GetBytes(Util.SwapEndianness(val)).CopyTo(strdata, i * 2); + } + BitConverter.GetBytes((ushort)0xFFFF).CopyTo(strdata, strdata.Length - 2); + return strdata; + } + // Gen3 && 3->4 Conversion has two character tables, and translates to the same character map. public static ushort getG4Val(byte val, bool jp) { return jp ? G34_4J[val] : G34_4E[val]; } public static ushort getG3Char(byte val, bool jp) { return val2charG4(getG4Val(val, jp)); } diff --git a/PKHeX/Saves/SAV1.cs b/PKHeX/Saves/SAV1.cs index e3ac3727b..7367f3757 100644 --- a/PKHeX/Saves/SAV1.cs +++ b/PKHeX/Saves/SAV1.cs @@ -159,7 +159,7 @@ public override byte[] Write(bool DSV) public int SIZE_STOREDBOX => PokemonList1.GetDataLength(Japanese ? PokemonList1.CapacityType.StoredJP : PokemonList1.CapacityType.Stored, Japanese); public override PKM BlankPKM => new PK1(null, null, Japanese); - protected override Type PKMType => typeof(PK1); + public override Type PKMType => typeof(PK1); public override int MaxMoveID => 165; public override int MaxSpeciesID => 151; diff --git a/PKHeX/Saves/SAV2.cs b/PKHeX/Saves/SAV2.cs index 320256757..eba3971e0 100644 --- a/PKHeX/Saves/SAV2.cs +++ b/PKHeX/Saves/SAV2.cs @@ -208,7 +208,7 @@ private void getSAVOffsets() public int SIZE_STOREDBOX => PokemonList2.GetDataLength(Japanese ? PokemonList2.CapacityType.StoredJP : PokemonList2.CapacityType.Stored, Japanese); public override PKM BlankPKM => new PK2(null, null, Japanese); - protected override Type PKMType => typeof(PK2); + public override Type PKMType => typeof(PK2); public override int MaxMoveID => 251; public override int MaxSpeciesID => 251; diff --git a/PKHeX/Saves/SAV3.cs b/PKHeX/Saves/SAV3.cs index 257a0ed70..be6411dfd 100644 --- a/PKHeX/Saves/SAV3.cs +++ b/PKHeX/Saves/SAV3.cs @@ -157,7 +157,7 @@ public override byte[] Write(bool DSV) public override int SIZE_STORED => PKX.SIZE_3STORED; public override int SIZE_PARTY => PKX.SIZE_3PARTY; public override PKM BlankPKM => new PK3(); - protected override Type PKMType => typeof(PK3); + public override Type PKMType => typeof(PK3); public override int MaxMoveID => 354; public override int MaxSpeciesID => 386; diff --git a/PKHeX/Saves/SAV3RSBox.cs b/PKHeX/Saves/SAV3RSBox.cs index ed106f925..ec789d6f3 100644 --- a/PKHeX/Saves/SAV3RSBox.cs +++ b/PKHeX/Saves/SAV3RSBox.cs @@ -71,7 +71,7 @@ public override byte[] Write(bool DSV) public override int SIZE_STORED => PKX.SIZE_3STORED + 4; public override int SIZE_PARTY => PKX.SIZE_3PARTY; // unused public override PKM BlankPKM => new PK3(); - protected override Type PKMType => typeof(PK3); + public override Type PKMType => typeof(PK3); public override int MaxMoveID => 354; public override int MaxSpeciesID => 386; diff --git a/PKHeX/Saves/SAV4.cs b/PKHeX/Saves/SAV4.cs index bc24e9847..e9f144f7e 100644 --- a/PKHeX/Saves/SAV4.cs +++ b/PKHeX/Saves/SAV4.cs @@ -44,7 +44,7 @@ public SAV4(byte[] data = null, GameVersion versionOverride = GameVersion.Any) public override int SIZE_STORED => PKX.SIZE_4STORED; public override int SIZE_PARTY => PKX.SIZE_4PARTY; public override PKM BlankPKM => new PK4(); - protected override Type PKMType => typeof(PK4); + public override Type PKMType => typeof(PK4); public override int BoxCount => 18; public override int MaxEV => 255; diff --git a/PKHeX/Saves/SAV4BR.cs b/PKHeX/Saves/SAV4BR.cs new file mode 100644 index 000000000..bf209512d --- /dev/null +++ b/PKHeX/Saves/SAV4BR.cs @@ -0,0 +1,306 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; + +namespace PKHeX +{ + public sealed class SAV4BR : SaveFile + { + public override string BAKName => $"{FileName} [{Version} #{SaveCount.ToString("0000")}].bak"; + public override string Filter => "PbrSaveData|*"; + public override string Extension => ""; + + public SAV4BR(byte[] data = null) + { + Data = data == null ? new byte[SaveUtil.SIZE_G4BR] : (byte[])data.Clone(); + BAK = (byte[])Data.Clone(); + Exportable = !Data.SequenceEqual(new byte[Data.Length]); + + if (SaveUtil.getIsG4BRSAV(Data) != GameVersion.BATREV) + return; + + Data = DecryptPBRSaveData(data); + + // Detect active save + if (Util.SwapEndianness(BitConverter.ToUInt32(Data, 0x1C004C)) > + Util.SwapEndianness(BitConverter.ToUInt32(Data, 0x4C))) + { + byte[] tempData = new byte[0x1C0000]; + Array.Copy(Data, 0, tempData, 0, 0x1C0000); + Array.Copy(Data, 0x1C0000, Data, 0, 0x1C0000); + tempData.CopyTo(Data, 0x1C0000); + } + + SaveSlots = new List(); + SaveNames = new string[4]; + for (int i = 0; i < 4; i++) + { + if (BitConverter.ToUInt16(Data, 0x390 + 0x6FF00*i) != 0) + { + SaveSlots.Add(i); + SaveNames[i] = Encoding.BigEndianUnicode.GetString(Data, 0x390 + 0x6FF00*i, 0x10); + } + } + + CurrentSlot = SaveSlots.First(); + + Personal = PersonalTable.DP; + HeldItems = Legal.HeldItems_DP; + + if (!Exportable) + resetBoxes(); + } + + private readonly int SaveCount; + public override byte[] Write(bool dsv = false) + { + setChecksums(); + return EncryptPBRSaveData(Data); + } + + // Configuration + public override SaveFile Clone() { return new SAV4BR(Write()); } + + public List SaveSlots; + public string[] SaveNames; + public int CurrentSlot; + protected override int Box { + get { return 0x978 + 0x6FF00*CurrentSlot; } + set { } + } + + public override int SIZE_STORED => PKX.SIZE_4STORED; + public override int SIZE_PARTY => PKX.SIZE_4PARTY - 0x10; // PBR has a party + public override PKM BlankPKM => new BK4(); + public override Type PKMType => typeof(BK4); + + public override int MaxMoveID => 467; + public override int MaxSpeciesID => 493; + public override int MaxAbilityID => 123; + public override int MaxItemID => 536; + public override int MaxBallID => 0x18; + public override int MaxGameID => 15; + + public override int MaxEV => 255; + public override int Generation => 4; + protected override int GiftCountMax => 1; + public override int OTLength => 8; + public override int NickLength => 10; + public override int MaxMoney => 999999; + + public override int BoxCount => 18; + public override bool HasParty => false; + + // Checksums + protected override void setChecksums() + { + SetChecksum(Data, 0, 0x100, 8); + SetChecksum(Data, 0, 0x1C0000, 0x1BFF80); + SetChecksum(Data, 0x1C0000, 0x100, 0x1C0008); + SetChecksum(Data, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000); + } + public override bool ChecksumsValid + { + get { + bool valid = VerifyChecksum(Data, 0, 0x1C0000, 0x1BFF80); + valid &= VerifyChecksum(Data, 0, 0x100, 8); + valid &= VerifyChecksum(Data, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000); + valid &= VerifyChecksum(Data, 0x1C0000, 0x100, 0x1C0008); + return valid; + } + } + public override string ChecksumInfo + { + get + { + return $"Checksums valid: {ChecksumsValid}."; + } + } + + // Trainer Info + public override GameVersion Version { get { return GameVersion.BATREV; } protected set { } } + + // Storage + public override int getPartyOffset(int slot) + { + return -1; + } + public override int getBoxOffset(int box) + { + return Box + SIZE_STORED * box * 30; + } + public override int CurrentBox + { + get { return 0; } + set { /* This isn't a real field. */ } + } + public override int getBoxWallpaper(int box) + { + return 0; + } + public override string getBoxName(int box) + { + return $"BOX {box + 1}"; + } + public override void setBoxName(int box, string value) + { + /* No custom box names here. */ + } + public override PKM getPKM(byte[] data) + { + byte[] pkm = data.Take(SIZE_STORED).ToArray(); + PKM bk = new BK4(pkm); + return bk; + } + public override byte[] decryptPKM(byte[] data) + { + return data; + } + + protected override void setDex(PKM pkm) { } + + public override void setStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null) + { + if (pkm == null) return; + if (pkm.GetType() != PKMType) + throw new InvalidCastException($"PKM Format needs to be {PKMType} when setting to a Battle Revolution Save File."); + if (trade ?? SetUpdatePKM) + setPKM(pkm); + if (dex ?? SetUpdateDex) + setDex(pkm); + byte[] data = pkm.EncryptedBoxData; + setData(data, offset); + + Edited = true; + } + + public static byte[] DecryptPBRSaveData(byte[] input) + { + byte[] output = new byte[input.Length]; + for (int base_ofs = 0; base_ofs < SaveUtil.SIZE_G4BR; base_ofs += 0x1C0000) + { + + Array.Copy(input, base_ofs, output, base_ofs, 8); + + ushort[] keys = new ushort[4]; + for (int i = 0; i < keys.Length; i++) + keys[i] = Util.SwapEndianness(BitConverter.ToUInt16(input, base_ofs + i * 2)); + + for (int ofs = base_ofs + 8; ofs < base_ofs + 0x1C0000; ofs += 8) + { + for (int i = 0; i < keys.Length; i++) + { + ushort val = Util.SwapEndianness(BitConverter.ToUInt16(input, ofs + i*2)); + val -= keys[i]; + BitConverter.GetBytes(Util.SwapEndianness(val)).CopyTo(output, ofs + i*2); + } + ushort[] oldKeys = (ushort[])keys.Clone(); + oldKeys[0] += 0x43; + oldKeys[1] += 0x29; + oldKeys[2] += 0x17; + oldKeys[3] += 0x13; + keys[0] = (ushort)((oldKeys[0] & 0xf) | ((oldKeys[1] << 4) & 0xf0) | ((oldKeys[2] << 8) & 0xf00) | ((oldKeys[3] << 12) & 0xf000)); + keys[1] = (ushort)(((oldKeys[0] >> 4) & 0xf) | (oldKeys[1] & 0xf0) | ((oldKeys[2] << 4) & 0xf00) | ((oldKeys[3] << 8) & 0xf000)); + keys[2] = (ushort)((oldKeys[2] & 0xf00) | ((oldKeys[1] & 0xf00) >> 4) | ((oldKeys[0] & 0xf00) >> 8) | ((oldKeys[3] << 4) & 0xf000)); + keys[3] = (ushort)(((oldKeys[0] >> 12) & 0xf) | ((oldKeys[1] >> 8) & 0xf0) | ((oldKeys[2] >> 4) & 0xf00) | (oldKeys[3] & 0xf000)); + } + } + return output; + } + + public static byte[] EncryptPBRSaveData(byte[] input) + { + byte[] output = new byte[input.Length]; + for (int base_ofs = 0; base_ofs < SaveUtil.SIZE_G4BR; base_ofs += 0x1C0000) + { + + Array.Copy(input, base_ofs, output, base_ofs, 8); + + ushort[] keys = new ushort[4]; + for (int i = 0; i < keys.Length; i++) + keys[i] = Util.SwapEndianness(BitConverter.ToUInt16(input, base_ofs + i * 2)); + + for (int ofs = base_ofs + 8; ofs < base_ofs + 0x1C0000; ofs += 8) + { + for (int i = 0; i < keys.Length; i++) + { + ushort val = Util.SwapEndianness(BitConverter.ToUInt16(input, ofs + i * 2)); + val += keys[i]; + BitConverter.GetBytes(Util.SwapEndianness(val)).CopyTo(output, ofs + i * 2); + } + ushort[] oldKeys = (ushort[])keys.Clone(); + oldKeys[0] += 0x43; + oldKeys[1] += 0x29; + oldKeys[2] += 0x17; + oldKeys[3] += 0x13; + keys[0] = (ushort)((oldKeys[0] & 0xf) | ((oldKeys[1] << 4) & 0xf0) | ((oldKeys[2] << 8) & 0xf00) | ((oldKeys[3] << 12) & 0xf000)); + keys[1] = (ushort)(((oldKeys[0] >> 4) & 0xf) | (oldKeys[1] & 0xf0) | ((oldKeys[2] << 4) & 0xf00) | ((oldKeys[3] << 8) & 0xf000)); + keys[2] = (ushort)((oldKeys[2] & 0xf00) | ((oldKeys[1] & 0xf00) >> 4) | ((oldKeys[0] & 0xf00) >> 8) | ((oldKeys[3] << 4) & 0xf000)); + keys[3] = (ushort)(((oldKeys[0] >> 12) & 0xf) | ((oldKeys[1] >> 8) & 0xf0) | ((oldKeys[2] >> 4) & 0xf00) | (oldKeys[3] & 0xf000)); + } + } + return output; + } + + public static bool VerifyChecksum(byte[] input, int offset, int len, int checksum_offset) + { + uint[] storedChecksums = new uint[16]; + for (int i = 0; i < storedChecksums.Length; i++) + { + storedChecksums[i] = Util.SwapEndianness(BitConverter.ToUInt32(input, checksum_offset + i*4)); + BitConverter.GetBytes((uint) 0).CopyTo(input, checksum_offset + i*4); + } + + uint[] checksums = new uint[16]; + + for (int i = 0; i < len; i += 2) + { + ushort val = Util.SwapEndianness(BitConverter.ToUInt16(input, offset + i)); + for (int j = 0; j < 16; j++) + { + checksums[j] += (uint)((val >> j) & 1); + } + } + + for (int i = 0; i < storedChecksums.Length; i++) + { + BitConverter.GetBytes(Util.SwapEndianness(storedChecksums[i])).CopyTo(input, checksum_offset + i*4); + } + + return checksums.SequenceEqual(storedChecksums); + } + + public static void SetChecksum(byte[] input, int offset, int len, int checksum_offset) + { + uint[] storedChecksums = new uint[16]; + for (int i = 0; i < storedChecksums.Length; i++) + { + storedChecksums[i] = Util.SwapEndianness(BitConverter.ToUInt32(input, checksum_offset + i * 4)); + BitConverter.GetBytes((uint)0).CopyTo(input, checksum_offset + i * 4); + } + + uint[] checksums = new uint[16]; + + for (int i = 0; i < len; i += 2) + { + ushort val = Util.SwapEndianness(BitConverter.ToUInt16(input, offset + i)); + for (int j = 0; j < 16; j++) + { + checksums[j] += (uint)((val >> j) & 1); + } + } + + for (int i = 0; i < checksums.Length; i++) + { + BitConverter.GetBytes(Util.SwapEndianness(checksums[i])).CopyTo(input, checksum_offset + i * 4); + } + } + + private void SwapBytes(byte[] input, int offset, int num_bytes) + { + input.Skip(offset).Take(num_bytes).Reverse().ToArray().CopyTo(input, offset); + } + } +} diff --git a/PKHeX/Saves/SAV5.cs b/PKHeX/Saves/SAV5.cs index d8e89dde9..a29a75abd 100644 --- a/PKHeX/Saves/SAV5.cs +++ b/PKHeX/Saves/SAV5.cs @@ -98,7 +98,7 @@ public SAV5(byte[] data = null, GameVersion versionOverride = GameVersion.Any) public override int SIZE_STORED => PKX.SIZE_5STORED; public override int SIZE_PARTY => PKX.SIZE_5PARTY; public override PKM BlankPKM => new PK5(); - protected override Type PKMType => typeof(PK5); + public override Type PKMType => typeof(PK5); public override int BoxCount => 24; public override int MaxEV => 255; diff --git a/PKHeX/Saves/SAV6.cs b/PKHeX/Saves/SAV6.cs index 9063f81ec..b3054145f 100644 --- a/PKHeX/Saves/SAV6.cs +++ b/PKHeX/Saves/SAV6.cs @@ -32,7 +32,7 @@ public SAV6(byte[] data = null) public override int SIZE_STORED => PKX.SIZE_6STORED; public override int SIZE_PARTY => PKX.SIZE_6PARTY; public override PKM BlankPKM => new PK6(); - protected override Type PKMType => typeof(PK6); + public override Type PKMType => typeof(PK6); public override int BoxCount => 31; public override int MaxEV => 252; diff --git a/PKHeX/Saves/SaveFile.cs b/PKHeX/Saves/SaveFile.cs index e45d190e8..0dc4f42da 100644 --- a/PKHeX/Saves/SaveFile.cs +++ b/PKHeX/Saves/SaveFile.cs @@ -26,7 +26,7 @@ public abstract class SaveFile public bool GetJapanese => Japanese; // General PKM Properties - protected abstract Type PKMType { get; } + public abstract Type PKMType { get; } public abstract PKM getPKM(byte[] data); public abstract PKM BlankPKM { get; } public abstract byte[] decryptPKM(byte[] data); @@ -111,7 +111,7 @@ public virtual byte[] Write(bool DSV) public virtual int MaxMoney { get; } = 9999999; // Offsets - protected int Box { get; set; } = int.MinValue; + protected virtual int Box { get; set; } = int.MinValue; protected int Party { get; set; } = int.MinValue; protected int Trainer1 { get; set; } = int.MinValue; protected int Daycare { get; set; } = int.MinValue; diff --git a/PKHeX/Saves/SaveUtil.cs b/PKHeX/Saves/SaveUtil.cs index 273e34298..550ac0e37 100644 --- a/PKHeX/Saves/SaveUtil.cs +++ b/PKHeX/Saves/SaveUtil.cs @@ -3,12 +3,14 @@ using System.IO; using System.Linq; using System.Text; +using System.Windows.Forms; namespace PKHeX { public enum GameVersion { /* I don't want to assign Gen I/II... */ + BATREV = -7, RSBOX = -5, GS = -4, C = -3, @@ -46,6 +48,7 @@ public static class SaveUtil internal const int SIZE_G5RAW = 0x80000; internal const int SIZE_G5BW = 0x24000; internal const int SIZE_G5B2W2 = 0x26000; + internal const int SIZE_G4BR = 0x380000; internal const int SIZE_G4RAW = 0x80000; internal const int SIZE_G3BOX = 0x76000; internal const int SIZE_G3BOXGCI = 0x76040; // +64 if has GCI data @@ -77,6 +80,8 @@ public static int getSAVGeneration(byte[] data) return (int)GameVersion.RSBOX; if (getIsG4SAV(data) != GameVersion.Invalid) return 4; + if (getIsG4BRSAV(data) != GameVersion.Invalid) + return (int) GameVersion.BATREV; if (getIsG5SAV(data) != GameVersion.Invalid) return 5; if (getIsG6SAV(data) != GameVersion.Invalid) @@ -278,6 +283,23 @@ public static GameVersion getIsG4SAV(byte[] data) return GameVersion.Invalid; } + /// Determines the type of 4th gen Battle Revolution + /// Save data of which to determine the type + /// Version Identifier or Invalid if type cannot be determined. + public static GameVersion getIsG4BRSAV(byte[] data) + { + if (data.Length != SIZE_G4BR) + return GameVersion.Invalid; + + byte[] sav = SAV4BR.DecryptPBRSaveData(data); + + bool valid = SAV4BR.VerifyChecksum(sav, 0, 0x1C0000, 0x1BFF80); + valid &= SAV4BR.VerifyChecksum(sav, 0, 0x100, 8); + valid &= SAV4BR.VerifyChecksum(sav, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000); + valid &= SAV4BR.VerifyChecksum(sav, 0x1C0000, 0x100, 0x1C0008); + + return valid ? GameVersion.BATREV : GameVersion.Invalid; + } /// Determines the type of 5th gen save /// Save data of which to determine the type /// Version Identifier or Invalid if type cannot be determined. @@ -396,6 +418,8 @@ public static SaveFile getVariantSAV(byte[] data) return new SAV3RSBox(data); case 4: return new SAV4(data); + case (int)GameVersion.BATREV: + return new SAV4BR(data); case 5: return new SAV5(data); case 6: diff --git a/PKHeX/Subforms/Save Editors/SAV_BoxViewer.cs b/PKHeX/Subforms/Save Editors/SAV_BoxViewer.cs index 664871bbf..1ba0180d8 100644 --- a/PKHeX/Subforms/Save Editors/SAV_BoxViewer.cs +++ b/PKHeX/Subforms/Save Editors/SAV_BoxViewer.cs @@ -239,7 +239,7 @@ private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) PKM temp = mg != null ? mg.convertToPKM(SAV) : PKMConverter.getPKMfromBytes(data); string c; - PKM pk = PKMConverter.convertToFormat(temp, SAV.Generation, out c); + PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk == null) { Util.Error(c); Console.WriteLine(c); return; }