Fixed around 140 wrong namings and other refactorings which were marked as warnings

This commit is contained in:
Kwoth
2022-01-08 11:51:41 +01:00
parent a6330119e8
commit 2ce3262d59
109 changed files with 698 additions and 760 deletions

View File

@@ -52,7 +52,7 @@ public sealed partial class YtLoader
public sealed class YtTrackInfo : TrackInfo
{
private const string BaseYoutubeUrl = "https://youtube.com/watch?v=";
private const string BASE_YOUTUBE_URL = "https://youtube.com/watch?v=";
public override string Url { get; }
public override string Title { get; }
public override TimeSpan Duration { get; }
@@ -62,7 +62,7 @@ public sealed partial class YtLoader
public YtTrackInfo(string title, string videoId, TimeSpan duration)
{
Title = title;
Url = BaseYoutubeUrl + videoId;
Url = BASE_YOUTUBE_URL + videoId;
Duration = duration;
_videoId = videoId;

View File

@@ -7,10 +7,10 @@ namespace NadekoBot.Modules.Music.Services;
public sealed partial class YtLoader
{
private static readonly byte[] YT_RESULT_INITIAL_DATA = Encoding.UTF8.GetBytes("var ytInitialData = ");
private static readonly byte[] YT_RESULT_JSON_END = Encoding.UTF8.GetBytes(";<");
private static readonly byte[] _ytResultInitialData = Encoding.UTF8.GetBytes("var ytInitialData = ");
private static readonly byte[] _ytResultJsonEnd = Encoding.UTF8.GetBytes(";<");
private static readonly string[] durationFormats =
private static readonly string[] _durationFormats =
{
@"m\:ss", @"mm\:ss", @"h\:mm\:ss", @"hh\:mm\:ss", @"hhh\:mm\:ss"
};
@@ -98,7 +98,7 @@ public sealed partial class YtLoader
var durationString = elem.GetProperty("lengthText").GetProperty("simpleText").GetString();
if (!TimeSpan.TryParseExact(durationString,
durationFormats,
_durationFormats,
CultureInfo.InvariantCulture,
out var duration))
{
@@ -117,13 +117,13 @@ public sealed partial class YtLoader
private Memory<byte> GetScriptResponseSpan(byte[] response)
{
var responseSpan = response.AsSpan()[140_000..];
var startIndex = responseSpan.IndexOf(YT_RESULT_INITIAL_DATA);
var startIndex = responseSpan.IndexOf(_ytResultInitialData);
if (startIndex == -1)
return null; // todo future try selecting html
startIndex += YT_RESULT_INITIAL_DATA.Length;
startIndex += _ytResultInitialData.Length;
var endIndex =
140_000 + startIndex + responseSpan[(startIndex + 20_000)..].IndexOf(YT_RESULT_JSON_END) + 20_000;
140_000 + startIndex + responseSpan[(startIndex + 20_000)..].IndexOf(_ytResultJsonEnd) + 20_000;
startIndex += 140_000;
return response.AsMemory(startIndex, endIndex - startIndex);
}

View File

@@ -42,9 +42,9 @@ public sealed partial class MusicQueue : IMusicQueue
{
// just make sure the internal logic runs first
// to make sure that some potential indermediate value is not returned
lock (locker)
lock (_locker)
{
return _index;
return index;
}
}
}
@@ -53,128 +53,128 @@ public sealed partial class MusicQueue : IMusicQueue
{
get
{
lock (locker)
lock (_locker)
{
return _tracks.Count;
return tracks.Count;
}
}
}
private LinkedList<QueuedTrackInfo> _tracks;
private LinkedList<QueuedTrackInfo> tracks;
private int _index;
private int index;
private readonly object locker = new();
private readonly object _locker = new();
public MusicQueue()
{
_index = 0;
_tracks = new();
index = 0;
tracks = new();
}
public IQueuedTrackInfo Enqueue(ITrackInfo trackInfo, string queuer, out int index)
public IQueuedTrackInfo Enqueue(ITrackInfo trackInfo, string queuer, out int enqueuedAt)
{
lock (locker)
lock (_locker)
{
var added = new QueuedTrackInfo(trackInfo, queuer);
index = _tracks.Count;
_tracks.AddLast(added);
enqueuedAt = tracks.Count;
tracks.AddLast(added);
return added;
}
}
public IQueuedTrackInfo EnqueueNext(ITrackInfo trackInfo, string queuer, out int index)
public IQueuedTrackInfo EnqueueNext(ITrackInfo trackInfo, string queuer, out int trackIndex)
{
lock (locker)
lock (_locker)
{
if (_tracks.Count == 0) return Enqueue(trackInfo, queuer, out index);
if (tracks.Count == 0) return Enqueue(trackInfo, queuer, out trackIndex);
var currentNode = _tracks.First!;
var currentNode = tracks.First!;
int i;
for (i = 1; i <= _index; i++)
for (i = 1; i <= this.index; i++)
currentNode = currentNode.Next!; // can't be null because index is always in range of the count
var added = new QueuedTrackInfo(trackInfo, queuer);
index = i;
trackIndex = i;
_tracks.AddAfter(currentNode, added);
tracks.AddAfter(currentNode, added);
return added;
}
}
public void EnqueueMany(IEnumerable<ITrackInfo> tracks, string queuer)
public void EnqueueMany(IEnumerable<ITrackInfo> toEnqueue, string queuer)
{
lock (locker)
lock (_locker)
{
foreach (var track in tracks)
foreach (var track in toEnqueue)
{
var added = new QueuedTrackInfo(track, queuer);
_tracks.AddLast(added);
this.tracks.AddLast(added);
}
}
}
public IReadOnlyCollection<IQueuedTrackInfo> List()
{
lock (locker)
lock (_locker)
{
return _tracks.ToList();
return tracks.ToList();
}
}
public IQueuedTrackInfo? GetCurrent(out int index)
public IQueuedTrackInfo? GetCurrent(out int currentIndex)
{
lock (locker)
lock (_locker)
{
index = _index;
return _tracks.ElementAtOrDefault(_index);
currentIndex = index;
return tracks.ElementAtOrDefault(index);
}
}
public void Advance()
{
lock (locker)
lock (_locker)
{
if (++_index >= _tracks.Count)
_index = 0;
if (++index >= tracks.Count)
index = 0;
}
}
public void Clear()
{
lock (locker)
lock (_locker)
{
_tracks.Clear();
tracks.Clear();
}
}
public bool SetIndex(int index)
public bool SetIndex(int newIndex)
{
lock (locker)
lock (_locker)
{
if (index < 0 || index >= _tracks.Count)
if (newIndex < 0 || newIndex >= tracks.Count)
return false;
_index = index;
this.index = newIndex;
return true;
}
}
private void RemoveAtInternal(int index, out IQueuedTrackInfo trackInfo)
private void RemoveAtInternal(int remoteAtIndex, out IQueuedTrackInfo trackInfo)
{
var removedNode = _tracks.First!;
var removedNode = tracks.First!;
int i;
for (i = 0; i < index; i++) removedNode = removedNode.Next!;
for (i = 0; i < remoteAtIndex; i++) removedNode = removedNode.Next!;
trackInfo = removedNode.Value;
_tracks.Remove(removedNode);
tracks.Remove(removedNode);
if (i <= _index)
--_index;
if (i <= this.index)
--this.index;
if (_index < 0)
_index = Count;
if (this.index < 0)
this.index = Count;
// if it was the last song in the queue
// // wrap back to start
@@ -188,10 +188,10 @@ public sealed partial class MusicQueue : IMusicQueue
public void RemoveCurrent()
{
lock (locker)
lock (_locker)
{
if (_index < _tracks.Count)
RemoveAtInternal(_index, out _);
if (index < tracks.Count)
RemoveAtInternal(index, out _);
}
}
@@ -204,29 +204,29 @@ public sealed partial class MusicQueue : IMusicQueue
if (to == from)
throw new ArgumentException($"{nameof(from)} and {nameof(to)} must be different");
lock (locker)
lock (_locker)
{
if (from >= Count || to >= Count)
return null;
// update current track index
if (from == _index)
if (from == index)
{
// if the song being moved is the current track
// it means that it will for sure end up on the destination
_index = to;
index = to;
}
else
{
// moving a track from below the current track means
// means it will drop down
if (from < _index)
_index--;
if (from < index)
index--;
// moving a track to below the current track
// means it will rise up
if (to <= _index)
_index++;
if (to <= index)
index++;
// if both from and to are below _index - net change is + 1 - 1 = 0
@@ -236,78 +236,76 @@ public sealed partial class MusicQueue : IMusicQueue
}
// get the node which needs to be moved
var fromNode = _tracks.First!;
var fromNode = tracks.First!;
for (var i = 0; i < from; i++)
fromNode = fromNode.Next!;
// remove it from the queue
_tracks.Remove(fromNode);
tracks.Remove(fromNode);
// if it needs to be added as a first node,
// add it directly and return
if (to == 0)
{
_tracks.AddFirst(fromNode);
tracks.AddFirst(fromNode);
return fromNode.Value;
}
// else find the node at the index before the specified target
var addAfterNode = _tracks.First!;
var addAfterNode = tracks.First!;
for (var i = 1; i < to; i++)
addAfterNode = addAfterNode.Next!;
// and add after it
_tracks.AddAfter(addAfterNode, fromNode);
tracks.AddAfter(addAfterNode, fromNode);
return fromNode.Value;
}
}
public void Shuffle(Random rng)
{
lock (locker)
lock (_locker)
{
var list = _tracks.ToList();
var list = tracks.ToList();
for (var i = 0; i < list.Count; i++)
{
var struck = rng.Next(i, list.Count);
var temp = list[struck];
list[struck] = list[i];
list[i] = temp;
(list[struck], list[i]) = (list[i], list[struck]);
// could preserving the index during shuffling be done better?
if (i == _index)
_index = struck;
else if (struck == _index)
_index = i;
if (i == index)
index = struck;
else if (struck == index)
index = i;
}
_tracks = new(list);
tracks = new(list);
}
}
public bool IsLast()
{
lock (locker)
lock (_locker)
{
return _index == _tracks.Count // if there are no tracks
|| _index == _tracks.Count - 1;
return index == tracks.Count // if there are no tracks
|| index == tracks.Count - 1;
}
}
public bool TryRemoveAt(int index, out IQueuedTrackInfo? trackInfo, out bool isCurrent)
public bool TryRemoveAt(int remoteAt, out IQueuedTrackInfo? trackInfo, out bool isCurrent)
{
lock (locker)
lock (_locker)
{
isCurrent = false;
trackInfo = null;
if (index < 0 || index >= _tracks.Count)
if (remoteAt < 0 || remoteAt >= tracks.Count)
return false;
if (index == _index) isCurrent = true;
if (remoteAt == this.index) isCurrent = true;
RemoveAtInternal(index, out trackInfo);
RemoveAtInternal(remoteAt, out trackInfo);
return true;
}

View File

@@ -17,7 +17,7 @@ public sealed class VoiceProxy : IVoiceProxy
private const int DELAY_ON_ERROR_MILISECONDS = 200;
public VoiceProxyState State
=> _gateway switch
=> gateway switch
{
{ Started: true, Stopped: false } => VoiceProxyState.Started,
{ Stopped: false } => VoiceProxyState.Created,
@@ -25,16 +25,16 @@ public sealed class VoiceProxy : IVoiceProxy
};
private VoiceGateway _gateway;
private VoiceGateway gateway;
public VoiceProxy(VoiceGateway initial)
=> _gateway = initial;
=> gateway = initial;
public bool SendPcmFrame(VoiceClient vc, Span<byte> data, int length)
{
try
{
var gw = _gateway;
var gw = gateway;
if (gw is null || gw.Stopped || !gw.Started) return false;
vc.SendPcmFrame(gw, data, 0, length);
@@ -55,7 +55,7 @@ public sealed class VoiceProxy : IVoiceProxy
try
{
var gw = _gateway;
var gw = gateway;
if (gw is null || !gw.ConnectingFinished.Task.IsCompleted)
{
++errorCount;
@@ -78,8 +78,8 @@ public sealed class VoiceProxy : IVoiceProxy
return State != VoiceProxyState.Stopped && errorCount <= MAX_ERROR_COUNT;
}
public void SetGateway(VoiceGateway gateway)
=> _gateway = gateway;
public void SetGateway(VoiceGateway newGateway)
=> gateway = newGateway;
public Task StartSpeakingAsync()
=> RunGatewayAction(gw => gw.SendSpeakingAsync(VoiceSpeaking.State.Microphone));
@@ -88,11 +88,11 @@ public sealed class VoiceProxy : IVoiceProxy
=> RunGatewayAction(gw => gw.SendSpeakingAsync(VoiceSpeaking.State.None));
public async Task StartGateway()
=> await _gateway.Start();
=> await gateway.Start();
public Task StopGateway()
{
if (_gateway is { } gw)
if (gateway is { } gw)
return gw.StopAsync();
return Task.CompletedTask;

View File

@@ -45,7 +45,8 @@ public sealed class LocalTrackResolver : ILocalTrackResolver
if (!x.Attributes.HasFlag(FileAttributes.Hidden | FileAttributes.System)
&& _musicExtensions.Contains(x.Extension.ToUpperInvariant())) return true;
return false;
});
})
.ToList();
var firstFile = files.FirstOrDefault()?.FullName;
if (firstFile is null)