2018-04-28 16:34:54 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
|
|
|
|
public partial class TrainerID : UserControl
|
|
|
|
|
{
|
|
|
|
|
public TrainerID() => InitializeComponent();
|
2020-10-18 18:02:39 +00:00
|
|
|
|
public event EventHandler? UpdatedID;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
|
|
|
|
|
private int Format = -1;
|
2020-10-18 18:02:39 +00:00
|
|
|
|
private ITrainerID Trainer = null!;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
|
|
|
|
|
public void UpdateTSV()
|
|
|
|
|
{
|
2018-04-28 18:52:48 +00:00
|
|
|
|
var tsv = GetTSV();
|
|
|
|
|
if (tsv < 0)
|
2018-04-28 18:06:58 +00:00
|
|
|
|
return;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2022-02-07 00:00:51 +00:00
|
|
|
|
string IDstr = $"TSV: {tsv:d4}{Environment.NewLine}{GetAlternateRepresentation(Trainer, Format)}";
|
2018-04-28 16:34:54 +00:00
|
|
|
|
TSVTooltip.SetToolTip(TB_TID, IDstr);
|
|
|
|
|
TSVTooltip.SetToolTip(TB_SID, IDstr);
|
|
|
|
|
TSVTooltip.SetToolTip(TB_TID7, IDstr);
|
|
|
|
|
TSVTooltip.SetToolTip(TB_SID7, IDstr);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2022-02-07 00:00:51 +00:00
|
|
|
|
private static string GetAlternateRepresentation(ITrainerID tr, int format)
|
|
|
|
|
{
|
|
|
|
|
if (format >= 7)
|
|
|
|
|
return $"ID: {tr.TID:D5}/{tr.SID:D5}";
|
|
|
|
|
var repack = (uint)((tr.SID << 16) | tr.TID); // 32bit
|
|
|
|
|
return $"G7ID: ({repack / 1_000_000:D4}){repack % 1_000_000:D6}";
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-28 18:52:48 +00:00
|
|
|
|
private int GetTSV()
|
|
|
|
|
{
|
|
|
|
|
if (Format <= 2)
|
|
|
|
|
return -1;
|
|
|
|
|
var xor = Trainer.SID ^ Trainer.TID;
|
|
|
|
|
if (Format <= 5)
|
|
|
|
|
return xor >> 3;
|
|
|
|
|
return xor >> 4;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 18:06:58 +00:00
|
|
|
|
public void LoadIDValues(ITrainerID tr)
|
2018-04-28 16:34:54 +00:00
|
|
|
|
{
|
2018-04-28 18:06:58 +00:00
|
|
|
|
Trainer = tr;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
int format = tr.GetTrainerIDFormat();
|
2018-04-28 18:06:58 +00:00
|
|
|
|
SetFormat(format);
|
2018-04-28 16:34:54 +00:00
|
|
|
|
LoadValues();
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
public void UpdateSID() => LoadValues();
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
public void LoadInfo(ITrainerInfo info)
|
|
|
|
|
{
|
2018-04-28 18:06:58 +00:00
|
|
|
|
Trainer.TID = info.TID;
|
|
|
|
|
Trainer.SID = info.SID;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
LoadValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LoadValues()
|
|
|
|
|
{
|
|
|
|
|
if (Format <= 2)
|
2018-04-28 18:06:58 +00:00
|
|
|
|
TB_TID.Text = Trainer.TID.ToString();
|
2018-04-28 16:34:54 +00:00
|
|
|
|
else if (Format <= 6)
|
2018-04-28 18:06:58 +00:00
|
|
|
|
LoadTID(Trainer.TID, Trainer.SID);
|
2018-04-28 16:34:54 +00:00
|
|
|
|
else
|
2018-04-28 18:06:58 +00:00
|
|
|
|
LoadTID7(Trainer.TID, Trainer.SID);
|
2018-04-28 16:34:54 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
private void LoadTID(int tid, int sid)
|
|
|
|
|
{
|
|
|
|
|
TB_TID.Text = tid.ToString("D5");
|
|
|
|
|
TB_SID.Text = sid.ToString("D5");
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
private void LoadTID7(int tid, int sid)
|
|
|
|
|
{
|
|
|
|
|
var repack = (uint)((sid << 16) | tid);
|
|
|
|
|
sid = (int)(repack / 1_000_000);
|
|
|
|
|
tid = (int)(repack % 1_000_000);
|
|
|
|
|
|
|
|
|
|
TB_TID7.Text = tid.ToString("D6");
|
|
|
|
|
TB_SID7.Text = sid.ToString("D4");
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
private void SetFormat(int format)
|
|
|
|
|
{
|
|
|
|
|
if (format == Format)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var controls = GetControlsForFormat(format);
|
|
|
|
|
FLP.Controls.Clear(); int i = 0;
|
|
|
|
|
foreach (var c in controls)
|
|
|
|
|
{
|
|
|
|
|
FLP.Controls.Add(c);
|
|
|
|
|
FLP.Controls.SetChildIndex(c, i++); // because you don't listen the first time
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Format = format;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2021-01-05 01:31:43 +00:00
|
|
|
|
private IEnumerable<Control> GetControlsForFormat(int format) => format switch
|
2018-04-28 16:34:54 +00:00
|
|
|
|
{
|
2021-01-05 01:31:43 +00:00
|
|
|
|
>= 7 => new Control[] {Label_SID, TB_SID7, Label_TID, TB_TID7},
|
|
|
|
|
>= 3 => new Control[] {Label_TID, TB_TID, Label_SID, TB_SID },
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => new Control[] {Label_TID, TB_TID}, // Gen1/2
|
2021-01-05 01:31:43 +00:00
|
|
|
|
};
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
private void UpdateTSV(object sender, EventArgs e) => UpdateTSV();
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
private void Update_ID(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-12-23 05:24:41 +00:00
|
|
|
|
if (sender is not MaskedTextBox mt)
|
2018-04-28 16:34:54 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
if (!int.TryParse(mt.Text, out var value))
|
|
|
|
|
value = 0;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
if (mt == TB_TID7)
|
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
if (value > 999_999)
|
2018-04-28 16:34:54 +00:00
|
|
|
|
{
|
|
|
|
|
mt.Text = "999999";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-14 16:20:48 +00:00
|
|
|
|
if (!int.TryParse(TB_SID7.Text, out var sid))
|
|
|
|
|
sid = 0;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
SanityCheckSID7(value, sid);
|
2018-04-28 16:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
else if (mt == TB_SID7)
|
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
if (value > 4294) // max 4 digits of 32bit int
|
2018-04-28 16:34:54 +00:00
|
|
|
|
{
|
|
|
|
|
mt.Text = "4294";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-14 16:20:48 +00:00
|
|
|
|
if (!int.TryParse(TB_TID7.Text, out var tid))
|
|
|
|
|
tid = 0;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
SanityCheckSID7(tid, value);
|
2018-04-28 16:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
if (value > ushort.MaxValue) // prior to gen7
|
|
|
|
|
mt.Text = (value = ushort.MaxValue).ToString();
|
2018-04-28 16:34:54 +00:00
|
|
|
|
|
|
|
|
|
if (mt == TB_TID)
|
2022-01-03 05:35:59 +00:00
|
|
|
|
Trainer.TID = value;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
else
|
2022-01-03 05:35:59 +00:00
|
|
|
|
Trainer.SID = value;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdatedID?.Invoke(sender, e);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-04-28 16:34:54 +00:00
|
|
|
|
private void SanityCheckSID7(int tid, int sid)
|
|
|
|
|
{
|
2018-07-27 02:34:27 +00:00
|
|
|
|
var repack = ((long)sid * 1_000_000) + tid;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
if (repack > uint.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
TB_SID7.Text = (sid - 1).ToString();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-28 18:06:58 +00:00
|
|
|
|
Trainer.SID = (ushort)(repack >> 16);
|
|
|
|
|
Trainer.TID = (ushort)repack;
|
2018-04-28 16:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|