Minor tweaks

Extract some interfaces
Suppress some warning messages with commented reasons if appropriate
This commit is contained in:
Kurt
2020-09-09 12:47:24 -07:00
parent 5d3bc289b6
commit 01fb233e48
37 changed files with 154 additions and 72 deletions

View File

@@ -71,15 +71,10 @@ private void VerifyIVsSlot(LegalityAnalysis data, EncounterSlot w)
{
case 6: VerifyIVsGen6(data, w); break;
case 7: VerifyIVsGen7(data); break;
case 8: VerifyIVsGen8(data); break;
// case 8: VerifyIVsGen8(data); break; // No rules for IV generation.
}
}
private void VerifyIVsGen8(LegalityAnalysis data)
{
// todo special rules
}
private void VerifyIVsGen7(LegalityAnalysis data)
{
var pkm = data.pkm;

View File

@@ -102,7 +102,7 @@ private void VerifyECPIDWurmple(LegalityAnalysis data)
}
}
private void VerifyEC(LegalityAnalysis data)
private static void VerifyEC(LegalityAnalysis data)
{
var pkm = data.pkm;
var Info = data.Info;
@@ -124,7 +124,7 @@ private void VerifyEC(LegalityAnalysis data)
// Gen1-2, Gen6+ should have PID != EC
if (pkm.PID == pkm.EncryptionConstant)
{
data.AddLine(GetInvalid(LPIDEqualsEC)); // better to flag than 1:2^32 odds since RNG is not feasible to yield match
data.AddLine(GetInvalid(LPIDEqualsEC, CheckIdentifier.EC)); // better to flag than 1:2^32 odds since RNG is not feasible to yield match
return;
}
@@ -137,7 +137,7 @@ private void VerifyEC(LegalityAnalysis data)
}
}
private void VerifyTransferEC(LegalityAnalysis data)
private static void VerifyTransferEC(LegalityAnalysis data)
{
var pkm = data.pkm;
// When transferred to Generation 6, the Encryption Constant is copied from the PID.

View File

@@ -281,13 +281,13 @@ public override IReadOnlyList<int> Relearn
public string GetOT(int language) => Util.TrimFromZero(Encoding.Unicode.GetString(Data, GetOTOffset(language), 0x1A));
public void SetOT(int language, string value) => Encoding.Unicode.GetBytes(value.PadRight(0x1A / 2, '\0')).CopyTo(Data, GetOTOffset(language));
private int GetNicknameOffset(int language)
private static int GetNicknameOffset(int language)
{
int index = GetLanguageIndex(language);
return 0x04 + (index * 0x1A);
}
private int GetOTOffset(int language)
private static int GetOTOffset(int language)
{
int index = GetLanguageIndex(language);
return 0xEE + (index * 0x1A);

View File

@@ -27,7 +27,7 @@ public sealed class CK3 : G3PKM, IShadowPKM
public override PKM Clone() => new CK3((byte[])Data.Clone()) {Identifier = Identifier};
private string GetString(int Offset, int Count) => StringConverter3.GetBEString3(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter3.SetBEString3(value, maxLength);
private static byte[] SetString(string value, int maxLength) => StringConverter3.SetBEString3(value, maxLength);
// Trash Bytes
public override byte[] Nickname_Trash { get => GetData(0x2E, 20); set { if (value.Length == 20) value.CopyTo(Data, 0x2E); } }

View File

@@ -431,13 +431,7 @@ public int[] Stats
public int[] Moves
{
get => new[] { Move1, Move2, Move3, Move4 };
set
{
Move1 = value.Length > 0 ? value[0] : 0;
Move2 = value.Length > 1 ? value[1] : 0;
Move3 = value.Length > 2 ? value[2] : 0;
Move4 = value.Length > 3 ? value[3] : 0;
}
set => SetMoves(value);
}
public void SetMoves(IReadOnlyList<int> value)

View File

@@ -104,7 +104,7 @@ protected T ConvertTo<T>() where T : G3PKM, new()
SID = SID,
EXP = EXP,
HeldItem = HeldItem,
AbilityNumber = AbilityNumber,
AbilityBit = AbilityBit,
IsEgg = IsEgg,
FatefulEncounter = FatefulEncounter,

View File

@@ -26,7 +26,7 @@ public sealed class XK3 : G3PKM, IShadowPKM
public override PKM Clone() => new XK3((byte[])Data.Clone()){Identifier = Identifier, Purification = Purification};
private string GetString(int Offset, int Count) => StringConverter3.GetBEString3(Data, Offset, Count);
private byte[] SetString(string value, int maxLength) => StringConverter3.SetBEString3(value, maxLength);
private static byte[] SetString(string value, int maxLength) => StringConverter3.SetBEString3(value, maxLength);
// Trash Bytes
public override byte[] Nickname_Trash { get => GetData(0x4E, 20); set { if (value.Length == 20) value.CopyTo(Data, 0x4E); } }

View File

@@ -140,6 +140,7 @@ public T Value
set => Parent.SetValue(_value = value);
}
// ReSharper disable once UnusedMember.Local
[Description("Type of Value this Block stores")]
public string ValueType => typeof(T).Name;

View File

@@ -8,7 +8,7 @@ namespace PKHeX.Core
/// <summary>
/// Base Class for Save Files
/// </summary>
public abstract class SaveFile : ITrainerInfo, IGameValueLimit
public abstract class SaveFile : ITrainerInfo, IGameValueLimit, IBoxDetailWallpaper, IBoxDetailName
{
// General Object Properties
public byte[] Data;

View File

@@ -1,6 +1,6 @@
namespace PKHeX.Core
{
public enum BattleStyle
public enum BattleStyle6
{
Single,
Double,
@@ -8,4 +8,4 @@ public enum BattleStyle
Rotation,
Multi,
}
}
}

View File

@@ -1,6 +1,6 @@
namespace PKHeX.Core
{
public sealed class BoxLayout6 : SaveBlock
public sealed class BoxLayout6 : SaveBlock, IBoxDetailName, IBoxDetailWallpaper
{
// gfstr5[31] boxNames;
// byte[31] wallpapers;
@@ -20,7 +20,7 @@ public sealed class BoxLayout6 : SaveBlock
public BoxLayout6(SAV6XY sav, int offset) : base(sav) => Offset = offset;
public BoxLayout6(SAV6AO sav, int offset) : base(sav) => Offset = offset;
public int GetBoxWallpaperOffset(int box) => Offset + PCBackgrounds + box;
public int GetBoxWallpaperOffset(int box) => Offset + PCBackgrounds + box;
public int GetBoxWallpaper(int box)
{

View File

@@ -7,7 +7,14 @@ public sealed class MaisonBlock : SaveBlock
public MaisonBlock(SAV6XY sav, int offset) : base(sav) => Offset = offset;
public MaisonBlock(SAV6AO sav, int offset) : base(sav) => Offset = offset;
public ushort GetMaisonStat(int index) { return BitConverter.ToUInt16(Data, Offset + 0x1C0 + (2 * index)); }
public void SetMaisonStat(int index, ushort value) { BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x1C0 + (2 * index)); }
// 5 * [u16*4: normal,super,normalStreak,superStreak]
public const int MaisonStatCount = 20;
public ushort GetMaisonStat(int index) => BitConverter.ToUInt16(Data, Offset + 0x1C0 + (2 * index));
public void SetMaisonStat(int index, ushort value) => BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x1C0 + (2 * index));
private static int GetMaisonStatIndex(BattleStyle6 type, bool streak, bool super) => ((int)type << 2) | (streak ? 2 : 0) | (super ? 1 : 0);
public ushort GetMaisonStat(BattleStyle6 type, bool streak, bool super) => GetMaisonStat(GetMaisonStatIndex(type, streak, super));
public void SetMaisonStat(BattleStyle6 type, bool streak, bool super, ushort value) => SetMaisonStat(GetMaisonStatIndex(type, streak, super), value);
}
}

View File

@@ -8,7 +8,8 @@ public sealed class MyItem6AO : MyItem
private const int Medicine = 0x970; // 3, +2 items shift because 2 HMs added
private const int Berry = 0xA70; // 4
public MyItem6AO(SAV6 SAV, int offset) : base(SAV) => Offset = offset;
public MyItem6AO(SAV6AO SAV, int offset) : base(SAV) => Offset = offset;
public MyItem6AO(SAV6AODemo SAV, int offset) : base(SAV) => Offset = offset;
public override InventoryPouch[] Inventory
{

View File

@@ -2,6 +2,9 @@
namespace PKHeX.Core
{
/// <summary>
/// SUBE block that stores in-game event results.
/// </summary>
public abstract class SubEventLog6 : SaveBlock, IGymTeamInfo
{
protected SubEventLog6(SAV6 sav, int offset) : base(sav) => Offset = offset;

View File

@@ -2,7 +2,7 @@
namespace PKHeX.Core
{
public sealed class BoxLayout7 : SaveBlock
public sealed class BoxLayout7 : SaveBlock, IBoxDetailName, IBoxDetailWallpaper, ITeamIndexSet
{
private const int BoxCount = 32;
@@ -118,6 +118,12 @@ public void SaveBattleTeams()
}
}
public void UnlockAllTeams()
{
for (int i = 0; i < TeamCount; i++)
SetIsTeamLocked(i, false);
}
public bool GetIsTeamLocked(int team) => Data[Offset + PCBackgrounds - TeamCount - team] == 1;
public void SetIsTeamLocked(int team, bool value) => Data[Offset + PCBackgrounds - TeamCount - team] = (byte)(value ? 1 : 0);

View File

@@ -1,6 +1,6 @@
namespace PKHeX.Core
{
public enum BattleStyle7
public enum PlayerBattleStyle7
{
Normal,
Elegant,

View File

@@ -1,6 +1,6 @@
namespace PKHeX.Core
{
public enum SkinColor7
public enum PlayerSkinColor7
{
M_Pale,
F_Pale,
@@ -11,4 +11,4 @@ public enum SkinColor7
M_Dark,
F_Dark,
}
}
}

View File

@@ -1,6 +1,6 @@
namespace PKHeX.Core
{
public sealed class BoxLayout8 : SaveBlock
public sealed class BoxLayout8 : SaveBlock, IBoxDetailName
{
public const int BoxCount = 32;

View File

@@ -5,6 +5,8 @@ namespace PKHeX.Core
{
public sealed class MyStatus8 : SaveBlock
{
public const uint MaxWatt = 9999999;
public MyStatus8(SAV8SWSH sav, SCBlock block) : base(sav, block.Data) { }
public string Number
@@ -176,7 +178,5 @@ public uint Watt
SAV.SetData(Data, BitConverter.GetBytes(value), Offset + 0xD0);
}
}
public uint MaxWatt => 9999999;
}
}

View File

@@ -1,5 +1,8 @@
namespace PKHeX.Core
{
/// <summary>
/// Contains information pertaining to Inventory Pouch capacity in Generation 8.
/// </summary>
public static class PouchSize8
{
/// <summary>
@@ -35,12 +38,12 @@ public static class PouchSize8
/// <summary>
/// Pouch5 Item Max Capacity
/// </summary>
public const int Treasures = 100; // todo
public const int Treasures = 100;
/// <summary>
/// Pouch5 Item Max Capacity
/// </summary>
public const int Ingredients = 100; // todo
public const int Ingredients = 100;
/// <summary>
/// Pouch5 Item Max Capacity

View File

@@ -3,10 +3,15 @@
namespace PKHeX.Core
{
public sealed class RentalTeam8
/// <summary>
/// Container block for Generation 8 saved Rental Teams
/// </summary>
public sealed class RentalTeam8 : IRentalTeam<PK8>
{
private const int LEN_META = 0x56;
private const int LEN_POKE = PokeCrypto.SIZE_8PARTY;
private const int LEN_STORED = PokeCrypto.SIZE_8STORED; // 0x148
private const int LEN_POKE = PokeCrypto.SIZE_8PARTY; // 0x158
private const int LEN_PARTYSTAT = LEN_POKE - PokeCrypto.SIZE_8STORED; // 0x10
private const int COUNT_POKE = 6;
private const int OFS_META = 0;
@@ -35,7 +40,8 @@ public void SetSlot(int slot, PK8 pkm)
{
var ofs = GetSlotOffset(slot);
var data = pkm.EncryptedPartyData;
Array.Clear(data, LEN_POKE - 0x10, 0x10);
// Wipe Party Stats
Array.Clear(data, LEN_STORED, LEN_PARTYSTAT);
data.CopyTo(Data, ofs);
}
@@ -63,4 +69,4 @@ public static int GetSlotOffset(int slot)
public byte[] GetMetadataStart() => Data.Slice(OFS_META, LEN_META);
public byte[] GetMetadataEnd() => Data.SliceEnd(POST_META);
}
}
}

View File

@@ -12,15 +12,5 @@ public sealed class Situation8 : SaveBlock
public float Z { get => BitConverter.ToSingle(Data, Offset + 0x10); set => BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x10); }
public float Y { get => (int)BitConverter.ToSingle(Data, Offset + 0x18); set => BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x18); }
public float R { get => (int)BitConverter.ToSingle(Data, Offset + 0x20); set => BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x20); }
public void UpdateOverworldCoordinates()
{
//var o = ((SAV8)SAV).Overworld;
//o.M = M;
//o.X = X;
//o.Z = Z;
//o.Y = Y;
//o.R = R;
}
}
}
}

