Extracting packages

This commit is contained in:
Paul Chote
2011-01-20 14:08:35 +13:00
parent 775ccc112b
commit 5ce5d48b04
4 changed files with 128 additions and 27 deletions

View File

@@ -38,10 +38,7 @@ namespace OpenRA.Graphics
Game.modData.LoadScreen.Display(); Game.modData.LoadScreen.Display();
foreach (var sequence in cursor.Nodes) foreach (var sequence in cursor.Nodes)
{
Console.WriteLine(sequence.Key);
cursors.Add(sequence.Key, new CursorSequence(cursorSrc, cursor.Value, sequence.Value)); cursors.Add(sequence.Key, new CursorSequence(cursorSrc, cursor.Value, sequence.Value));
}
} }
public static bool HasCursorSequence(string cursor) public static bool HasCursorSequence(string cursor)

View File

@@ -17,12 +17,16 @@ using System.Diagnostics;
using System; using System;
using System.Net; using System.Net;
using System.ComponentModel; using System.ComponentModel;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace OpenRA.Mods.RA.Widgets.Delegates namespace OpenRA.Mods.RA.Widgets.Delegates
{ {
public class GameInitDelegate : IWidgetDelegate public class GameInitDelegate : IWidgetDelegate
{ {
GameInitInfoWidget Info; GameInitInfoWidget Info;
Widget window;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public GameInitDelegate([ObjectCreator.Param] Widget widget) public GameInitDelegate([ObjectCreator.Param] Widget widget)
@@ -64,7 +68,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
void ShowInstallMethodDialog() void ShowInstallMethodDialog()
{ {
var window = Widget.OpenWindow("INIT_CHOOSEINSTALL"); window = Widget.OpenWindow("INIT_CHOOSEINSTALL");
window.GetWidget("DOWNLOAD").OnMouseUp = mi => { ShowDownloadDialog(); return true; }; window.GetWidget("DOWNLOAD").OnMouseUp = mi => { ShowDownloadDialog(); return true; };
window.GetWidget("FROMCD").OnMouseUp = mi => window.GetWidget("FROMCD").OnMouseUp = mi =>
{ {
@@ -77,20 +81,104 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
void ShowDownloadDialog() void ShowDownloadDialog()
{ {
var window = Widget.OpenWindow("INIT_DOWNLOAD"); window = Widget.OpenWindow("INIT_DOWNLOAD");
var status = window.GetWidget<LabelWidget>("STATUS"); var status = window.GetWidget<LabelWidget>("STATUS");
status.GetText = () => "Initializing..."; status.GetText = () => "Initializing...";
// TODO: Download to a temp location or the support dir
var file = Info.PackageName;
var progress = window.GetWidget<ProgressBarWidget>("PROGRESS"); var progress = window.GetWidget<ProgressBarWidget>("PROGRESS");
var dl = DownloadUrl(Info.PackageURL, Info.PackagePath, window.GetWidget<ButtonWidget>("EXTRACT").OnMouseUp = mi =>
(_,i) => { {
status.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived/1024, i.TotalBytesToReceive/1024); if (ExtractZip(file, Info.PackagePath))
progress.Percentage = i.ProgressPercentage; ContinueLoading(Info);
}, return true;
(_,i) => status.GetText = () => "Download Complete"); };
window.GetWidget("CANCEL").OnMouseUp = mi => { CancelDownload(dl); ShowInstallMethodDialog(); return true; };
if (File.Exists(file))
{
window.GetWidget<ButtonWidget>("EXTRACT").IsVisible = () => true;
status.GetText = () => "Download Cached";
progress.Percentage = 100;
}
else
{
var dl = DownloadUrl(Info.PackageURL, file,
(_,i) => {
status.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived/1024, i.TotalBytesToReceive/1024);
progress.Percentage = i.ProgressPercentage;
},
(_,i) => {
if (i.Error != null)
{
ShowDownloadError(i.Error.Message);
}
else
{
status.GetText = () => "Download Complete";
window.GetWidget<ButtonWidget>("EXTRACT").IsVisible = () => true;
window.GetWidget("CANCEL").IsVisible = () => false;
}
}
);
window.GetWidget("CANCEL").IsVisible = () => true;
window.GetWidget("RETRY").IsVisible = () => true;
window.GetWidget("CANCEL").OnMouseUp = mi => { dl.CancelAsync(); ShowInstallMethodDialog(); return true; };
window.GetWidget("RETRY").OnMouseUp = mi => { dl.CancelAsync(); ShowDownloadDialog(); return true; };
}
}
void ShowDownloadError(string e)
{
window.GetWidget<LabelWidget>("STATUS").GetText = () => e;
window.GetWidget<ButtonWidget>("RETRY").IsVisible = () => true;
window.GetWidget<ButtonWidget>("CANCEL").IsVisible = () => true;
}
// TODO: This needs to live on a different process if we want to run it as root
public bool ExtractZip(string zipFile, string path)
{
if (!File.Exists(zipFile)) { ShowDownloadError("Download Corrupted"); return false; }
List<string> extracted = new List<string>();
try
{
ZipEntry entry;
var z = new ZipInputStream(File.OpenRead(zipFile));
while ((entry = z.GetNextEntry()) != null)
{
if (!entry.IsFile) continue;
if (!Directory.Exists(Path.Combine(path, Path.GetDirectoryName(entry.Name))))
Directory.CreateDirectory(Path.Combine(path, Path.GetDirectoryName(entry.Name)));
window.GetWidget<LabelWidget>("STATUS").GetText = () => "Status: Extracting {0}".F(entry.Name);
var destPath = path + Path.DirectorySeparatorChar + entry.Name;
Console.WriteLine("Extracting to {0}",destPath);
extracted.Add(path);
using (var f = File.Create(destPath))
{
int bufSize = 2048;
byte[] buf = new byte[bufSize];
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
f.Write(buf, 0, bufSize);
}
}
z.Close();
}
catch (SharpZipBaseException)
{
foreach(var f in extracted)
File.Delete(f);
ShowDownloadError("Download Corrupted");
return false;
}
return true;
} }
void SelectDisk(Action<string> withPath) void SelectDisk(Action<string> withPath)
@@ -122,16 +210,10 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
wc.DownloadProgressChanged += onProgress; wc.DownloadProgressChanged += onProgress;
wc.DownloadFileCompleted += onComplete; wc.DownloadFileCompleted += onComplete;
wc.DownloadFileCompleted += (_,a) => {}; wc.DownloadFileCompleted += (_,a) => {Game.OnQuit -= () => wc.CancelAsync();};
wc.DownloadFileAsync(new Uri(url), path); wc.DownloadFileAsync(new Uri(url), path);
Game.OnQuit += () => CancelDownload(wc); Game.OnQuit += () => wc.CancelAsync();
return wc; return wc;
} }
public static void CancelDownload(WebClient wc)
{
Game.OnQuit -= () => CancelDownload(wc);
wc.CancelAsync();
}
} }
} }

