From cb93955cc5cf9bf1f1275f540e69a7265e41416c Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 19 Nov 2010 11:14:58 +1300 Subject: [PATCH] Generalize package downloading to support arbitrary urls and download locations. --- OpenRA.Utility/Command.cs | 44 +++++++++++++-------------------------- OpenRA.Utility/Program.cs | 4 ++-- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/OpenRA.Utility/Command.cs b/OpenRA.Utility/Command.cs index 7faf5bfe21..162af19db9 100644 --- a/OpenRA.Utility/Command.cs +++ b/OpenRA.Utility/Command.cs @@ -52,36 +52,26 @@ namespace OpenRA.Utility } } - public static void DownloadPackages(string argValue) + public static void DownloadUrl(string argValue) { string[] args = argValue.Split(','); - string mod = ""; - string destPath = Path.GetTempPath(); - - if (args.Length >= 1) - mod = args[0]; - if (args.Length >= 2) - destPath = args[1].Replace("~",Environment.GetFolderPath(Environment.SpecialFolder.Personal)); - - string destFile = string.Format("{0}{1}{2}-packages.zip", destPath, Path.DirectorySeparatorChar, mod); - - if (File.Exists(destFile)) + + if (args.Length != 2) { - Console.WriteLine("Downloaded file already exists, using it instead."); - Util.ExtractPackagesFromZip(mod, destPath); + Console.WriteLine("Error: invalid syntax"); return; } - + string url = args[0]; + string path = args[1].Replace("~",Environment.GetFolderPath(Environment.SpecialFolder.Personal)); + WebClient wc = new WebClient(); wc.DownloadProgressChanged += DownloadProgressChanged; wc.DownloadFileCompleted += DownloadFileCompleted; - Console.WriteLine("Downloading {0}-packages.zip to {1}", mod, destPath); - Console.WriteLine("Initializing..."); - wc.DownloadFileAsync( - new Uri(string.Format("http://open-ra.org/get-dependency.php?file={0}-packages", mod)), - destFile, - new string[] { mod, destPath }); + Console.WriteLine("Downloading {0} to {1}", url, path); + Console.WriteLine("Status: Initializing"); + + wc.DownloadFileAsync(new Uri(url), path); while (!completed) Thread.Sleep(500); } @@ -91,21 +81,15 @@ namespace OpenRA.Utility static void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { if (e.Error != null) - { Console.WriteLine("Error: {0}", e.Error.Message); - completed = true; - return; - } - - Console.WriteLine("Download Completed"); - string[] modAndDest = (string[])e.UserState; - Util.ExtractPackagesFromZip(modAndDest[0], modAndDest[1]); + else + Console.WriteLine("Status: Completed"); completed = true; } static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { - Console.WriteLine("{0}% {1}/{2} bytes", e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive); + Console.WriteLine("Status: {0}% {1}/{2} bytes", e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive); } public static void InstallRAPackages(string path) diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index cdbcb54f29..e81fc2b7bc 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -36,7 +36,7 @@ namespace OpenRA.Utility argCallbacks.Add("-l", Command.ListMods); argCallbacks.Add("--mod-info", Command.ListModInfo); argCallbacks.Add("-i", Command.ListModInfo); - argCallbacks.Add("--download-packages", Command.DownloadPackages); + argCallbacks.Add("--download-url", Command.DownloadUrl); argCallbacks.Add("--install-ra-packages", Command.InstallRAPackages); argCallbacks.Add("--install-cnc-packages", Command.InstallCncPackages); argCallbacks.Add("--settings-value", Command.Settings); @@ -81,7 +81,7 @@ namespace OpenRA.Utility Console.WriteLine(); Console.WriteLine(" -l,--list-mods List currently installed mods"); Console.WriteLine(" -i=MODS,--mod-info=MODS List metadata for MODS (comma separated list of mods)"); - Console.WriteLine(" --download-packages=MOD{,DEST} Download packages for MOD to DEST (def: system temp folder) and install them"); + Console.WriteLine(" --download-url=URL,DEST Download a file from URL to DEST"); Console.WriteLine(" --install-ra-packages=PATH Install required packages for RA from PATH to CD"); Console.WriteLine(" --install-cnc-packages=PATH Install required packages for C&C from PATH to CD"); Console.WriteLine(" --settings-value=SUPPORTDIR,KEY Get value of KEY in SUPPORTDIR/settings.yaml");