PKHeX.Form1.openFile C# (CSharp) Method

openFile() private method

private openFile ( byte input, string path, string ext ) : void
input byte
path string
ext string
return void
        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
                if (BitConverter.ToUInt64(input, 0x10) != 0) // encrypted save
                { Util.Error("PKHeX only edits decrypted save files.", "This save file is encrypted."); 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) return;

                    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;

                    savindex = (sdr == DialogResult.Yes) ? 0 : 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) return;

                    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
                    }
                    savindex = (sdr == DialogResult.Yes) ? 0 : 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." + Environment.NewLine + "Only valid .pk* .ek* .bin supported.", String.Format("File Loaded:{0}{1}", Environment.NewLine, path));
            }
            #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);
            }
            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);
            }
            #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)
            { 
                Array.Copy(input, 0, savefile, SaveGame.Box + 0xE8 * 30 * ((input.Length == 0xE8*30) ? CB_BoxSelect.SelectedIndex : 0), input.Length); 
                setPKXBoxes();
                Width = largeWidth;
                Util.Alert("Box Binary loaded."); }
            #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)) continue;
                    offset = i; break;
                }
                if (offset < 0) { Util.Alert(path, "Unable to read the input file; not an expected injectiondebug.bin."); return; }
                CB_BoxSelect.SelectedIndex = 0;
                Array.Copy(input, offset, savefile, SaveGame.Box + 0xE8 * 30 * CB_BoxSelect.SelectedIndex, 9 * 30 * 0xE8);
                setPKXBoxes();
                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 { return; }
                ramsav = (byte[])input.Clone();
            }
            #endregion
            #region Battle Video
            else if (input.Length == 0x2E60 && BitConverter.ToUInt64(input, 0xE18) != 0 && BitConverter.ToUInt16(input, 0xE12) == 0)
            {
                if (Util.Prompt(MessageBoxButtons.YesNo, "Load Batte Video Pokémon data to " + CB_BoxSelect.Text + "?", "The first 24 slots will be overwritten.") != DialogResult.Yes) return;
                for (int i = 0; i < 24; i++)
                    Array.Copy(input, 0xE18 + 260 * i + (i / 6) * 8, savefile, SaveGame.Box + i * 0xE8 + CB_BoxSelect.SelectedIndex * 30 * 0xE8, 0xE8);
                setPKXBoxes();
            }
            #endregion
            #region Wondercard
            else if (input.Length == 0x108 && ext == ".wc6") 
                new SAV_Wondercard(this, input).Show();
            #endregion
            else
                Util.Error("Attempted to load an unsupported file type/size.", "File Loaded:" + Environment.NewLine + path, "File Size:" + Environment.NewLine + new FileInfo(path).Length.ToString("X8"));
        }
        private void openMAIN(byte[] input, string path, string GameType, bool oras, bool ram = false)
Form1