@@ -16,10 +16,7 @@ | |||||
<PropertyGroup Condition=" '$(BuildNumber)' != '' And $(IsTagBuild) != 'true' "> | <PropertyGroup Condition=" '$(BuildNumber)' != '' And $(IsTagBuild) != 'true' "> | ||||
<VersionSuffix Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)-$(BuildNumber)</VersionSuffix> | <VersionSuffix Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)-$(BuildNumber)</VersionSuffix> | ||||
<VersionSuffix Condition=" '$(VersionSuffix)' == '' ">build-$(BuildNumber)</VersionSuffix> | <VersionSuffix Condition=" '$(VersionSuffix)' == '' ">build-$(BuildNumber)</VersionSuffix> | ||||
</PropertyGroup> | |||||
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' Or '$(TargetFramework)' == 'netstandard2.0' "> | |||||
<DefineConstants>$(DefineConstants);FORMATSTR;UNIXTIME;MSTRYBUFFER;UDPDISPOSE</DefineConstants> | |||||
</PropertyGroup> | |||||
</PropertyGroup> | |||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | <PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||||
<NoWarn>$(NoWarn);CS1573;CS1591</NoWarn> | <NoWarn>$(NoWarn);CS1573;CS1591</NoWarn> | ||||
<WarningsAsErrors>true</WarningsAsErrors> | <WarningsAsErrors>true</WarningsAsErrors> | ||||
@@ -1,4 +1,4 @@ | |||||
using System; | |||||
using System; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace Discord.Logging | namespace Discord.Logging | ||||
@@ -35,7 +35,7 @@ namespace Discord.Logging | |||||
} | } | ||||
catch { } | catch { } | ||||
} | } | ||||
#if FORMATSTR | |||||
public async Task LogAsync(LogSeverity severity, string source, FormattableString message, Exception ex = null) | public async Task LogAsync(LogSeverity severity, string source, FormattableString message, Exception ex = null) | ||||
{ | { | ||||
try | try | ||||
@@ -45,52 +45,49 @@ namespace Discord.Logging | |||||
} | } | ||||
catch { } | catch { } | ||||
} | } | ||||
#endif | |||||
public Task ErrorAsync(string source, Exception ex) | public Task ErrorAsync(string source, Exception ex) | ||||
=> LogAsync(LogSeverity.Error, source, ex); | => LogAsync(LogSeverity.Error, source, ex); | ||||
public Task ErrorAsync(string source, string message, Exception ex = null) | public Task ErrorAsync(string source, string message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Error, source, message, ex); | => LogAsync(LogSeverity.Error, source, message, ex); | ||||
#if FORMATSTR | |||||
public Task ErrorAsync(string source, FormattableString message, Exception ex = null) | public Task ErrorAsync(string source, FormattableString message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Error, source, message, ex); | => LogAsync(LogSeverity.Error, source, message, ex); | ||||
#endif | |||||
public Task WarningAsync(string source, Exception ex) | public Task WarningAsync(string source, Exception ex) | ||||
=> LogAsync(LogSeverity.Warning, source, ex); | => LogAsync(LogSeverity.Warning, source, ex); | ||||
public Task WarningAsync(string source, string message, Exception ex = null) | public Task WarningAsync(string source, string message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Warning, source, message, ex); | => LogAsync(LogSeverity.Warning, source, message, ex); | ||||
#if FORMATSTR | |||||
public Task WarningAsync(string source, FormattableString message, Exception ex = null) | public Task WarningAsync(string source, FormattableString message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Warning, source, message, ex); | => LogAsync(LogSeverity.Warning, source, message, ex); | ||||
#endif | |||||
public Task InfoAsync(string source, Exception ex) | public Task InfoAsync(string source, Exception ex) | ||||
=> LogAsync(LogSeverity.Info, source, ex); | => LogAsync(LogSeverity.Info, source, ex); | ||||
public Task InfoAsync(string source, string message, Exception ex = null) | public Task InfoAsync(string source, string message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Info, source, message, ex); | => LogAsync(LogSeverity.Info, source, message, ex); | ||||
#if FORMATSTR | |||||
public Task InfoAsync(string source, FormattableString message, Exception ex = null) | public Task InfoAsync(string source, FormattableString message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Info, source, message, ex); | => LogAsync(LogSeverity.Info, source, message, ex); | ||||
#endif | |||||
public Task VerboseAsync(string source, Exception ex) | public Task VerboseAsync(string source, Exception ex) | ||||
=> LogAsync(LogSeverity.Verbose, source, ex); | => LogAsync(LogSeverity.Verbose, source, ex); | ||||
public Task VerboseAsync(string source, string message, Exception ex = null) | public Task VerboseAsync(string source, string message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Verbose, source, message, ex); | => LogAsync(LogSeverity.Verbose, source, message, ex); | ||||
#if FORMATSTR | |||||
public Task VerboseAsync(string source, FormattableString message, Exception ex = null) | public Task VerboseAsync(string source, FormattableString message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Verbose, source, message, ex); | => LogAsync(LogSeverity.Verbose, source, message, ex); | ||||
#endif | |||||
public Task DebugAsync(string source, Exception ex) | public Task DebugAsync(string source, Exception ex) | ||||
=> LogAsync(LogSeverity.Debug, source, ex); | => LogAsync(LogSeverity.Debug, source, ex); | ||||
public Task DebugAsync(string source, string message, Exception ex = null) | public Task DebugAsync(string source, string message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Debug, source, message, ex); | => LogAsync(LogSeverity.Debug, source, message, ex); | ||||
#if FORMATSTR | |||||
public Task DebugAsync(string source, FormattableString message, Exception ex = null) | public Task DebugAsync(string source, FormattableString message, Exception ex = null) | ||||
=> LogAsync(LogSeverity.Debug, source, message, ex); | => LogAsync(LogSeverity.Debug, source, message, ex); | ||||
#endif | |||||
public Logger CreateLogger(string name) => new Logger(this, name); | public Logger CreateLogger(string name) => new Logger(this, name); | ||||
@@ -1,4 +1,4 @@ | |||||
using System; | |||||
using System; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
namespace Discord.Logging | namespace Discord.Logging | ||||
@@ -20,54 +20,53 @@ namespace Discord.Logging | |||||
=> _manager.LogAsync(severity, Name, exception); | => _manager.LogAsync(severity, Name, exception); | ||||
public Task LogAsync(LogSeverity severity, string message, Exception exception = null) | public Task LogAsync(LogSeverity severity, string message, Exception exception = null) | ||||
=> _manager.LogAsync(severity, Name, message, exception); | => _manager.LogAsync(severity, Name, message, exception); | ||||
#if FORMATSTR | |||||
public Task LogAsync(LogSeverity severity, FormattableString message, Exception exception = null) | public Task LogAsync(LogSeverity severity, FormattableString message, Exception exception = null) | ||||
=> _manager.LogAsync(severity, Name, message, exception); | => _manager.LogAsync(severity, Name, message, exception); | ||||
#endif | |||||
public Task ErrorAsync(Exception exception) | public Task ErrorAsync(Exception exception) | ||||
=> _manager.ErrorAsync(Name, exception); | => _manager.ErrorAsync(Name, exception); | ||||
public Task ErrorAsync(string message, Exception exception = null) | public Task ErrorAsync(string message, Exception exception = null) | ||||
=> _manager.ErrorAsync(Name, message, exception); | => _manager.ErrorAsync(Name, message, exception); | ||||
#if FORMATSTR | |||||
public Task ErrorAsync(FormattableString message, Exception exception = null) | public Task ErrorAsync(FormattableString message, Exception exception = null) | ||||
=> _manager.ErrorAsync(Name, message, exception); | => _manager.ErrorAsync(Name, message, exception); | ||||
#endif | |||||
public Task WarningAsync(Exception exception) | public Task WarningAsync(Exception exception) | ||||
=> _manager.WarningAsync(Name, exception); | => _manager.WarningAsync(Name, exception); | ||||
public Task WarningAsync(string message, Exception exception = null) | public Task WarningAsync(string message, Exception exception = null) | ||||
=> _manager.WarningAsync(Name, message, exception); | => _manager.WarningAsync(Name, message, exception); | ||||
#if FORMATSTR | |||||
public Task WarningAsync(FormattableString message, Exception exception = null) | public Task WarningAsync(FormattableString message, Exception exception = null) | ||||
=> _manager.WarningAsync(Name, message, exception); | => _manager.WarningAsync(Name, message, exception); | ||||
#endif | |||||
public Task InfoAsync(Exception exception) | public Task InfoAsync(Exception exception) | ||||
=> _manager.InfoAsync(Name, exception); | => _manager.InfoAsync(Name, exception); | ||||
public Task InfoAsync(string message, Exception exception = null) | public Task InfoAsync(string message, Exception exception = null) | ||||
=> _manager.InfoAsync(Name, message, exception); | => _manager.InfoAsync(Name, message, exception); | ||||
#if FORMATSTR | |||||
public Task InfoAsync(FormattableString message, Exception exception = null) | public Task InfoAsync(FormattableString message, Exception exception = null) | ||||
=> _manager.InfoAsync(Name, message, exception); | => _manager.InfoAsync(Name, message, exception); | ||||
#endif | |||||
public Task VerboseAsync(Exception exception) | public Task VerboseAsync(Exception exception) | ||||
=> _manager.VerboseAsync(Name, exception); | => _manager.VerboseAsync(Name, exception); | ||||
public Task VerboseAsync(string message, Exception exception = null) | public Task VerboseAsync(string message, Exception exception = null) | ||||
=> _manager.VerboseAsync(Name, message, exception); | => _manager.VerboseAsync(Name, message, exception); | ||||
#if FORMATSTR | |||||
public Task VerboseAsync(FormattableString message, Exception exception = null) | public Task VerboseAsync(FormattableString message, Exception exception = null) | ||||
=> _manager.VerboseAsync(Name, message, exception); | => _manager.VerboseAsync(Name, message, exception); | ||||
#endif | |||||
public Task DebugAsync(Exception exception) | public Task DebugAsync(Exception exception) | ||||
=> _manager.DebugAsync(Name, exception); | => _manager.DebugAsync(Name, exception); | ||||
public Task DebugAsync(string message, Exception exception = null) | public Task DebugAsync(string message, Exception exception = null) | ||||
=> _manager.DebugAsync(Name, message, exception); | => _manager.DebugAsync(Name, message, exception); | ||||
#if FORMATSTR | |||||
public Task DebugAsync(FormattableString message, Exception exception = null) | public Task DebugAsync(FormattableString message, Exception exception = null) | ||||
=> _manager.DebugAsync(Name, message, exception); | => _manager.DebugAsync(Name, message, exception); | ||||
#endif | |||||
} | } | ||||
} | } |
@@ -1,57 +1,14 @@ | |||||
using System; | |||||
using System; | |||||
namespace Discord | namespace Discord | ||||
{ | { | ||||
//Source: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/DateTimeOffset.cs | //Source: https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/DateTimeOffset.cs | ||||
internal static class DateTimeUtils | internal static class DateTimeUtils | ||||
{ | { | ||||
#if !UNIXTIME | |||||
private const long UnixEpochTicks = 621_355_968_000_000_000; | |||||
private const long UnixEpochSeconds = 62_135_596_800; | |||||
private const long UnixEpochMilliseconds = 62_135_596_800_000; | |||||
#endif | |||||
public static DateTimeOffset FromTicks(long ticks) | public static DateTimeOffset FromTicks(long ticks) | ||||
=> new DateTimeOffset(ticks, TimeSpan.Zero); | => new DateTimeOffset(ticks, TimeSpan.Zero); | ||||
public static DateTimeOffset? FromTicks(long? ticks) | public static DateTimeOffset? FromTicks(long? ticks) | ||||
=> ticks != null ? new DateTimeOffset(ticks.Value, TimeSpan.Zero) : (DateTimeOffset?)null; | => ticks != null ? new DateTimeOffset(ticks.Value, TimeSpan.Zero) : (DateTimeOffset?)null; | ||||
public static DateTimeOffset FromUnixSeconds(long seconds) | |||||
{ | |||||
#if UNIXTIME | |||||
return DateTimeOffset.FromUnixTimeSeconds(seconds); | |||||
#else | |||||
long ticks = seconds * TimeSpan.TicksPerSecond + UnixEpochTicks; | |||||
return new DateTimeOffset(ticks, TimeSpan.Zero); | |||||
#endif | |||||
} | |||||
public static DateTimeOffset FromUnixMilliseconds(long milliseconds) | |||||
{ | |||||
#if UNIXTIME | |||||
return DateTimeOffset.FromUnixTimeMilliseconds(milliseconds); | |||||
#else | |||||
long ticks = milliseconds * TimeSpan.TicksPerMillisecond + UnixEpochTicks; | |||||
return new DateTimeOffset(ticks, TimeSpan.Zero); | |||||
#endif | |||||
} | |||||
public static long ToUnixSeconds(DateTimeOffset dto) | |||||
{ | |||||
#if UNIXTIME | |||||
return dto.ToUnixTimeSeconds(); | |||||
#else | |||||
long seconds = dto.UtcDateTime.Ticks / TimeSpan.TicksPerSecond; | |||||
return seconds - UnixEpochSeconds; | |||||
#endif | |||||
} | |||||
public static long ToUnixMilliseconds(DateTimeOffset dto) | |||||
{ | |||||
#if UNIXTIME | |||||
return dto.ToUnixTimeMilliseconds(); | |||||
#else | |||||
long milliseconds = dto.UtcDateTime.Ticks / TimeSpan.TicksPerMillisecond; | |||||
return milliseconds - UnixEpochMilliseconds; | |||||
#endif | |||||
} | |||||
} | } | ||||
} | } |
@@ -5,8 +5,8 @@ namespace Discord | |||||
public static class SnowflakeUtils | public static class SnowflakeUtils | ||||
{ | { | ||||
public static DateTimeOffset FromSnowflake(ulong value) | public static DateTimeOffset FromSnowflake(ulong value) | ||||
=> DateTimeUtils.FromUnixMilliseconds((long)((value >> 22) + 1420070400000UL)); | |||||
=> DateTimeOffset.FromUnixTimeMilliseconds((long)((value >> 22) + 1420070400000UL)); | |||||
public static ulong ToSnowflake(DateTimeOffset value) | public static ulong ToSnowflake(DateTimeOffset value) | ||||
=> ((ulong)DateTimeUtils.ToUnixMilliseconds(value) - 1420070400000UL) << 22; | |||||
=> ((ulong)value.ToUnixTimeMilliseconds() - 1420070400000UL) << 22; | |||||
} | } | ||||
} | } |
@@ -230,7 +230,7 @@ namespace Discord.Net.Queue | |||||
#endif | #endif | ||||
} | } | ||||
var now = DateTimeUtils.ToUnixSeconds(DateTimeOffset.UtcNow); | |||||
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); | |||||
DateTimeOffset? resetTick = null; | DateTimeOffset? resetTick = null; | ||||
//Using X-RateLimit-Remaining causes a race condition | //Using X-RateLimit-Remaining causes a race condition | ||||
@@ -1,4 +1,4 @@ | |||||
using System; | |||||
using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
namespace Discord.Net | namespace Discord.Net | ||||
@@ -21,7 +21,7 @@ namespace Discord.Net | |||||
Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) && | Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) && | ||||
int.TryParse(temp, out var remaining) ? remaining : (int?)null; | int.TryParse(temp, out var remaining) ? remaining : (int?)null; | ||||
Reset = headers.TryGetValue("X-RateLimit-Reset", out temp) && | Reset = headers.TryGetValue("X-RateLimit-Reset", out temp) && | ||||
int.TryParse(temp, out var reset) ? DateTimeUtils.FromUnixSeconds(reset) : (DateTimeOffset?)null; | |||||
int.TryParse(temp, out var reset) ? DateTimeOffset.FromUnixTimeSeconds(reset) : (DateTimeOffset?)null; | |||||
RetryAfter = headers.TryGetValue("Retry-After", out temp) && | RetryAfter = headers.TryGetValue("Retry-After", out temp) && | ||||
int.TryParse(temp, out var retryAfter) ? retryAfter : (int?)null; | int.TryParse(temp, out var retryAfter) ? retryAfter : (int?)null; | ||||
Lag = headers.TryGetValue("Date", out temp) && | Lag = headers.TryGetValue("Date", out temp) && | ||||
@@ -365,7 +365,7 @@ namespace Discord.WebSocket | |||||
await ApiClient.SendStatusUpdateAsync( | await ApiClient.SendStatusUpdateAsync( | ||||
status, | status, | ||||
status == UserStatus.AFK, | status == UserStatus.AFK, | ||||
statusSince != null ? DateTimeUtils.ToUnixMilliseconds(_statusSince.Value) : (long?)null, | |||||
statusSince != null ? _statusSince.Value.ToUnixTimeMilliseconds() : (long?)null, | |||||
gameModel).ConfigureAwait(false); | gameModel).ConfigureAwait(false); | ||||
} | } | ||||
@@ -1,4 +1,4 @@ | |||||
#pragma warning disable CS1591 | |||||
#pragma warning disable CS1591 | |||||
using Discord.API; | using Discord.API; | ||||
using Discord.API.Voice; | using Discord.API.Voice; | ||||
using Discord.Net.Converters; | using Discord.Net.Converters; | ||||
@@ -129,7 +129,7 @@ namespace Discord.Audio | |||||
//WebSocket | //WebSocket | ||||
public async Task SendHeartbeatAsync(RequestOptions options = null) | public async Task SendHeartbeatAsync(RequestOptions options = null) | ||||
{ | { | ||||
await SendAsync(VoiceOpCode.Heartbeat, DateTimeUtils.ToUnixMilliseconds(DateTimeOffset.UtcNow), options: options).ConfigureAwait(false); | |||||
await SendAsync(VoiceOpCode.Heartbeat, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), options: options).ConfigureAwait(false); | |||||
} | } | ||||
public async Task SendIdentityAsync(ulong userId, string sessionId, string token) | public async Task SendIdentityAsync(ulong userId, string sessionId, string token) | ||||
{ | { | ||||
@@ -84,11 +84,7 @@ namespace Discord.Net.Udp | |||||
if (_udp != null) | if (_udp != null) | ||||
{ | { | ||||
#if UDPDISPOSE | |||||
try { _udp.Dispose(); } | try { _udp.Dispose(); } | ||||
#else | |||||
try { _udp.Close(); } | |||||
#endif | |||||
catch { } | catch { } | ||||
_udp = null; | _udp = null; | ||||
} | } | ||||
@@ -208,14 +208,9 @@ namespace Discord.Net.WebSockets | |||||
//Use the internal buffer if we can get it | //Use the internal buffer if we can get it | ||||
resultCount = (int)stream.Length; | resultCount = (int)stream.Length; | ||||
#if MSTRYBUFFER | |||||
if (stream.TryGetBuffer(out var streamBuffer)) | |||||
result = streamBuffer.Array; | |||||
else | |||||
result = stream.ToArray(); | |||||
#else | |||||
result = stream.GetBuffer(); | |||||
#endif | |||||
result = stream.TryGetBuffer(out var streamBuffer) ? streamBuffer.Array : stream.ToArray(); | |||||
} | } | ||||
} | } | ||||
else | else | ||||