diff --git a/SiraUtil/Web/Implementations/UWRHttpService.cs b/SiraUtil/Web/Implementations/UWRHttpService.cs index c3b2d21..3f46e25 100644 --- a/SiraUtil/Web/Implementations/UWRHttpService.cs +++ b/SiraUtil/Web/Implementations/UWRHttpService.cs @@ -24,10 +24,7 @@ public string? Token Headers.Remove("Authorization"); if (value is not null) - if (Headers.ContainsKey("Authorization")) - Headers["Authorization"] = $"Bearer {value}"; - else - Headers.Add("Authorization", $"Bearer {value}"); + Headers["Authorization"] = $"Bearer {value}"; } } @@ -35,12 +32,7 @@ public string? Token public string? UserAgent { - get - { - if (Headers.TryGetValue("User-Agent", out string value)) - return value; - else return null; - } + get => Headers.TryGetValue("User-Agent", out var value) ? value : null; set { if (value is null) @@ -48,10 +40,7 @@ public string? UserAgent Headers.Remove("User-Agent"); if (value is not null) - if (Headers.ContainsKey("User-Agent")) - Headers["User-Agent"] = value; - else - Headers.Add("User-Agent", value); + Headers["User-Agent"] = value; } } @@ -82,13 +71,12 @@ public Task DeleteAsync(string url, CancellationToken? cancellati public async Task SendAsync(HTTPMethod method, string url, string? body = null, IDictionary? withHeaders = null, IProgress? downloadProgress = null, CancellationToken? cancellationToken = null) { - if (body != null) + if (body is not null) { - if (withHeaders == null) - withHeaders = new Dictionary(); + withHeaders ??= new Dictionary(); withHeaders.Add("Content-Type", "application/json"); } - return await SendRawAsync(method, url, Encoding.UTF8.GetBytes(body), withHeaders, downloadProgress, cancellationToken); + return await SendRawAsync(method, url, body is not null ? Encoding.UTF8.GetBytes(body) : null, withHeaders, downloadProgress, cancellationToken); } public async Task SendRawAsync(HTTPMethod method, string url, byte[]? body = null, IDictionary? withHeaders = null, IProgress? downloadProgress = null, CancellationToken? cancellationToken = null) @@ -96,12 +84,12 @@ public async Task SendRawAsync(HTTPMethod method, string url, byt // I HATE UNITY I HATE UNITY I HATE UNITY var response = await await UnityMainThreadTaskScheduler.Factory.StartNew(async () => { - string newURL = url; + var newURL = url; if (BaseURL != null) newURL = Path.Combine(BaseURL, url); DownloadHandler? dHandler = new DownloadHandlerBuffer(); - HTTPMethod originalMethod = method; + var originalMethod = method; if (method == HTTPMethod.POST && body != null) method = HTTPMethod.PUT; @@ -119,28 +107,28 @@ public async Task SendRawAsync(HTTPMethod method, string url, byt if (body != null && originalMethod == HTTPMethod.POST && method == HTTPMethod.PUT) request.method = originalMethod.ToString(); - float _lastProgress = -1f; + var lastProgress = -1f; AsyncOperation asyncOp = request.SendWebRequest(); while (!asyncOp.isDone) { - if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) + if (cancellationToken is { IsCancellationRequested: true }) { request.Abort(); break; } if (downloadProgress is not null && dHandler is not null) { - float currentProgress = asyncOp.progress; - if (_lastProgress != currentProgress) + var currentProgress = asyncOp.progress; + if (Math.Abs(lastProgress - currentProgress) > 0.001f) { downloadProgress.Report(currentProgress); - _lastProgress = currentProgress; + lastProgress = currentProgress; } } await Task.Delay(10); } downloadProgress?.Report(1f); - bool successful = request.isDone && request.result == UnityWebRequest.Result.Success; + var successful = request is { isDone: true, result: UnityWebRequest.Result.Success }; return new UnityWebRequestHttpResponse(request, successful); }); return response; diff --git a/SiraUtil/manifest.json b/SiraUtil/manifest.json index dc9590d..e81938f 100644 --- a/SiraUtil/manifest.json +++ b/SiraUtil/manifest.json @@ -3,7 +3,7 @@ "id": "SiraUtil", "name": "SiraUtil", "author": "Auros", - "version": "3.1.4", + "version": "3.1.5", "icon": "SiraUtil.Resources.logo.png", "description": "A powerful utility mod which provides more tools to Beat Saber modders.", "gameVersion": "1.31.1",