diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index f01c21eef6..2393f90062 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -252,7 +252,7 @@ namespace OpenRA JoinLocal(); viewport = new Viewport(new int2(Renderer.Resolution), Rectangle.Empty, Renderer); - modData.WidgetLoader.LoadWidget( new Dictionary(), Widget.RootWidget, "MAINMENU_INIT" ); + modData.WidgetLoader.LoadWidget( new Dictionary(), Widget.RootWidget, "INIT_SETUP" ); } public static void LoadShellMap() @@ -273,6 +273,7 @@ namespace OpenRA } static bool quit; + public static event Action OnQuit = () => {}; internal static void Run() { while (!quit) @@ -280,6 +281,7 @@ namespace OpenRA Tick( orderManager, viewport ); Application.DoEvents(); } + OnQuit(); } public static void Exit() { quit = true; } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs index d5def98374..63315a036a 100755 --- a/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs @@ -15,6 +15,8 @@ using OpenRA.Server; using OpenRA.Widgets; using System.Diagnostics; using System; +using System.Net; +using System.ComponentModel; namespace OpenRA.Mods.RA.Widgets.Delegates { @@ -25,7 +27,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates [ObjectCreator.UseCtor] public GameInitDelegate([ObjectCreator.Param] Widget widget) { - Info = widget.GetWidget("INFO"); + Info = (widget as GameInitInfoWidget); Game.ConnectionStateChanged += orderManager => { Widget.CloseWindow(); @@ -56,23 +58,36 @@ namespace OpenRA.Mods.RA.Widgets.Delegates ContinueLoading(widget); else { - widget.GetWidget("INIT_DOWNLOAD").OnMouseUp = mi => - { - ContinueLoading(widget); - return true; - }; - - widget.GetWidget("INIT_FROMCD").OnMouseUp = mi => - { - SelectDisk(path => System.Console.WriteLine(path)); - return true; - }; - - widget.GetWidget("INIT_QUIT").OnMouseUp = mi => { Game.Exit(); return true; }; + ShowInstallMethodDialog(); } } + void ShowInstallMethodDialog() + { + var window = Widget.OpenWindow("INIT_CHOOSEINSTALL"); + window.GetWidget("DOWNLOAD").OnMouseUp = mi => { ShowDownloadDialog(); return true; }; + window.GetWidget("FROMCD").OnMouseUp = mi => + { + SelectDisk(path => System.Console.WriteLine(path)); + return true; + }; + + window.GetWidget("QUIT").OnMouseUp = mi => { Game.Exit(); return true; }; + } + void ShowDownloadDialog() + { + var window = Widget.OpenWindow("INIT_DOWNLOAD"); + var status = window.GetWidget("STATUS"); + status.GetText = () => "Initializing..."; + + var dl = DownloadUrl(Info.PackageURL, Info.PackagePath, + (_,i) => status.GetText = () => "{0}% {1}/{2} bytes".F(i.ProgressPercentage, i.BytesReceived, i.TotalBytesToReceive), + (_,i) => status.GetText = () => "Download Complete"); + + window.GetWidget("CANCEL").OnMouseUp = mi => { CancelDownload(dl); ShowInstallMethodDialog(); return true; }; + } + void SelectDisk(Action withPath) { Process p = new Process(); @@ -94,5 +109,24 @@ namespace OpenRA.Mods.RA.Widgets.Delegates Widget.RootWidget.Children.Remove(widget); Widget.OpenWindow("MAINMENU_BG"); } + + public static WebClient DownloadUrl(string url, string path, DownloadProgressChangedEventHandler onProgress, AsyncCompletedEventHandler onComplete) + { + WebClient wc = new WebClient(); + wc.Proxy = null; + + wc.DownloadProgressChanged += onProgress; + wc.DownloadFileCompleted += onComplete; + wc.DownloadFileCompleted += (_,a) => {}; + wc.DownloadFileAsync(new Uri(url), path); + Game.OnQuit += () => CancelDownload(wc); + return wc; + } + + public static void CancelDownload(WebClient wc) + { + Game.OnQuit -= () => CancelDownload(wc); + wc.CancelAsync(); + } } } diff --git a/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs b/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs index 4b67bb1e6e..063d1d9eaf 100755 --- a/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs +++ b/OpenRA.Mods.RA/Widgets/GameInitInfoWidget.cs @@ -16,6 +16,8 @@ namespace OpenRA.Mods.RA.Widgets { public string TestFile; public string GameTitle; + public string PackageURL; + public string PackagePath; public override void DrawInner() {} } diff --git a/mods/cnc/chrome/gameinit.yaml b/mods/cnc/chrome/gameinit.yaml new file mode 100644 index 0000000000..ec1aa20d21 --- /dev/null +++ b/mods/cnc/chrome/gameinit.yaml @@ -0,0 +1,97 @@ +GameInitInfo@INIT_SETUP: + Id:INIT_SETUP + TestFile: fakefile.mix + GameTitle: Command & Conquer + PackageURL:http://open-ra.org/get-dependency.php?file=cnc-packages + PackagePath:cnc-packages.zip + Delegate:GameInitDelegate + +Background@INIT_CHOOSEINSTALL: + Id:INIT_CHOOSEINSTALL + X:(WINDOW_RIGHT - WIDTH)/2 + Y:(WINDOW_BOTTOM - HEIGHT)/2 + Width:500 + Height:160 + Children: + Label@TITLE: + X:0 + Y:20 + Width:PARENT_RIGHT + Height:25 + Text:Install Required + Align:Center + Bold:True + Label@DESC1: + X:0 + Y:50 + Width:PARENT_RIGHT + Height:25 + Text:OpenRA requires the original C&C game content. + Align:Center + Label@DESC2: + X:0 + Y:70 + Width:PARENT_RIGHT + Height:25 + Text:Content can be downloaded automatically or installed from CD. + Align:Center + Button@DOWNLOAD: + Id:DOWNLOAD + X:PARENT_RIGHT - 420 + Y:PARENT_BOTTOM - 45 + Width:120 + Height:25 + Text:Download + Bold:True + Button@FROMCD: + Id:FROMCD + X:PARENT_RIGHT - 280 + Y:PARENT_BOTTOM - 45 + Width:120 + Height:25 + Text:From CD + Bold:True + Button@QUIT: + Id:QUIT + X:PARENT_RIGHT - 140 + Y:PARENT_BOTTOM - 45 + Width:120 + Height:25 + Text:Quit + Bold:True + +Background@INIT_DOWNLOAD: + Id:INIT_DOWNLOAD + X:(WINDOW_RIGHT - WIDTH)/2 + Y:(WINDOW_BOTTOM - HEIGHT)/2 + Width:500 + Height:160 + Children: + Label@TITLE: + X:0 + Y:20 + Width:PARENT_RIGHT + Height:25 + Text:Downloading C&C Content + Align:Center + Bold:True + TextField@PROGRESS: + X:50 + Y:65 + Width:PARENT_RIGHT - 100 + Height:25 + Label@STATUS: + Id:STATUS + X:50 + Y:PARENT_BOTTOM - 50 + Width:PARENT_RIGHT - 100 + Height:25 + Align:Left + Button@CANCEL: + Id:CANCEL + X:PARENT_RIGHT - 140 + Y:PARENT_BOTTOM - 45 + Width:120 + Height:25 + Text:Cancel + Bold:True diff --git a/mods/cnc/chrome/mainmenu.yaml b/mods/cnc/chrome/mainmenu.yaml index f71f3ea0fb..7e78b686fb 100644 --- a/mods/cnc/chrome/mainmenu.yaml +++ b/mods/cnc/chrome/mainmenu.yaml @@ -1,49 +1,3 @@ -Background@MAINMENU_INIT: - Id:MAINMENU_INIT - X:(WINDOW_RIGHT - WIDTH)/2 - Y:(WINDOW_BOTTOM - HEIGHT)/2 - Width:250 - Height:330 - Visible:true - Delegate:GameInitDelegate - Children: - GameInitInfo: - Id:INFO - TestFile: fakefile.mix - GameTitle: Command & Conquer - Label@MAINMENU_LABEL_TITLE: - Id:MAINMENU_LABEL_TITLE - X:0 - Y:20 - Width:250 - Height:25 - Text:Install Game Content - Align:Center - Bold:True - Button@INIT_DOWNLOAD: - Id:INIT_DOWNLOAD - X:45 - Y:70 - Width:160 - Height:25 - Text:Download - Bold:True - Button@INIT_FROMCD: - Id:INIT_FROMCD - X:45 - Y:110 - Width:160 - Height:25 - Text:From CD - Bold:True - Button@INIT_QUIT: - Id:INIT_QUIT - X:45 - Y:150 - Width:160 - Height:25 - Text:Quit - Bold:True Background@MAINMENU_BG: Id:MAINMENU_BG X:(WINDOW_RIGHT - WIDTH)/2 diff --git a/mods/cnc/mod.yaml b/mods/cnc/mod.yaml index 2504229c1c..b44b8dbd3c 100644 --- a/mods/cnc/mod.yaml +++ b/mods/cnc/mod.yaml @@ -56,6 +56,7 @@ Assemblies: mods/cnc/OpenRA.Mods.Cnc.dll ChromeLayout: + mods/cnc/chrome/gameinit.yaml mods/cnc/chrome/ingame.yaml mods/cnc/chrome/mainmenu.yaml mods/cnc/chrome/videoplayer.yaml