Replace WebClient with HttpClient

This commit is contained in:
teinarss
2021-02-21 16:56:55 +01:00
committed by Paul Chote
parent ed43071792
commit 7073279ab8
17 changed files with 604 additions and 562 deletions

View File

@@ -11,10 +11,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Zip;
using OpenRA.Support;
using OpenRA.Widgets;
@@ -65,47 +65,35 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var cancelButton = panel.Get<ButtonWidget>("CANCEL_BUTTON");
var file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Action deleteTempFile = () =>
{
Log.Write("install", "Deleting temporary file " + file);
File.Delete(file);
};
Action<DownloadProgressChangedEventArgs> onDownloadProgress = i =>
void OnDownloadProgress(long total, long read, int progressPercentage)
{
var dataReceived = 0.0f;
var dataTotal = 0.0f;
var mag = 0;
var dataSuffix = "";
if (i.TotalBytesToReceive < 0)
if (total < 0)
{
mag = (int)Math.Log(i.BytesReceived, 1024);
dataReceived = i.BytesReceived / (float)(1L << (mag * 10));
mag = (int)Math.Log(read, 1024);
dataReceived = read / (float)(1L << (mag * 10));
dataSuffix = SizeSuffixes[mag];
getStatusText = () => "Downloading from {2} {0:0.00} {1}".F(dataReceived,
dataSuffix,
downloadHost ?? "unknown host");
getStatusText = () => "Downloading from {2} {0:0.00} {1}".F(dataReceived, dataSuffix, downloadHost ?? "unknown host");
progressBar.Indeterminate = true;
}
else
{
mag = (int)Math.Log(i.TotalBytesToReceive, 1024);
dataTotal = i.TotalBytesToReceive / (float)(1L << (mag * 10));
dataReceived = i.BytesReceived / (float)(1L << (mag * 10));
mag = (int)Math.Log(total, 1024);
dataTotal = total / (float)(1L << (mag * 10));
dataReceived = read / (float)(1L << (mag * 10));
dataSuffix = SizeSuffixes[mag];
getStatusText = () => "Downloading from {4} {1:0.00}/{2:0.00} {3} ({0}%)".F(i.ProgressPercentage,
dataReceived, dataTotal, dataSuffix,
downloadHost ?? "unknown host");
getStatusText = () => "Downloading from {4} {1:0.00}/{2:0.00} {3} ({0}%)".F(progressPercentage, dataReceived, dataTotal, dataSuffix, downloadHost ?? "unknown host");
progressBar.Indeterminate = false;
}
progressBar.Percentage = i.ProgressPercentage;
};
progressBar.Percentage = progressPercentage;
}
Action<string> onExtractProgress = s => Game.RunAfterTick(() => getStatusText = () => s);
@@ -120,137 +108,140 @@ namespace OpenRA.Mods.Common.Widgets.Logic
cancelButton.OnClick = Ui.CloseWindow;
});
Action<AsyncCompletedEventArgs> onDownloadComplete = i =>
{
if (i.Cancelled)
{
deleteTempFile();
Game.RunAfterTick(Ui.CloseWindow);
return;
}
if (i.Error != null)
{
deleteTempFile();
onError(Download.FormatErrorMessage(i.Error));
return;
}
// Validate integrity
if (!string.IsNullOrEmpty(download.SHA1))
{
getStatusText = () => "Verifying archive...";
progressBar.Indeterminate = true;
var archiveValid = false;
try
{
using (var stream = File.OpenRead(file))
{
var archiveSHA1 = CryptoUtil.SHA1Hash(stream);
Log.Write("install", "Downloaded SHA1: " + archiveSHA1);
Log.Write("install", "Expected SHA1: " + download.SHA1);
archiveValid = archiveSHA1 == download.SHA1;
}
}
catch (Exception e)
{
Log.Write("install", "SHA1 calculation failed: " + e.ToString());
}
if (!archiveValid)
{
onError("Archive validation failed");
deleteTempFile();
return;
}
}
// Automatically extract
getStatusText = () => "Extracting...";
progressBar.Indeterminate = true;
var extracted = new List<string>();
try
{
using (var stream = File.OpenRead(file))
using (var z = new ZipFile(stream))
{
foreach (var kv in download.Extract)
{
var entry = z.GetEntry(kv.Value);
if (entry == null || !entry.IsFile)
continue;
onExtractProgress("Extracting " + entry.Name);
Log.Write("install", "Extracting " + entry.Name);
var targetPath = Platform.ResolvePath(kv.Key);
Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
extracted.Add(targetPath);
using (var zz = z.GetInputStream(entry))
using (var f = File.Create(targetPath))
zz.CopyTo(f);
}
z.Close();
}
Game.RunAfterTick(() => { Ui.CloseWindow(); onSuccess(); });
}
catch (Exception e)
{
Log.Write("install", "Archive extraction failed: " + e.ToString());
foreach (var f in extracted)
{
Log.Write("install", "Deleting " + f);
File.Delete(f);
}
onError("Archive extraction failed");
}
finally
{
deleteTempFile();
}
};
Action<string> downloadUrl = url =>
{
Log.Write("install", "Downloading " + url);
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
downloadHost = new Uri(url).Host;
var dl = new Download(url, file, onDownloadProgress, onDownloadComplete);
cancelButton.OnClick = dl.CancelAsync;
cancelButton.OnClick = () =>
{
tokenSource.Cancel();
Game.RunAfterTick(Ui.CloseWindow);
};
retryButton.OnClick = ShowDownloadDialog;
Task.Run(async () =>
{
var file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
try
{
var client = HttpClientFactory.Create();
var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, token);
using (var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, 8192, true))
{
await response.ReadAsStreamWithProgress(fileStream, OnDownloadProgress, token);
}
// Validate integrity
if (!string.IsNullOrEmpty(download.SHA1))
{
getStatusText = () => "Verifying archive...";
progressBar.Indeterminate = true;
var archiveValid = false;
try
{
using (var stream = File.OpenRead(file))
{
var archiveSHA1 = CryptoUtil.SHA1Hash(stream);
Log.Write("install", "Downloaded SHA1: " + archiveSHA1);
Log.Write("install", "Expected SHA1: " + download.SHA1);
archiveValid = archiveSHA1 == download.SHA1;
}
}
catch (Exception e)
{
Log.Write("install", "SHA1 calculation failed: " + e.ToString());
}
if (!archiveValid)
{
onError("Archive validation failed");
return;
}
}
// Automatically extract
getStatusText = () => "Extracting...";
progressBar.Indeterminate = true;
var extracted = new List<string>();
try
{
using (var stream = File.OpenRead(file))
using (var z = new ZipFile(stream))
{
foreach (var kv in download.Extract)
{
var entry = z.GetEntry(kv.Value);
if (entry == null || !entry.IsFile)
continue;
onExtractProgress("Extracting " + entry.Name);
Log.Write("install", "Extracting " + entry.Name);
var targetPath = Platform.ResolvePath(kv.Key);
Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
extracted.Add(targetPath);
using (var zz = z.GetInputStream(entry))
using (var f = File.Create(targetPath))
zz.CopyTo(f);
}
z.Close();
}
Game.RunAfterTick(() =>
{
Ui.CloseWindow();
onSuccess();
});
}
catch (Exception e)
{
Log.Write("install", "Archive extraction failed: " + e.ToString());
foreach (var f in extracted)
{
Log.Write("install", "Deleting " + f);
File.Delete(f);
}
onError("Archive extraction failed");
}
}
catch (Exception e)
{
onError(e.ToString());
}
finally
{
File.Delete(file);
}
}, token);
};
if (download.MirrorList != null)
{
Log.Write("install", "Fetching mirrors from " + download.MirrorList);
Action<DownloadDataCompletedEventArgs> onFetchMirrorsComplete = i =>
Task.Run(async () =>
{
progressBar.Indeterminate = true;
if (i.Cancelled)
{
Game.RunAfterTick(Ui.CloseWindow);
return;
}
if (i.Error != null)
{
onError(Download.FormatErrorMessage(i.Error));
return;
}
var client = HttpClientFactory.Create();
var httpResponseMessage = await client.GetAsync(download.MirrorList);
var result = await httpResponseMessage.Content.ReadAsStringAsync();
try
{
var data = Encoding.UTF8.GetString(i.Result);
var mirrorList = data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
var mirrorList = result.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
downloadUrl(mirrorList.Random(new MersenneTwister()));
}
catch (Exception e)
@@ -259,11 +250,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
Log.Write("install", e.ToString());
onError("Online mirror is not available. Please install from an original disc.");
}
};
var updateMirrors = new Download(download.MirrorList, onDownloadProgress, onFetchMirrorsComplete);
cancelButton.OnClick = updateMirrors.CancelAsync;
retryButton.OnClick = ShowDownloadDialog;
});
}
else
downloadUrl(download.URL);