View File

@@ -2,7 +2,7 @@
namespace PKHeX.Core
{
public sealed class TeamIndexes8 : SaveBlock
public sealed class TeamIndexes8 : SaveBlock, ITeamIndexSet
{
private const int TeamCount = 6;
private const int NONE_SELECTED = -1;

View File

@@ -0,0 +1,11 @@
namespace PKHeX.Core
{
/// <summary>
/// Provides details about box names within the save file.
/// </summary>
public interface IBoxDetailName
{
public string GetBoxName(int box);
public void SetBoxName(int box, string value);
}
}

View File

@@ -0,0 +1,11 @@
namespace PKHeX.Core
{
/// <summary>
/// Provides details about box wallpaper values within the save file.
/// </summary>
public interface IBoxDetailWallpaper
{
public int GetBoxWallpaper(int box);
public void SetBoxWallpaper(int box, int value);
}
}

View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
namespace PKHeX.Core
{
public interface IRentalTeam<T> where T : PKM
{
T GetSlot(int slot);
void SetSlot(int slot, T pkm);
T[] GetTeam();
void SetTeam(IReadOnlyList<T> team);
}
}

View File

@@ -0,0 +1,12 @@
namespace PKHeX.Core
{
public interface ITeamIndexSet
{
bool GetIsTeamLocked(int team);
void SetIsTeamLocked(int team, bool value);
void ClearBattleTeams();
void SaveBattleTeams();
void UnlockAllTeams();
}
}

View File

@@ -159,7 +159,9 @@ private void ApplyLine(string l)
pi.SetValue(this, value);
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
Debug.WriteLine($"Failed to write {name} to {value}!");
Debug.WriteLine(e.Message);

View File

@@ -231,16 +231,18 @@ private void FormLoadCheckForUpdates()
L_UpdateAvailable.Click += (sender, e) => Process.Start(ThreadPath);
Task.Run(() =>
{
try
{
var latestVersion = NetUtil.GetLatestPKHeXVersion();
if (latestVersion > CurrentProgramVersion)
Invoke((MethodInvoker)(() => NotifyNewVersionAvailable(latestVersion)));
}
Version latestVersion;
// User might not be connected to the internet or with a flaky connection.
try { latestVersion = NetUtil.GetLatestPKHeXVersion(); }
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
Debug.WriteLine($"Exception while checking for latest version: {ex}");
return;
}
if (latestVersion > CurrentProgramVersion)
Invoke((MethodInvoker)(() => NotifyNewVersionAvailable(latestVersion)));
});
}
@@ -565,14 +567,18 @@ private void OpenFromPath(string path)
return;
}
byte[] input; try { input = File.ReadAllBytes(path); }
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e) { WinFormsUtil.Error(MsgFileInUse + path, e); return; }
#pragma warning restore CA1031 // Do not catch general exception types
string ext = fi.Extension;
#if DEBUG
OpenFile(input, path, ext);
#else
try { OpenFile(input, path, ext); }
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e) { WinFormsUtil.Error(MsgFileLoadFail + "\nPath: " + path, e); }
#pragma warning restore CA1031 // Do not catch general exception types
#endif
}

