Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Remove some duplication
  • Loading branch information
pchote committed Jan 27, 2011
1 parent 724a72c commit 8e5f307
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
65 changes: 28 additions & 37 deletions OpenRA.Game/Utilities.cs
Expand Up @@ -24,37 +24,44 @@ public Utilities(string 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)
{
ExecuteUtilityAsync(Utility, "\"--extract-zip={0},{1}\"".F(zipFile, path), parseOutput, onComplete);
}

public void InstallRAFilesAsync(string cdPath, Action<string> parseOutput, Action onComplete)
{
ExecuteUtilityAsync(Utility, "\"--install-ra-packages={0}\"".F(cdPath), parseOutput, onComplete);
}

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();
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<string> parseOutput, Action onComplete)
void ExecuteUtilityAsync(string executable, string args, Action<string> 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;
Expand All @@ -76,21 +83,5 @@ public void CopyRAFiles(string cdPath, Action<string> parseOutput, Action onComp
}) { IsBackground = true };
t.Start();
}

public void PromptFilepathAsync(string title, string message, bool directory, Action<string> 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();
}
}
}
4 changes: 2 additions & 2 deletions OpenRA.Mods.RA/Widgets/Delegates/GameInitDelegate.cs
Expand Up @@ -165,7 +165,7 @@ void InstallFromCD(string path)
};

if (Info.InstallMode == "ra")
Game.Utilities.CopyRAFiles(path, parseOutput, onComplete);
Game.Utilities.InstallRAFilesAsync(path, parseOutput, onComplete);
else
ShowDownloadError("Installing from CD not supported");
}
Expand Down Expand Up @@ -211,7 +211,7 @@ void ShowDownloadDialog()
Game.RunAfterTick(() => ContinueLoading(Info));
};
Game.RunAfterTick(() => Game.Utilities.ExtractZip(file, Info.PackagePath, parseOutput, onComplete));
Game.RunAfterTick(() => Game.Utilities.ExtractZipAsync(file, Info.PackagePath, parseOutput, onComplete));
}
};

Expand Down

0 comments on commit 8e5f307

Please sign in to comment.