Skip to content

Commit

Permalink
Utility now uses named pipe if passed --pipe. Installing mods now wor…
Browse files Browse the repository at this point in the history
…ks properly too.
  • Loading branch information
Matthew Bowra-Dean authored and pchote committed Nov 21, 2010
1 parent da384af commit 0c319e8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 33 deletions.
32 changes: 23 additions & 9 deletions OpenRA.Launcher/ConfigureModsDialog.cs
Expand Up @@ -41,9 +41,14 @@ public ConfigureModsDialog(string[] activeMods)

Mod GetMetadata(string mod)
{
var response = UtilityProgram.Call("-i", mod);
if (response.IsError) return null;
string[] lines = response.ResponseLines;
string responseString;
using (var response = UtilityProgram.Call("-i", mod))
{
responseString = response.ReadToEnd();
}

if (Util.IsError(ref responseString)) return null;
string[] lines = responseString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

string title = "", version = "", author = "", description = "", requires = "";
bool standalone = false;
Expand Down Expand Up @@ -83,12 +88,17 @@ Mod GetMetadata(string mod)

void RefreshMods()
{
var response = UtilityProgram.Call("--list-mods");
string responseString;
using (var response = UtilityProgram.Call("--list-mods"))
{
responseString = response.ReadToEnd();
}

string[] mods;
if (!response.IsError)
mods = response.ResponseLines;
if (!Util.IsError(ref responseString))
mods = responseString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
else
throw new Exception(string.Format("Could not list mods: {0}", response.Response));
throw new Exception(string.Format("Could not list mods: {0}", responseString));

allMods = mods.ToDictionary(x => x, x => GetMetadata(x));

Expand All @@ -98,8 +108,12 @@ void RefreshMods()
private void InstallMod(object sender, EventArgs e)
{
if (installModDialog.ShowDialog() != DialogResult.OK) return;
UtilityProgram.CallWithAdmin("--install-mods", installModDialog.FileName);
RefreshModTree(treeView1, allMods.Keys.ToArray());
using (var response = UtilityProgram.CallWithAdmin("--install-mod", installModDialog.FileName))
{
string s = response.ReadToEnd();
}

RefreshMods();
}

void RefreshModTree(TreeView treeView, string[] modList)
Expand Down
6 changes: 4 additions & 2 deletions OpenRA.Launcher/InstallPackagesDialog.cs
Expand Up @@ -30,7 +30,7 @@ public InstallPackagesDialog(string mod)

private void button2_Click(object sender, EventArgs e)
{
UtilityProgramResponse response = null;
StreamReader response = null;
if (radioButton1.Checked)
response = UtilityProgram.CallWithAdmin("--download-packages", mod);

Expand All @@ -41,11 +41,13 @@ private void button2_Click(object sender, EventArgs e)
folderBrowserDialog1.SelectedPath + Path.DirectorySeparatorChar);
}

if (response.IsError)
string s = response.ReadToEnd();
if (Util.IsError(ref s))
DialogResult = DialogResult.No;
else
DialogResult = DialogResult.OK;

response.Close();
Close();
}
}
Expand Down
14 changes: 8 additions & 6 deletions OpenRA.Launcher/MainForm.cs
Expand Up @@ -24,12 +24,14 @@ public MainForm()
{
InitializeComponent();
quitButton.Click += (o, e) => { Application.Exit(); };
var response = UtilityProgram.Call("--settings-value", configPath, "Game.Mods");

if (response.IsError)
currentMods = new string[] { "ra" };
else
currentMods = response.Response.Split(',');
using (var s = UtilityProgram.Call("--settings-value", configPath, "Game.Mods"))
{
var response = s.ReadToEnd();
if (Util.IsError(ref response))
currentMods = new string[] { "ra" };
else
currentMods = response.Split(',');
}

UpdateModLabel();
}
Expand Down
12 changes: 12 additions & 0 deletions OpenRA.Launcher/Util.cs
Expand Up @@ -22,5 +22,17 @@ static public void UacShield(Button b)
b.FlatStyle = FlatStyle.System;
SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);
}

