From b386a0e22abf5326bf8bc16d4e5c822ca2ae8c71 Mon Sep 17 00:00:00 2001 From: enonibobble Date: Wed, 31 Dec 2025 19:42:25 +0000 Subject: [PATCH] Switch lock backport package to #if defs instead (#3217) --- src/Discord.Net.Commands/Map/CommandMapNode.cs | 4 ++++ src/Discord.Net.Core/Discord.Net.Core.csproj | 6 +----- .../Entities/Interactions/CommandBase/RestCommandBase.cs | 4 ++++ .../MessageComponents/RestMessageComponent.cs | 6 +++++- .../Entities/Interactions/Modals/RestModal.cs | 6 +++++- .../SlashCommands/RestAutocompleteInteraction.cs | 4 ++++ src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs | 4 ++++ src/Discord.Net.WebSocket/DiscordShardedClient.cs | 6 +++++- .../Entities/Channels/SocketThreadChannel.cs | 5 +++++ .../MessageComponents/SocketMessageComponent.cs | 8 ++++++-- .../Entities/Interaction/Modals/SocketModal.cs | 6 +++++- .../SlashCommands/SocketAutocompleteInteraction.cs | 4 ++++ .../Interaction/SocketBaseCommand/SocketCommandBase.cs | 4 ++++ .../Entities/Users/SocketGlobalUser.cs | 4 ++++ 14 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/Discord.Net.Commands/Map/CommandMapNode.cs b/src/Discord.Net.Commands/Map/CommandMapNode.cs index cb523765..68edd02f 100644 --- a/src/Discord.Net.Commands/Map/CommandMapNode.cs +++ b/src/Discord.Net.Commands/Map/CommandMapNode.cs @@ -12,7 +12,11 @@ namespace Discord.Commands private readonly ConcurrentDictionary _nodes; private readonly string _name; +#if NET9_0_OR_GREATER private readonly Lock _lockObj = new(); +#else + private readonly object _lockObj = new(); +#endif private ImmutableArray _commands; public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0; diff --git a/src/Discord.Net.Core/Discord.Net.Core.csproj b/src/Discord.Net.Core/Discord.Net.Core.csproj index 92df09af..56b9e76e 100644 --- a/src/Discord.Net.Core/Discord.Net.Core.csproj +++ b/src/Discord.Net.Core/Discord.Net.Core.csproj @@ -19,9 +19,5 @@ all - - all - analyzers - - \ No newline at end of file + diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs index af44aeb4..9c73e576 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs @@ -33,7 +33,11 @@ namespace Discord.Rest /// internal new RestCommandBaseData Data { get; private set; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif internal RestCommandBase(DiscordRestClient client, Model model) : base(client, model.Id) diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index cee09444..6859bcb4 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -25,7 +25,11 @@ namespace Discord.Rest /// public RestUserMessage Message { get; private set; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif internal RestMessageComponent(BaseDiscordClient client, Model model) : base(client, model.Id) @@ -457,7 +461,7 @@ namespace Discord.Rest } /// - /// + /// /// /// /// diff --git a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs index 727e8fc1..4ecef291 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs @@ -42,7 +42,11 @@ namespace Discord.Rest return entity; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif /// /// Acknowledges this interaction with the if the modal was created @@ -139,7 +143,7 @@ namespace Discord.Rest MessageComponent component = null, Embed embed = null, RequestOptions options = null, - PollProperties poll = null, + PollProperties poll = null, MessageFlags flags = MessageFlags.None) { if (!IsValidToken) diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs index e1b768f0..239e8b07 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs @@ -20,7 +20,11 @@ namespace Discord.Rest /// public new RestAutocompleteInteractionData Data { get; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif internal RestAutocompleteInteraction(DiscordRestClient client, Model model) : base(client, model.Id) diff --git a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs index 7b790dbc..a3ed516c 100644 --- a/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs +++ b/src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs @@ -17,7 +17,11 @@ namespace Discord.Net.Queue { private const int MinimumSleepTimeMs = 750; +#if NET9_0_OR_GREATER private readonly Lock _lock; +#else + private readonly object _lock; +#endif private readonly RequestQueue _queue; private int _semaphore; private DateTimeOffset? _resetTick; diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 1f2c6e21..959e2eb9 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -21,7 +21,11 @@ namespace Discord.WebSocket private ImmutableArray> _defaultStickers; private int _totalShards; private SemaphoreSlim[] _identifySemaphores; +#if NET9_0_OR_GREATER private readonly Lock _semaphoreResetLock; +#else + private readonly object _semaphoreResetLock; +#endif private Task _semaphoreResetTask; private bool _isDisposed; @@ -524,7 +528,7 @@ namespace Discord.WebSocket client.AuditLogCreated += (arg1, arg2) => _auditLogCreated.InvokeAsync(arg1, arg2); client.VoiceChannelStatusUpdated += (arg1, arg2, arg3) => _voiceChannelStatusUpdated.InvokeAsync(arg1, arg2, arg3); - + client.EntitlementCreated += (arg1) => _entitlementCreated.InvokeAsync(arg1); client.EntitlementUpdated += (arg1, arg2) => _entitlementUpdated.InvokeAsync(arg1, arg2); client.EntitlementDeleted += (arg1) => _entitlementDeleted.InvokeAsync(arg1); diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs index 5ba56103..4576f872 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs @@ -111,8 +111,13 @@ namespace Discord.WebSocket private bool _usersDownloaded; +#if NET9_0_OR_GREATER private readonly Lock _downloadLock = new(); private readonly Lock _ownerLock = new(); +#else + private readonly object _downloadLock = new(); + private readonly object _ownerLock = new(); +#endif private ulong _ownerId; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs index 9b69ec24..fcb1ec34 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs @@ -21,11 +21,15 @@ namespace Discord.WebSocket /// Gets the data received with this interaction, contains the button that was clicked. /// public new SocketMessageComponentData Data { get; } - + /// public SocketUserMessage Message { get; private set; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif public override bool HasResponded { get; internal set; } = false; internal SocketMessageComponent(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) @@ -359,7 +363,7 @@ namespace Discord.WebSocket flags |= MessageFlags.Ephemeral; Preconditions.ValidateMessageFlags(flags); - + var args = new API.Rest.CreateWebhookMessageParams { diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs index 36a8e507..37a5d2e3 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Modals/SocketModal.cs @@ -67,7 +67,11 @@ namespace Discord.WebSocket /// public override bool HasResponded { get; internal set; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif /// public override async Task RespondWithFilesAsync( @@ -440,7 +444,7 @@ namespace Discord.WebSocket } /// - /// + /// /// Acknowledges this interaction with the if the modal was created /// in a response to a message component interaction, otherwise. /// diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs index 5528e07e..6b6e68b5 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SlashCommands/SocketAutocompleteInteraction.cs @@ -22,7 +22,11 @@ namespace Discord.WebSocket /// public override bool HasResponded { get; internal set; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) : base(client, model.Id, channel, user) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs index c0c2eebd..272e6e04 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketBaseCommand/SocketCommandBase.cs @@ -36,7 +36,11 @@ namespace Discord.WebSocket /// public override bool HasResponded { get; internal set; } +#if NET9_0_OR_GREATER private readonly Lock _lock = new(); +#else + private readonly object _lock = new(); +#endif internal SocketCommandBase(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) : base(client, model.Id, channel, user) diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs index 5b453c18..b5ef7f11 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs @@ -32,7 +32,11 @@ namespace Discord.WebSocket /// internal override SocketGlobalUser GlobalUser { get => this; set => throw new NotImplementedException(); } +#if NET9_0_OR_GREATER private readonly Lock _lockObj = new(); +#else + private readonly object _lockObj = new(); +#endif private ushort _references; private SocketGlobalUser(DiscordSocketClient discord, ulong id)