Use Direct Casts and Conditional Access to Avoid NPEs

This commit is contained in:
Tyler Dunn
2016-07-18 14:51:26 -04:00
parent 016a0e3aee
commit de8cebf2a4
10 changed files with 47 additions and 47 deletions

View File

@@ -92,7 +92,7 @@ public Main()
#endregion
#region Localize & Populate Fields
string[] args = Environment.GetCommandLineArgs();
string filename = args.Length > 0 ? Path.GetFileNameWithoutExtension(args[0]).ToLower() : "";
string filename = args.Length > 0 ? Path.GetFileNameWithoutExtension(args[0])?.ToLower() : "";
HaX = filename.IndexOf("hax", StringComparison.Ordinal) >= 0;
// Try and detect the language
@@ -698,7 +698,7 @@ private void openSAV(byte[] input, string path)
SAV.FilePath = Path.GetDirectoryName(path);
SAV.FileName = Path.GetExtension(path) == ".bak"
? Path.GetFileName(path).Split(new[] {" ["}, StringSplitOptions.None)[0]
? Path.GetFileName(path)?.Split(new[] {" ["}, StringSplitOptions.None)[0]
: Path.GetFileName(path);
L_Save.Text = $"SAV{SAV.Generation}: {Path.GetFileNameWithoutExtension(Util.CleanFileName(SAV.BAKName))}"; // more descriptive
@@ -785,10 +785,10 @@ private void openSAV(byte[] input, string path)
getFieldsfromPKM = populateFieldsPK6;
getPKMfromFields = preparePK6;
extraBytes = PK6.ExtraBytes;
TB_GameSync.Enabled = (SAV as SAV6).GameSyncID != 0;
TB_GameSync.Text = (SAV as SAV6).GameSyncID.ToString("X16");
TB_Secure1.Text = (SAV as SAV6).Secure1.ToString("X16");
TB_Secure2.Text = (SAV as SAV6).Secure2.ToString("X16");
TB_GameSync.Enabled = ((SAV6) SAV).GameSyncID != 0;
TB_GameSync.Text = ((SAV6) SAV).GameSyncID.ToString("X16");
TB_Secure1.Text = ((SAV6) SAV).Secure1.ToString("X16");
TB_Secure2.Text = ((SAV6) SAV).Secure2.ToString("X16");
break;
}
PKM pk = preparePKM();
@@ -1294,16 +1294,16 @@ private void clickStatLabel(object sender, MouseEventArgs e)
private void clickIV(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Control)
(sender as MaskedTextBox).Text = 31.ToString();
((MaskedTextBox) sender).Text = 31.ToString();
else if (ModifierKeys == Keys.Alt)
(sender as MaskedTextBox).Text = 0.ToString();
((MaskedTextBox) sender).Text = 0.ToString();
}
private void clickEV(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Control) // EV
(sender as MaskedTextBox).Text = Math.Min(Math.Max(510 - Util.ToInt32(TB_EVTotal.Text) + Util.ToInt32((sender as MaskedTextBox).Text), 0), 252).ToString();
((MaskedTextBox) sender).Text = Math.Min(Math.Max(510 - Util.ToInt32(TB_EVTotal.Text) + Util.ToInt32((sender as MaskedTextBox).Text), 0), 252).ToString();
else if (ModifierKeys == Keys.Alt)
(sender as MaskedTextBox).Text = 0.ToString();
((MaskedTextBox) sender).Text = 0.ToString();
}
private void clickOT(object sender, EventArgs e)
{
@@ -1437,8 +1437,8 @@ private void updateHPType(object sender, EventArgs e)
private void updateIVs(object sender, EventArgs e)
{
if (changingFields || !fieldsInitialized) return;
if (sender != null && Util.ToInt32((sender as MaskedTextBox).Text) > 31)
(sender as MaskedTextBox).Text = "31";
if (sender != null && Util.ToInt32(((MaskedTextBox) sender).Text) > 31)
((MaskedTextBox) sender).Text = "31";
changingFields = true;
@@ -1470,8 +1470,8 @@ private void updateIVs(object sender, EventArgs e)
private void updateEVs(object sender, EventArgs e)
{
if (sender != null)
if (Util.ToInt32((sender as MaskedTextBox).Text) > SAV.MaxEV)
(sender as MaskedTextBox).Text = SAV.MaxEV.ToString();
if (Util.ToInt32(((MaskedTextBox) sender).Text) > SAV.MaxEV)
((MaskedTextBox) sender).Text = SAV.MaxEV.ToString();
changingFields = true;
int EV_HP = Util.ToInt32(TB_HPEV.Text);
@@ -1606,8 +1606,8 @@ private void updateHackedStatText(object sender, EventArgs e)
}
private void update255_MTB(object sender, EventArgs e)
{
if (Util.ToInt32((sender as MaskedTextBox).Text) > byte.MaxValue)
(sender as MaskedTextBox).Text = "255";
if (Util.ToInt32(((MaskedTextBox) sender).Text) > byte.MaxValue)
((MaskedTextBox) sender).Text = "255";
}
private void updateForm(object sender, EventArgs e)
{
@@ -1885,8 +1885,8 @@ private void updateExtraByteValue(object sender, EventArgs e)
if (CB_ExtraBytes.Items.Count == 0)
return;
// Changed Extra Byte's Value
if (Util.ToInt32((sender as MaskedTextBox).Text) > byte.MaxValue)
(sender as MaskedTextBox).Text = "255";
if (Util.ToInt32(((MaskedTextBox) sender).Text) > byte.MaxValue)
((MaskedTextBox) sender).Text = "255";
int value = Util.ToInt32(TB_ExtraByte.Text);
int offset = Convert.ToInt32(CB_ExtraBytes.Text, 16);
@@ -2131,7 +2131,7 @@ private void validateMove(object sender, EventArgs e)
{
if (pkm.Format < 6)
return;
(pkm as PK6).RelearnMoves = new[] { Util.getIndex(CB_RelearnMove1), Util.getIndex(CB_RelearnMove2), Util.getIndex(CB_RelearnMove3), Util.getIndex(CB_RelearnMove4) };
((PK6) pkm).RelearnMoves = new[] { Util.getIndex(CB_RelearnMove1), Util.getIndex(CB_RelearnMove2), Util.getIndex(CB_RelearnMove3), Util.getIndex(CB_RelearnMove4) };
Legality.updateRelearnLegality();
for (int i = 0; i < 4; i++)
movePB[i].Visible = !Legality.vRelearn[i].Valid;
@@ -2470,7 +2470,7 @@ private void clickExportSAV(object sender, EventArgs e)
if (SAV.HasBox)
SAV.CurrentBox = CB_BoxSelect.SelectedIndex;
bool dsv = Path.GetExtension(main.FileName).ToLower() == ".dsv";
bool dsv = Path.GetExtension(main.FileName)?.ToLower() == ".dsv";
File.WriteAllBytes(main.FileName, SAV.Write(dsv));
Util.Alert("SAV exported to:", main.FileName);
}
@@ -2704,11 +2704,11 @@ private void updateU64(object sender, EventArgs e)
// Currently saved Value
ulong oldval = 0;
if (tb == TB_GameSync)
oldval = (SAV as SAV6).GameSyncID;
oldval = ((SAV6) SAV).GameSyncID;
else if (tb == TB_Secure1)
oldval = (SAV as SAV6).Secure1;
oldval = ((SAV6) SAV).Secure1;
else if (tb == TB_Secure2)
oldval = (SAV as SAV6).Secure2;
oldval = ((SAV6) SAV).Secure2;
string filterText = Util.getOnlyHex(tb.Text);
@@ -2725,11 +2725,11 @@ private void updateU64(object sender, EventArgs e)
if (newval != oldval)
{
if (tb == TB_GameSync)
(SAV as SAV6).GameSyncID = newval;
((SAV6) SAV).GameSyncID = newval;
else if (tb == TB_Secure1)
(SAV as SAV6).Secure1 = newval;
((SAV6) SAV).Secure1 = newval;
else if (tb == TB_Secure2)
(SAV as SAV6).Secure2 = newval;
((SAV6) SAV).Secure2 = newval;
SAV.Edited = true;
}
}
@@ -3120,7 +3120,7 @@ private void B_OpenOPowers_Click(object sender, EventArgs e)
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x00, 0x00, 0x00,
}.CopyTo(SAV.Data, (SAV as SAV6).OPower);
}.CopyTo(SAV.Data, ((SAV6) SAV).OPower);
}
else if (SAV.XY)
new SAV_OPower().ShowDialog();
@@ -3140,7 +3140,7 @@ private void B_OUTPasserby_Click(object sender, EventArgs e)
return;
string result = "PSS List" + Environment.NewLine;
string[] headers = { "PSS Data - Friends", "PSS Data - Acquaintances", "PSS Data - Passerby", };
int offset = (SAV as SAV6).PSS;
int offset = ((SAV6) SAV).PSS;
for (int g = 0; g < 3; g++)
{
result += Environment.NewLine

View File

@@ -104,11 +104,11 @@ internal static PKM convertToFormat(PKM pk, int Format, out string comment)
pkm.Met_Location = 30001; // Pokétransfer
}
if (pkm.Format == 3 && Format > 3)
pkm = (pkm as PK3).convertToPK4();
pkm = ((PK3) pkm).convertToPK4();
if (pkm.Format == 4 && Format > 4)
pkm = (pkm as PK4).convertToPK5();
pkm = ((PK4) pkm).convertToPK5();
if (pkm.Format == 5 && Format > 5)
pkm = (pkm as PK5).convertToPK6();
pkm = ((PK5) pkm).convertToPK6();
comment = $"Converted from pk{currentFormat} to pk{Format}";
return pkm;
}

