Enable mod asset package downloading from the modchooser

This commit is contained in:
Pavel Penev
2015-09-04 13:22:21 +03:00
parent f0a2e58ccd
commit bdcb0fee58
4 changed files with 84 additions and 25 deletions

View File

@@ -9,11 +9,10 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Text;
using OpenRA.Support; using OpenRA.Support;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -21,24 +20,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
public class DownloadPackagesLogic public class DownloadPackagesLogic
{ {
static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
readonly Widget panel; readonly Widget panel;
readonly string modId;
readonly string mirrorListUrl; readonly string mirrorListUrl;
readonly ProgressBarWidget progressBar; readonly ProgressBarWidget progressBar;
readonly LabelWidget statusLabel; readonly LabelWidget statusLabel;
readonly Action afterInstall; readonly Action afterInstall;
string mirror; string mirror;
static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public DownloadPackagesLogic(Widget widget, Action afterInstall, string mirrorListUrl) public DownloadPackagesLogic(Widget widget, Action afterInstall, string mirrorListUrl, string modId)
{ {
this.mirrorListUrl = mirrorListUrl; this.mirrorListUrl = mirrorListUrl;
this.afterInstall = afterInstall; this.afterInstall = afterInstall;
this.modId = modId;
panel = widget.Get("INSTALL_DOWNLOAD_PANEL"); panel = widget.Get("INSTALL_DOWNLOAD_PANEL");
progressBar = panel.Get<ProgressBarWidget>("PROGRESS_BAR"); progressBar = panel.Get<ProgressBarWidget>("PROGRESS_BAR");
statusLabel = panel.Get<LabelWidget>("STATUS_LABEL"); statusLabel = panel.Get<LabelWidget>("STATUS_LABEL");
var text = "Downloading {0} assets...".F(ModMetadata.AllMods[modId].Title);
panel.Get<LabelWidget>("TITLE").Text = text;
ShowDownloadDialog(); ShowDownloadDialog();
} }
@@ -52,9 +56,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var cancelButton = panel.Get<ButtonWidget>("CANCEL_BUTTON"); var cancelButton = panel.Get<ButtonWidget>("CANCEL_BUTTON");
var mirrorsFile = Platform.ResolvePath("^", "Content", Game.ModData.Manifest.Mod.Id, "mirrors.txt");
var file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); var file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var dest = Platform.ResolvePath("^", "Content", Game.ModData.Manifest.Mod.Id); var dest = Platform.ResolvePath("^", "Content", modId);
Action<DownloadProgressChangedEventArgs> onDownloadProgress = i => Action<DownloadProgressChangedEventArgs> onDownloadProgress = i =>
{ {
@@ -100,7 +103,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic
onError(Download.FormatErrorMessage(i.Error)); onError(Download.FormatErrorMessage(i.Error));
return; return;
} }
else if (cancelled)
if (cancelled)
{ {
onError("Download cancelled"); onError("Download cancelled");
return; return;
@@ -119,7 +123,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
}; };
Action<AsyncCompletedEventArgs, bool> onFetchMirrorsComplete = (i, cancelled) => Action<DownloadDataCompletedEventArgs, bool> onFetchMirrorsComplete = (i, cancelled) =>
{ {
progressBar.Indeterminate = true; progressBar.Indeterminate = true;
@@ -128,21 +132,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
onError(Download.FormatErrorMessage(i.Error)); onError(Download.FormatErrorMessage(i.Error));
return; return;
} }
else if (cancelled)
if (cancelled)
{ {
onError("Download cancelled"); onError("Download cancelled");
return; return;
} }
var mirrorList = new List<string>(); var data = Encoding.UTF8.GetString(i.Result);
using (var r = new StreamReader(mirrorsFile)) var mirrorList = data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
{
string line;
while ((line = r.ReadLine()) != null)
if (!string.IsNullOrEmpty(line))
mirrorList.Add(line);
}
mirror = mirrorList.Random(new MersenneTwister()); mirror = mirrorList.Random(new MersenneTwister());
// Save the package to a temp file // Save the package to a temp file
@@ -152,7 +150,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
}; };
// Get the list of mirrors // Get the list of mirrors
var updateMirrors = new Download(mirrorListUrl, mirrorsFile, onDownloadProgress, onFetchMirrorsComplete); var updateMirrors = new Download(mirrorListUrl, onDownloadProgress, onFetchMirrorsComplete);
cancelButton.OnClick = () => { updateMirrors.Cancel(); Ui.CloseWindow(); }; cancelButton.OnClick = () => { updateMirrors.Cancel(); Ui.CloseWindow(); };
retryButton.OnClick = () => { updateMirrors.Cancel(); ShowDownloadDialog(); }; retryButton.OnClick = () => { updateMirrors.Cancel(); ShowDownloadDialog(); };
} }

