From 422e674e23c67c254eff0861d09f8e7df086fb61 Mon Sep 17 00:00:00 2001 From: Emik Date: Thu, 9 May 2024 18:30:04 +0200 Subject: [PATCH 1/3] Avoid Console.WriteLine --- Quaver.API/Maps/AutoMod/AutoMod.cs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Quaver.API/Maps/AutoMod/AutoMod.cs b/Quaver.API/Maps/AutoMod/AutoMod.cs index 31823ce1b..22a8d3e72 100644 --- a/Quaver.API/Maps/AutoMod/AutoMod.cs +++ b/Quaver.API/Maps/AutoMod/AutoMod.cs @@ -99,16 +99,16 @@ private void LoadAudioTrackData() { var path = Qua.GetAudioPath(); - if (path == null || !File.Exists(path)) + if (!File.Exists(path)) return; try { AudioTrackInfo = new Track(path, true); } - catch (Exception e) + catch (Exception) { - Console.WriteLine(e); + // ignored } } @@ -419,16 +419,12 @@ private void DetectImageFileIssues(string item, string path, int maxSize, int mi try { using (var image = Image.Load(path)) - { if (image.Width < minWidth || image.Height < minHeight || image.Width > maxWidth || image.Height > maxHeight) Issues.Add(new AutoModIssueImageResolution(item, minWidth, minHeight, maxWidth, maxHeight)); - - image.Dispose(); - } } - catch (Exception e) + catch (Exception) { - Console.WriteLine(e); + // ignored } } From eb13497e15a609f4a95c3177dcd5db40c4d94408 Mon Sep 17 00:00:00 2001 From: Emik Date: Thu, 9 May 2024 18:35:36 +0200 Subject: [PATCH 2/3] Dispose after use --- Quaver.API/Maps/Qua.cs | 4 ++-- Quaver.API/Replays/Replay.cs | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Quaver.API/Maps/Qua.cs b/Quaver.API/Maps/Qua.cs index 74e1cf75d..c6e1ebbea 100644 --- a/Quaver.API/Maps/Qua.cs +++ b/Quaver.API/Maps/Qua.cs @@ -242,7 +242,7 @@ public bool EqualByValue(Qua other) /// public static Qua Parse(byte[] buffer, bool checkValidity = true) { - var input = new StringReader(Encoding.UTF8.GetString(buffer, 0, buffer.Length)); + using var input = new StringReader(Encoding.UTF8.GetString(buffer, 0, buffer.Length)); var deserializer = new DeserializerBuilder(); deserializer.IgnoreUnmatchedProperties(); @@ -361,7 +361,7 @@ public string Serialize() Bookmarks = null; var serializer = new Serializer(); - var stringWriter = new StringWriter {NewLine = "\r\n"}; + using var stringWriter = new StringWriter {NewLine = "\r\n"}; serializer.Serialize(stringWriter, this); var serialized = stringWriter.ToString(); diff --git a/Quaver.API/Replays/Replay.cs b/Quaver.API/Replays/Replay.cs index 515ce7773..289c24c93 100644 --- a/Quaver.API/Replays/Replay.cs +++ b/Quaver.API/Replays/Replay.cs @@ -231,28 +231,30 @@ public Replay(string path, bool readHeaderless = false) if (!readHeaderless) { - frames = Encoding.ASCII.GetString(LZMACoder.Decompress(br.BaseStream).ToArray()).Split(',').ToList(); + using var stream = LZMACoder.Decompress(br.BaseStream); + frames = Encoding.ASCII.GetString(stream.ToArray()).Split(',').ToList(); } else - { - frames = Encoding.ASCII.GetString(LZMACoder.Decompress(br.ReadBytes((int) br.BaseStream.Length))).Split(',').ToList(); - } + frames = Encoding.ASCII.GetString(LZMACoder.Decompress(br.ReadBytes((int)br.BaseStream.Length))).Split(',').ToList(); // Add all the replay frames to the object foreach (var frame in frames) - { try { // Split up the frame string by SongTime|KeyPressState var frameSplit = frame.Split('|'); - Frames.Add(new ReplayFrame(int.Parse(frameSplit[0]), (ReplayKeyPressState)Enum.Parse(typeof(ReplayKeyPressState), frameSplit[1]))); + Frames.Add( + new ReplayFrame( + int.Parse(frameSplit[0]), + (ReplayKeyPressState)Enum.Parse(typeof(ReplayKeyPressState), frameSplit[1]) + ) + ); } - catch (Exception e) + catch (Exception) { - continue; + // ignored } - } } } @@ -299,7 +301,9 @@ public void Write(string path) bw.Write(CountMiss); bw.Write(PauseCount); bw.Write(RandomizeModifierSeed); - bw.Write(StreamHelper.ConvertStreamToByteArray(LZMACoder.Compress(replayDataStream))); + + using var stream = LZMACoder.Compress(replayDataStream); + bw.Write(StreamHelper.ConvertStreamToByteArray(stream)); } } From 6537654df851ae4f588841e398f48a9a012c1002 Mon Sep 17 00:00:00 2001 From: Emik Date: Thu, 9 May 2024 19:18:25 +0200 Subject: [PATCH 3/3] Use TryParse over exception-wrapped Parse --- Quaver.API/Helpers/Drain.cs | 49 +++++++++++++++++++ Quaver.API/Maps/Structures/EditorLayerInfo.cs | 25 +++------- Quaver.API/Replays/Replay.cs | 24 +++------ 3 files changed, 65 insertions(+), 33 deletions(-) create mode 100644 Quaver.API/Helpers/Drain.cs diff --git a/Quaver.API/Helpers/Drain.cs b/Quaver.API/Helpers/Drain.cs new file mode 100644 index 000000000..c5d76ba83 --- /dev/null +++ b/Quaver.API/Helpers/Drain.cs @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: MPL-2.0 +using System; + +namespace Quaver.API.Helpers +{ + /// + /// Represents a sequence of items separated by a separator. + /// + /// The type of items. + public readonly ref struct Drain + where T : IEquatable + { + readonly ReadOnlySpan _remaining; + + readonly T _separator; + + /// + /// Creates a new instance. + /// + /// The remaining items. + /// The separator. + public Drain(ReadOnlySpan remaining, T separator) + { + _remaining = remaining; + _separator = separator; + } + + /// + /// Collects the next , + /// while also initializing the next . + /// + /// The next . + /// The rest as . + public void Deconstruct(out ReadOnlySpan next, out Drain rest) + { + var i = _remaining.IndexOf(_separator); + + if (i is -1) + { + next = _remaining; + rest = default; + return; + } + + next = _remaining[..i]; + rest = new Drain(_remaining[(i + 1)..], _separator); + } + } +} diff --git a/Quaver.API/Maps/Structures/EditorLayerInfo.cs b/Quaver.API/Maps/Structures/EditorLayerInfo.cs index b10719046..072317749 100644 --- a/Quaver.API/Maps/Structures/EditorLayerInfo.cs +++ b/Quaver.API/Maps/Structures/EditorLayerInfo.cs @@ -3,7 +3,7 @@ using System.Drawing; using MoonSharp.Interpreter; using MoonSharp.Interpreter.Interop; -using YamlDotNet.Serialization; +using Quaver.API.Helpers; namespace Quaver.API.Maps.Structures { @@ -31,22 +31,13 @@ public class EditorLayerInfo /// /// [MoonSharpVisible(false)] - public Color GetColor() - { - if (ColorRgb == null) - return Color.White; - - var split = ColorRgb.Split(','); - - try - { - return Color.FromArgb(byte.Parse(split[0]), byte.Parse(split[1]), byte.Parse(split[2])); - } - catch (Exception) - { - return Color.White; - } - } + public Color GetColor() => + new Drain(ColorRgb, ',') is var (tr, (tg, (tb, _))) && + byte.TryParse(tr, out var r) && + byte.TryParse(tg, out var g) && + byte.TryParse(tb, out var b) + ? Color.FromArgb(r, g, b) + : Color.White; /// /// By-value comparer, auto-generated by Rider. diff --git a/Quaver.API/Replays/Replay.cs b/Quaver.API/Replays/Replay.cs index 289c24c93..62a18d8de 100644 --- a/Quaver.API/Replays/Replay.cs +++ b/Quaver.API/Replays/Replay.cs @@ -239,22 +239,14 @@ public Replay(string path, bool readHeaderless = false) // Add all the replay frames to the object foreach (var frame in frames) - try - { - // Split up the frame string by SongTime|KeyPressState - var frameSplit = frame.Split('|'); - - Frames.Add( - new ReplayFrame( - int.Parse(frameSplit[0]), - (ReplayKeyPressState)Enum.Parse(typeof(ReplayKeyPressState), frameSplit[1]) - ) - ); - } - catch (Exception) - { - // ignored - } + { + // Split up the frame string by SongTime|KeyPressState + var (tTime, (tKeys, _)) = new Drain(frame, '|'); + + if (int.TryParse(tTime, out var time) && + Enum.TryParse(tKeys.ToString(), out ReplayKeyPressState keys)) + Frames.Add(new ReplayFrame(time, keys)); + } } }