View File

@@ -17,6 +17,7 @@ namespace OpenRA.Mods.RA.Widgets
public string TestFile; public string TestFile;
public string GameTitle; public string GameTitle;
public string PackageURL; public string PackageURL;
public string PackageName;
public string PackagePath; public string PackagePath;
public override void DrawInner() {} public override void DrawInner() {}

View File

@@ -1,9 +1,11 @@
GameInitInfo@INIT_SETUP: GameInitInfo@INIT_SETUP:
Id:INIT_SETUP Id:INIT_SETUP
TestFile: fakefile.mix TestFile: conquer.mix
GameTitle: Command & Conquer GameTitle: Command & Conquer
PackageURL:http://open-ra.org/get-dependency.php?file=cnc-packages # PackageURL:http://open-ra.org/get-dependency.php?file=cnc-packages
PackagePath:cnc-packages.zip PackageURL:http://localhost/~paul/cnc-packages.zip
PackageName:cnc-packages.zip
PackagePath:mods/cnc/packages
Delegate:GameInitDelegate Delegate:GameInitDelegate
Background@INIT_CHOOSEINSTALL: Background@INIT_CHOOSEINSTALL:
@@ -78,21 +80,40 @@ Background@INIT_DOWNLOAD:
ProgressBar@PROGRESS: ProgressBar@PROGRESS:
Id:PROGRESS Id:PROGRESS
X:50 X:50
Y:65 Y:55
Width:PARENT_RIGHT - 100 Width:PARENT_RIGHT - 100
Height:25 Height:25
Label@STATUS: Label@STATUS:
Id:STATUS Id:STATUS
X:50 X:50
Y:PARENT_BOTTOM - 50 Y:80
Width:PARENT_RIGHT - 100 Width:PARENT_RIGHT - 100
Height:25 Height:25
Align:Left 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: Button@CANCEL:
Id:CANCEL Id:CANCEL
X:PARENT_RIGHT - 140 X:PARENT_RIGHT - 140
Y:PARENT_BOTTOM - 45 Y:PARENT_BOTTOM - 45
Width:120 Width:120
Height:25 Height:25
Visible: false
Text:Cancel Text:Cancel
Bold:True Bold:True
Button@EXTRACT:
Id:EXTRACT
X:PARENT_RIGHT - 140
Y:PARENT_BOTTOM - 45
Width:120
Height:25
Visible: false
Text:Extract
Bold:True