View File

@@ -14,7 +14,9 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenRA.Network;
using OpenRA.Support;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
@@ -274,19 +276,47 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{
if (!fetchedNews)
{
// Send the mod and engine version to support version-filtered news (update prompts)
var newsURL = "{0}?version={1}&mod={2}&modversion={3}".F(
webServices.GameNews,
Uri.EscapeUriString(Game.EngineVersion),
Uri.EscapeUriString(Game.ModData.Manifest.Id),
Uri.EscapeUriString(Game.ModData.Manifest.Metadata.Version));
Task.Run(async () =>
{
try
{
var client = HttpClientFactory.Create();
// Parameter string is blank if the player has opted out
newsURL += SystemInfoPromptLogic.CreateParameterString();
// Send the mod and engine version to support version-filtered news (update prompts)
var url = new HttpQueryBuilder(webServices.GameNews)
{
{ "version", Game.EngineVersion },
{ "mod", Game.ModData.Manifest.Id },
{ "modversion", Game.ModData.Manifest.Metadata.Version }
}.ToString();
new Download(newsURL, cacheFile, e => { },
e => NewsDownloadComplete(e, cacheFile, currentNews,
() => OpenNewsPanel(newsButton)));
// Parameter string is blank if the player has opted out
url += SystemInfoPromptLogic.CreateParameterString();
var response = await client.GetStringAsync(url);
await File.WriteAllTextAsync(cacheFile, response);
Game.RunAfterTick(() => // run on the main thread
{
fetchedNews = true;
var newNews = ParseNews(cacheFile);
if (newNews == null)
return;
DisplayNews(newNews);
if (currentNews == null || newNews.Any(n => !currentNews.Select(c => c.DateTime).Contains(n.DateTime)))
OpenNewsPanel(newsButton);
});
}
catch (Exception e)
{
Game.RunAfterTick(() => // run on the main thread
{
SetNewsStatus("Failed to retrieve news: {0}".F(e));
});
}
});
}
newsButton.OnClick = () => OpenNewsPanel(newsButton);
@@ -364,28 +394,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return null;
}
void NewsDownloadComplete(AsyncCompletedEventArgs e, string cacheFile, NewsItem[] oldNews, Action onNewsDownloaded)
{
Game.RunAfterTick(() => // run on the main thread
{
if (e.Error != null)
{
SetNewsStatus("Failed to retrieve news: {0}".F(Download.FormatErrorMessage(e.Error)));
return;
}
fetchedNews = true;
var newNews = ParseNews(cacheFile);
if (newNews == null)
return;
DisplayNews(newNews);
if (oldNews == null || newNews.Any(n => !oldNews.Select(c => c.DateTime).Contains(n.DateTime)))
onNewsDownloaded();
});
}
void DisplayNews(IEnumerable<NewsItem> newsItems)
{
newsPanel.RemoveChildren();

View File

@@ -11,10 +11,10 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using OpenRA.Graphics;
using OpenRA.Network;
using OpenRA.Support;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
@@ -157,79 +157,81 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var messageText = "Loading player profile...";
var messageWidth = messageFont.Measure(messageText).X + 2 * message.Bounds.Left;
Action<DownloadDataCompletedEventArgs> onQueryComplete = i =>
Task.Run(async () =>
{
try
{
if (i.Error == null)
var httpClient = HttpClientFactory.Create();
var httpResponseMessage = await httpClient.GetAsync(playerDatabase.Profile + client.Fingerprint);
var result = await httpResponseMessage.Content.ReadAsStringAsync();
var yaml = MiniYaml.FromString(result).First();
if (yaml.Key == "Player")
{
var yaml = MiniYaml.FromString(Encoding.UTF8.GetString(i.Result)).First();
if (yaml.Key == "Player")
profile = FieldLoader.Load<PlayerProfile>(yaml.Value);
Game.RunAfterTick(() =>
{
profile = FieldLoader.Load<PlayerProfile>(yaml.Value);
Game.RunAfterTick(() =>
var nameLabel = profileHeader.Get<LabelWidget>("PROFILE_NAME");
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
var rankLabel = profileHeader.Get<LabelWidget>("PROFILE_RANK");
var rankFont = Game.Renderer.Fonts[rankLabel.Font];
var adminContainer = profileHeader.Get("GAME_ADMIN");
var adminLabel = adminContainer.Get<LabelWidget>("LABEL");
var adminFont = Game.Renderer.Fonts[adminLabel.Font];
var headerSizeOffset = profileHeader.Bounds.Height - messageHeader.Bounds.Height;
nameLabel.GetText = () => profile.ProfileName;
rankLabel.GetText = () => profile.ProfileRank;
profileWidth = Math.Max(profileWidth, nameFont.Measure(profile.ProfileName).X + 2 * nameLabel.Bounds.Left);
profileWidth = Math.Max(profileWidth, rankFont.Measure(profile.ProfileRank).X + 2 * rankLabel.Bounds.Left);
header.Bounds.Height += headerSizeOffset;
badgeContainer.Bounds.Y += header.Bounds.Height;
if (client.IsAdmin)
{
var nameLabel = profileHeader.Get<LabelWidget>("PROFILE_NAME");
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
var rankLabel = profileHeader.Get<LabelWidget>("PROFILE_RANK");
var rankFont = Game.Renderer.Fonts[rankLabel.Font];
profileWidth = Math.Max(profileWidth, adminFont.Measure(adminLabel.Text).X + 2 * adminLabel.Bounds.Left);
var adminContainer = profileHeader.Get("GAME_ADMIN");
var adminLabel = adminContainer.Get<LabelWidget>("LABEL");
var adminFont = Game.Renderer.Fonts[adminLabel.Font];
adminContainer.IsVisible = () => true;
profileHeader.Bounds.Height += adminLabel.Bounds.Height;
header.Bounds.Height += adminLabel.Bounds.Height;
badgeContainer.Bounds.Y += adminLabel.Bounds.Height;
}
var headerSizeOffset = profileHeader.Bounds.Height - messageHeader.Bounds.Height;
Func<int, int> negotiateWidth = badgeWidth =>
{
profileWidth = Math.Min(Math.Max(badgeWidth, profileWidth), maxProfileWidth);
return profileWidth;
};
nameLabel.GetText = () => profile.ProfileName;
rankLabel.GetText = () => profile.ProfileRank;
profileWidth = Math.Max(profileWidth, nameFont.Measure(profile.ProfileName).X + 2 * nameLabel.Bounds.Left);
profileWidth = Math.Max(profileWidth, rankFont.Measure(profile.ProfileRank).X + 2 * rankLabel.Bounds.Left);
header.Bounds.Height += headerSizeOffset;
badgeContainer.Bounds.Y += header.Bounds.Height;
if (client.IsAdmin)
if (profile.Badges.Any())
{
var badges = Ui.LoadWidget("PLAYER_PROFILE_BADGES_INSERT", badgeContainer, new WidgetArgs()
{
profileWidth = Math.Max(profileWidth, adminFont.Measure(adminLabel.Text).X + 2 * adminLabel.Bounds.Left);
{ "worldRenderer", worldRenderer },
{ "profile", profile },
{ "negotiateWidth", negotiateWidth }
});
adminContainer.IsVisible = () => true;
profileHeader.Bounds.Height += adminLabel.Bounds.Height;
header.Bounds.Height += adminLabel.Bounds.Height;
badgeContainer.Bounds.Y += adminLabel.Bounds.Height;
if (badges.Bounds.Height > 0)
{
badgeContainer.Bounds.Height = badges.Bounds.Height;
badgeContainer.IsVisible = () => true;
}
}
Func<int, int> negotiateWidth = badgeWidth =>
{
profileWidth = Math.Min(Math.Max(badgeWidth, profileWidth), maxProfileWidth);
return profileWidth;
};
profileWidth = Math.Min(profileWidth, maxProfileWidth);
header.Bounds.Width = widget.Bounds.Width = badgeContainer.Bounds.Width = profileWidth;
widget.Bounds.Height = header.Bounds.Height + badgeContainer.Bounds.Height;
if (profile.Badges.Any())
{
var badges = Ui.LoadWidget("PLAYER_PROFILE_BADGES_INSERT", badgeContainer, new WidgetArgs()
{
{ "worldRenderer", worldRenderer },
{ "profile", profile },
{ "negotiateWidth", negotiateWidth }
});
if (badgeSeparator != null)
badgeSeparator.Bounds.Width = profileWidth - 2 * badgeSeparator.Bounds.X;
if (badges.Bounds.Height > 0)
{
badgeContainer.Bounds.Height = badges.Bounds.Height;
badgeContainer.IsVisible = () => true;
}
}
profileWidth = Math.Min(profileWidth, maxProfileWidth);
header.Bounds.Width = widget.Bounds.Width = badgeContainer.Bounds.Width = profileWidth;
widget.Bounds.Height = header.Bounds.Height + badgeContainer.Bounds.Height;
if (badgeSeparator != null)
badgeSeparator.Bounds.Width = profileWidth - 2 * badgeSeparator.Bounds.X;
profileLoaded = true;
});
}
profileLoaded = true;
});
}
}
catch (Exception e)
@@ -245,15 +247,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
header.Bounds.Width = widget.Bounds.Width = messageWidth;
}
}
};
});
message.GetText = () => messageText;
header.Bounds.Height += messageHeader.Bounds.Height;
header.Bounds.Width = widget.Bounds.Width = messageWidth;
widget.Bounds.Height = header.Bounds.Height;
badgeContainer.Visible = false;
new Download(playerDatabase.Profile + client.Fingerprint, _ => { }, onQueryComplete);
}
}

