PKHeX.Form1.loadBoxesFromDB C# (CSharp) Method

loadBoxesFromDB() private method

private loadBoxesFromDB ( string path ) : void
path string
return void
        private void loadBoxesFromDB(string path)
        {
            if (path == "") return;
            int offset = SaveGame.Box;
            int ctr = CB_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;
            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();

            foreach (string t in filepaths)
            {
                long len = new FileInfo(t).Length;
                if (len > 260)
                    continue;
                if (
                    len != 232 && len != 260 // 6th Gen
                    && len != 136 && len != 220 && len != 236 // 5th Gen
                    && len != 100 && len != 80) // 4th Gen
                    continue;
                byte[] data = new byte[232];
                switch (Path.GetExtension(t)) // Filter all files by extension
                {
                    case ".pk5":
                    case ".pk4":
                    case ".pk3":
                    case ".3gpkm":
                    case ".pkm":
                    {
                        // Verify PKM (decrypted)
                        byte[] input = File.ReadAllBytes(t);
                        if (!PKX.verifychk(input)) continue;
                        {
                            try // to convert g5pkm
                            { data = PKX.encryptArray(Converter.ConvertPKM(input, savefile, savindex)); pastctr++; }
                            catch { continue; }
                        }
                    }
                        break;
                    case ".pk6":
                    case ".pkx":
                    {
                        byte[] input = File.ReadAllBytes(t);
                        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);

                            if (PKX.getCHK(input) != BitConverter.ToUInt16(input, 0x6)) continue;
                            data = PKX.encryptArray(input);
                        }
                    }
                        break;
                    case ".ek6":
                    case ".ekx":
                    {
                        byte[] input = File.ReadAllBytes(t);
                        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;
                        // We'll allow blank ekx files for those wanting to see the decrypted data.
                        if (PKX.getCHK(input) != BitConverter.ToUInt16(decrypteddata, 0x6)) continue;
                    }
                        break;
                    default:
                        continue;
                }
                Array.Copy(data, 0, savefile, offset + ctr++ * 232, 232);
                setPokedex(PKX.decryptArray(data)); // Set the Pokedex data
                if (ctr == 30 * 31) break; // break out if we have written all 31 boxes
            }
            if (ctr <= 0) return; 
            // 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
            {
                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);
        }
        private void B_SaveBoxBin_Click(object sender, EventArgs e)
Form1