View File

@@ -207,7 +207,7 @@ public string getText()
// First Line: Name, Nickname, Gender, Item
string result = string.Format(species[Species] != Nickname ? "{0} ({1})" : "{1}", Nickname,
species[Species] + ((Form ?? "") != "" ? "-" + Form.Replace("Mega ", "Mega-") : "")) // Species (& Form if necessary)
species[Species] + ((Form ?? "") != "" ? "-" + Form?.Replace("Mega ", "Mega-") : "")) // Species (& Form if necessary)
+ Gender + (Item != 0 ? " @ " + items[Item] : "") + Environment.NewLine;
// IVs

View File

@@ -425,9 +425,9 @@ private void changeCountryIndex(object sender, EventArgs e)
}
private void changeCountryText(object sender, EventArgs e)
{
if ((sender as ComboBox).Text == "")
if (((ComboBox) sender).Text == "")
{
(sender as ComboBox).SelectedValue = 0;
((ComboBox) sender).SelectedValue = 0;
changeCountryIndex(sender, e);
}
}
@@ -437,7 +437,7 @@ private void update255_MTB(object sender, EventArgs e)
MaskedTextBox mtb = sender as MaskedTextBox;
try
{
int val = Util.ToInt32(mtb.Text);
int val = Util.ToInt32(mtb?.Text);
if (val > 255) mtb.Text = "255";
}
catch { mtb.Text = "0"; }