View File

@@ -12,12 +12,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using BeaconLib;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Server;
using OpenRA.Support;
using OpenRA.Traits;
using OpenRA.Widgets;
@@ -59,7 +59,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
SearchStatus searchStatus = SearchStatus.Fetching;
Download currentQuery;
bool activeQuery;
IEnumerable<BeaconLocation> lanGameLocations;
public string ProgressLabelText()
@@ -322,41 +322,48 @@ namespace OpenRA.Mods.Common.Widgets.Logic
public void RefreshServerList()
{
// Query in progress
if (currentQuery != null)
if (activeQuery)
return;
searchStatus = SearchStatus.Fetching;
Action<DownloadDataCompletedEventArgs> onComplete = i =>
var queryURL = new HttpQueryBuilder(services.ServerList)
{
currentQuery = null;
{ "protocol", GameServer.ProtocolVersion },
{ "engine", Game.EngineVersion },
{ "mod", Game.ModData.Manifest.Id },
{ "version", Game.ModData.Manifest.Metadata.Version }
}.ToString();
List<GameServer> games = null;
if (i.Error == null)
Task.Run(async () =>
{
var games = new List<GameServer>();
var client = HttpClientFactory.Create();
var httpResponseMessage = await client.GetAsync(queryURL);
var result = await httpResponseMessage.Content.ReadAsStringAsync();
activeQuery = true;
try
{
games = new List<GameServer>();
try
var yaml = MiniYaml.FromString(result);
foreach (var node in yaml)
{
var data = Encoding.UTF8.GetString(i.Result);
var yaml = MiniYaml.FromString(data);
foreach (var node in yaml)
try
{
try
{
var gs = new GameServer(node.Value);
if (gs.Address != null)
games.Add(gs);
}
catch
{
// Ignore any invalid games advertised.
}
var gs = new GameServer(node.Value);
if (gs.Address != null)
games.Add(gs);
}
catch
{
// Ignore any invalid games advertised.
}
}
catch
{
searchStatus = SearchStatus.Failed;
}
}
catch
{
searchStatus = SearchStatus.Failed;
}
var lanGames = new List<GameServer>();
@@ -398,15 +405,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
games = groupedLanGames.ToList();
Game.RunAfterTick(() => RefreshServerListInner(games));
};
var queryURL = services.ServerList + "?protocol={0}&engine={1}&mod={2}&version={3}".F(
GameServer.ProtocolVersion,
Uri.EscapeUriString(Game.EngineVersion),
Uri.EscapeUriString(Game.ModData.Manifest.Id),
Uri.EscapeUriString(Game.ModData.Manifest.Metadata.Version));
currentQuery = new Download(queryURL, _ => { }, onComplete);
activeQuery = false;
});
}
int GroupSortOrder(GameServer testEntry)