Exemplo n.º 1
0
        private void HandleDragDrop(object sender, DragEventArgs e)
        {
            Control c = sender as Control;

            if (!(c.FindForm() is MainForm))
            {
                return;
            }
            MainForm mf = c.FindForm() as MainForm;

            if (mf.getInChildForm())
            {
                return;
            }
            if (!(c.Parent is PokemonListPanel))
            {
                return;
            }
            PokemonListPanel parent = c.Parent as PokemonListPanel;
            int slot = getSlot(sender);

            // Check for In-Dropped files
            object[] files = (object[])e.Data.GetData(DataFormats.FileDrop);
            if (e.Effect == DragDropEffects.Copy)
            {
                if (files.Length <= 0)
                {
                    return;
                }
                FileInfo fi = new FileInfo((string)files[0]);
                if ((int)fi.Length == PokemonList.GetDataLength(PokemonList.CapacityType.Single) && !mf.JP_Mode)
                {
                    byte[] data = File.ReadAllBytes((string)files[0]);
                    if (data[0] == 1 && data[2] == 0xFF && data[1] == data[3]) // PK1
                    {
                        parent.set(slot, new PokemonList(data)[0]);
                    }
                    else
                    {
                        mf.openFile((string)files[0]);
                    }
                }
                else if ((int)fi.Length == PokemonList.GetDataLength(PokemonList.CapacityType.Single, true) && mf.JP_Mode)
                {
                    byte[] data = File.ReadAllBytes((string)files[0]);
                    if (data[0] == 1 && data[2] == 0xFF && data[1] == data[3]) // JPK1
                    {
                        parent.set(slot, new PokemonList(data, PokemonList.CapacityType.Single, true)[0]);
                    }
                    else
                    {
                        mf.openFile((string)files[0]);
                    }
                }
                else
                {
                    mf.openFile((string)files[0]);
                }
            }
            else if (e.Effect == DragDropEffects.Move)
            {
                if (files.Length != 1)
                {
                    return;
                }
                if (sender_parent == null)
                {
                    FileInfo fi = new FileInfo((string)files[0]);
                    if ((int)fi.Length == PokemonList.GetDataLength(PokemonList.CapacityType.Single) && !mf.JP_Mode)
                    {
                        byte[] data = File.ReadAllBytes((string)files[0]);
                        if (data[0] == 1 && data[2] == 0xFF && data[1] == data[3]) // PK1
                        {
                            parent.set(slot, new PokemonList(data)[0]);
                        }
                        else
                        {
                            mf.openFile((string)files[0]);
                        }
                    }
                    else if ((int)fi.Length == PokemonList.GetDataLength(PokemonList.CapacityType.Single, true) && mf.JP_Mode)
                    {
                        byte[] data = File.ReadAllBytes((string)files[0]);
                        if (data[0] == 1 && data[2] == 0xFF && data[1] == data[3]) // JPK1
                        {
                            parent.set(slot, new PokemonList(data, PokemonList.CapacityType.Single, true)[0]);
                        }
                        else
                        {
                            mf.openFile((string)files[0]);
                        }
                    }
                    else
                    {
                        mf.openFile((string)files[0]);
                    }
                }
                else
                {
                    PK1 temp  = parent.pokemonlist[slot];
                    int count = parent.pokemonlist.Count;
                    parent.set(slot, sender_parent.pokemonlist[sender_slot]);
                    if (slot < count)
                    {
                        sender_parent.set(sender_slot, temp);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void openFile(byte[] input, string path, string ext)
        {
            // Fix case sensitivity with extensions
            ext = ext.ToLower();
            #region PK1
            if (input.Length == PokemonList.GetDataLength(PokemonList.CapacityType.Single))
            {
                if (new[] { ".pk1", "" }.Contains(ext) && input[0] == 1 && input[2] == 0xFF && input[1] == input[3])
                {
                    if (!saveLoaded && JP_Mode)
                    {
                        JP_Mode     = false;
                        pk1_wrapper = new PokemonList(PokemonList.CapacityType.Single, JP_Mode);
                    }
                    if (!JP_Mode)
                    {
                        PopulateFields(new PokemonList(input)[0]);
                    }
                    else
                    {
                        Util.Error("Cannot open a PK1 with a JP save open.");
                    }
                }
                else
                {
                    Util.Error("Unable to recognize file." + Environment.NewLine + "Only valid .pk1/.bin supported.", string.Format($"File Loaded:{Environment.NewLine}{path}"));
                }
            }
            else if (input.Length == PokemonList.GetDataLength(PokemonList.CapacityType.Single, true))
            {
                if (new[] { ".jpk1", "" }.Contains(ext) && input[0] == 1 && input[2] == 0xFF && input[1] == input[3])
                {
                    if (!saveLoaded && !JP_Mode)
                    {
                        JP_Mode     = true;
                        pk1_wrapper = new PokemonList(PokemonList.CapacityType.Single, JP_Mode);
                    }
                    if (JP_Mode)
                    {
                        PopulateFields(new PokemonList(input, PokemonList.CapacityType.Single, true)[0]);
                    }
                    else
                    {
                        Util.Error("Cannot open a JPK1 with a non-JP save open.");
                    }
                }
                else
                {
                    Util.Error("Unable to recognize file." + Environment.NewLine + "Only valid .jpk1/.bin supported.", string.Format($"File Loaded:{Environment.NewLine}{path}"));
                }
            }
            else if (input.Length == 0x8000 || input.Length == 0x802C)
            {
                if (ext != ".dat" && ext != ".sav")
                {
                    Util.Error("Unable to recognize file." + Environment.NewLine + "Only valid .sav/.dat supported.", string.Format($"File Loaded:{Environment.NewLine}{path}"));
                }
                if (input.Length == 0x802C) // Support Emulator saves
                {
                    Array.Resize(ref input, 0x8000);
                }
                saveLoaded                = Menu_ExportSAV.Enabled = false;
                B_Apply.Enabled           = false;
                Menu_JapaneseMode.Enabled = true;
                foreach (TabPage tp in tabBoxMulti.TabPages)
                {
                    tp.Enabled = false;
                }
                if (SAV1.IsAmerican(input))
                {
                    if (JP_Mode)
                    {
                        JP_Mode     = false;
                        pk1_wrapper = new PokemonList(PokemonList.CapacityType.Single, JP_Mode);
                        InitializeFields();
                    }
                    CB_BoxSelect.Items.Clear();
                    CB_BoxSelect.Items.AddRange(Enumerable.Range(1, 12).Select(d => string.Format("Box {0}", d)).ToArray());
                    PopulateFields(new USAV1(input)
                    {
                        FilePath = Path.GetDirectoryName(path),
                        FileName = Path.GetFileName(path)
                    });
                    SetStringLengths();
                }
                else if (SAV1.IsJapanese(input))
                {
                    if (!JP_Mode)
                    {
                        JP_Mode     = true;
                        pk1_wrapper = new PokemonList(PokemonList.CapacityType.Single, JP_Mode);
                        InitializeFields();
                    }
                    CB_BoxSelect.Items.Clear();
                    CB_BoxSelect.Items.AddRange(Enumerable.Range(1, 8).Select(d => string.Format("Box {0}", d)).ToArray());
                    PopulateFields(new JSAV1(input)
                    {
                        FilePath = Path.GetDirectoryName(path),
                        FileName = Path.GetFileName(path)
                    });
                    SetStringLengths();
                }
                else
                {
                    Util.Error("Unable to recognize file." + Environment.NewLine + "Save file may be corrupted.");
                    return;
                }
                foreach (TabPage tp in tabBoxMulti.TabPages)
                {
                    tp.Enabled = true;
                }
                saveLoaded                = Menu_ExportSAV.Enabled = true;
                B_Apply.Enabled           = true;
                Menu_JapaneseMode.Enabled = false;

                // Indicate audibly the save is loaded
                SystemSounds.Beep.Play();
            }
            else
            {
                {
                    Util.Error("Unable to recognize file.", string.Format($"File Loaded:{Environment.NewLine}{path}"));
                }
            }
            #endregion
        }