diff --git a/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs b/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs index ac2ccc4..f72148c 100644 --- a/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs +++ b/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs @@ -66,7 +66,7 @@ namespace BililiveRecorder.Core.Api.Danmaku StatusChanged?.Invoke(this, StatusChangedEventArgs.False); } - public async Task ConnectAsync(int roomid, DanmakuTransportMode transportMode, CancellationToken cancellationToken) + public async Task ConnectAsync(int roomId, DanmakuTransportMode transportMode, CancellationToken cancellationToken) { if (this.disposedValue) throw new ObjectDisposedException(nameof(DanmakuClient)); @@ -74,19 +74,19 @@ namespace BililiveRecorder.Core.Api.Danmaku if (!Enum.IsDefined(typeof(DanmakuTransportMode), transportMode)) throw new ArgumentOutOfRangeException(nameof(transportMode), transportMode, "Invalid danmaku transport mode."); - await this.semaphoreSlim.WaitAsync().ConfigureAwait(false); + await this.semaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (this.danmakuTransport != null) return; - var serverInfo = await this.apiClient.GetDanmakuServerAsync(roomid).ConfigureAwait(false); + var serverInfo = await this.apiClient.GetDanmakuServerAsync(roomId).ConfigureAwait(false); if (serverInfo.Data is null) return; var danmakuServerInfo = serverInfo.Data.SelectDanmakuServer(transportMode); - this.logger.Debug("连接弹幕服务器 {Mode} {Host}:{Port} 房间: {RoomId} TokenLength: {TokenLength}", danmakuServerInfo.TransportMode, danmakuServerInfo.Host, danmakuServerInfo.Port, roomid, danmakuServerInfo.Token?.Length); + this.logger.Debug("连接弹幕服务器 {Mode} {Host}:{Port} 房间: {RoomId} TokenLength: {TokenLength}", danmakuServerInfo.TransportMode, danmakuServerInfo.Host, danmakuServerInfo.Port, roomId, danmakuServerInfo.Token?.Length); IDanmakuTransport transport = danmakuServerInfo.TransportMode switch { @@ -100,7 +100,7 @@ namespace BililiveRecorder.Core.Api.Danmaku this.danmakuTransport = transport; - await this.SendHelloAsync(roomid, this.apiClient.GetUid(), this.apiClient.GetBuvid3(), danmakuServerInfo.Token ?? string.Empty).ConfigureAwait(false); + await this.SendHelloAsync(roomId, this.apiClient.GetUid(), this.apiClient.GetBuvid3(), danmakuServerInfo.Token ?? string.Empty).ConfigureAwait(false); await this.SendPingAsync().ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) @@ -129,7 +129,7 @@ namespace BililiveRecorder.Core.Api.Danmaku await this.DisconnectAsync().ConfigureAwait(false); } catch (Exception) { } - }); + }, CancellationToken.None); } finally { @@ -370,7 +370,7 @@ namespace BililiveRecorder.Core.Api.Danmaku case 5: { if (buffer.Length > int.MaxValue) - throw new ArgumentOutOfRangeException("ParseCommandNormalBody buffer length larger than int.MaxValue"); + throw new ArgumentOutOfRangeException(nameof(buffer), "ParseCommandNormalBody buffer length larger than int.MaxValue"); var b = ArrayPool.Shared.Rent((int)buffer.Length); try diff --git a/BililiveRecorder.Core/Api/Http/HttpApiClient.cs b/BililiveRecorder.Core/Api/Http/HttpApiClient.cs index ce971c5..ff0ce79 100644 --- a/BililiveRecorder.Core/Api/Http/HttpApiClient.cs +++ b/BililiveRecorder.Core/Api/Http/HttpApiClient.cs @@ -59,7 +59,7 @@ namespace BililiveRecorder.Core.Api.Http if (!string.IsNullOrWhiteSpace(cookie_string)) { headers.Add("Cookie", cookie_string); - long.TryParse(matchCookieUidRegex.Match(cookie_string).Groups[1].Value, out var uid); + _ = long.TryParse(matchCookieUidRegex.Match(cookie_string).Groups[1].Value, out var uid); this.uid = uid; var buvid3 = matchCookieBuvid3Regex.Match(cookie_string).Groups[1].Value; if (!string.IsNullOrWhiteSpace(buvid3)) diff --git a/BililiveRecorder.Core/Recording/RawDataRecordTask.cs b/BililiveRecorder.Core/Recording/RawDataRecordTask.cs index b451cc6..189888b 100644 --- a/BililiveRecorder.Core/Recording/RawDataRecordTask.cs +++ b/BililiveRecorder.Core/Recording/RawDataRecordTask.cs @@ -59,14 +59,24 @@ namespace BililiveRecorder.Core.Recording while (!this.ct.IsCancellationRequested) { +#if NET6_0_OR_GREATER + var bytesRead = await stream.ReadAsync(buffer, this.ct).ConfigureAwait(false); +#else var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, this.ct).ConfigureAwait(false); +#endif if (bytesRead == 0) break; Interlocked.Add(ref this.ioNetworkDownloadedBytes, bytesRead); this.ioDiskStopwatch.Restart(); + +#if NET6_0_OR_GREATER + await file.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false); +#else await file.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false); +#endif + this.ioDiskStopwatch.Stop(); lock (this.ioDiskStatsLock) diff --git a/BililiveRecorder.Core/Recording/RecordTaskBase.cs b/BililiveRecorder.Core/Recording/RecordTaskBase.cs index 90c666c..7ba5dc2 100644 --- a/BililiveRecorder.Core/Recording/RecordTaskBase.cs +++ b/BililiveRecorder.Core/Recording/RecordTaskBase.cs @@ -244,10 +244,8 @@ namespace BililiveRecorder.Core.Recording } const int DefaultQn = 10000; - var codecItem = await this.apiClient.GetCodecItemInStreamUrlAsync(roomid: roomid, qn: DefaultQn).ConfigureAwait(false); - - if (codecItem is null) - throw new Exception("no supported stream url, qn: " + DefaultQn); + var codecItem = await this.apiClient.GetCodecItemInStreamUrlAsync(roomid: roomid, qn: DefaultQn).ConfigureAwait(false) + ?? throw new Exception("no supported stream url, qn: " + DefaultQn); int selected_qn; // Select first avaiable qn @@ -331,7 +329,7 @@ match_qn_success: streamHostInfoBuilder.Append(originalUri.Host); streamHostInfoBuilder.Append(" ["); streamHostInfoBuilder.Append(scriptIp); - streamHostInfoBuilder.Append("]"); + streamHostInfoBuilder.Append(']'); goto sendRequest; } @@ -367,7 +365,7 @@ match_qn_success: streamHostInfoBuilder.Append(originalUri.Host); streamHostInfoBuilder.Append(" ["); streamHostInfoBuilder.Append(selected); - streamHostInfoBuilder.Append("]"); + streamHostInfoBuilder.Append(']'); if (selected is null) { diff --git a/BililiveRecorder.Core/Scripting/Runtime/JintURLSearchParams.cs b/BililiveRecorder.Core/Scripting/Runtime/JintURLSearchParams.cs index 5e204a4..e6fd3df 100644 --- a/BililiveRecorder.Core/Scripting/Runtime/JintURLSearchParams.cs +++ b/BililiveRecorder.Core/Scripting/Runtime/JintURLSearchParams.cs @@ -33,20 +33,9 @@ namespace BililiveRecorder.Core.Scripting.Runtime } } - public void Append(string name, string value) - { - this.query.Add(name, value, nullValueHandling: NullValueHandling.NameOnly); - } - - public void Delete(string name) - { - this.query.Remove(name); - } - - public string?[][] Entries() - { - return this.query.Select(x => new string?[] { x.Name, x.Value.ToString() }).ToArray(); - } + public void Append(string name, string value) => this.query.Add(name, value, nullValueHandling: NullValueHandling.NameOnly); + public void Delete(string name) => this.query.Remove(name); + public string?[][] Entries() => this.query.Select(x => new string?[] { x.Name, x.Value.ToString() }).ToArray(); public void ForEach(FunctionInstance callback, JsValue thisArg) { @@ -58,44 +47,18 @@ namespace BililiveRecorder.Core.Scripting.Runtime } } - public string? Get(string name) - { - return this.query.TryGetFirst(name, out var value) ? value.ToString() : null; - } - - public string?[] GetAll(string name) - { - return this.query.GetAll(name).Select(x => x.ToString()).ToArray(); - } - - public bool Has(string name) - { - return this.query.Contains(name); - } - - public string[] Keys() - { - return this.query.Select(x => x.Name).ToArray(); - } - - public void Set(string name, string value) - { - this.query.AddOrReplace(name, value, nullValueHandling: NullValueHandling.NameOnly); - } + public string? Get(string name) => this.query.TryGetFirst(name, out var value) ? value.ToString() : null; + public string?[] GetAll(string name) => this.query.GetAll(name).Select(x => x.ToString()).ToArray(); + public bool Has(string name) => this.query.Contains(name); + public string[] Keys() => this.query.Select(x => x.Name).ToArray(); + public void Set(string name, string value) => this.query.AddOrReplace(name, value, nullValueHandling: NullValueHandling.NameOnly); public void Sort() { // do nothing } - public override string ToString() - { - return this.query.ToString(); - } - - public string?[] Values() - { - return this.query.Select(x => x.Value.ToString()).ToArray(); - } + public override string ToString() => this.query.ToString(); + public string?[] Values() => this.query.Select(x => x.Value.ToString()).ToArray(); } } diff --git a/BililiveRecorder.Core/Templating/FileNameGenerator.cs b/BililiveRecorder.Core/Templating/FileNameGenerator.cs index 570f889..fff570e 100644 --- a/BililiveRecorder.Core/Templating/FileNameGenerator.cs +++ b/BililiveRecorder.Core/Templating/FileNameGenerator.cs @@ -246,21 +246,21 @@ returnDefaultPath: var fullChild = Path.GetFullPath(child); var parentSegments = fullParent.Split(separator, StringSplitOptions.None).AsSpan(); - if (parentSegments[parentSegments.Length - 1] == "") + if (parentSegments[^1] == "") { - parentSegments = parentSegments.Slice(0, parentSegments.Length - 1); + parentSegments = parentSegments[..^1]; } var childSegments = fullChild.Split(separator, StringSplitOptions.None).AsSpan(); - if (childSegments[childSegments.Length - 1] == "") + if (childSegments[^1] == "") { - childSegments = childSegments.Slice(0, childSegments.Length - 1); + childSegments = childSegments[..^1]; } if (parentSegments.Length >= childSegments.Length) return false; - return childSegments.Slice(0, parentSegments.Length).SequenceEqual(parentSegments); + return childSegments[..parentSegments.Length].SequenceEqual(parentSegments); } } }