public void openFile(string path) { string ext = Path.GetExtension(path); FileInfo fi = new FileInfo(path); if (fi.Length > 0x802C) { Util.Error("Input file is too large.", path); } else { byte[] input; try { input = File.ReadAllBytes(path); } catch { Util.Error("File is in use by another program!", path); return; } try { openFile(input, path, ext); } catch { try { PokemonList empty = new PokemonList((byte[])PokemonList.EMPTY_LIST.Clone()) { [0] = { OT_Name = RBY_Encoding.GetBytes(JP_Mode ? "サイドン" : "Rhydon", JP_Mode) }, [0] = { Nickname = RBY_Encoding.GetBytes(JP_Mode ? "サイドン" : "RHYDON", JP_Mode) }, [0] = { TID = 12345 }, [0] = { Species = 1 }, [0] = { Level = 1 } }; openFile(empty.GetBytes(), path, ext); } catch { openFile(input, path, ext); } } } }
public PokemonListPanel(PokemonList pl, ContextMenuStrip cms, int num_visible = 6) { pokemonlist = pl; mnu = cms ?? new ContextMenuStrip(); Capacity = pokemonlist.GetCapacity(); PartySprites = new PictureBox[Capacity]; Pokemon = new Label[Capacity]; salt = string.Empty; mnu.Items[0].Click += HandleView; mnu.Items[1].Click += HandleSet; mnu.Items[2].Click += HandleDelete; AutoScroll = true; Size = MaximumSize = MinimumSize = new Size(191, 26 * num_visible); Location = new Point(5, 20); BackColor = Color.White; BorderStyle = BorderStyle.FixedSingle; for (int i = 0; i < Capacity; i++) { PartySprites[i] = new PictureBox { Size = new Size(16, 16), Location = new Point(10, 5 + i * 26), Name = salt + salt + "_" + i, ContextMenuStrip = mnu, Tag = pokemonlist, AllowDrop = true }; PartySprites[i].DragEnter += HandleDragEnter; PartySprites[i].DragDrop += HandleDragDrop; PartySprites[i].MouseDown += HandleMouseDown; Pokemon[i] = new Label { Font = Util.Pokered_US, AutoSize = true, Location = new Point(31, 5 + i * 26), Name = salt + salt + "_" + i, ContextMenuStrip = mnu, Tag = pokemonlist, AllowDrop = true }; Pokemon[i].DragEnter += HandleDragEnter; Pokemon[i].DragDrop += HandleDragDrop; Pokemon[i].MouseDown += HandleMouseDown; Controls.Add(PartySprites[i]); Controls.Add(Pokemon[i]); } for (int i = 0; i < pokemonlist.Count; i++) { update(i); } }
private void HandleMouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left || e.Clicks != 1) { return; } 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 index = getSlot(sender); if (index >= parent.pokemonlist.Count) { return; } Cursor.Current = Cursors.Hand; byte[] dragdata = new PokemonList(parent.pokemonlist[index]).GetBytes(); string filename = RBY_Encoding.GetString(parent.pokemonlist[index].Nickname, parent.pokemonlist[index] is JPK1) + " - " + RBY_Encoding.GetString(parent.pokemonlist[index].OT_Name, parent.pokemonlist[index] is JPK1) + (parent.pokemonlist[index] is JPK1 ? ".jpk1" : ".pk1"); string newfile = Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename)); try { File.WriteAllBytes(newfile, dragdata); sender_parent = parent; sender_slot = index; DoDragDrop(new DataObject(DataFormats.FileDrop, new [] { newfile }), DragDropEffects.Move); } catch (ArgumentException x) { Util.Error("Drag & Drop Error:", x.ToString()); } File.Delete(newfile); sender_slot = 0; sender_parent = null; }
private void toggleJapaneseMode(object sender, EventArgs e) { if (saveLoaded) { Util.Error("You can't toggle Japanese mode with a save loaded!"); Menu_JapaneseMode.Enabled = !saveLoaded; } JP_Mode = !JP_Mode; pk1_wrapper = new PokemonList(PokemonList.CapacityType.Single, JP_Mode); InitializeFields(); SetStringLengths(); }
public void SetList(PokemonList pl) { pokemonlist = pl; int old_cap = Capacity; Capacity = pokemonlist.GetCapacity(); if (Capacity != old_cap) { Size = MaximumSize = MinimumSize = new Size(191, 26 * Math.Min(6, Capacity)); PartySprites = new PictureBox[Capacity]; Pokemon = new Label[Capacity]; Controls.Clear(); for (int i = 0; i < Capacity; i++) { PartySprites[i] = new PictureBox { Size = new Size(16, 16), Location = new Point(10, 5 + i * 26), Name = salt + "_" + i, ContextMenuStrip = mnu, Tag = pokemonlist, AllowDrop = true }; PartySprites[i].DragEnter += HandleDragEnter; PartySprites[i].DragDrop += HandleDragDrop; PartySprites[i].MouseDown += HandleMouseDown; Pokemon[i] = new Label { Font = Util.Pokered_US, AutoSize = true, Location = new Point(31, 5 + i * 26), Name = salt + "_" + i, ContextMenuStrip = mnu, Tag = pokemonlist, AllowDrop = true }; Pokemon[i].DragEnter += HandleDragEnter; Pokemon[i].DragDrop += HandleDragDrop; Pokemon[i].MouseDown += HandleMouseDown; Controls.Add(PartySprites[i]); Controls.Add(Pokemon[i]); } } for (int i = 0; i < Capacity; i++) { update(i); } }
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); } } } }
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 }