View File

@@ -82,7 +82,9 @@ private void UpdateExceptionDetailsMessage()
details.AppendLine();
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
details.AppendLine("An error occurred while listing the Loaded Assemblies:");
details.AppendLine(ex.ToString());

View File

@@ -210,7 +210,9 @@ private void LoadDatabase()
PopulateComboBoxes();
}));
}
#pragma warning disable CA1031 // Do not catch general exception types
catch { /* Window Closed? */ }
#pragma warning restore CA1031 // Do not catch general exception types
}
// IO Usage

View File

@@ -128,7 +128,7 @@ private void GetTextBoxes()
// Maison Data
if (SAV is ISaveBlock6Main xyao)
{
for (int i = 0; i < MaisonRecords.Length; i++)
for (int i = 0; i < MaisonBlock.MaisonStatCount; i++)
MaisonRecords[i].Text = xyao.Maison.GetMaisonStat(i).ToString();
}
@@ -147,7 +147,10 @@ private void GetTextBoxes()
NUD_Z.Value = (decimal)sit.Z;
NUD_Y.Value = (decimal)sit.Y;
}
// If we can't accurately represent the coordinates, don't allow them to be changed.
#pragma warning disable CA1031 // Do not catch general exception types
catch { GB_Map.Enabled = false; }
#pragma warning restore CA1031 // Do not catch general exception types
}
// Load BP and PokeMiles
@@ -217,7 +220,7 @@ private void Save()
// Copy Maison Data in
if (SAV is ISaveBlock6Main xyao)
{
for (int i = 0; i < MaisonRecords.Length; i++)
for (int i = 0; i < MaisonBlock.MaisonStatCount; i++)
xyao.Maison.SetMaisonStat(i, ushort.Parse(MaisonRecords[i].Text));
}