View File

@@ -42,7 +42,7 @@ private void onClick(object sender, EventArgs e)
{
string nickname = TB_Nickname.Text;
if (nickname.Length < TB_Nickname.MaxLength)
TB_Nickname.Text += (sender as Label).Text;
TB_Nickname.Text += ((Label) sender).Text;
}
}
}

View File

@@ -73,7 +73,7 @@ private void removedropCB(object sender, KeyEventArgs e)
}
private void changeDisplayed(object sender, EventArgs e)
{
if (!(sender as CheckBox).Checked)
if (!((CheckBox) sender).Checked)
return;
CHK_P6.Checked = sender == CHK_P6;

View File

@@ -74,7 +74,7 @@ private void removedropCB(object sender, KeyEventArgs e)
}
private void changeDisplayed(object sender, EventArgs e)
{
if (!(sender as CheckBox).Checked)
if (!((CheckBox) sender).Checked)
return;
CHK_P6.Checked = sender == CHK_P6;

View File

@@ -59,7 +59,7 @@ private void Setup()
private void dropclick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 1) return;
((ComboBox)(sender as DataGridView).EditingControl).DroppedDown = true;
((ComboBox)((DataGridView) sender).EditingControl).DroppedDown = true;
}
private void B_Cancel_Click(object sender, EventArgs e)

View File

@@ -583,7 +583,7 @@ private void changeBadge(object sender, EventArgs e)
private void changeSpecial(object sender, EventArgs e)
{
MaskedTextBox box = sender as MaskedTextBox;
int val = Util.ToInt32(box.Text);
int val = Util.ToInt32(box?.Text);
if (box == MT_HairColor)
box.Text = (val > 7 ? 7 : val).ToString();
@@ -593,13 +593,13 @@ private void changeSpecial(object sender, EventArgs e)
private void change255(object sender, EventArgs e)
{
MaskedTextBox box = sender as MaskedTextBox;
if (box.Text == "") box.Text = "0";
if (box?.Text == "") box.Text = "0";
if (Util.ToInt32(box.Text) > 255) box.Text = "255";
}
private void changeFFFF(object sender, EventArgs e)
{
MaskedTextBox box = sender as MaskedTextBox;
if (box.Text == "") box.Text = "0";
if (box?.Text == "") box.Text = "0";
if (Util.ToInt32(box.Text) > 65535) box.Text = "65535";
}
private void changeStat(object sender, EventArgs e)

View File

@@ -36,9 +36,9 @@ public class Preview
public string OT => pkm.OT_Name;
public string Version => Main.gamelist[pkm.Version];
public string OTLang => Main.gamelanguages[pkm.Language] ?? $"UNK {pkm.Language}";
public string CountryID => pkm.Format > 5 ? (pkm as PK6).Country.ToString() : "N/A";
public string RegionID => pkm.Format > 5 ? (pkm as PK6).Region.ToString() : "N/A";
public string DSRegionID => pkm.Format > 5 ? (pkm as PK6).ConsoleRegion.ToString() : "N/A";
public string CountryID => pkm.Format > 5 ? ((PK6) pkm).Country.ToString() : "N/A";
public string RegionID => pkm.Format > 5 ? ((PK6) pkm).Region.ToString() : "N/A";
public string DSRegionID => pkm.Format > 5 ? ((PK6) pkm).ConsoleRegion.ToString() : "N/A";
#region Extraneous
public string EC => pkm.EncryptionConstant.ToString("X8");
@@ -65,9 +65,9 @@ public class Preview
public int Sheen => pkm.CNT_Sheen;
public int Markings => pkm.MarkByte;
public string NotOT => pkm.Format > 5 ? (pkm as PK6).HT_Name : "N/A";
public string NotOT => pkm.Format > 5 ? ((PK6) pkm).HT_Name : "N/A";
public int AbilityNum => pkm.Format > 5 ? (pkm as PK6).AbilityNumber : -1;
public int AbilityNum => pkm.Format > 5 ? ((PK6) pkm).AbilityNumber : -1;
public int GenderFlag => pkm.Gender;
public int AltForms => pkm.AltForm;
public int PKRS_Strain => pkm.PKRS_Strain;