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 // 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) 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"); onProgress("Extraction complete");
return true; 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading; using System.Threading;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Widgets; using OpenRA.Widgets;
using OpenRA.Mods.RA.Widgets.Delegates;
namespace OpenRA.Mods.Cnc.Widgets namespace OpenRA.Mods.Cnc.Widgets
{ {
@@ -41,7 +44,6 @@ namespace OpenRA.Mods.Cnc.Widgets
panel.GetWidget<CncMenuButtonWidget>("QUIT_BUTTON").OnClick = Game.Exit; panel.GetWidget<CncMenuButtonWidget>("QUIT_BUTTON").OnClick = Game.Exit;
// TODO: // TODO:
panel.GetWidget<CncMenuButtonWidget>("DOWNLOAD_BUTTON").IsDisabled = () => true;
panel.GetWidget<CncMenuButtonWidget>("MODS_BUTTON").IsDisabled = () => true; panel.GetWidget<CncMenuButtonWidget>("MODS_BUTTON").IsDisabled = () => true;
} }
} }
@@ -136,4 +138,93 @@ namespace OpenRA.Mods.Cnc.Widgets
t.Start(); 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"); var progress = window.GetWidget<ProgressBarWidget>("PROGRESS");
// Save the package to a temp file // 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 => Action<DownloadProgressChangedEventArgs> onDownloadChange = i =>
{ {
status.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived / 1024, i.TotalBytesToReceive / 1024); 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) 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"); var status = window.GetWidget<LabelWidget>("STATUS");
List<string> extracted = new List<string>(); return InstallUtils.ExtractZip( zipFile, dest,
try s => status.GetText = () => s,
{ e => ShowError(window, e));
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;
} }
bool ExtractFromPackage(Widget window, string srcPath, string package, string[] files, string destPath) 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) bool CopyFiles(Widget window, string srcPath, string[] files, string destPath)
{ {
var status = window.GetWidget<LabelWidget>("STATUS"); var status = window.GetWidget<LabelWidget>("STATUS");
return InstallUtils.CopyFiles(srcPath, files, destPath,
foreach (var file in files) s => status.GetText = () => s,
{ e => ShowError(window, e));
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;
} }
bool InstallRAPackages(Widget window, string source, string dest) bool InstallRAPackages(Widget window, string source, string dest)

View File

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