From 833483027f98e6b5f0cc0c539b57accacc19b007 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 29 Jul 2020 18:35:59 -0700 Subject: [PATCH] Minor clean memes --- NHSE.Injection/SysBot/USBBot.cs | 5 +- NHSE.Parsing/GameMSBTDumper.cs | 5 +- NHSE.Sprites/Item/ItemSprite.cs | 50 ++++++------------- NHSE.WinForms/Controls/ItemEditor.cs | 1 - .../Subforms/Program/ImageFetcher.cs | 46 ++++++++--------- 5 files changed, 42 insertions(+), 65 deletions(-) diff --git a/NHSE.Injection/SysBot/USBBot.cs b/NHSE.Injection/SysBot/USBBot.cs index 1859896..d8037ab 100644 --- a/NHSE.Injection/SysBot/USBBot.cs +++ b/NHSE.Injection/SysBot/USBBot.cs @@ -77,9 +77,8 @@ public void Disconnect() { if (SwDevice.IsOpen) { - IUsbDevice? wholeUsbDevice = SwDevice as IUsbDevice; - if (wholeUsbDevice != null) - wholeUsbDevice?.ReleaseInterface(0); + if (SwDevice is IUsbDevice wholeUsbDevice) + wholeUsbDevice.ReleaseInterface(0); SwDevice.Close(); } } diff --git a/NHSE.Parsing/GameMSBTDumper.cs b/NHSE.Parsing/GameMSBTDumper.cs index 206720b..af56994 100644 --- a/NHSE.Parsing/GameMSBTDumper.cs +++ b/NHSE.Parsing/GameMSBTDumper.cs @@ -17,7 +17,10 @@ public static string[] GetItemListResource(string msgPath) foreach (var item in list) result[item.Key] = item.Value; for (int i = 0; i < result.Length; i++) - result[i] ??= string.Empty; + { + if (result[i] == null) + result[i] = string.Empty; + } result[0] = "(None)"; result[5794] = "DIY recipe"; return result; diff --git a/NHSE.Sprites/Item/ItemSprite.cs b/NHSE.Sprites/Item/ItemSprite.cs index 741134b..bd204c4 100644 --- a/NHSE.Sprites/Item/ItemSprite.cs +++ b/NHSE.Sprites/Item/ItemSprite.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; @@ -42,9 +41,9 @@ public static Bitmap GetItemMarkup(Item item, Font font, int width, int height, if (id == Item.NONE) return null; - if (!GetItemImageSprite(id, out var path, count)) + if (!TryGetItemImageSprite(id, out var path, count)) { - if (!GetMenuIconSprite(id, out var img)) + if (!TryGetMenuIconSprite(id, out var img)) return Resources.leaf; else return img; @@ -63,17 +62,20 @@ public static Bitmap GetItemMarkup(Item item, Font font, int width, int height, } } - private static bool GetMenuIconSprite(ushort id, out Image? img) + private static bool TryGetMenuIconSprite(ushort id, out Image? img) { id = TryGetFieldItemId(id, ItemNames.Length); var iconType = ItemInfo.GetMenuIcon(id); - img = (Image?)Resources.ResourceManager.GetObject(iconType == ItemMenuIconType.Leaf ? iconType.ToString() + "1" : iconType.ToString()); // the 1 stops the original "leaf" being overwritten + + // the 1 stops the original "leaf" being overwritten + var name = iconType == ItemMenuIconType.Leaf ? $"{iconType}1" : iconType.ToString(); + + img = (Image?)Resources.ResourceManager.GetObject(name); return img != null; } - private static bool GetItemImageSprite(ushort id, out string? path, ushort count = 0) + private static bool TryGetItemImageSprite(ushort id, out string path, ushort count = 0) { - path = string.Empty; id = TryGetFieldItemId(id, ItemNames.Length); var name = $"{id:00000}_{count}"; @@ -87,7 +89,7 @@ private static bool GetItemImageSprite(ushort id, out string? path, ushort count return false; } - private static bool SpriteFileExists(string filename, out string? path) + private static bool SpriteFileExists(string filename, out string path) { path = Path.Combine(PlatformAppDataImagePath, filename + ".png"); return File.Exists(path); @@ -95,38 +97,18 @@ private static bool SpriteFileExists(string filename, out string? path) private static ushort TryGetFieldItemId(ushort id, int length) { - if (id >= length) - { - if (!FieldItemList.Items.TryGetValue(id, out var definition)) - return id; + if (id < length) + return id; + if (!FieldItemList.Items.TryGetValue(id, out var definition)) + return id; - var remap = definition.HeldItemId; - if (remap >= length) - return id; - - id = remap; - } - - return id; - } - - public static Bitmap? GetImage(Item item, Font font, int width, int height) - { - if (item.ItemId == Item.NONE) - return null; - - return CreateFake(item, font, width, height); + var remap = definition.HeldItemId; + return remap >= length ? id : remap; } private static readonly StringFormat Center = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; - public static Bitmap CreateFake(Item item, Font font, int width, int height) - { - var bmp = new Bitmap(width, height); - return CreateFake(item, font, width, height, bmp); - } - private static Bitmap CreateFake(Item item, Font font, int width, int height, Bitmap bmp) { using var gfx = Graphics.FromImage(bmp); diff --git a/NHSE.WinForms/Controls/ItemEditor.cs b/NHSE.WinForms/Controls/ItemEditor.cs index f3bb6df..35094b9 100644 --- a/NHSE.WinForms/Controls/ItemEditor.cs +++ b/NHSE.WinForms/Controls/ItemEditor.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Windows.Forms; using NHSE.Core; diff --git a/NHSE.WinForms/Subforms/Program/ImageFetcher.cs b/NHSE.WinForms/Subforms/Program/ImageFetcher.cs index 1982e07..313606e 100644 --- a/NHSE.WinForms/Subforms/Program/ImageFetcher.cs +++ b/NHSE.WinForms/Subforms/Program/ImageFetcher.cs @@ -4,10 +4,8 @@ using System.IO; using System.IO.Compression; using System.Net; -using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using NHSE.Core; using NHSE.Sprites; namespace NHSE.WinForms @@ -17,26 +15,28 @@ public sealed partial class ImageFetcher : Form private const string Filename = "image.zip"; private static string ZipFilePath { get => Path.Combine(ItemSprite.PlatformAppDataPath, Filename); } - private readonly List AllHosts; + private readonly IReadOnlyList AllHosts; public ImageFetcher() { InitializeComponent(); L_Status.Text = L_FileSize.Text = string.Empty; - AllHosts = new List(LoadHosts()); + var splitHosts = AllHosts = LoadHosts(); + + CB_HostSelect.Items.Clear(); + foreach (var host in splitHosts) + CB_HostSelect.Items.Add(CleanUrl(host)); + CB_HostSelect.SelectedIndex = 0; // set outside of initialise to update filesize via HEAD response CheckFileStatusLabel(); } - private string[] LoadHosts() + private static string[] LoadHosts() { - CB_HostSelect.Items.Clear(); var hosts = Properties.Resources.hosts_images; - var splitHosts = hosts.Split(new string[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); - foreach (var host in splitHosts) - CB_HostSelect.Items.Add(CleanUrl(host)); + var splitHosts = hosts.Split(new[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); return splitHosts; } @@ -55,12 +55,10 @@ private void B_Download_Click(object sender, EventArgs e) if (!Directory.Exists(path)) Directory.CreateDirectory(path); - using (var webClient = new WebClient()) - { - webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); - webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); - webClient.DownloadFileAsync(new Uri(hostSelected), ZipFilePath); - } + using var webClient = new WebClient(); + webClient.DownloadFileCompleted += Completed; + webClient.DownloadProgressChanged += ProgressChanged; + webClient.DownloadFileAsync(new Uri(hostSelected), ZipFilePath); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) @@ -130,9 +128,6 @@ private void SetUIDownloadState(bool val, bool success = false) private void CB_HostSelect_SelectedIndexChanged(object sender, EventArgs e) { - if (AllHosts == null) - return; - CheckNetworkFileSizeAsync(); } @@ -141,26 +136,25 @@ private async void CheckNetworkFileSizeAsync() L_FileSize.Text = string.Empty; try { - var webClient = new WebClient(); + using var webClient = new WebClient(); await webClient.OpenReadTaskAsync(new Uri(AllHosts[CB_HostSelect.SelectedIndex], UriKind.Absolute)); var totalSizeBytes = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]); var totalSizeMb = totalSizeBytes / 1e+6; - L_FileSize.Text = totalSizeMb.ToString("0.##") + "MB"; + L_FileSize.Text = $"{totalSizeMb:0.##}MB"; } - catch (Exception ex) +#pragma warning disable CA1031 // Do not catch general exception types + catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types { L_FileSize.Text = ex.Message; } } - private string CleanUrl(string url) + private static string CleanUrl(string url) { var uri = new Uri(url); - if (uri.Segments.Length < 2) - return url; - - return $"{uri.Host}/{uri.Segments[1]}"; + return uri.Segments.Length < 2 ? url : $"{uri.Host}/{uri.Segments[1]}"; } private bool CheckFileStatusLabel() => L_ImgStatus.Visible = ItemSprite.SingleSpriteExists;