Visitar URL original
use stdlib zstd instead of our own native by akiraveliara · Pull Request #2481 · DSharpPlus/DSharpPlus · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ body:
label: What .NET version are you using? Make sure to use the latest patch release for your major version.
multiple: false
options:
- .NET 8.0
- .NET 9.0
- .NET 10.0
- .NET 11.0 Preview
- .NET 11.0
- .NET 12.0 Preview
validations:
required: true
- type: input
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10
dotnet-version: 11
- name: Build Project
run: dotnet build
- name: Test Changes
Expand Down Expand Up @@ -101,7 +101,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9
dotnet-version: 11
- name: Build Project
run: |
dotnet build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10
dotnet-version: 11
- name: Build Project
run: dotnet build
- name: Test Changes
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 10
dotnet-version: 11
- name: Login to NuGet
uses: NuGet/login@v1
id: nuget-login
Expand Down
5 changes: 1 addition & 4 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

DSharpPlus, for the most part, is just like any other `dotnet` library. You will need the latest .NET SDK installed, either explicitly or bundled by the latest version of your IDE, and you will need to simply run `dotnet build`, `dotnet publish` or `dotnet pack` depending on whether you aim to create a dev-time build, prod builds or local package files.

Optionally, DSharpPlus can use zstd for its gateway compression and sodium for receiving [interactions](https://discord.com/developers/docs/interactions/overview#preparing-for-interactions) or [other events](https://discord.com/developers/docs/events/webhook-events). Both of those libraries are provided for platforms `{win, osx, linux, linux-musl}-{x64, arm64}` via the NuGet packages `DSharpPlus.Natives.Zstd` and `DSharpPlus.Natives.Sodium`. On other operating systems or other CPU architectures, you have to provide the libraries yourself.
Optionally, DSharpPlus can use libsodium for receiving [interactions](https://discord.com/developers/docs/interactions/overview#preparing-for-interactions) or [other events](https://discord.com/developers/docs/events/webhook-events). This library is provided for platforms `{win, osx, linux, linux-musl}-{x64, arm64}` via the NuGet package `DSharpPlus.Natives.Sodium`. On other operating systems or other CPU architectures, you have to provide the libraries yourself.

DSharpPlus does not support NativeAOT compilation or trimming.

Expand All @@ -22,6 +22,3 @@ DSharpPlus' voice implementation is a bit more finicky. On platforms `{win, osx,
- Fourth, if you built OpenSSL yourself, you may have to provide libcrypto to your bot as well, in the same way.

We may not accept bug reports only reproducible on unsupported platforms, and we may not be able to help with building for unsupported platforms. General experience with using cmake is highly recommended.

>[!NOTE]
> Our native builds for x86-64 target `x86-64-v2`, which is any CPU that supports SSE4.2, LAHF-SAHF, POPCNT and CMPXCHG16B at the very least, or any Intel/AMD CPU built after 2008. If you are using an older CPU that does not support these features, you will also need to self-build the natives according to the above instructions. Additionally, you must remove the native libraries provided by DSharpPlus, since they will be incompatible with your CPU.
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<TargetFramework>net10.0</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\DSharpPlus.Commands\DSharpPlus.Commands.csproj" />
<ProjectReference Include="..\DSharpPlus.Analyzers\DSharpPlus.Analyzers.csproj" />
<ProjectReference Include="..\..\DSharpPlus\DSharpPlus.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net100" />
<PackageReference Include="Microsoft.CodeAnalysis" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" />
<PackageReference Include="NUnit" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsPackable>false</IsPackable>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\DSharpPlus.Commands\DSharpPlus.Commands.csproj" />
<ProjectReference Include="..\DSharpPlus.Analyzers\DSharpPlus.Analyzers.csproj" />
<ProjectReference Include="..\..\DSharpPlus\DSharpPlus.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Basic.Reference.Assemblies.Net100" />
<PackageReference Include="Microsoft.CodeAnalysis" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
</Project>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions DSharpPlus.Analyzers/DSharpPlus.Analyzers.Test/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static CSharpAnalyzerTest<T, DefaultVerifier> CreateAnalyzerTest<T>()
{
ReferenceAssemblies = new ReferenceAssemblies
(
targetFramework: "net10.0",
referenceAssemblyPackage: new PackageIdentity("Microsoft.NETCore.App.Ref", "10.0.1"),
referenceAssemblyPath: Path.Combine("ref", "net10.0")
targetFramework: "net11.0",
referenceAssemblyPackage: new PackageIdentity("Microsoft.NETCore.App.Ref", "11.0.0"),
referenceAssemblyPath: Path.Combine("ref", "net11.0")
)
};
test.TestState.AdditionalReferences.Add(typeof(DiscordClient).Assembly);
Expand Down
3 changes: 0 additions & 3 deletions DSharpPlus.CommandsNext/DSharpPlus.CommandsNext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,4 @@
<ItemGroup>
<ProjectReference Include="$(ProjectRoot)\DSharpPlus\DSharpPlus.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion DSharpPlus.Tests/DSharpPlus.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down
1 change: 0 additions & 1 deletion DSharpPlus/DSharpPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="NetEscapades.EnumGenerators" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Polly.Core" />
<PackageReference Include="System.Threading.RateLimiting" />
<PackageReference Include="Ulid" />
Expand Down
2 changes: 1 addition & 1 deletion DSharpPlus/Entities/DiscordPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static DiscordPermissions AllBitsSet

[UnscopedRef]
private readonly ReadOnlySpan<byte> AsSpan
=> MemoryMarshal.Cast<uint, byte>(this.data);
=> MemoryMarshal.Cast<uint, byte>(MemoryMarshal.CreateReadOnlySpan(in this.data[0], ContainerElementCount));

private readonly bool GetFlag(int index)
=> this.data.HasFlag(index);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Channels;

Expand All @@ -12,7 +10,6 @@
using DSharpPlus.Net.InboundWebhooks;
using DSharpPlus.Net.InboundWebhooks.Transport;
using DSharpPlus.Net.Gateway.Compression;
using DSharpPlus.Net.Gateway.Compression.Zlib;
using DSharpPlus.Net.Gateway.Compression.Zstd;

using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -67,7 +64,7 @@ internal static IServiceCollection AddDSharpPlusServices(this IServiceCollection
.AddTransient<ITransportService, TransportService>()
.AddTransient<IGatewayClient, GatewayClient>()
.AddSingleton<GatewayMetricsContainer>()
.RegisterBestDecompressor()
.AddTransient<IPayloadDecompressor, ZstdDecompressor>()
.AddSingleton<IEventDispatcher, DefaultEventDispatcher>()
.AddSingleton<DiscordClient>();

Expand All @@ -79,18 +76,4 @@ internal static IServiceCollection AddDSharpPlusServices(this IServiceCollection

return serviceCollection;
}

private static IServiceCollection RegisterBestDecompressor(this IServiceCollection services)
{
if (NativeLibrary.TryLoad("libzstd", Assembly.GetEntryAssembly(), default, out _))
{
services.AddTransient<IPayloadDecompressor, ZstdDecompressor>();
}
else
{
services.AddTransient<IPayloadDecompressor, ZlibStreamDecompressor>();
}

return services;
}
}
44 changes: 23 additions & 21 deletions DSharpPlus/Net/Gateway/Compression/Zstd/ZstdDecompressor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Buffers;
using System.IO.Compression;

using CommunityToolkit.HighPerformance;
using CommunityToolkit.HighPerformance.Buffers;

namespace DSharpPlus.Net.Gateway.Compression.Zstd;
Expand All @@ -10,7 +11,7 @@ namespace DSharpPlus.Net.Gateway.Compression.Zstd;
/// </summary>
public sealed class ZstdDecompressor : IPayloadDecompressor
{
private ZstdInterop wrapper;
private ZstandardDecoder decoder;
private bool isInitialized = false;

/// <inheritdoc/>
Expand All @@ -27,22 +28,34 @@ public bool TryDecompress(ReadOnlySpan<byte> compressed, ArrayPoolBufferWriter<b
return false;
}

// the magic header goes missing, we have to try it anyway - all explodes if we fix the magic header up :ioa:
if (!this.wrapper.TryDecompress(compressed, decompressed))
OperationStatus status;

do
{
status = this.decoder.Decompress(compressed, decompressed.GetSpan(), out int consumed, out int written);
compressed = compressed[consumed..];
decompressed.Advance(written);
}
while (status == OperationStatus.DestinationTooSmall);

// discord still strips the magic bytes, so NeedMoreData is (ideally) also successful. if it isn't it'll be caught by serialization throwing
if (status is OperationStatus.Done or OperationStatus.NeedMoreData)
{
return true;
}
else
{
decompressed.Clear();
decompressed.Write(compressed);
return false;
}

return true;
}

/// <inheritdoc/>
public void Dispose()
{
if (this.isInitialized)
{
this.wrapper.Dispose();
this.decoder.Dispose();
}

GC.SuppressFinalize(this);
Expand All @@ -51,7 +64,7 @@ public void Dispose()
/// <inheritdoc/>
public void Reset()
{
this.wrapper.Dispose();
this.decoder.Dispose();
this.isInitialized = false;
}

Expand All @@ -63,18 +76,7 @@ public void Initialize()
Reset();
}

this.wrapper = new();
this.decoder = new();
this.isInitialized = true;
}

/// <summary>
/// Frees the unmanaged zstd stream.
/// </summary>
~ZstdDecompressor()
{
if (this.isInitialized)
{
this.wrapper.Dispose();
}
}
}
17 changes: 0 additions & 17 deletions DSharpPlus/Net/Gateway/Compression/Zstd/ZstdErrorCode.cs

This file was deleted.

11 changes: 0 additions & 11 deletions DSharpPlus/Net/Gateway/Compression/Zstd/ZstdInputBuffer.cs

This file was deleted.

49 changes: 0 additions & 49 deletions DSharpPlus/Net/Gateway/Compression/Zstd/ZstdInterop.Bindings.cs

This file was deleted.

Loading
Loading