View File

@@ -9,7 +9,6 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic namespace OpenRA.Mods.Common.Widgets.Logic
@@ -17,17 +16,21 @@ namespace OpenRA.Mods.Common.Widgets.Logic
public class InstallLogic : Widget public class InstallLogic : Widget
{ {
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public InstallLogic(Widget widget, Action continueLoading) public InstallLogic(Widget widget, Action continueLoading, string mirrorListUrl, string modId)
{ {
var installData = Game.ModData.Manifest.Get<ContentInstaller>();
var panel = widget.Get("INSTALL_PANEL"); var panel = widget.Get("INSTALL_PANEL");
var widgetArgs = new WidgetArgs() var widgetArgs = new WidgetArgs
{ {
{ "afterInstall", () => { Ui.CloseWindow(); continueLoading(); } }, { "afterInstall", () => { Ui.CloseWindow(); continueLoading(); } },
{ "continueLoading", continueLoading }, { "continueLoading", continueLoading },
{ "mirrorListUrl", installData.PackageMirrorList }, { "mirrorListUrl", mirrorListUrl },
{ "modId", modId }
}; };
var modName = ModMetadata.AllMods[modId].Title;
var text = panel.Get<LabelWidget>("DESC1").Text;
panel.Get<LabelWidget>("DESC1").Text = text.F(modName);
panel.Get<ButtonWidget>("DOWNLOAD_BUTTON").OnClick = () => panel.Get<ButtonWidget>("DOWNLOAD_BUTTON").OnClick = () =>
Ui.OpenWindow("INSTALL_DOWNLOAD_PANEL", widgetArgs); Ui.OpenWindow("INSTALL_DOWNLOAD_PANEL", widgetArgs);

View File

@@ -167,7 +167,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var widgetArgs = new WidgetArgs var widgetArgs = new WidgetArgs
{ {
{ "continueLoading", () => { "continueLoading", () =>
Game.RunAfterTick(() => Game.InitializeMod(Game.Settings.Game.Mod, new Arguments())) } Game.RunAfterTick(() => Game.InitializeMod(Game.Settings.Game.Mod, new Arguments())) },
{ "mirrorListUrl", mod.Content.PackageMirrorList },
{ "modId", mod.Id }
}; };
Ui.OpenWindow("INSTALL_PANEL", widgetArgs); Ui.OpenWindow("INSTALL_PANEL", widgetArgs);

View File

@@ -62,3 +62,59 @@ Container@INSTALL_PANEL:
Font: Bold Font: Bold
Key: escape Key: escape
Container@INSTALL_DOWNLOAD_PANEL:
Logic: DownloadPackagesLogic
X: (WINDOW_RIGHT - WIDTH)/2
Y: (WINDOW_BOTTOM - HEIGHT)/2
Width: 500
Height: 177
Children:
Background:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
Background: panel-bg
Background@RULE:
X: 30
Y: 50
Width: 440
Height:150
Background:panel-rule
Label@TITLE:
X: 0
Y: 12
Width: PARENT_RIGHT
Height: 25
Align: Center
Font: MediumBold
ProgressBar@PROGRESS_BAR:
X: 50
Y: 64
Width: PARENT_RIGHT - 100
Height: 16
BarMargin: 0, 0
Label@STATUS_LABEL:
X: 36
Y: 85
Width: PARENT_RIGHT - 100
Height: 25
Align: Left
Button@RETRY_BUTTON:
X: PARENT_RIGHT - 280
Y: PARENT_BOTTOM - 52
Background:button-highlighted
Width: 120
Height: 32
Visible: false
Text: Retry
Font: Bold
Key: return
Button@CANCEL_BUTTON:
X: PARENT_RIGHT - 130
Y: PARENT_BOTTOM - 52
Background:button-highlighted
Width: 110
Height: 32
Text: Cancel
Font: Bold
Key: escape