From bdcb0fee584be39000ebbe6637f35c19e5a50120 Mon Sep 17 00:00:00 2001 From: Pavel Penev Date: Fri, 4 Sep 2015 13:22:21 +0300 Subject: [PATCH] Enable mod asset package downloading from the modchooser --- .../Installation/DownloadPackagesLogic.cs | 36 ++++++------ .../Logic/Installation/InstallLogic.cs | 13 +++-- .../Widgets/Logic/ModBrowserLogic.cs | 4 +- mods/modchooser/install.yaml | 56 +++++++++++++++++++ 4 files changed, 84 insertions(+), 25 deletions(-) diff --git a/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackagesLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackagesLogic.cs index a79e0da3ef..bbeb55bb29 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackagesLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Installation/DownloadPackagesLogic.cs @@ -9,11 +9,10 @@ #endregion using System; -using System.Collections.Generic; using System.ComponentModel; using System.IO; -using System.Linq; using System.Net; +using System.Text; using OpenRA.Support; using OpenRA.Widgets; @@ -21,24 +20,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class DownloadPackagesLogic { + static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; readonly Widget panel; + readonly string modId; readonly string mirrorListUrl; readonly ProgressBarWidget progressBar; readonly LabelWidget statusLabel; readonly Action afterInstall; string mirror; - static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; [ObjectCreator.UseCtor] - public DownloadPackagesLogic(Widget widget, Action afterInstall, string mirrorListUrl) + public DownloadPackagesLogic(Widget widget, Action afterInstall, string mirrorListUrl, string modId) { this.mirrorListUrl = mirrorListUrl; this.afterInstall = afterInstall; + this.modId = modId; panel = widget.Get("INSTALL_DOWNLOAD_PANEL"); progressBar = panel.Get("PROGRESS_BAR"); statusLabel = panel.Get("STATUS_LABEL"); + var text = "Downloading {0} assets...".F(ModMetadata.AllMods[modId].Title); + panel.Get("TITLE").Text = text; + ShowDownloadDialog(); } @@ -52,9 +56,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic var cancelButton = panel.Get("CANCEL_BUTTON"); - var mirrorsFile = Platform.ResolvePath("^", "Content", Game.ModData.Manifest.Mod.Id, "mirrors.txt"); 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 onDownloadProgress = i => { @@ -100,7 +103,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic onError(Download.FormatErrorMessage(i.Error)); return; } - else if (cancelled) + + if (cancelled) { onError("Download cancelled"); return; @@ -119,7 +123,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic } }; - Action onFetchMirrorsComplete = (i, cancelled) => + Action onFetchMirrorsComplete = (i, cancelled) => { progressBar.Indeterminate = true; @@ -128,21 +132,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic onError(Download.FormatErrorMessage(i.Error)); return; } - else if (cancelled) + + if (cancelled) { onError("Download cancelled"); return; } - var mirrorList = new List(); - using (var r = new StreamReader(mirrorsFile)) - { - string line; - while ((line = r.ReadLine()) != null) - if (!string.IsNullOrEmpty(line)) - mirrorList.Add(line); - } - + var data = Encoding.UTF8.GetString(i.Result); + var mirrorList = data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); mirror = mirrorList.Random(new MersenneTwister()); // Save the package to a temp file @@ -152,7 +150,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic }; // 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(); }; retryButton.OnClick = () => { updateMirrors.Cancel(); ShowDownloadDialog(); }; } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Installation/InstallLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Installation/InstallLogic.cs index 4685a06e9c..d664368f3e 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Installation/InstallLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Installation/InstallLogic.cs @@ -9,7 +9,6 @@ #endregion using System; -using System.Collections.Generic; using OpenRA.Widgets; namespace OpenRA.Mods.Common.Widgets.Logic @@ -17,17 +16,21 @@ namespace OpenRA.Mods.Common.Widgets.Logic public class InstallLogic : Widget { [ObjectCreator.UseCtor] - public InstallLogic(Widget widget, Action continueLoading) + public InstallLogic(Widget widget, Action continueLoading, string mirrorListUrl, string modId) { - var installData = Game.ModData.Manifest.Get(); var panel = widget.Get("INSTALL_PANEL"); - var widgetArgs = new WidgetArgs() + var widgetArgs = new WidgetArgs { { "afterInstall", () => { Ui.CloseWindow(); continueLoading(); } }, { "continueLoading", continueLoading }, - { "mirrorListUrl", installData.PackageMirrorList }, + { "mirrorListUrl", mirrorListUrl }, + { "modId", modId } }; + var modName = ModMetadata.AllMods[modId].Title; + var text = panel.Get("DESC1").Text; + panel.Get("DESC1").Text = text.F(modName); + panel.Get("DOWNLOAD_BUTTON").OnClick = () => Ui.OpenWindow("INSTALL_DOWNLOAD_PANEL", widgetArgs); diff --git a/OpenRA.Mods.Common/Widgets/Logic/ModBrowserLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/ModBrowserLogic.cs index ca5af451db..3c0ab93760 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/ModBrowserLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/ModBrowserLogic.cs @@ -167,7 +167,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic var widgetArgs = new WidgetArgs { { "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); diff --git a/mods/modchooser/install.yaml b/mods/modchooser/install.yaml index 7580441d92..e9937e1857 100644 --- a/mods/modchooser/install.yaml +++ b/mods/modchooser/install.yaml @@ -62,3 +62,59 @@ Container@INSTALL_PANEL: Font: Bold 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 +