View File

@@ -45,7 +45,7 @@ public SAV_Trainer7(SaveFile sav)
private readonly bool Loading;
private bool MapUpdated;
private static readonly string[] AllStyles = Enum.GetNames(typeof(BattleStyle7));
private static readonly string[] AllStyles = Enum.GetNames(typeof(PlayerBattleStyle7));
private readonly List<string> BattleStyles = new List<string>(AllStyles);
private int[] FlyDestFlagOfs, MapUnmaskFlagOfs;
@@ -65,7 +65,7 @@ private void GetComboBoxes()
Main.SetCountrySubRegion(CB_Country, "countries");
CB_SkinColor.Items.Clear();
CB_SkinColor.Items.AddRange(Enum.GetNames(typeof(SkinColor7)));
CB_SkinColor.Items.AddRange(Enum.GetNames(typeof(PlayerSkinColor7)));
L_Vivillon.Text = GameInfo.Strings.Species[(int)Species.Vivillon] + ":";
CB_Vivillon.InitializeBinding();

View File

@@ -22,7 +22,7 @@ public SAV_Trainer8(SaveFile sav)
}
B_MaxCash.Click += (sender, e) => MT_Money.Text = SAV.MaxMoney.ToString();
B_MaxWatt.Click += (sender, e) => MT_Watt.Text = SAV.MyStatus.MaxWatt.ToString();
B_MaxWatt.Click += (sender, e) => MT_Watt.Text = MyStatus8.MaxWatt.ToString();
CB_Gender.Items.Clear();
CB_Gender.Items.AddRange(Main.GenderSymbols.Take(2).ToArray()); // m/f depending on unicode selection

