Remove some duplication

This commit is contained in:
Paul Chote
2011-01-22 18:20:41 +13:00
parent 724a72c98e
commit 8e5f307ba8
2 changed files with 30 additions and 39 deletions

View File

@@ -24,73 +24,64 @@ namespace OpenRA
NativeUtility = nativeUtility; NativeUtility = nativeUtility;
} }
public void ExtractZip(string zipFile, string path, Action<string> parseOutput, Action onComplete) public void ExtractZipAsync(string zipFile, string path, Action<string> parseOutput, Action onComplete)
{ {
Process p = new Process(); ExecuteUtilityAsync(Utility, "\"--extract-zip={0},{1}\"".F(zipFile, path), parseOutput, onComplete);
p.StartInfo.FileName = Utility;
p.StartInfo.Arguments = "\"--extract-zip={0},{1}\"".F(zipFile, path);
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
var t = new Thread( _ =>
{
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();
} }
public void CopyRAFiles(string cdPath, Action<string> parseOutput, Action onComplete) public void InstallRAFilesAsync(string cdPath, Action<string> parseOutput, Action onComplete)
{ {
Process p = new Process(); ExecuteUtilityAsync(Utility, "\"--install-ra-packages={0}\"".F(cdPath), parseOutput, onComplete);
p.StartInfo.FileName = Utility;
p.StartInfo.Arguments = "\"--install-ra-packages={0}\"".F(cdPath);
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
var t = new Thread( _ =>
{
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();
} }
public void PromptFilepathAsync(string title, string message, bool directory, Action<string> withPath) public void PromptFilepathAsync(string title, string message, bool directory, Action<string> 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<string> onComplete)
{ {
Process p = new Process(); Process p = new Process();
p.StartInfo.FileName = NativeUtility; p.StartInfo.FileName = executable;
p.StartInfo.Arguments = "--filepicker --title \"{0}\" --message \"{1}\" {2} --button-text \"Select\"".F(title, message, directory ? "--require-directory" : ""); p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false; p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true; p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true; p.EnableRaisingEvents = true;
p.Exited += (_,e) => p.Exited += (_,e) =>
{ {
withPath(p.StandardOutput.ReadToEnd().Trim()); onComplete(p.StandardOutput.ReadToEnd().Trim());
}; };
p.Start(); p.Start();
} }
void ExecuteUtilityAsync(string executable, string args, Action<string> parseOutput, Action onComplete)
{
Process p = new Process();
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( _ =>
{
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();
}
} }
} }

View File

@@ -165,7 +165,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
}; };
if (Info.InstallMode == "ra") if (Info.InstallMode == "ra")
Game.Utilities.CopyRAFiles(path, parseOutput, onComplete); Game.Utilities.InstallRAFilesAsync(path, parseOutput, onComplete);
else else
ShowDownloadError("Installing from CD not supported"); ShowDownloadError("Installing from CD not supported");
} }
@@ -211,7 +211,7 @@ namespace OpenRA.Mods.RA.Widgets.Delegates
if (!error) if (!error)
Game.RunAfterTick(() => ContinueLoading(Info)); Game.RunAfterTick(() => ContinueLoading(Info));
}; };
Game.RunAfterTick(() => Game.Utilities.ExtractZipAsync(file, Info.PackagePath, parseOutput, onComplete)); Game.RunAfterTick(() => Game.Utilities.ExtractZipAsync(file, Info.PackagePath, parseOutput, onComplete));
} }
}; };