Skip to content

Commit

Permalink
Bug fix for the Unity Web Request HTTP Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Auros committed Sep 5, 2023
1 parent 9e775ef commit 5e4268e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
40 changes: 14 additions & 26 deletions SiraUtil/Web/Implementations/UWRHttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,23 @@ 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}";
}
}

public string? BaseURL { get; set; }

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)
if (Headers.ContainsKey("User-Agent"))
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;
}
}

Expand Down Expand Up @@ -82,26 +71,25 @@ public Task<IHttpResponse> DeleteAsync(string url, CancellationToken? cancellati

public async Task<IHttpResponse> SendAsync(HTTPMethod method, string url, string? body = null, IDictionary<string, string>? withHeaders = null, IProgress<float>? downloadProgress = null, CancellationToken? cancellationToken = null)
{
if (body != null)
if (body is not null)
{
if (withHeaders == null)
withHeaders = new Dictionary<string, string>();
withHeaders ??= new Dictionary<string, string>();
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<IHttpResponse> SendRawAsync(HTTPMethod method, string url, byte[]? body = null, IDictionary<string, string>? withHeaders = null, IProgress<float>? downloadProgress = null, CancellationToken? cancellationToken = null)
{
// 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;
Expand All @@ -119,28 +107,28 @@ public async Task<IHttpResponse> 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;
Expand Down
2 changes: 1 addition & 1 deletion SiraUtil/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5e4268e

Please sign in to comment.