private void openFile(byte[] input, string path, string ext) { #region Powersaves Read-Only Conversion if (input.Length == 0x10009C) // Resize to 1MB { Array.Copy(input, 0x9C, input, 0, 0x100000); Array.Resize(ref input, 0x100000); } #endregion #region Saves if ((input.Length == 0x76000) && BitConverter.ToUInt32(input, 0x75E10) == 0x42454546) // ORAS openMAIN(input, path, "ORAS", true); else if ((input.Length == 0x65600) && BitConverter.ToUInt32(input, 0x65410) == 0x42454546) // XY openMAIN(input, path, "XY", false); // Verify the Data Input Size is Proper else if (input.Length == 0x100000) { if (openXOR(input, path)) // Check if we can load the save via xorpad return; // only if a save is loaded we abort else if (BitConverter.ToUInt64(input, 0x10) != 0) // encrypted save { Util.Error("PKHeX only edits decrypted save files.", "This save file is not decrypted."); return; } string GameType = "XY"; // Default Game Type to load. if (BitConverter.ToUInt32(input, 0x7B210) == 0x42454546) GameType = "ORAS"; // BEEF magic in checksum block if ((BitConverter.ToUInt32(input, 0x100) != 0x41534944) && (BitConverter.ToUInt32(input, 0x5234) != 0x6E69616D)) { DialogResult dialogResult = Util.Prompt(MessageBoxButtons.YesNo, "Save file is not decrypted.", "Press Yes to ignore this warning and continue loading the save file."); if (dialogResult == DialogResult.Yes) { DialogResult sdr = Util.Prompt(MessageBoxButtons.YesNoCancel, "Press Yes to load the sav at 0x3000", "Press No for the one at 0x82000"); if (sdr == DialogResult.Cancel) return; else if (sdr == DialogResult.Yes) savindex = 0; else savindex = 1; B_SwitchSAV.Enabled = true; open1MB(input, path, GameType, false); } } else if (PKX.detectSAVIndex(input, ref savindex) == 2) { DialogResult dialogResult = Util.Prompt(MessageBoxButtons.YesNo, "Hash verification failed.", "Press Yes to ignore this warning and continue loading the save file."); if (dialogResult == DialogResult.Yes) { DialogResult sdr = Util.Prompt(MessageBoxButtons.YesNoCancel, "Press Yes to load the sav at 0x3000", "Press No for the one at 0x82000"); if (sdr == DialogResult.Cancel) { savindex = 0; return; // abort load } else if (sdr == DialogResult.Yes) { savindex = 0; B_SwitchSAV.Enabled = true; } else savindex = 1; B_SwitchSAV.Enabled = true; open1MB(input, path, GameType, false); } } else { B_ExportSAV.Enabled = true; B_SwitchSAV.Enabled = true; PKX.detectSAVIndex(input, ref savindex); open1MB(input, path, GameType, false); } } #endregion #region PK6/EK6 else if ((input.Length == 260) || (input.Length == 232)) { // Check if Input is PKX if ((ext == ".pk6") || (ext == ".ek6") || (ext == ".pkx") || (ext == ".ekx") || (ext == ".bin") || (ext == "")) { // Check if Encrypted before Loading buff = (BitConverter.ToUInt16(input, 0xC8) == 0 && BitConverter.ToUInt16(input, 0x58) == 0) ? input : PKX.decryptArray(input); populateFields(buff); } else Util.Error("Unable to recognize file.", "Only valid .pk* .ek* .bin supported."); } #endregion #region PK3/PK4/PK5 else if ((input.Length == 136) || (input.Length == 220) || (input.Length == 236) || (input.Length == 100) || (input.Length == 80)) // to convert g5pkm { var Converter = new pk2pk(); if (!PKX.verifychk(input)) Util.Error("Invalid File (Checksum Error)"); try // to convert g5pkm { byte[] data = Converter.ConvertPKM(input, savefile, savindex); Array.Copy(data, buff, 232); populateFields(buff); } catch { Array.Copy(new byte[232], buff, 232); populateFields(buff); Util.Error("Attempted to load previous generation PKM.", "Conversion failed."); } } #endregion #region Trade Packets else if (input.Length == 363 && BitConverter.ToUInt16(input, 0x6B) == 0) { // EAD Packet of 363 length byte[] c = new byte[260]; Array.Copy(input, 0x67, c, 0, 260); input = c; } else if (input.Length == 407 && BitConverter.ToUInt16(input, 0x98) == 0) { // EAD Packet of 407 length byte[] c = new byte[260]; Array.Copy(input, 0x93, c, 0, 260); input = c; } #endregion #region Box Data else if ((input.Length == 0xE8 * 30 || input.Length == 0xE8 * 30 * 31) && BitConverter.ToUInt16(input, 4) == 0 && BitConverter.ToUInt32(input, 8) > 0) { if (!allintobox) { Array.Copy(input, 0, savefile, SaveGame.Box + 0xE8 * 30 * ((input.Length == 0xE8 * 30) ? C_BoxSelect.SelectedIndex : 0), input.Length); setPKXBoxes(); this.Width = largeWidth; } // Util.Alert("Box Binary loaded."); box_load = true; } #endregion #region injectiondebug else if (input.Length == 0x10000) { int offset = -1; // Seek to find data start for (int i = 0; i < 0x800; i++) { byte[] data = PKX.decryptArray(input.Skip(i).Take(0xE8).ToArray()); if (PKX.getCHK(data) == BitConverter.ToUInt16(data, 6)) { offset = i; break; } } if (offset < 0) { Util.Alert(path, "Unable to read the input file; not an expected injectiondebug.bin."); return; } C_BoxSelect.SelectedIndex = 0; Array.Copy(input, offset, savefile, SaveGame.Box + 0xE8 * 30 * C_BoxSelect.SelectedIndex, 9 * 30 * 0xE8); setPKXBoxes(); this.Width = largeWidth; Util.Alert("Injection Binary loaded."); } #endregion #region RAMSAV else if (( /*XY*/ input.Length == 0x70000 || /*ORAS*/ input.Length == 0x80000) && Path.GetFileName(path).Contains("ram")) { if (input.Length == 0x80000) // Scan for FEEB in XY location, 3DS only overwrites data if file already exists. for (int i = 0x60000; i < 0x64000; i += 4) if (BitConverter.ToUInt32(input, i) == 0x42454546) { Array.Resize(ref input, 0x70000); break; } bool o = (input.Length == 0x80000); try { openMAIN(ram2sav.getMAIN(input), path, (o) ? "ORAS" : "XY", o, true); } catch { } ramsav = (byte[])input.Clone(); } #endregion else Util.Error("Attempted to load an unsupported file type/size.", "File Loaded:" + Environment.NewLine + path); }
private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) { PictureBox pb = (PictureBox)(sender); int slot = getSlot(sender); int offset = getPKXOffset(slot); // Check for In-Dropped files (PKX,SAV,ETC) string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); if (Directory.Exists(files[0])) { loadBoxesFromDB(files[0]); return; } if (files != null && pkm_from_offset == 0) { if (files.Length > 0) { FileInfo fi = new FileInfo(files[0]); // Detect if PKM/PKX if ((fi.Length == 136) || (fi.Length == 220) || (fi.Length == 236) || (fi.Length == 100) || (fi.Length == 80)) { byte[] input = File.ReadAllBytes(files[0]); var Converter = new pk2pk(); if (!PKX.verifychk(input)) Util.Alert("Invalid File Loaded.", "Checksum is not valid."); try // to convert past gen pkm { byte[] data = Converter.ConvertPKM(input, savefile, savindex); Array.Copy(PKX.encryptArray(data), 0, savefile, offset, 0xE8); } catch { Util.Error("Attempted to load previous generation PKM.", "Conversion failed."); } } else if (fi.Length == 232 || fi.Length == 260) { byte[] data = File.ReadAllBytes(files[0]); if (fi.Extension == ".pkx" || fi.Extension == ".pk6") data = PKX.encryptArray(data); else if (fi.Extension != ".ekx" && fi.Extension != ".ek6") { openQuick(files[0]); return; } // lazy way of aborting byte[] decdata = PKX.decryptArray(data); if (!PKX.verifychk(decdata)) Util.Alert("Attempted to load Invalid File.", "Checksum is not valid."); else { Array.Copy(data, 0, savefile, offset, 0xE8); setPokedex(decdata); getQuickFiller(getPictureBox(slot), decdata); getSlotColor(slot, Properties.Resources.slotSet); } } else // not PKX/EKX, so load with the general function { openQuick(files[0]); } } } else { if (ModifierKeys == Keys.Alt && slot > -1) // overwrite delete old slot { byte[] cleardata = new Byte[0xE8]; // Clear from slot picture getQuickFiller(getPictureBox(pkm_from_slot), cleardata); // Clear from slot data Array.Copy(PKX.encryptArray(cleardata), 0, savefile, pkm_from_offset, 0xE8); } else if (ModifierKeys != Keys.Control && slot > -1) { // Load data from destination byte[] swapdata = new Byte[0xE8]; Array.Copy(savefile, offset, swapdata, 0, 0xE8); // Swap slot picture getQuickFiller(getPictureBox(pkm_from_slot), PKX.decryptArray(swapdata)); // Swap slot data to source Array.Copy(swapdata, 0, savefile, pkm_from_offset, 0xE8); } // Copy from temp slot to new. Array.Copy(pkm_from, 0, savefile, offset, 0xE8); getQuickFiller(getPictureBox(slot), PKX.decryptArray(pkm_from)); pkm_from_offset = 0; // Clear offset value } savedited = true; }
private void loadBoxesFromDB(string path) { if (path == "") return; int offset = SaveGame.Box; int ctr = C_BoxSelect.SelectedIndex * 30; int pastctr = 0; // Clear out the box data array. // Array.Clear(savefile, offset, size * 30 * 31); DialogResult dr = Util.Prompt(MessageBoxButtons.YesNoCancel, "Clear subsequent boxes when importing data?", "If you only want to overwrite for new data, press no."); if (dr == DialogResult.Cancel) return; else if (dr == DialogResult.Yes) { byte[] ezeros = PKX.encryptArray(new byte[232]); for (int i = ctr; i < 30 * 31; i++) Array.Copy(ezeros, 0, savefile, offset + i * 232, 232); } string[] filepaths = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly); var Converter = new pk2pk(); for (int i = 0; i < filepaths.Length; i++) { long len = new FileInfo(filepaths[i]).Length; if (len > 260) continue; else if ( len != 232 && len != 260 // 6th Gen && len != 136 && len != 220 && len != 236 // 5th Gen && len != 100 && len != 80) // 4th Gen continue; string name = filepaths[i]; byte[] data = new byte[232]; string ext = Path.GetExtension(filepaths[i]); if (ext == ".pkm" || ext == ".3gpkm" || ext == ".pk3" || ext == ".pk4" || ext == ".pk5") { // Verify PKM (decrypted) byte[] input = File.ReadAllBytes(filepaths[i]); if (!PKX.verifychk(input)) continue; else { try // to convert g5pkm { data = PKX.encryptArray(Converter.ConvertPKM(input, savefile, savindex)); pastctr++; } catch { continue; } } } else if (ext == ".pkx" || ext == ".pk6") { byte[] input = File.ReadAllBytes(filepaths[i]); if ((BitConverter.ToUInt16(input, 0xC8) == 0) && (BitConverter.ToUInt16(input, 0x58) == 0)) { if (BitConverter.ToUInt16(input, 0x8) == 0) // if species = 0 continue; Array.Resize(ref input, 232); ushort chk = 0; for (int z = 8; z < 232; z += 2) // Loop through the entire PKX chk += BitConverter.ToUInt16(input, z); if (chk != BitConverter.ToUInt16(input, 0x6)) continue; data = PKX.encryptArray(input); } } else if (ext == ".ekx" || ext == ".ek6") { byte[] input = File.ReadAllBytes(filepaths[i]); Array.Resize(ref input, 232); Array.Copy(input, data, 232); // check if it is good data byte[] decrypteddata = PKX.decryptArray(input); if (!(BitConverter.ToUInt16(decrypteddata, 0xC8) == 0) && !(BitConverter.ToUInt16(decrypteddata, 0x58) == 0)) continue; // don't allow improperly encrypted files. they must be encrypted properly. //else if (BitConverter.ToUInt16(decrypteddata, 0x8) == 0) // if species = 0 // continue; ushort chk = 0; for (int z = 8; z < 232; z += 2) // Loop through the entire PKX chk += BitConverter.ToUInt16(decrypteddata, z); if (chk != BitConverter.ToUInt16(decrypteddata, 0x6)) continue; } else continue; Array.Copy(data, 0, savefile, offset + ctr * 232, 232); setPokedex(PKX.decryptArray(data)); // Set the Pokedex data ctr++; if (ctr == 30 * 31) break; // break out if we have written all 31 boxes } if (ctr > 0) // if we've written at least one pk6 in, go ahead and make sure the window is stretched. { if (Width < Height) // expand if boxes aren't visible { this.Width = largeWidth; tabBoxMulti.SelectedIndex = 0; } setPKXBoxes(); string result = String.Format("Loaded {0} files to boxes.", ctr); if (pastctr > 0) Util.Alert(result, String.Format("Conversion successful for {0} past generation files.", pastctr)); else Util.Alert(result); } }