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/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
}
}
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/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 515ce7773..62a18d8de 100644
--- a/Quaver.API/Replays/Replay.cs
+++ b/Quaver.API/Replays/Replay.cs
@@ -231,27 +231,21 @@ 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('|');
+ // Split up the frame string by SongTime|KeyPressState
+ var (tTime, (tKeys, _)) = new Drain(frame, '|');
- Frames.Add(new ReplayFrame(int.Parse(frameSplit[0]), (ReplayKeyPressState)Enum.Parse(typeof(ReplayKeyPressState), frameSplit[1])));
- }
- catch (Exception e)
- {
- continue;
- }
+ if (int.TryParse(tTime, out var time) &&
+ Enum.TryParse(tKeys.ToString(), out ReplayKeyPressState keys))
+ Frames.Add(new ReplayFrame(time, keys));
}
}
}
@@ -299,7 +293,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));
}
}