From 8e5f307ba8a91c17bf96cf1e4241b1ae0836d41c Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 22 Jan 2011 18:20:41 +1300 Subject: [PATCH] Remove some duplication --- OpenRA.Game/Utilities.cs | 65 ++++++++----------- .../Widgets/Delegates/GameInitDelegate.cs | 4 +- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/OpenRA.Game/Utilities.cs b/OpenRA.Game/Utilities.cs index 96c88224b2..f50454c478 100644 --- a/OpenRA.Game/Utilities.cs +++ b/OpenRA.Game/Utilities.cs @@ -24,37 +24,44 @@ namespace OpenRA NativeUtility = nativeUtility; } - public void ExtractZip(string zipFile, string path, Action parseOutput, Action onComplete) + public void ExtractZipAsync(string zipFile, string path, Action parseOutput, Action onComplete) + { + ExecuteUtilityAsync(Utility, "\"--extract-zip={0},{1}\"".F(zipFile, path), parseOutput, onComplete); + } + + public void InstallRAFilesAsync(string cdPath, Action parseOutput, Action onComplete) + { + ExecuteUtilityAsync(Utility, "\"--install-ra-packages={0}\"".F(cdPath), parseOutput, onComplete); + } + + public void PromptFilepathAsync(string title, string message, bool directory, Action withPath) + { + ExecuteUtility(NativeUtility, + "--filepicker --title \"{0}\" --message \"{1}\" {2} --button-text \"Select\"".F(title, message, directory ? "--require-directory" : ""), + withPath); + } + + void ExecuteUtility(string executable, string args, Action onComplete) { Process p = new Process(); - p.StartInfo.FileName = Utility; - p.StartInfo.Arguments = "\"--extract-zip={0},{1}\"".F(zipFile, path); + p.StartInfo.FileName = executable; + p.StartInfo.Arguments = args; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardOutput = true; - p.Start(); - var t = new Thread( _ => + p.EnableRaisingEvents = true; + p.Exited += (_,e) => { - using (var reader = p.StandardOutput) - { - // This is wrong, chrisf knows why - while (!p.HasExited) - { - string s = reader.ReadLine(); - if (string.IsNullOrEmpty(s)) continue; - parseOutput(s); - } - } - onComplete(); - }) { IsBackground = true }; - t.Start(); + onComplete(p.StandardOutput.ReadToEnd().Trim()); + }; + p.Start(); } - public void CopyRAFiles(string cdPath, Action parseOutput, Action onComplete) + void ExecuteUtilityAsync(string executable, string args, Action parseOutput, Action onComplete) { Process p = new Process(); - p.StartInfo.FileName = Utility; - p.StartInfo.Arguments = "\"--install-ra-packages={0}\"".F(cdPath); + p.StartInfo.FileName = executable; + p.StartInfo.Arguments = args; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardOutput = true; @@ -76,21 +83,5 @@ namespace OpenRA }) { IsBackground = true }; t.Start(); } - - public void PromptFilepathAsync(string title, string message, bool directory, Action withPath) - { - Process p = new Process(); - p.StartInfo.FileName = NativeUtility; - p.StartInfo.Arguments = "--filepicker --title \"{0}\" --message \"{1}\" {2} --button-text \"Select\"".F(title, message, directory ? "--require-directory" : ""); - p.StartInfo.UseShellExecute = false; - p.StartInfo.CreateNoWindow = true; - p.StartInfo.RedirectStandardOutput = true; - p.EnableRaisingEvents = true; - p.Exited += (_,e) => - { - withPath(p.StandardOutput.ReadToEnd().Trim()); - }; - p.Start(); - } } } diff --git a/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs b/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs index 90624984ee..ada626b115 100755 --- a/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs +++ b/OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs @@ -165,7 +165,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates }; if (Info.InstallMode == "ra") - Game.Utilities.CopyRAFiles(path, parseOutput, onComplete); + Game.Utilities.InstallRAFilesAsync(path, parseOutput, onComplete); else ShowDownloadError("Installing from CD not supported"); } @@ -211,7 +211,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates Game.RunAfterTick(() => ContinueLoading(Info)); }; - Game.RunAfterTick(() => Game.Utilities.ExtractZip(file, Info.PackagePath, parseOutput, onComplete)); + Game.RunAfterTick(() => Game.Utilities.ExtractZipAsync(file, Info.PackagePath, parseOutput, onComplete)); } };