View File

@@ -151,7 +151,9 @@ private void AddFlagList(string[] list)
num.Add(n);
desc.Add(split[1]);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch
#pragma warning restore CA1031 // Do not catch general exception types
{
// Ignore bad user values
Debug.WriteLine(string.Concat(split));

View File

@@ -92,7 +92,10 @@ private void ViewGiftData(DataMysteryGift g)
PB_Preview.Image = g.Sprite();
mg = g;
}
// Some user input mystery gifts can have out-of-bounds values. Just swallow any exception.
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
WinFormsUtil.Error(MsgMysteryGiftParseTypeUnknown, e);
RTB.Clear();
@@ -392,7 +395,10 @@ private void BoxSlot_MouseDown(object sender, MouseEventArgs e)
File.WriteAllBytes(newfile, gift.Write());
DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move);
}
// Sometimes the drag-drop is canceled or ends up at a bad location. Don't bother recovering from an exception; just display a safe error message.
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception x)
#pragma warning restore CA1031 // Do not catch general exception types
{ WinFormsUtil.Error("Drag & Drop Error", x); }
File.Delete(newfile);
wc_slot = -1;
@@ -409,15 +415,19 @@ private void BoxSlot_DragDrop(object sender, DragEventArgs e)
if (wc_slot == -1) // dropped
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length < 1)
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files == null || files.Length == 0)
return;
if (!MysteryGift.IsMysteryGift(new FileInfo(files[0]).Length))
{ WinFormsUtil.Alert(MsgFileUnsupported, files[0]); return; }
byte[] data = File.ReadAllBytes(files[0]);
MysteryGift gift = MysteryGift.GetMysteryGift(data, new FileInfo(files[0]).Extension);
var first = files[0];
var fi = new FileInfo(first);
if (!MysteryGift.IsMysteryGift(fi.Length))
{ WinFormsUtil.Alert(MsgFileUnsupported, first); return; }
byte[] data = File.ReadAllBytes(first);
var gift = MysteryGift.GetMysteryGift(data, fi.Extension);
if (gift == null)
{ WinFormsUtil.Alert(MsgFileUnsupported, first); return; }
if (gift is PCD pcd && mga.Gifts[index] is PGT)
{

View File

@@ -25,7 +25,9 @@ static FontUtil()
catch (FileNotFoundException ex){
Debug.WriteLine($"Unable to read font file: {ex.Message}");
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
Debug.WriteLine($"Unable to add in-game font: {ex.Message}");
}