Skip to content

Commit

Permalink
Merge pull request #166 from Emik03/avoid-console-writeline
Browse files Browse the repository at this point in the history
Avoid exceptions and Console.WriteLine
  • Loading branch information
AiAe authored May 10, 2024
2 parents d8e2cd3 + 6537654 commit 01546ee
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 43 deletions.
49 changes: 49 additions & 0 deletions Quaver.API/Helpers/Drain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: MPL-2.0
using System;

namespace Quaver.API.Helpers
{
/// <summary>
/// Represents a sequence of items separated by a separator.
/// </summary>
/// <typeparam name="T">The type of items.</typeparam>
public readonly ref struct Drain<T>
where T : IEquatable<T>
{
readonly ReadOnlySpan<T> _remaining;

readonly T _separator;

/// <summary>
/// Creates a new <see cref="Drain{T}"/> instance.
/// </summary>
/// <param name="remaining">The remaining items.</param>
/// <param name="separator">The separator.</param>
public Drain(ReadOnlySpan<T> remaining, T separator)
{
_remaining = remaining;
_separator = separator;
}

/// <summary>
/// Collects the next <see cref="ReadOnlySpan{T}"/>,
/// while also initializing the next <see cref="Drain{T}"/>.
/// </summary>
/// <param name="next">The next <see cref="ReadOnlySpan{T}"/>.</param>
/// <param name="rest">The rest as <see cref="Drain{T}"/>.</param>
public void Deconstruct(out ReadOnlySpan<T> next, out Drain<T> rest)
{
var i = _remaining.IndexOf(_separator);

if (i is -1)
{
next = _remaining;
rest = default;
return;
}

next = _remaining[..i];
rest = new Drain<T>(_remaining[(i + 1)..], _separator);
}
}
}
14 changes: 5 additions & 9 deletions Quaver.API/Maps/AutoMod/AutoMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down
4 changes: 2 additions & 2 deletions Quaver.API/Maps/Qua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public bool EqualByValue(Qua other)
/// <returns></returns>
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();
Expand Down Expand Up @@ -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();

Expand Down
25 changes: 8 additions & 17 deletions Quaver.API/Maps/Structures/EditorLayerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -31,22 +31,13 @@ public class EditorLayerInfo
/// </summary>
/// <returns></returns>
[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<char>(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;

/// <summary>
/// By-value comparer, auto-generated by Rider.
Expand Down
26 changes: 11 additions & 15 deletions Quaver.API/Replays/Replay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(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));
}
}
}
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 01546ee

Please sign in to comment.