diff --git a/src/Discord.Net.Analyzers/AssemblyInfo.cs b/src/Discord.Net.Analyzers/AssemblyInfo.cs new file mode 100644 index 000000000..5e9efa5bc --- /dev/null +++ b/src/Discord.Net.Analyzers/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Discord.Net.Tests")] \ No newline at end of file diff --git a/src/Discord.Net.Rest/DiscordRestConfig.cs b/src/Discord.Net.Rest/DiscordRestConfig.cs index 33a3cb4e8..c3cd70683 100644 --- a/src/Discord.Net.Rest/DiscordRestConfig.cs +++ b/src/Discord.Net.Rest/DiscordRestConfig.cs @@ -10,6 +10,6 @@ namespace Discord.Rest internal const int WebSocketQueueInterval = 100; /// Gets or sets the provider used to generate new REST connections. - public RestClientProvider RestClientProvider { get; set; } = url => new DefaultRestClient(url); + public RestClientProvider RestClientProvider { get; set; } = DefaultRestClientProvider.Instance; } } diff --git a/src/Discord.Net.Rest/Net/DefaultRestClientProvider.cs b/src/Discord.Net.Rest/Net/DefaultRestClientProvider.cs new file mode 100644 index 000000000..311a53562 --- /dev/null +++ b/src/Discord.Net.Rest/Net/DefaultRestClientProvider.cs @@ -0,0 +1,19 @@ +using System; + +namespace Discord.Net.Rest +{ + public static class DefaultRestClientProvider + { + public static readonly RestClientProvider Instance = url => + { + try + { + return new DefaultRestClient(url); + } + catch (PlatformNotSupportedException ex) + { + throw new PlatformNotSupportedException("The default RestClientProvider is not supported on this platform.", ex); + } + }; + } +} diff --git a/src/Discord.Net.Rest/API/RpcFrame.cs b/src/Discord.Net.Rpc/API/RpcFrame.cs similarity index 100% rename from src/Discord.Net.Rest/API/RpcFrame.cs rename to src/Discord.Net.Rpc/API/RpcFrame.cs diff --git a/src/Discord.Net.Rest/API/SocketFrame.cs b/src/Discord.Net.WebSocket/API/SocketFrame.cs similarity index 100% rename from src/Discord.Net.Rest/API/SocketFrame.cs rename to src/Discord.Net.WebSocket/API/SocketFrame.cs diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 832e35578..e897a0b40 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -70,7 +70,7 @@ namespace Discord.WebSocket } } private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config) - => new API.DiscordSocketApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, config.WebSocketProvider); + => new API.DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent); protected override async Task OnLoginAsync(TokenType tokenType, string token) { diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index fa6995c27..ea0250c9b 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -142,7 +142,7 @@ namespace Discord.WebSocket _largeGuilds = new ConcurrentQueue(); } private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config) - => new API.DiscordSocketApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, config.WebSocketProvider); + => new API.DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent); protected override async Task OnLoginAsync(TokenType tokenType, string token) { @@ -232,7 +232,8 @@ namespace Discord.WebSocket ConnectionState = ConnectionState.Connected; await _gatewayLogger.InfoAsync("Connected").ConfigureAwait(false); - await ProcessUserDownloadsAsync(_downloadUsersFor.Select(x => GetGuild(x)).Where(x => x != null).ToImmutableArray()).ConfigureAwait(false); + await ProcessUserDownloadsAsync(_downloadUsersFor.Select(x => GetGuild(x)) + .Where(x => x != null).ToImmutableArray()).ConfigureAwait(false); } catch (Exception) { diff --git a/src/Discord.Net.WebSocket/Net/DefaultUdpSocketProvider.cs b/src/Discord.Net.WebSocket/Net/DefaultUdpSocketProvider.cs new file mode 100644 index 000000000..cba4fecb0 --- /dev/null +++ b/src/Discord.Net.WebSocket/Net/DefaultUdpSocketProvider.cs @@ -0,0 +1,27 @@ +using System; + +namespace Discord.Net.Udp +{ + public static class DefaultUdpSocketProvider + { +#if NETSTANDARD1_3 + public static readonly UdpSocketProvider Instance = () => + { + try + { + return new DefaultUdpSocket(); + } + catch (PlatformNotSupportedException ex) + { + throw new PlatformNotSupportedException("The default UdpSocketProvider is not supported on this platform.", ex); + } + }; +#else + public static readonly UdpSocketProvider Instance = () => + { + throw new PlatformNotSupportedException("The default UdpSocketProvider is not supported on this platform.\n" + + "You must specify a UdpSocketProvider or target a runtime supporting .NET Standard 1.3, such as .NET Framework 4.6+."); + }; +#endif + } +} \ No newline at end of file diff --git a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs index aa8bb6986..6f667ae41 100644 --- a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs +++ b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClient.cs @@ -206,15 +206,11 @@ namespace Discord.Net.WebSockets //Use the internal buffer if we can get it resultCount = (int)stream.Length; -#if NETSTANDARD1_3 ArraySegment streamBuffer; if (stream.TryGetBuffer(out streamBuffer)) result = streamBuffer.Array; else result = stream.ToArray(); -#else - result = stream.ToArray(); -#endif } } else diff --git a/src/Discord.Net.WebSocket/Net/DefaultWebSocketClientProvider.cs b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClientProvider.cs new file mode 100644 index 000000000..d93ded57d --- /dev/null +++ b/src/Discord.Net.WebSocket/Net/DefaultWebSocketClientProvider.cs @@ -0,0 +1,27 @@ +using System; + +namespace Discord.Net.WebSockets +{ + public static class DefaultWebSocketProvider + { +#if NETSTANDARD1_3 + public static readonly WebSocketProvider Instance = () => + { + try + { + return new DefaultWebSocketClient(); + } + catch (PlatformNotSupportedException ex) + { + throw new PlatformNotSupportedException("The default WebSocketProvider is not supported on this platform.", ex); + } + }; +#else + public static readonly WebSocketProvider Instance = () => + { + throw new PlatformNotSupportedException("The default WebSocketProvider is not supported on this platform.\n" + + "You must specify a WebSocketProvider or target a runtime supporting .NET Standard 1.3, such as .NET Framework 4.6+."); + }; +#endif + } +} \ No newline at end of file