Package download works. Download error messages need polish.

This commit is contained in:
Paul Chote
2011-05-10 09:39:38 +12:00
parent 37577afc36
commit 3d493d3ace
4 changed files with 200 additions and 108 deletions

View File

@@ -27,29 +27,6 @@ namespace OpenRA.FileFormats
}
}
public static void ExtractZip(this ZipInputStream z, string destPath, List<string> extracted, Action<string> Extracting)
{
foreach (var entry in z.GetEntries())
{
if (!entry.IsFile) continue;
Extracting(entry.Name);
Directory.CreateDirectory(Path.Combine(destPath, Path.GetDirectoryName(entry.Name)));
var path = Path.Combine(destPath, entry.Name);
extracted.Add(path);
using (var f = File.Create(path))
{
int bufSize = 2048;
byte[] buf = new byte[bufSize];
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
f.Write(buf, 0, bufSize);
}
}
z.Close();
}
// TODO: The package should be mounted into its own context to avoid name collisions with installed files
public static bool ExtractFromPackage(string srcPath, string package, string[] files, string destPath, Action<string> onProgress, Action<string> onError)
{
@@ -93,5 +70,56 @@ namespace OpenRA.FileFormats
onProgress("Extraction complete");
return true;
}
public static bool ExtractZip(string zipFile, string dest, Action<string> onProgress, Action<string> onError)
{
if (!File.Exists(zipFile))
{
onError("Invalid path: "+zipFile);
return false;
}
List<string> extracted = new List<string>();
try
{
var z = new ZipInputStream(File.OpenRead(zipFile));
z.ExtractZip(dest, extracted, s => onProgress("Extracting "+s));
onProgress("Extraction complete");
}
catch (SharpZipBaseException)
{
foreach(var f in extracted)
File.Delete(f);
onError("Archive corrupt");
return false;
}
return true;
}
// TODO: this belongs in FileSystem/ZipFile
static void ExtractZip(this ZipInputStream z, string destPath, List<string> extracted, Action<string> onProgress)
{
foreach (var entry in z.GetEntries())
{
if (!entry.IsFile) continue;
onProgress(entry.Name);
Directory.CreateDirectory(Path.Combine(destPath, Path.GetDirectoryName(entry.Name)));
var path = Path.Combine(destPath, entry.Name);
extracted.Add(path);
using (var f = File.Create(path))
{
int bufSize = 2048;
byte[] buf = new byte[bufSize];
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
f.Write(buf, 0, bufSize);
}
}
z.Close();
}
}
}

View File

@@ -10,11 +10,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using OpenRA.FileFormats;
using OpenRA.Widgets;
using OpenRA.Mods.RA.Widgets.Delegates;
namespace OpenRA.Mods.Cnc.Widgets
{
@@ -41,7 +44,6 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget<CncMenuButtonWidget>("QUIT_BUTTON").OnClick = Game.Exit;
// TODO:
panel.GetWidget<CncMenuButtonWidget>("DOWNLOAD_BUTTON").IsDisabled = () => true;
panel.GetWidget<CncMenuButtonWidget>("MODS_BUTTON").IsDisabled = () => true;
}
}
@@ -136,4 +138,93 @@ namespace OpenRA.Mods.Cnc.Widgets
t.Start();
}
}
public class CncDownloadPackagesLogic : IWidgetDelegate
{
Widget panel;
Dictionary<string,string> installData;
ProgressBarWidget progressBar;
LabelWidget statusLabel;
Action continueLoading;
[ObjectCreator.UseCtor]
public CncDownloadPackagesLogic([ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] Dictionary<string,string> installData,
[ObjectCreator.Param] Action continueLoading)
{
this.installData = installData;
this.continueLoading = continueLoading;
panel = widget.GetWidget("INSTALL_DOWNLOAD_PANEL");
progressBar = panel.GetWidget<ProgressBarWidget>("PROGRESS_BAR");
statusLabel = panel.GetWidget<LabelWidget>("STATUS_LABEL");
ShowDownloadDialog();
}
void ShowDownloadDialog()
{
statusLabel.GetText = () => "Initializing...";
progressBar.SetIndeterminate(false);
var retryButton = panel.GetWidget<CncMenuButtonWidget>("RETRY_BUTTON");
retryButton.IsVisible = () => false;
var cancelButton = panel.GetWidget<CncMenuButtonWidget>("CANCEL_BUTTON");
// Save the package to a temp file
var file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var dest = new string[] { Platform.SupportDir, "Content", "cnc" }.Aggregate(Path.Combine);
Action<DownloadProgressChangedEventArgs> onDownloadProgress = i =>
{
progressBar.Percentage = i.ProgressPercentage;
statusLabel.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived / 1024, i.TotalBytesToReceive / 1024);
};
Action<string> onExtractProgress = s =>
{
statusLabel.GetText = () => s;
};
Action<string> onError = s =>
{
statusLabel.GetText = () => "Error: "+s;
retryButton.IsVisible = () => true;
};
Action<AsyncCompletedEventArgs, bool> onDownloadComplete = (i, cancelled) =>
{
if (i.Error != null)
{
onError(i.Error.Message);
return;
}
else if (cancelled)
{
onError("Download cancelled");
return;
}
// Automatically extract
statusLabel.GetText = () => "Extracting...";
progressBar.SetIndeterminate(true);
if (InstallUtils.ExtractZip(file, dest, onExtractProgress, onError))
{
Game.RunAfterTick(() =>
{
Widget.CloseWindow(); // Progress panel
Widget.CloseWindow(); // Install choice panel
continueLoading();
});
}
};
var dl = new Download(installData["PackageURL"], file, onDownloadProgress, onDownloadComplete);
cancelButton.OnClick = () => { dl.Cancel(); Widget.CloseWindow(); };
retryButton.OnClick = () => { dl.Cancel(); ShowDownloadDialog(); };
}
}
}

