PKHeX.Form1.clickExportSAV C# (CSharp) Method

clickExportSAV() private method

private clickExportSAV ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void clickExportSAV(object sender, EventArgs e)
        {
            // Set the current box to the save
            savefile[SaveGame.PCLayout + savindex * 0x7FFFF + 0x43F] = (byte)
                (tabBoxMulti.SelectedIndex == 1 ? 0xFF // If Battle/Party selected
                : CB_BoxSelect.SelectedIndex); // Box
            // Create another version of the save file.
            byte[] editedsav = new byte[0x100000];
            Array.Copy(savefile, editedsav, savefile.Length);
            // Since we only edited one of the save files, we only have to fix half of the chk/hashes!

            // Fix Checksums
            editedsav = PKX.writeG6CHK(editedsav, savegame_oras, savindex);
            if (!cybergadget) // Fix Hashes
                editedsav = PKX.writeG6SHA(editedsav, savegame_oras, savindex);
            // Write the active save index
            editedsav[0x168] = (byte)(savindex ^ 1);
            // File Integrity has been restored as well as it can. Export!

            if (cybergadget)
            #region Saving CyberGadget/RAMSAV
            {
                byte[] cybersav = new byte[savegame_oras ? 0x76000 : 0x65600];
                Array.Copy(editedsav, 0x5400, cybersav, 0, cybersav.Length);
                if (ramsav == null)
                {
                    // Chunk Error Checking
                    byte[] FFFF = new byte[0x200];
                    byte[] section = new byte[0x200];
                    for (int i = 0; i < 0x200; i++)
                        FFFF[i] = 0xFF;

                    for (int i = 0; i < cybersav.Length / 0x200; i++)
                    {
                        Array.Copy(cybersav, i * 0x200, section, 0, 0x200);
                        if (!section.SequenceEqual(FFFF)) continue;
                        string problem = String.Format("0x200 chunk @ 0x{0} is FF'd.", (i * 0x200).ToString("X5"))
                                         + Environment.NewLine + "Cyber will screw up (as of August 31st)." + Environment.NewLine + Environment.NewLine;

                        // Check to see if it is in the Pokedex
                        if (i * 0x200 > 0x14E00 && i * 0x200 < 0x15700)
                        {
                            problem += "Problem lies in the Pokedex. ";
                            if (i * 0x200 == 0x15400)
                                problem += "Remove a language flag for a species ~ ex " + specieslist[548];
                        }

                        if (Util.Prompt(MessageBoxButtons.YesNo, problem, "Continue saving?") != DialogResult.Yes)
                            return;
                    }
                }
                SaveFileDialog cySAV = new SaveFileDialog();

                // Try for file path
                string cyberpath = Util.GetTempFolder();
                if (ramsav != null && Directory.Exists(path3DS))
                {
                    cySAV.InitialDirectory = path3DS;
                    cySAV.RestoreDirectory = true;
                }
                else if (path3DS != null && File.Exists(Path.Combine(Path.GetDirectoryName(path3DS), "SaveDataBackup", "main")))
                {
                    cySAV.InitialDirectory = Path.Combine(Path.GetDirectoryName(path3DS), "SaveDataBackup");
                    cySAV.RestoreDirectory = true;
                }
                else if (pathSDF != null && Directory.Exists(pathSDF))
                {
                    cySAV.InitialDirectory = pathSDF;
                    cySAV.RestoreDirectory = true;
                }
                else if (Directory.Exists(Path.Combine(cyberpath, "root")))
                {
                    cySAV.InitialDirectory = Path.Combine(cyberpath, "root");
                    cySAV.RestoreDirectory = true;
                }
                else if (Directory.Exists(cyberpath))
                {
                    cySAV.InitialDirectory = cyberpath;
                    cySAV.RestoreDirectory = true;
                }
                if (ramsavloaded && ModifierKeys == Keys.Shift) // Export RAM SAV to another.
                {
                    Util.Alert("Please specify the target cart/console-RAM save.");
                    OpenFileDialog ofd = new OpenFileDialog();
                    if (ofd.ShowDialog() != DialogResult.OK) return;
                    string target = ofd.FileName;
                    byte[] targetRAM = File.ReadAllBytes(target);
                    byte[] newRAM = ram2sav.getRAM(targetRAM, cybersav);

                    cySAV.Filter = "ramsav|*.bin";
                    cySAV.FileName = "ramsav.bin";
                    DialogResult sdr = cySAV.ShowDialog();
                    if (sdr != DialogResult.OK) return;
                    string path = cySAV.FileName;
                    File.WriteAllBytes(path, newRAM);
                    Util.Alert("Saved RAM SAV to:" + Environment.NewLine + path, "Target RAM:" + Environment.NewLine + target);
                }
                else if (ramsavloaded && ModifierKeys != Keys.Control) // Export RAM SAV if it is the currently loaded one.
                {
                    cySAV.Filter = "ramsav|*.bin";
                    cySAV.FileName = "ramsav.bin";
                    DialogResult sdr = cySAV.ShowDialog();
                    if (sdr != DialogResult.OK) return;
                    string path = cySAV.FileName;
                    File.WriteAllBytes(path, ram2sav.getRAM(ramsav, cybersav));
                    Util.Alert("Saved RAM SAV to:", path);
                }
                else
                {
                    cySAV.Filter = "Cyber SAV|*.*";
                    cySAV.FileName = L_Save.Text.Split(new[] { ": " }, StringSplitOptions.None)[1];
                    DialogResult sdr = cySAV.ShowDialog();
                    if (sdr != DialogResult.OK) return;
                    string path = cySAV.FileName;
                    File.WriteAllBytes(path, cybersav);
                    Util.Alert("Saved Cyber SAV to:", path);
                }
            }
            #endregion
            else
            #region Saving Full Save File
            {
                // Save Full Save File
                SaveFileDialog savesav = new SaveFileDialog
                {
                    Filter = "SAV|*.bin;*.sav",
                    FileName = L_Save.Text.Split(new[] {": "}, StringSplitOptions.None)[1]
                };
                DialogResult result = savesav.ShowDialog();
                if (result != DialogResult.OK) return;
                string path = savesav.FileName;

                if (File.Exists(path))
                {
                    // File already exists, save a .bak
                    byte[] backupfile = File.ReadAllBytes(path);
                    File.WriteAllBytes(path + ".bak", backupfile);
                }
                File.WriteAllBytes(path, editedsav);
                Util.Alert("Saved 1MB SAV to:", path);
            }
            #endregion
        }
        // Box/SAV Functions //
Form1