2014-12-12 05:44:05 +00:00
|
|
|
|
using System;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
using System.Drawing;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
using System.Linq;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
using System.Windows.Forms;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
using PKHeX.Core;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.WinForms
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public partial class TrashEditor : Form
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
2017-05-23 04:55:05 +00:00
|
|
|
|
private readonly SaveFile SAV;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public TrashEditor(TextBoxBase TB_NN, byte[] raw, SaveFile sav)
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage);
|
2018-04-03 03:36:13 +00:00
|
|
|
|
SAV = sav;
|
|
|
|
|
bigendian = new[] { GameVersion.COLO, GameVersion.XD, GameVersion.BATREV, }.Contains(SAV.Version);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
|
|
|
|
FinalString = TB_NN.Text;
|
|
|
|
|
Raw = FinalBytes = raw;
|
|
|
|
|
|
|
|
|
|
editing = true;
|
|
|
|
|
if (raw != null)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
AddTrashEditing(raw.Length);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
AddCharEditing();
|
2017-04-09 21:06:50 +00:00
|
|
|
|
TB_Text.MaxLength = TB_NN.MaxLength;
|
|
|
|
|
TB_Text.Text = TB_NN.Text;
|
|
|
|
|
TB_Text.Font = pkxFont;
|
|
|
|
|
|
2017-04-10 04:53:53 +00:00
|
|
|
|
if (FLP_Characters.Controls.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
FLP_Characters.Visible = false;
|
|
|
|
|
FLP_Hex.Height *= 2;
|
|
|
|
|
}
|
|
|
|
|
else if (FLP_Hex.Controls.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
FLP_Characters.Location = FLP_Hex.Location;
|
|
|
|
|
FLP_Characters.Height *= 2;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-09 21:06:50 +00:00
|
|
|
|
editing = false;
|
|
|
|
|
CenterToParent();
|
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-09 21:06:50 +00:00
|
|
|
|
private readonly List<NumericUpDown> Bytes = new List<NumericUpDown>();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private readonly Font pkxFont = FontUtil.GetPKXFont(12F);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
public string FinalString;
|
|
|
|
|
public byte[] FinalBytes { get; private set; }
|
|
|
|
|
private readonly byte[] Raw;
|
|
|
|
|
private bool editing;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
private readonly bool bigendian;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
private void B_Cancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
|
private void B_Save_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
FinalString = TB_Text.Text;
|
|
|
|
|
if (FinalBytes != null)
|
|
|
|
|
FinalBytes = Raw;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void AddCharEditing()
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ushort[] chars = GetChars(SAV.Generation);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
if (chars.Length == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FLP_Characters.Visible = true;
|
|
|
|
|
foreach (ushort c in chars)
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var l = GetLabel((char)c+"");
|
2017-04-09 21:06:50 +00:00
|
|
|
|
l.Font = pkxFont;
|
|
|
|
|
l.AutoSize = false;
|
|
|
|
|
l.Size = new Size(20, 20);
|
|
|
|
|
l.Click += (s, e) => { if (TB_Text.Text.Length < TB_Text.MaxLength) TB_Text.AppendText(l.Text); };
|
|
|
|
|
FLP_Characters.Controls.Add(l);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void AddTrashEditing(int count)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
|
|
|
|
FLP_Hex.Visible = true;
|
|
|
|
|
GB_Trash.Visible = true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
NUD_Generation.Value = SAV.Generation;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
Font courier = new Font("Courier New", 8);
|
2017-05-11 05:52:14 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var l = GetLabel($"${i:X2}");
|
2017-04-09 21:06:50 +00:00
|
|
|
|
l.Font = courier;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var n = GetNUD(hex: true, min: 0, max: 255);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
n.Click += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
switch (ModifierKeys)
|
|
|
|
|
{
|
2017-04-10 04:53:53 +00:00
|
|
|
|
case Keys.Shift: n.Value = n.Maximum; break;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
case Keys.Alt: n.Value = n.Minimum; break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
n.Value = Raw[i];
|
2017-06-18 01:37:19 +00:00
|
|
|
|
n.ValueChanged += UpdateNUD;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-09 21:06:50 +00:00
|
|
|
|
FLP_Hex.Controls.Add(l);
|
|
|
|
|
FLP_Hex.Controls.Add(n);
|
|
|
|
|
Bytes.Add(n);
|
2014-12-12 05:44:05 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
TB_Text.TextChanged += UpdateString;
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
|
|
|
|
CB_Species.DisplayMember = "Text";
|
|
|
|
|
CB_Species.ValueMember = "Value";
|
|
|
|
|
CB_Species.DataSource = new BindingSource(GameInfo.SpeciesDataSource, null);
|
|
|
|
|
|
|
|
|
|
CB_Language.DisplayMember = "Text";
|
|
|
|
|
CB_Language.ValueMember = "Value";
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var languages = Util.GetUnsortedCBList("languages");
|
2017-05-23 04:55:05 +00:00
|
|
|
|
if (SAV.Generation < 7)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
languages = languages.Where(l => l.Value <= 8).ToList(); // Korean
|
|
|
|
|
CB_Language.DataSource = languages;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
}
|
2016-01-17 21:27:24 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void UpdateNUD(object sender, EventArgs e)
|
2014-12-12 05:44:05 +00:00
|
|
|
|
{
|
2017-04-09 21:06:50 +00:00
|
|
|
|
if (editing)
|
|
|
|
|
return;
|
|
|
|
|
editing = true;
|
|
|
|
|
// build bytes
|
|
|
|
|
var nud = sender as NumericUpDown;
|
|
|
|
|
int index = Bytes.IndexOf(nud);
|
|
|
|
|
Raw[index] = (byte)nud.Value;
|
|
|
|
|
|
2017-09-23 04:34:15 +00:00
|
|
|
|
string str = GetString();
|
2017-04-09 21:06:50 +00:00
|
|
|
|
TB_Text.Text = str;
|
|
|
|
|
editing = false;
|
2014-12-12 05:44:05 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void UpdateString(object sender, EventArgs e)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (editing)
|
|
|
|
|
return;
|
|
|
|
|
editing = true;
|
|
|
|
|
// build bytes
|
2017-09-23 04:34:15 +00:00
|
|
|
|
byte[] data = SetString(TB_Text.Text);
|
2017-04-24 04:44:12 +00:00
|
|
|
|
Array.Copy(data, Raw, Math.Min(data.Length, Raw.Length));
|
|
|
|
|
for (int i = 0; i < Raw.Length; i++)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
Bytes[i].Value = Raw[i];
|
|
|
|
|
editing = false;
|
|
|
|
|
}
|
|
|
|
|
private void B_ApplyTrash_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string species = PKX.GetSpeciesNameGeneration(WinFormsUtil.GetIndex(CB_Species),
|
|
|
|
|
WinFormsUtil.GetIndex(CB_Language), (int) NUD_Generation.Value);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2018-05-13 02:07:32 +00:00
|
|
|
|
if (species?.Length == 0) // no result
|
2017-04-09 21:06:50 +00:00
|
|
|
|
species = CB_Species.Text;
|
|
|
|
|
|
2017-09-23 04:34:15 +00:00
|
|
|
|
byte[] current = SetString(TB_Text.Text);
|
|
|
|
|
byte[] data = SetString(species);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
if (data.Length <= current.Length)
|
|
|
|
|
{
|
2017-04-10 03:07:24 +00:00
|
|
|
|
WinFormsUtil.Alert("Trash byte layer is hidden by current text.",
|
|
|
|
|
$"Current Bytes: {current.Length}" + Environment.NewLine + $"Layer Bytes: {data.Length}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (data.Length > Bytes.Count)
|
|
|
|
|
{
|
|
|
|
|
WinFormsUtil.Alert("Trash byte layer is too long to apply.");
|
2017-04-09 21:06:50 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (int i = current.Length; i < data.Length; i++)
|
|
|
|
|
Bytes[i].Value = data[i];
|
|
|
|
|
}
|
|
|
|
|
private void B_ClearTrash_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2017-09-23 04:34:15 +00:00
|
|
|
|
byte[] current = SetString(TB_Text.Text);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
for (int i = current.Length; i < Bytes.Count; i++)
|
|
|
|
|
Bytes[i].Value = 0;
|
|
|
|
|
}
|
2017-09-23 04:34:15 +00:00
|
|
|
|
private byte[] SetString(string text)
|
|
|
|
|
{
|
|
|
|
|
return SAV is SAV2 s && s.Korean
|
|
|
|
|
? StringConverter.SetString2KOR(text, Raw.Length)
|
|
|
|
|
: StringConverter.SetString(text, SAV.Generation, SAV.Japanese, bigendian, Raw.Length, SAV.Language);
|
|
|
|
|
}
|
|
|
|
|
private string GetString()
|
|
|
|
|
{
|
|
|
|
|
return SAV is SAV2 s && s.Korean
|
|
|
|
|
? StringConverter.GetString2KOR(Raw, 0, Raw.Length)
|
|
|
|
|
: StringConverter.GetString(Raw, SAV.Generation, SAV.Japanese, bigendian, Raw.Length);
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
|
|
|
|
// Helpers
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static Label GetLabel(string str) => new Label {Text = str, AutoSize = true};
|
|
|
|
|
private static NumericUpDown GetNUD(int min, int max, bool hex) => new NumericUpDown
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
|
|
|
|
Maximum = max,
|
|
|
|
|
Minimum = min,
|
|
|
|
|
Hexadecimal = hex,
|
|
|
|
|
Width = 36,
|
|
|
|
|
Padding = new Padding(0),
|
|
|
|
|
Margin = new Padding(0),
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static ushort[] GetChars(int generation)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
|
|
|
|
switch (generation)
|
|
|
|
|
{
|
|
|
|
|
case 6:
|
|
|
|
|
case 7:
|
|
|
|
|
return chars67;
|
|
|
|
|
default: return new ushort[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private static readonly ushort[] chars67 =
|
|
|
|
|
{
|
|
|
|
|
0xE081, 0xE082, 0xE083, 0xE084, 0xE085, 0xE086, 0xE087, 0xE08D,
|
|
|
|
|
0xE08E, 0xE08F, 0xE090, 0xE091, 0xE092, 0xE093, 0xE094, 0xE095,
|
|
|
|
|
0xE096, 0xE097, 0xE098, 0xE099, 0xE09A, 0xE09B, 0xE09C, 0xE09D,
|
|
|
|
|
0xE09E, 0xE09F, 0xE0A0, 0xE0A1, 0xE0A2, 0xE0A3, 0xE0A4, 0xE0A5,
|
|
|
|
|
};
|
2014-12-12 05:44:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|