static public bool IsError(ref string utilityResponseLine)
{
utilityResponseLine = utilityResponseLine.Trim('\r', '\n');
if (utilityResponseLine.StartsWith("Error:"))
{
utilityResponseLine = utilityResponseLine.Remove(0, 7);
return true;
}

return false;
}
}
}
24 changes: 14 additions & 10 deletions OpenRA.Launcher/UtilityProgram.cs
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.IO.Pipes;

namespace OpenRA.Launcher
{
Expand Down Expand Up @@ -55,26 +56,26 @@ static string BuildArgs(string command, string[] args)
return arguments.ToString();
}

public static UtilityProgramResponse Call(string command, params string[] args)
public static StreamReader Call(string command, params string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "OpenRA.Utility.exe";
p.StartInfo.Arguments = BuildArgs(command, args);
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Arguments = BuildArgs(command, args) + " --pipe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

p.Start();

return new UtilityProgramResponse(p.StandardOutput.ReadToEnd());
NamedPipeClientStream pipe = new NamedPipeClientStream(".", "OpenRA.Utility", PipeDirection.In);
pipe.Connect();
return new StreamReader(pipe);
}

public static UtilityProgramResponse CallWithAdmin(string command, params string[] args)
public static StreamReader CallWithAdmin(string command, params string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "OpenRA.Utility.exe";
p.StartInfo.Arguments = BuildArgs(command, args);
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = BuildArgs(command, args) + " --pipe";
p.StartInfo.Verb = "runas";

try
Expand All @@ -84,13 +85,16 @@ public static UtilityProgramResponse CallWithAdmin(string command, params string
catch (Win32Exception e)
{
if (e.NativeErrorCode == 1223) //ERROR_CANCELLED
return new UtilityProgramResponse("Error: User cancelled elevation prompt.");
return null;
throw e;
}

NamedPipeClientStream pipe = new NamedPipeClientStream(".", "OpenRA.Utility", PipeDirection.In);
pipe.Connect();

p.WaitForExit();

return new UtilityProgramResponse(File.ReadAllText("output.txt"));
return new StreamReader(pipe);
}
}
}
20 changes: 14 additions & 6 deletions OpenRA.Utility/Program.cs
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.IO;
using System.Security.Principal;
using System.IO.Pipes;

namespace OpenRA.Utility
{
Expand Down Expand Up @@ -43,20 +44,27 @@ static void Main(string[] args)
argCallbacks.Add("--settings-value", Command.Settings);
argCallbacks.Add("--install-mod", Command.InstallMod);

WindowsIdentity id = WindowsIdentity.GetCurrent();
WindowsPrincipal p = new WindowsPrincipal(id);
if (p.IsInRole(WindowsBuiltInRole.Administrator))
Console.SetOut(new StreamWriter(File.Create("output.txt")));

if (args.Length == 0) { PrintUsage(); return; }
var arg = SplitArgs(args[0]);

bool piping = false;
if (args.Length > 1 && args[1] == "--pipe")
{
piping = true;
var ps = new PipeSecurity();
ps.AddAccessRule(new PipeAccessRule("EVERYONE", PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
NamedPipeServerStream pipe = new NamedPipeServerStream("OpenRA.Utility", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.None, 1024, 1024, ps);
pipe.WaitForConnection();
Console.SetOut(new StreamWriter(pipe) { AutoFlush = true });
}

ArgCallback callback;
if (argCallbacks.TryGetValue(arg.Key, out callback))
callback(arg.Value);
else
PrintUsage();

if (p.IsInRole(WindowsBuiltInRole.Administrator))
if (piping)
Console.Out.Close();
}

Expand Down
2 changes: 2 additions & 0 deletions OpenRA.Utility/Util.cs
Expand Up @@ -61,6 +61,8 @@ public static void ExtractZip(this ZipInputStream z, string destPath)
if (!entry.IsFile) continue;

Console.WriteLine("Extracting {0}", entry.Name);
if (!Directory.Exists(Path.Combine(destPath, Path.GetDirectoryName(entry.Name))))
Directory.CreateDirectory(Path.Combine(destPath, Path.GetDirectoryName(entry.Name)));
using (var f = File.Create(destPath + Path.DirectorySeparatorChar + entry.Name))
{
int bufSize = 2048;
Expand Down

0 comments on commit 0c319e8

Please sign in to comment.