Utility now uses named pipe if passed --pipe. Installing mods now works properly too.

This commit is contained in:
Matthew Bowra-Dean
2010-11-05 15:24:32 +13:00
committed by Paul Chote
parent da384af339
commit 0c319e88c3
7 changed files with 77 additions and 33 deletions

View File

@@ -41,9 +41,14 @@ namespace OpenRA.Launcher
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;
@@ -83,12 +88,17 @@ namespace OpenRA.Launcher
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));
@@ -98,8 +108,12 @@ namespace OpenRA.Launcher
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)

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Launcher
private void button2_Click(object sender, EventArgs e)
{
UtilityProgramResponse response = null;
StreamReader response = null;
if (radioButton1.Checked)
response = UtilityProgram.CallWithAdmin("--download-packages", mod);
@@ -41,11 +41,13 @@ namespace OpenRA.Launcher
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();
}
}

View File

@@ -24,12 +24,14 @@ namespace OpenRA.Launcher
{
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();
}

View File

@@ -22,5 +22,17 @@ namespace OpenRA.Launcher
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;
}
}
}

View File

@@ -3,6 +3,7 @@ using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.IO.Pipes;
namespace OpenRA.Launcher
{
@@ -55,26 +56,26 @@ namespace OpenRA.Launcher
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
@@ -84,13 +85,16 @@ namespace OpenRA.Launcher
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);
}
}
}

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Principal;
using System.IO.Pipes;
namespace OpenRA.Utility
{
@@ -43,20 +44,27 @@ namespace OpenRA.Utility
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();
}

View File

@@ -61,6 +61,8 @@ namespace OpenRA.Utility
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;