Minor clean

dual bounds check -> single check via uint cast
pkx: keep array reference for reuse in next check
format return to second line to stick out more; lines of code are cheap
:)
This commit is contained in:
Kurt
2019-09-11 22:17:16 -07:00
parent 477187a66c
commit 0d9c18e1c6
14 changed files with 122 additions and 59 deletions

View File

@@ -69,7 +69,8 @@ protected static int GetDataSize(PokeListType c, bool jp, int entrySize)
{
get
{
if (i > Capacity || i < 0) throw new ArgumentOutOfRangeException($"Invalid {nameof(PokeListGB<T>)} Access: {i}");
if ((uint)i > Capacity)
throw new ArgumentOutOfRangeException($"Invalid {nameof(PokeListGB<T>)} Access: {i}");
return Pokemon[i];
}
set

View File

@@ -91,12 +91,14 @@ public static class PKX
/// <remarks>Should only be used externally for message displays; for accurate in-game names use <see cref="GetSpeciesNameGeneration"/>.</remarks>
public static string GetSpeciesName(int species, int lang)
{
if (lang < 0 || SpeciesLang.Length <= lang)
return string.Empty;
if (species < 0 || SpeciesLang[0].Length <= species)
if ((uint)lang >= SpeciesLang.Length)
return string.Empty;
return SpeciesLang[lang][species];
var arr = SpeciesLang[lang];
if ((uint)species >= arr.Length)
return string.Empty;
return arr[species];
}
/// <summary>

View File

@@ -351,7 +351,7 @@ public override StorageSlotFlag GetSlotFlags(int index)
public int GetFusedSlotOffset(int slot)
{
if (Fused < 0 || slot < 0 || slot >= FusedCount)
if ((uint)slot >= FusedCount)
return -1;
return Fused + (PKX.SIZE_6PARTY * slot); // 0x104*slot
}

View File

@@ -58,14 +58,14 @@ public int DaysFromRefreshed
public int GetSurfScore(int recordID)
{
if (recordID < 0 || recordID > 4)
if ((uint)recordID >= 4)
recordID = 0;
return BitConverter.ToInt32(Data, Offset + 0x138 + (4 * recordID));
}
public void SetSurfScore(int recordID, int score)
{
if (recordID < 0 || recordID > 4)
if ((uint)recordID >= 4)
recordID = 0;
SAV.SetData(BitConverter.GetBytes(score), Offset + 0x138 + (4 * recordID));
}

View File

@@ -256,7 +256,7 @@ private void ChangeStat1(object sender, EventArgs e)
if (loading)
return;
int facility = CB_Stats1.SelectedIndex;
if (facility < 0 || facility >= BFN.Length)
if ((uint)facility >= BFN.Length)
return;
editingcont = true;
CB_Stats2.Items.Clear();
@@ -292,7 +292,8 @@ private void ChangeStat(object sender, EventArgs e)
private void StatAddrControl(int SetValToSav = -2, bool SetSavToVal = false)
{
int Facility = CB_Stats1.SelectedIndex;
if (Facility < 0) return;
if (Facility < 0)
return;
int BattleType = CB_Stats2.SelectedIndex;
if (BFT[BFF[Facility][1]] == null)
@@ -311,13 +312,15 @@ private void StatAddrControl(int SetValToSav = -2, bool SetSavToVal = false)
return;
RBi = i;
}
if (RBi < 0) return;
if (RBi < 0)
return;
if (SetValToSav >= 0)
{
ushort val = (ushort)StatNUDA[SetValToSav].Value;
SetValToSav = Array.IndexOf(BFV[BFF[Facility][0]], SetValToSav);
if (SetValToSav < 0) return;
if (SetValToSav < 0)
return;
if (val > 9999) val = 9999;
BitConverter.GetBytes(val).CopyTo(SAV.Data, SAV.GetBlockOffset(0) + BFF[Facility][2 + SetValToSav] + (4 * BattleType) + (2 * RBi));
return;
@@ -430,7 +433,8 @@ private void SaveBattleFrontier()
private void BTN_Symbol_Click(object sender, EventArgs e)
{
int index = Array.IndexOf(SymbolButtonA, sender);
if (index < 0) return;
if (index < 0)
return;
// 0 (none) | 1 (silver) | 2 (silver) | 3 (gold)
// bit rotation 00 -> 01 -> 11 -> 00

View File

@@ -553,7 +553,8 @@ private void SetPrints()
private void BTN_Print_Click(object sender, EventArgs e)
{
int index = Array.IndexOf(PrintButtonA, sender);
if (index < 0) return;
if (index < 0)
return;
Prints[index] = (Prints[index] + 1) % 3;
SetPrints();
}
@@ -653,7 +654,8 @@ private void StatAddrControl(int SetValToSav = -2, bool SetSavToVal = false)
BitConverter.GetBytes(val).CopyTo(SAV.General, addrFlag + TowerContinueCountOfs + (BattleType << 1));
SetValToSav = Array.IndexOf(BFV[BFF[Facility][0]], SetValToSav);
if (SetValToSav < 0) return;
if (SetValToSav < 0)
return;
BitConverter.GetBytes((ushort)(val > 9999 ? 9999 : val)).CopyTo(SAV.General, addrVal + (SetValToSav << 1));
return;
}
@@ -785,7 +787,8 @@ private void NUD_HallType_ValueChanged(object sender, EventArgs e)
private void NUD_HallStreaks_ValueChanged(object sender, EventArgs e)
{
if (editing || ofsHallStat < 0) return;
if (editing || ofsHallStat < 0)
return;
BitConverter.GetBytes((ushort)NUD_HallStreaks.Value).CopyTo(SAV.Data, ofsHallStat + 4 + (0x3DE * CB_Stats2.SelectedIndex) + (species << 1));
HallStatUpdated = true;
}

View File

@@ -466,14 +466,16 @@ private void SetNudMax(bool? isBlack = null)
private void SetFMVal(int ofsB, int len, uint val)
{
int s = LB_FunfestMissions.SelectedIndex;
if (s < 0 || s >= FMUnlockConditions.Length) return;
if ((uint)s >= FMUnlockConditions.Length)
return;
BitConverter.GetBytes((BitConverter.ToUInt32(SAV.Data, ofsFM + (s << 2)) & ~(~(uint)0 >> (32 - len) << ofsB)) | val << ofsB).CopyTo(SAV.Data, ofsFM + (s << 2));
}
private void LB_FunfestMissions_SelectedIndexChanged(object sender, EventArgs e)
{
int s = LB_FunfestMissions.SelectedIndex;
if (s < 0 || s >= FMUnlockConditions.Length) return;
if ((uint)s >= FMUnlockConditions.Length)
return;
editing = true;
bool FirstMissionCleared = (SAV.Data[0x2025E + (2438 >> 3)] & 1 << (2438 & 7)) != 0;
L_FMUnlocked.Visible = s == 0 ? !FirstMissionCleared : FirstMissionCleared && FMUnlockConditions[s]?.All(v => (SAV.Data[0x2025E + (v >> 3)] & 1 << (v & 7)) != 0) != false;

View File

@@ -157,7 +157,7 @@ private bool MoveItem(int direction)
int newIndex = LB_BoxSelect.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= LB_BoxSelect.Items.Count)
if ((uint)newIndex >= LB_BoxSelect.Items.Count)
return false; // Index out of range - nothing to do
object selected = LB_BoxSelect.SelectedItem;

View File

@@ -212,7 +212,8 @@ private void NUP_PartyIndex_ValueChanged(object sender, EventArgs e)
int index = LB_DataEntry.SelectedIndex;
int offset = (index * 0x1B4) + ((Convert.ToInt32(NUP_PartyIndex.Value)-1) * 0x48);
if (offset < 0) return;
if (offset < 0)
return;
int species = BitConverter.ToUInt16(data, offset + 0x00);
CB_Species.SelectedValue = species;

View File

@@ -81,7 +81,8 @@ private void B_SAV2FAV(object sender, EventArgs e)
{
loading = true;
int index = LB_Favorite.SelectedIndex;
if (index < 0) return;
if (index < 0)
return;
var offset = GetSecretBaseOffset(index);
var bdata = new SecretBase6(SAV.Data, offset);
@@ -156,7 +157,8 @@ private void B_FAV2SAV(object sender, EventArgs e)
var name = LB_Favorite.Items[index].ToString();
if (name == "* " || name == $"{index} Empty")
{ WinFormsUtil.Error("Sorry, no overwriting an empty base with someone else's."); return; }
if (index < 0) return;
if (index < 0)
return;
int offset = GetSecretBaseOffset(index);
var bdata = new SecretBase6(SAV.Data, offset);
@@ -171,7 +173,8 @@ private void B_FAV2SAV(object sender, EventArgs e)
bdata.Saying4 = TB_FSay4.Text;
int baseloc = (int)NUD_FBaseLocation.Value;
if (baseloc < 3) baseloc = 0; // skip 1/2 baselocs as they are dummied out ingame.
if (baseloc < 3)
baseloc = 0; // skip 1/2 baselocs as they are dummied out ingame.
bdata.BaseLocation = baseloc;
// Copy back Objects

View File

@@ -105,7 +105,8 @@ private void DropClick(object sender, DataGridViewCellEventArgs e)
private void ChangeListRecordSelection(object sender, EventArgs e)
{
int index = listBox1.SelectedIndex;
if (index < 0) return;
if (index < 0)
return;
loading = true;
TB_Time.Text = BitConverter.ToSingle(SAV.Data, offsetTime + (4 * index)).ToString();
TB_Unk.Text = BitConverter.ToUInt16(SAV.Data, offsetVal + (4 * index)).ToString();

View File

@@ -195,7 +195,7 @@ public SAV_FestivalPlaza(SaveFile sav)
private int TypeIndexToType(int typeIndex)
{
if (typeIndex < 0 || typeIndex > typeMAX) return -1;
if ((uint)typeIndex > typeMAX + 1) return -1;
if (typeIndex < 0x0F) return 0;
if (typeIndex < 0x1E) return 1;
if (typeIndex < 0x2F) return 2;
@@ -231,7 +231,8 @@ private void LoadFacility()
{
case 5:
int lucky = (facility.UsedLuckyPlace * 3) + facility.UsedLuckyRank - 3;
if (lucky < 0 || lucky >= CB_LuckyResult.Items.Count) lucky = 0;
if ((uint)lucky >= CB_LuckyResult.Items.Count)
lucky = 0;
CB_LuckyResult.SelectedIndex = lucky;
break;
case 7:
@@ -297,7 +298,7 @@ private void LoadBattleAgency()
{
int j = GetSavData16(0x6C56C + (0x14 * i));
var m = (int)NUD_Trainers[i].Maximum;
NUD_Trainers[i].Value = j < 0 || j > m ? m : j;
NUD_Trainers[i].Value = (uint)j > m ? m : j;
}
B_AgentGlass.Enabled = (SAV.GetData(SAV.Fashion + 0xD0, 1)[0] & 1) == 0;
}
@@ -391,7 +392,8 @@ private void LoadOTlabel(int b)
private void Label_OTGender_Click(object sender, EventArgs e)
{
if (entry < 0) return;
if (entry < 0)
return;
var b = f[entry].Gender;
b ^= 1;
f[entry].Gender = b;
@@ -402,10 +404,15 @@ private void Label_OTGender_Click(object sender, EventArgs e)
private void CB_FacilityMessage_SelectedIndexChanged(object sender, EventArgs e)
{
if (editing) return;
if (editing)
return;
int fmIndex = CB_FacilityMessage.SelectedIndex;
if (fmIndex < 0) return;
if (entry < 0) return;
if (fmIndex < 0)
return;
if (entry < 0)
return;
editing = true;
LoadFMessage(fmIndex);
editing = false;
@@ -413,17 +420,25 @@ private void CB_FacilityMessage_SelectedIndexChanged(object sender, EventArgs e)
private void NUD_FacilityMessage_ValueChanged(object sender, EventArgs e)
{
if (editing) return;
if (editing)
return;
int fmIndex = CB_FacilityMessage.SelectedIndex;
if (fmIndex < 0) return;
if (entry < 0) return;
if (fmIndex < 0)
return;
if (entry < 0)
return;
f[entry].SetMessage(fmIndex, (ushort)NUD_FacilityMessage.Value);
}
private void HexTextBox_TextChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
string t = Util.GetOnlyHex(((TextBox)sender).Text);
if (string.IsNullOrWhiteSpace(t))
t = "0";
@@ -458,11 +473,15 @@ private void HexTextBox_TextChanged(object sender, EventArgs e)
private void NUD_FacilityColor_ValueChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
f[entry].Color = (byte)NUD_FacilityColor.Value;
int type = TypeIndexToType(CB_FacilityType.SelectedIndex);
if (type < 0) return;
if (type < 0)
return;
editing = true;
LoadColorLabel(type);
editing = false;
@@ -470,10 +489,15 @@ private void NUD_FacilityColor_ValueChanged(object sender, EventArgs e)
private void CB_FacilityType_SelectedIndexChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
int typeIndex = CB_FacilityType.SelectedIndex;
if (typeIndex < 0) return;
if (typeIndex < 0)
return;
var facility = f[entry];
facility.Type = typeIndex;
// reset color
@@ -575,32 +599,45 @@ private void NUD_Rank_ValueChanged(object sender, EventArgs e)
private void NUD_MyMessage_ValueChanged(object sender, EventArgs e)
{
if (editing) return;
if (editing)
return;
int mmIndex = Array.IndexOf(NUD_Messages, (NumericUpDown)sender);
if (mmIndex < 0) return;
if (mmIndex < 0)
return;
SAV.Festa.SetFestaMessage(mmIndex, (ushort)((NumericUpDown)sender).Value);
}
private void CHK_FacilityIntroduced_CheckedChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
f[entry].IsIntroduced = CHK_FacilityIntroduced.Checked;
}
private void TB_OTName_TextChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
f[entry].OT_Name = TB_OTName.Text;
}
private void LB_FacilityIndex_SelectedIndexChanged(object sender, EventArgs e)
{
if (editing) return;
if (editing)
return;
SaveFacility();
entry = LB_FacilityIndex.SelectedIndex;
if (entry < 0) return;
if (entry < 0)
return;
LoadFacility();
}
@@ -652,7 +689,8 @@ private void MnuSave_Click(object sender, EventArgs e)
{
sender = WinFormsUtil.GetUnderlyingControl(sender);
int i = Array.IndexOf(PBs, sender);
if (i < 0) return;
if (i < 0)
return;
WinFormsUtil.SavePKMDialog(p[i]);
}
@@ -669,17 +707,22 @@ private void NUD_Grade_ValueChanged(object sender, EventArgs e)
private void NUD_Exchangable_ValueChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
f[entry].ExchangeLeftCount = (byte)NUD_Exchangable.Value;
}
private void CB_LuckyResult_SelectedIndexChanged(object sender, EventArgs e)
{
if (editing) return;
if (entry < 0) return;
if (editing)
return;
if (entry < 0)
return;
int lucky = CB_LuckyResult.SelectedIndex;
if (lucky-- < 0) return;
if (lucky-- < 0)
return;
// both 0 if "none"
f[entry].UsedLuckyRank = lucky < 0 ? 0 : (lucky % 3) + 1;
f[entry].UsedLuckyPlace = lucky < 0 ? 0 : (lucky / 3) + 1;

View File

@@ -449,10 +449,12 @@ private DialogResult ModifyHeldItem()
private void B_Delete_Click(object sender, EventArgs e)
{
if (entry < 0) return;
if (entry < 0)
return;
if (entry < p.Count)
{
if (ModifyHeldItem() == DialogResult.Cancel) return;
if (ModifyHeldItem() == DialogResult.Cancel)
return;
}
switch (m[entry])
{

View File

@@ -246,7 +246,8 @@ private void B_Save_Click(object sender, EventArgs e)
// Delete Received Flag
private void ClearRecievedFlag(object sender, EventArgs e)
{
if (LB_Received.SelectedIndex < 0) return;
if (LB_Received.SelectedIndex < 0)
return;
if (LB_Received.Items.Count > 0)
LB_Received.Items.Remove(LB_Received.Items[LB_Received.SelectedIndex]);