diff --git a/PKHeX.Core/Editing/Bulk/BatchEditing.cs b/PKHeX.Core/Editing/Bulk/BatchEditing.cs index 21451801b..91a9fe6f9 100644 --- a/PKHeX.Core/Editing/Bulk/BatchEditing.cs +++ b/PKHeX.Core/Editing/Bulk/BatchEditing.cs @@ -170,7 +170,10 @@ public static bool IsFilterMatch(IEnumerable filters, object if (pi.IsValueEqual(obj, cmd.PropertyValue) == cmd.Evaluator) continue; } +#pragma warning disable CA1031 // Do not catch general exception types + // User provided inputs can mismatch the type's required value format, and fail to be compared. catch (Exception e) +#pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}."); Debug.WriteLine(e.Message); @@ -214,7 +217,10 @@ internal static ModifyResult TryModifyPKM(PKM pk, IEnumerable if (!IsFilterMatch(cmd, info, pi)) return ModifyResult.Filtered; } +#pragma warning disable CA1031 // Do not catch general exception types + // Swallow any error because this can be malformed user input. catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine(MsgBEModifyFailCompare + " " + ex.Message, cmd.PropertyName, cmd.PropertyValue); return ModifyResult.Error; diff --git a/PKHeX.Core/MysteryGifts/MysteryUtil.cs b/PKHeX.Core/MysteryGifts/MysteryUtil.cs index d57a992c6..a8a7a125c 100644 --- a/PKHeX.Core/MysteryGifts/MysteryUtil.cs +++ b/PKHeX.Core/MysteryGifts/MysteryUtil.cs @@ -59,7 +59,9 @@ public static IEnumerable GetDescription(this MysteryGift gift, IBasicSt { AddLinesPKM(gift, strings, result); } +#pragma warning disable CA1031 // Do not catch general exception types catch { result.Add(MsgMysteryGiftParseFail); } +#pragma warning restore CA1031 // Do not catch general exception types } else if (gift.IsBP) { diff --git a/PKHeX.Core/PKM/PKM.cs b/PKHeX.Core/PKM/PKM.cs index 656d90c3d..eb38ec891 100644 --- a/PKHeX.Core/PKM/PKM.cs +++ b/PKHeX.Core/PKM/PKM.cs @@ -1075,15 +1075,14 @@ public void TransferPropertiesWithReflection(PKM Destination) public int GetMove(int index) { - switch (index) + return index switch { - case 0: return Move1; - case 1: return Move2; - case 2: return Move3; - case 3: return Move4; - default: - throw new IndexOutOfRangeException(nameof(index)); - } + 0 => Move1, + 1 => Move2, + 2 => Move3, + 3 => Move4, + _ => throw new IndexOutOfRangeException(nameof(index)), + }; } public void SetMove(int index, int value) diff --git a/PKHeX.Core/PKM/Util/QRMessageUtil.cs b/PKHeX.Core/PKM/Util/QRMessageUtil.cs index de32ae5ea..da02399b8 100644 --- a/PKHeX.Core/PKM/Util/QRMessageUtil.cs +++ b/PKHeX.Core/PKM/Util/QRMessageUtil.cs @@ -94,7 +94,9 @@ public static string GetMessageBase64(byte[] data, string server) url = url.Substring(payloadBegin + 1); // Trim URL to right after # return Convert.FromBase64String(url); } +#pragma warning disable CA1031 // Do not catch general exception types catch +#pragma warning restore CA1031 // Do not catch general exception types { return null; } diff --git a/PKHeX.Core/Saves/Access/SCBlockAccessor.cs b/PKHeX.Core/Saves/Access/SCBlockAccessor.cs index a727dab3f..5ea64e855 100644 --- a/PKHeX.Core/Saves/Access/SCBlockAccessor.cs +++ b/PKHeX.Core/Saves/Access/SCBlockAccessor.cs @@ -16,10 +16,17 @@ public abstract class SCBlockAccessor : ISaveBlockAccessor // Binary Search doesn't require extra memory like a Dictionary would; also, we only need to find a few blocks. public SCBlock GetBlock(uint key) => BinarySearch(BlockInfo, key); + /// + /// Tries to grab the actual block, and returns a new dummy if the block does not exist. + /// + /// Block Key + /// Block if exists, dummy if not. Dummy key will not match requested key. public SCBlock GetBlockSafe(uint key) { try { return GetBlock(key); } +#pragma warning disable CA1031 // Do not catch general exception types catch (KeyNotFoundException) { return new SCBlock(0); } +#pragma warning restore CA1031 // Do not catch general exception types } private static SCBlock BinarySearch(IReadOnlyList arr, uint key) diff --git a/PKHeX.Core/Saves/MemeCrypto/SCBlockUtil.cs b/PKHeX.Core/Saves/MemeCrypto/SCBlockUtil.cs index 0942aad3a..eb10ce80d 100644 --- a/PKHeX.Core/Saves/MemeCrypto/SCBlockUtil.cs +++ b/PKHeX.Core/Saves/MemeCrypto/SCBlockUtil.cs @@ -85,7 +85,9 @@ public static List ImportBlocksFromFolder(string path, SAV8SWSH sav) var data = File.ReadAllBytes(f); data.CopyTo(block.Data, 0); } +#pragma warning disable CA1031 // Do not catch general exception types catch +#pragma warning restore CA1031 // Do not catch general exception types { failed.Add(fn); } diff --git a/PKHeX.Core/Saves/Util/BoxUtil.cs b/PKHeX.Core/Saves/Util/BoxUtil.cs index f1dcb323b..f200f671c 100644 --- a/PKHeX.Core/Saves/Util/BoxUtil.cs +++ b/PKHeX.Core/Saves/Util/BoxUtil.cs @@ -198,5 +198,32 @@ private static IEnumerable GetPossiblePKMsFromPaths(SaveFile sav, IEnumerab } } } + + /// + /// Gets box names for all boxes in the save file. + /// + /// that box names are being dumped for. + /// Returns default English box names in the event the save file does not have names (not exportable), or fails to return a box name. + public static string[] GetBoxNames(SaveFile sav) + { + int count = sav.BoxCount; + var result = new string[count]; + if (!sav.Exportable) + { + for (int i = 0; i < count; i++) + result[i] = $"Box {i + 1}"; + return result; + } + + for (int i = 0; i < count; i++) + { + try { result[i] = sav.GetBoxName(i); } +#pragma warning disable CA1031 // Do not catch general exception types + catch { result[i] = $"Box {i + 1}"; } +#pragma warning restore CA1031 // Do not catch general exception types + } + + return result; + } } } diff --git a/PKHeX.Core/Saves/Util/SaveUtil.cs b/PKHeX.Core/Saves/Util/SaveUtil.cs index 524aba0bc..c3e40ac79 100644 --- a/PKHeX.Core/Saves/Util/SaveUtil.cs +++ b/PKHeX.Core/Saves/Util/SaveUtil.cs @@ -691,15 +691,12 @@ public static bool GetSavesFromFolder(string folderPath, bool deep, out IEnumera // force evaluation so that an invalid path will throw before we return true/false. // EnumerateFiles throws an exception while iterating, which won't be caught by the try-catch here. var files = Directory.GetFiles(folderPath, "*", searchOption); - static int GetFileSize(string file) - { - try { return (int) new FileInfo(file).Length; } - catch { return -1; } // Bad File / Locked - } - result = files.Where(f => IsSizeValid(GetFileSize(f))); + result = files.Where(f => IsSizeValid(FileUtil.GetFileSize(f))); return true; } +#pragma warning disable CA1031 // Do not catch general exception types catch (ArgumentException) +#pragma warning restore CA1031 // Do not catch general exception types { result = new[] { MsgFileLoadFailAuto + Environment.NewLine + folderPath, MsgFileLoadFailAutoAdvise + Environment.NewLine + MsgFileLoadFailAutoCause }; return false; diff --git a/PKHeX.Core/Util/DataUtil.cs b/PKHeX.Core/Util/DataUtil.cs index bf2bc5c71..5f747e8d6 100644 --- a/PKHeX.Core/Util/DataUtil.cs +++ b/PKHeX.Core/Util/DataUtil.cs @@ -248,7 +248,10 @@ private static void SetLocalization(Type t, IReadOnlyCollection lines) { ReflectUtil.SetValue(t, prop, value); } +#pragma warning disable CA1031 // Do not catch general exception types + // Malformed translation files, log catch (Exception e) +#pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine($"Property not present: {prop} || Value written: {value}"); Debug.WriteLine(e.Message); diff --git a/PKHeX.Core/Util/FileUtil.cs b/PKHeX.Core/Util/FileUtil.cs index efbeff1c9..ed703e961 100644 --- a/PKHeX.Core/Util/FileUtil.cs +++ b/PKHeX.Core/Util/FileUtil.cs @@ -28,7 +28,10 @@ public static class FileUtil var ext = Path.GetExtension(path); return GetSupportedFile(data, ext, reference); } +#pragma warning disable CA1031 // Do not catch general exception types + // User input data can be fuzzed; if anything blows up, just fail safely. catch (Exception e) +#pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine(MessageStrings.MsgFileInUse); Debug.WriteLine(e.Message); @@ -62,6 +65,28 @@ public static class FileUtil return null; } + public static bool IsFileLocked(string path) + { + try { return (File.GetAttributes(path) & FileAttributes.ReadOnly) != 0; } +#pragma warning disable CA1031 // Do not catch general exception types + catch { return true; } +#pragma warning restore CA1031 // Do not catch general exception types + } + + public static int GetFileSize(string path) + { + try + { + var size = new FileInfo(path).Length; + if (size > int.MaxValue) + return -1; + return (int)size; + } +#pragma warning disable CA1031 // Do not catch general exception types + catch { return -1; } // Bad File / Locked +#pragma warning restore CA1031 // Do not catch general exception types + } + private static bool TryGetGP1(byte[] data, out GP1? gp1) { gp1 = null; @@ -77,7 +102,7 @@ private static bool TryGetGP1(byte[] data, out GP1? gp1) /// File size public static bool IsFileTooBig(long length) { - if (length <= 0x100000) + if (length <= 0x10_0000) // 10 MB return false; if (length == SaveUtil.SIZE_G4BR || length == SaveUtil.SIZE_G8SWSH || length == SaveUtil.SIZE_G8SWSH_1 || length == SaveUtil.SIZE_G8SWSH_2 || length == SaveUtil.SIZE_G8SWSH_2B) return false; @@ -90,7 +115,7 @@ public static bool IsFileTooBig(long length) /// Checks if the length is too small to be a detectable file. /// /// File size - public static bool IsFileTooSmall(long length) => length < 0x20; + public static bool IsFileTooSmall(long length) => length < 0x20; // bigger than PK1 /// /// Tries to get an object from the input parameters. diff --git a/PKHeX.Core/Util/NetUtil.cs b/PKHeX.Core/Util/NetUtil.cs index bb1dc200d..e95aa270e 100644 --- a/PKHeX.Core/Util/NetUtil.cs +++ b/PKHeX.Core/Util/NetUtil.cs @@ -16,7 +16,10 @@ public static class NetUtil using var reader = new StreamReader(stream); return reader.ReadToEnd(); } +#pragma warning disable CA1031 // Do not catch general exception types + // No internet? catch (Exception e) +#pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine(e.Message); return null; diff --git a/PKHeX.Core/Util/ReflectUtil.cs b/PKHeX.Core/Util/ReflectUtil.cs index 601c3e6a6..c0d916050 100644 --- a/PKHeX.Core/Util/ReflectUtil.cs +++ b/PKHeX.Core/Util/ReflectUtil.cs @@ -113,6 +113,13 @@ public static IEnumerable GetAllTypeInfo(this TypeInfo? typeInfo) } } + /// + /// Checks if the has the requested property . + /// + /// Object to check for property existence. + /// Name of the property. + /// Reference to the property info for the object, if it exists. + /// True if has property, and false if does not have property. is null when returning false. public static bool HasProperty(object obj, string name, out PropertyInfo? pi) => (pi = GetPropertyInfo(obj.GetType().GetTypeInfo(), name)) != null; public static PropertyInfo? GetPropertyInfo(this TypeInfo typeInfo, string name) diff --git a/PKHeX.Drawing/QR/QRDecode.cs b/PKHeX.Drawing/QR/QRDecode.cs index cf471c34b..8cb7ef476 100644 --- a/PKHeX.Drawing/QR/QRDecode.cs +++ b/PKHeX.Drawing/QR/QRDecode.cs @@ -36,7 +36,9 @@ public static QRDecodeResult GetQRData(string address, out byte[] result) if (data.Contains("filetype not supported")) return QRDecodeResult.BadType; } +#pragma warning disable CA1031 // Do not catch general exception types catch { return QRDecodeResult.BadConnection; } +#pragma warning restore CA1031 // Do not catch general exception types // Quickly convert the json response to a data string try @@ -44,7 +46,9 @@ public static QRDecodeResult GetQRData(string address, out byte[] result) result = DecodeQRJson(data); return QRDecodeResult.Success; } +#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(e.Message); return QRDecodeResult.BadConversion; diff --git a/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs index 83c7dedaf..68e5ce200 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/BoxEditor.cs @@ -145,33 +145,14 @@ public void ResetBoxNames(int box = -1) { if (!SAV.HasBox) return; - if (!SAV.Exportable) - { - getBoxNamesDefault(); - } - else - { - try { getBoxNamesFromSave(); } - catch { getBoxNamesDefault(); } - } + + CB_BoxSelect.Items.Clear(); + CB_BoxSelect.Items.AddRange(BoxUtil.GetBoxNames(SAV)); if (box < 0 && (uint)SAV.CurrentBox < CB_BoxSelect.Items.Count) CurrentBox = SAV.CurrentBox; // restore selected box else CurrentBox = box; - - void getBoxNamesFromSave() - { - CB_BoxSelect.Items.Clear(); - for (int i = 0; i < SAV.BoxCount; i++) - CB_BoxSelect.Items.Add(SAV.GetBoxName(i)); - } - void getBoxNamesDefault() - { - CB_BoxSelect.Items.Clear(); - for (int i = 0; i < SAV.BoxCount; i++) - CB_BoxSelect.Items.Add($"Box {i+1}"); - } } public void ResetSlots() diff --git a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs index 5a74d9974..7fda57e30 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SlotChangeManager.cs @@ -172,9 +172,12 @@ private string CreateDragDropPKM(PictureBox pb, bool encrypt, out bool external) var data = encrypt ? pk.EncryptedPartyData : pk.DecryptedPartyData; external = TryMakeDragDropPKM(pb, data, newfile); } +#pragma warning disable CA1031 // Do not catch general exception types + // Tons of things can happen with drag & drop; don't try to handle things, just indicate failure. catch (Exception x) +#pragma warning restore CA1031 // Do not catch general exception types { - WinFormsUtil.Error("Drag & Drop Error", x); + WinFormsUtil.Error("Drag && Drop Error", x); external = false; } diff --git a/PKHeX.WinForms/Controls/Slots/CryPlayer.cs b/PKHeX.WinForms/Controls/Slots/CryPlayer.cs index 05b7f759d..e8bccc2a0 100644 --- a/PKHeX.WinForms/Controls/Slots/CryPlayer.cs +++ b/PKHeX.WinForms/Controls/Slots/CryPlayer.cs @@ -21,7 +21,9 @@ public void PlayCry(PKM pk) Sounds.SoundLocation = path; try { Sounds.Play(); } +#pragma warning disable CA1031 // Do not catch general exception types catch { Debug.WriteLine("Failed to play sound."); } +#pragma warning restore CA1031 // Do not catch general exception types } public void Stop() => Sounds.Stop(); diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 0a0a3a12a..192cfac7a 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -837,19 +837,13 @@ private static bool TryBackupExportCheck(SaveFile sav, string path) if (sav.Exportable && Directory.Exists(BackupPath) && !File.Exists(backupName)) File.WriteAllBytes(backupName, sav.BAK); - if (!IsFileLocked(path)) + if (!FileUtil.IsFileLocked(path)) return true; WinFormsUtil.Alert(MsgFileWriteProtected + Environment.NewLine + path, MsgFileWriteProtectedAdvice); return false; } - private static bool IsFileLocked(string path) - { - try { return (File.GetAttributes(path) & FileAttributes.ReadOnly) != 0; } - catch { return true; } - } - private static bool SanityCheckSAV(ref SaveFile sav) { ParseSettings.InitFromSaveFileData(sav); // physical GB, no longer used in logic @@ -1121,7 +1115,10 @@ private void Dragout_MouseDown(object sender, MouseEventArgs e) C_SAV.M.Drag.Info.Cursor = Cursor = new Cursor(((Bitmap)pb.Image).GetHicon()); DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move); } +#pragma warning disable CA1031 // Do not catch general exception types + // Tons of things can happen with drag & drop; don't try to handle things, just indicate failure. catch (Exception x) +#pragma warning restore CA1031 // Do not catch general exception types { WinFormsUtil.Error("Drag && Drop Error", x); } C_SAV.M.Drag.ResetCursor(this); File.Delete(newfile); @@ -1174,7 +1171,10 @@ private static void SaveSettings() settings.Draw = Draw.ToString(); settings.Save(); } +#pragma warning disable CA1031 // Do not catch general exception types catch (Exception x) + // Config might be corrupted, or their dotnet runtime is insufficient (<4.6?) +#pragma warning restore CA1031 // Do not catch general exception types { File.WriteAllLines("config error.txt", new[] {x.ToString()}); } @@ -1191,11 +1191,11 @@ private void ClickExportSAVBAK(object sender, EventArgs e) private void ClickExportSAV(object sender, EventArgs e) { - if (Menu_ExportSAV.Enabled) - { - C_SAV.ExportSaveFile(); - Text = GetProgramTitle(C_SAV.SAV); - } + if (!Menu_ExportSAV.Enabled) + return; // hot-keys can't cheat the system! + + C_SAV.ExportSaveFile(); + Text = GetProgramTitle(C_SAV.SAV); } private void ClickSaveFileName(object sender, EventArgs e) @@ -1227,7 +1227,11 @@ private static void PromptBackup() Directory.CreateDirectory(BackupPath); WinFormsUtil.Alert(MsgBackupSuccess, string.Format(MsgBackupDelete, BackupPath)); } - catch (Exception ex) { WinFormsUtil.Error($"{MsgBackupUnable} @ {BackupPath}", ex); } +#pragma warning disable CA1031 // Do not catch general exception types + catch (Exception ex) + // Maybe they put their exe in a folder that we can't create files/folders to. +#pragma warning restore CA1031 // Do not catch general exception types + { WinFormsUtil.Error($"{MsgBackupUnable} @ {BackupPath}", ex); } } private void ClickUndo(object sender, EventArgs e) => C_SAV.ClickUndo(); diff --git a/PKHeX.WinForms/MainWindow/PluginLoader.cs b/PKHeX.WinForms/MainWindow/PluginLoader.cs index 611c570bf..3248779d3 100644 --- a/PKHeX.WinForms/MainWindow/PluginLoader.cs +++ b/PKHeX.WinForms/MainWindow/PluginLoader.cs @@ -49,7 +49,10 @@ private static IEnumerable GetPluginTypes(Assembly z, Type pluginType) var types = z.GetTypes(); return types.Where(type => IsTypePlugin(type, pluginType)); } +#pragma warning disable CA1031 // Do not catch general exception types + // User plugins can be out of date, with mismatching API surfaces. catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message); return Enumerable.Empty(); diff --git a/PKHeX.WinForms/Misc/QR.cs b/PKHeX.WinForms/Misc/QR.cs index cee464825..ced08c728 100644 --- a/PKHeX.WinForms/Misc/QR.cs +++ b/PKHeX.WinForms/Misc/QR.cs @@ -77,7 +77,10 @@ private void PB_QR_Click(object sender, EventArgs e) if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgQRClipboardImage)) return; try { Clipboard.SetImage(PB_QR.Image); } +#pragma warning disable CA1031 // Do not catch general exception types + // Clipboard can be locked periodically, just notify on failure. catch { WinFormsUtil.Alert(MsgQRClipboardFail); } +#pragma warning restore CA1031 // Do not catch general exception types } private void UpdateBoxSlotCopies(object sender, EventArgs e) diff --git a/PKHeX.WinForms/Subforms/SAV_Database.cs b/PKHeX.WinForms/Subforms/SAV_Database.cs index 2f36a7411..c4d4ea1a8 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.cs @@ -321,7 +321,9 @@ private void LoadDatabase() while (!IsHandleCreated) { } BeginInvoke(new MethodInvoker(() => SetResults(RawDB))); } +#pragma warning disable CA1031 // Do not catch general exception types catch { /* Window Closed? */ } +#pragma warning restore CA1031 // Do not catch general exception types } private static List LoadPKMSaves(string pkmdb, string savdb, string EXTERNAL_SAV, SaveFile SAV) @@ -635,8 +637,14 @@ private void Menu_DeleteClones_Click(object sender, EventArgs e) var clones = SearchUtil.GetExtraClones(db); foreach (var pk in clones) { - try { File.Delete(pk.Identifier); ++deleted; } + var path = pk.Identifier; + if (path == null || !File.Exists(path)) + continue; + + try { File.Delete(path); ++deleted; } +#pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { WinFormsUtil.Error(MsgDBDeleteCloneFail + Environment.NewLine + ex.Message + Environment.NewLine + pk.Identifier); } +#pragma warning restore CA1031 // Do not catch general exception types } if (deleted == 0) diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Misc4.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Misc4.cs index 329ea3cac..aa7425de1 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Misc4.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen4/SAV_Misc4.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; -using System.IO; using System.Linq; using System.Windows.Forms; using PKHeX.Core; @@ -278,19 +277,16 @@ private void SetPictureBoxFromFlags(byte[] inp) private void SetFlagsFromFileName(string inpFileName) { - if (new FileInfo(inpFileName).Length > 2058) return; // 24*20*4(ARGB)=1920 - Bitmap bmp; - FileStream fs = new FileStream(inpFileName, FileMode.Open, FileAccess.Read); - try - { - bmp = (Bitmap)Image.FromStream(fs); - } - catch - { - bmp = null; - } - fs.Close(); - if (bmp == null || bmp.Width != 24 || bmp.Height != 20) return; + if (FileUtil.GetFileSize(inpFileName) > 2058) + return; // 24*20*4(ARGB)=1920 + + Bitmap bmp; try { bmp = (Bitmap)Image.FromFile(inpFileName); } +#pragma warning disable CA1031 // Do not catch general exception types + catch { return; } +#pragma warning restore CA1031 // Do not catch general exception types + + if (bmp.Width != 24 || bmp.Height != 20) + return; byte[] BrightMap = new byte[480]; byte[] BrightCount = new byte[0x100]; @@ -306,7 +302,8 @@ private void SetFlagsFromFileName(string inpFileName) } int ColorCount = BrightCount.Count(v => v > 0); - if (ColorCount > 4 || ColorCount == 0) return; + if (ColorCount > 4 || ColorCount == 0) + return; int errmin = int.MaxValue; byte[] LCT = new byte[4]; byte[] mLCT = new byte[4]; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs index 2d7e11887..bd7894296 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen5/SAV_CGearSkin.cs @@ -45,7 +45,9 @@ private void B_ImportPNG_Click(object sender, EventArgs e) bg = CGearImage.GetCGearBackground(img); PB_Background.Image = CGearImage.GetBitmap(bg); } +#pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { WinFormsUtil.Error(ex.Message); } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs index afa84bd50..acc23fafe 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs @@ -100,7 +100,9 @@ private void DropClick(object sender, DataGridViewCellEventArgs e) ComboBox comboBox = (ComboBox)dataGridView1.EditingControl; comboBox.DroppedDown = true; } +#pragma warning disable CA1031 // Do not catch general exception types catch { Console.WriteLine("Failed to modify item."); } +#pragma warning restore CA1031 // Do not catch general exception types } private bool loading = true; diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs index 5f00e6a2c..b3b4c70fc 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs @@ -129,7 +129,10 @@ private void GetTextBoxes() NUD_Y.Value = (decimal)SAV.Situation.Y; NUD_R.Value = (decimal)SAV.Situation.R; } +#pragma warning disable CA1031 // Do not catch general exception types + // Sometimes the coordinates aren't really decimal/float coordinates? catch { GB_Map.Enabled = false; } +#pragma warning restore CA1031 // Do not catch general exception types // Load Play Time MT_Hours.Text = SAV.PlayedHours.ToString(); diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs index 184bc3375..87ad5d767 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_EventFlags.cs @@ -223,11 +223,13 @@ private void AddConstList(string[] list) desc.Add(split[1]); enums.Add(split.Length == 3 ? split[2] : string.Empty); } +#pragma warning disable CA1031 // Do not catch general exception types catch { // Ignore bad user values Debug.WriteLine(string.Concat(split)); } +#pragma warning restore CA1031 // Do not catch general exception types } if (num.Count == 0) { diff --git a/PKHeX.WinForms/Subforms/SettingsEditor.cs b/PKHeX.WinForms/Subforms/SettingsEditor.cs index dd5916615..a9e29e903 100644 --- a/PKHeX.WinForms/Subforms/SettingsEditor.cs +++ b/PKHeX.WinForms/Subforms/SettingsEditor.cs @@ -111,7 +111,9 @@ private static void DeleteSettings() System.Diagnostics.Process.Start(Application.ExecutablePath); Environment.Exit(0); } +#pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { WinFormsUtil.Error("Failed to delete settings.", ex.Message); } diff --git a/PKHeX.WinForms/Util/WinFormsTranslator.cs b/PKHeX.WinForms/Util/WinFormsTranslator.cs index ed78ef257..129ffd519 100644 --- a/PKHeX.WinForms/Util/WinFormsTranslator.cs +++ b/PKHeX.WinForms/Util/WinFormsTranslator.cs @@ -61,7 +61,9 @@ private static IEnumerable GetTranslationFile(string lang) if (File.Exists(externalLangPath)) { try { return File.ReadAllLines(externalLangPath); } +#pragma warning disable CA1031 // Do not catch general exception types catch { /* In use? Just return the internal resource. */ } +#pragma warning restore CA1031 // Do not catch general exception types } if (Util.IsStringListCached(file, out var result)) @@ -174,7 +176,10 @@ public static void LoadAllForms(params string[] banlist) { var _ = (Form)System.Activator.CreateInstance(t, new object[argCount]); } +#pragma warning disable CA1031 // Do not catch general exception types + // This is a debug utility method, will always be logging. Shouldn't ever fail. catch +#pragma warning restore CA1031 // Do not catch general exception types { Debug.Write($"Failed to create a new form {t}"); } diff --git a/PKHeX.WinForms/Util/WinFormsUtil.cs b/PKHeX.WinForms/Util/WinFormsUtil.cs index 18f6748e1..8d133caf4 100644 --- a/PKHeX.WinForms/Util/WinFormsUtil.cs +++ b/PKHeX.WinForms/Util/WinFormsUtil.cs @@ -139,7 +139,10 @@ internal static bool SetClipboardText(string text) { Error(MsgClipboardFailWrite, x); } +#pragma warning disable CA1031 // Do not catch general exception types + // Clipboard might be locked sometimes catch +#pragma warning restore CA1031 // Do not catch general exception types { Error(MsgClipboardFailWrite); }