From c4c5aeb36aa0113be4384d2c4d0b39b8285b5a29 Mon Sep 17 00:00:00 2001 From: genteure Date: Fri, 25 Aug 2023 00:26:19 +0800 Subject: [PATCH] feat(core): support brotli compressed danmaku --- .../Api/Danmaku/DanmakuClient.cs | 22 ++++++++++++++----- .../BililiveRecorder.Core.csproj | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs b/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs index f72148c..a006595 100644 --- a/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs +++ b/BililiveRecorder.Core/Api/Danmaku/DanmakuClient.cs @@ -1,6 +1,7 @@ using System; using System.Buffers; using System.Buffers.Binary; +using System.IO; using System.IO.Compression; using System.IO.Pipelines; using System.Net; @@ -221,7 +222,7 @@ namespace BililiveRecorder.Core.Api.Danmaku { uid, roomid, - protover = 0, + protover = 3, buvid, platform = "web", type = 2, @@ -334,17 +335,28 @@ namespace BililiveRecorder.Core.Api.Danmaku } if (header.Version == 2 && header.Action == 5) - ParseCommandDeflateBody(ref bodySlice, callback); + { + using var deflate = new DeflateStream(bodySlice.Slice(2, bodySlice.End).AsStream(), CompressionMode.Decompress, leaveOpen: false); + ParseCommandCompressedBody(deflate, callback); + } + else if (header.Version == 3 && header.Action == 5) + { +#if NET6_0_OR_GREATER + using var brotli = new BrotliStream(bodySlice.AsStream(), CompressionMode.Decompress, leaveOpen: false); +#else + using var brotli = new BrotliSharpLib.BrotliStream(bodySlice.AsStream(), CompressionMode.Decompress, leaveOpen: false); +#endif + ParseCommandCompressedBody(brotli, callback); + } else ParseCommandNormalBody(ref bodySlice, header.Action, callback); return true; } - private static void ParseCommandDeflateBody(ref ReadOnlySequence buffer, Action callback) + private static void ParseCommandCompressedBody(Stream decompressed, Action callback) { - using var deflate = new DeflateStream(buffer.Slice(2, buffer.End).AsStream(), CompressionMode.Decompress, leaveOpen: false); - var reader = PipeReader.Create(deflate); + var reader = PipeReader.Create(decompressed); while (true) { #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits diff --git a/BililiveRecorder.Core/BililiveRecorder.Core.csproj b/BililiveRecorder.Core/BililiveRecorder.Core.csproj index e1925cb..c44863e 100644 --- a/BililiveRecorder.Core/BililiveRecorder.Core.csproj +++ b/BililiveRecorder.Core/BililiveRecorder.Core.csproj @@ -38,6 +38,7 @@ +