View File

@@ -142,7 +142,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
var progress = window.GetWidget<ProgressBarWidget>("PROGRESS");
// Save the package to a temp file
var file = Path.GetTempPath() + Path.DirectorySeparatorChar + Path.GetRandomFileName();
var file = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Action<DownloadProgressChangedEventArgs> onDownloadChange = i =>
{
status.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived / 1024, i.TotalBytesToReceive / 1024);
@@ -181,27 +181,10 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
bool ExtractZip(Widget window, string zipFile, string dest)
{
if (!File.Exists(zipFile))
{
ShowError(window, "Invalid path: "+zipFile);
return false;
}
var status = window.GetWidget<LabelWidget>("STATUS");
List<string> extracted = new List<string>();
try
{
new ZipInputStream(File.OpenRead(zipFile)).ExtractZip(dest, extracted, s => status.GetText = () => "Extracting "+s);
}
catch (SharpZipBaseException)
{
foreach(var f in extracted)
File.Delete(f);
ShowError(window, "Archive corrupt");
return false;
}
status.GetText = () => "Extraction complete";
return true;
return InstallUtils.ExtractZip( zipFile, dest,
s => status.GetText = () => s,
e => ShowError(window, e));
}
bool ExtractFromPackage(Widget window, string srcPath, string package, string[] files, string destPath)
@@ -216,19 +199,9 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
bool CopyFiles(Widget window, string srcPath, string[] files, string destPath)
{
var status = window.GetWidget<LabelWidget>("STATUS");
foreach (var file in files)
{
var fromPath = Path.Combine(srcPath, file);
if (!File.Exists(fromPath))
{
ShowError(window, "Cannot find "+file);
return false;
}
status.GetText = () => "Extracting "+file.ToLowerInvariant();
File.Copy(fromPath, Path.Combine(destPath, Path.GetFileName(file).ToLowerInvariant()), true);
}
return true;
return InstallUtils.CopyFiles(srcPath, files, destPath,
s => status.GetText = () => s,
e => ShowError(window, e));
}
bool InstallRAPackages(Widget window, string source, string dest)

View File

@@ -71,6 +71,7 @@ Container@INSTALL_PANEL:
Width:140
Height:35
Text:Use CD
Container@INSTALL_FROMCD_PANEL:
Id:INSTALL_FROMCD_PANEL
Delegate:CncInstallFromCDLogic
@@ -123,57 +124,56 @@ Container@INSTALL_FROMCD_PANEL:
Width:140
Height:35
Text:Retry
Background@INIT_DOWNLOAD:
Id:INIT_DOWNLOAD
Container@INSTALL_DOWNLOAD_PANEL:
Id:INSTALL_DOWNLOAD_PANEL
Delegate:CncDownloadPackagesLogic
X:(WINDOW_RIGHT - WIDTH)/2
Y:(WINDOW_BOTTOM - HEIGHT)/2
Width:500
Height:160
Y:(WINDOW_BOTTOM - 150)/2
Width:640
Height:150
Children:
Label@TITLE:
X:0
Y:20
Width:PARENT_RIGHT
Height:25
Text:Downloading C&C Content
Y:0-25
Font:BigBold
Contrast:true
Align:Center
Bold:True
ProgressBar@PROGRESS:
Id:PROGRESS
X:50
Y:55
Width:PARENT_RIGHT - 100
Height:25
Label@STATUS:
Id:STATUS
X:50
Y:80
Width:PARENT_RIGHT - 100
Text:Downloading Content
Background@bg:
Width:640
Height:150
Background:panel-black
Children:
Image@INSTALL:
X:11
Y:11
ImageCollection: install
ImageName: logo
ProgressBar@PROGRESS_BAR:
Id:PROGRESS_BAR
X:170
Y:45
Width:PARENT_RIGHT - 185
Height:35
Label@STATUS_LABEL:
Id:STATUS_LABEL
X:170
Y:85
Width:PARENT_RIGHT - 185
Height:25
Align:Left
Button@RETRY:
Id:RETRY
X:PARENT_RIGHT - 280
Y:PARENT_BOTTOM - 45
Width:120
Height:25
Visible: false
Text:Retry
Bold:True
Button@CANCEL:
Id:CANCEL
X:PARENT_RIGHT - 140
Y:PARENT_BOTTOM - 45
Width:120
Height:25
Text:Initialising...
CncMenuButton@CANCEL_BUTTON:
Id:CANCEL_BUTTON
Y:149
Width:140
Height:35
Text:Cancel
Bold:True
Button@EXTRACT:
Id:EXTRACT
X:PARENT_RIGHT - 140
Y:PARENT_BOTTOM - 45
Width:120
Height:25
Visible: false
Text:Extract
Bold:True
CncMenuButton@RETRY_BUTTON:
Id:RETRY_BUTTON
X:500
Y:149
Width:140
Height:35
Text:Retry