From 6afbb5550af69c668b0ccebdfb4d9fceee998a9d Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sat, 5 Feb 2011 11:49:43 +1300 Subject: [PATCH] fix some more stuff in utility --- OpenRA.FileFormats/Exts.cs | 18 +++++++------- OpenRA.Utility/Program.cs | 48 +++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index 2b09647e30..890f781504 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -24,10 +24,10 @@ namespace OpenRA return string.Format(fmt, args); } - public static void Do( this IEnumerable e, Action fn ) + public static void Do(this IEnumerable e, Action fn) { - foreach( var ee in e ) - fn( ee ); + foreach (var ee in e) + fn(ee); } public static IEnumerable GetNamespaces(this Assembly a) @@ -75,16 +75,16 @@ namespace OpenRA return mi.GetCustomAttributes(typeof(T), true).Length != 0; } - public static T[] GetCustomAttributes( this MemberInfo mi, bool inherit ) + public static T[] GetCustomAttributes(this MemberInfo mi, bool inherit) where T : class { - return (T[])mi.GetCustomAttributes( typeof( T ), inherit ); + return (T[])mi.GetCustomAttributes(typeof(T), inherit); } - public static T[] GetCustomAttributes( this ParameterInfo mi ) + public static T[] GetCustomAttributes(this ParameterInfo mi) where T : class { - return (T[])mi.GetCustomAttributes( typeof( T ), true ); + return (T[])mi.GetCustomAttributes(typeof(T), true); } public static T Clamp(this T val, T min, T max) where T : IComparable @@ -96,12 +96,12 @@ namespace OpenRA else return val; } - + public static bool Contains(this Rectangle r, int2 p) { return r.Contains(p.ToPoint()); } - + public static bool Contains(this RectangleF r, int2 p) { return r.Contains(p.ToPointF()); diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index 1513d1f91b..b399fbefc7 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -19,13 +19,23 @@ namespace OpenRA.Utility { class Program { - delegate void ArgCallback(string[] args); + static Dictionary> actions; + const int PipeBufferSize = 1024 * 1024; - static Dictionary argCallbacks; + static PipeSecurity MakePipeSecurity() + { + var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); + if (!principal.IsInRole(WindowsBuiltInRole.Administrator)) + return null; // no special pipe security required + + var ps = new PipeSecurity(); + ps.AddAccessRule(new PipeAccessRule("EVERYONE", (PipeAccessRights)2032031, AccessControlType.Allow)); + return ps; + } static void Main(string[] args) { - argCallbacks = new Dictionary() + actions = new Dictionary>() { { "--extract-zip-inner", Command.ExtractZip }, { "--install-ra-packages-inner", Command.InstallRAPackages }, @@ -44,28 +54,22 @@ namespace OpenRA.Utility if (args.Length > 1 && i >= 0) { piping = true; - string pipename = args[i+1]; - NamedPipeServerStream pipe; - var id = WindowsIdentity.GetCurrent(); - var principal = new WindowsPrincipal(id); - if (principal.IsInRole(WindowsBuiltInRole.Administrator)) - { - var ps = new PipeSecurity(); - ps.AddAccessRule(new PipeAccessRule("EVERYONE", (PipeAccessRights)2032031, AccessControlType.Allow)); - pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.None, 1024*1024, 1024*1024, ps); - } - else - pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.None, 1024*1024,1024*1024,null); + var pipename = args[i + 1]; + + var pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, + PipeTransmissionMode.Byte, PipeOptions.None, PipeBufferSize, PipeBufferSize, + MakePipeSecurity()); pipe.WaitForConnection(); Console.SetOut(new StreamWriter(pipe) { AutoFlush = true }); } - ArgCallback callback; - if (argCallbacks.TryGetValue(args[0], out callback)) - callback(args); - else + + var action = WithDefault( null, () => actions[args[0]]); + if (action == null) PrintUsage(); + else + action(args); if (piping) Console.Out.Close(); @@ -80,5 +84,11 @@ namespace OpenRA.Utility Console.WriteLine(" --install-cnc-packages PATH Install required packages for C&C from CD to PATH"); Console.WriteLine(" --settings-value SUPPORTDIR KEY Get value of KEY in SUPPORTDIR/settings.yaml"); } + + static T WithDefault(T def, Func f) + { + try { return f(); } + catch { return def; } + } } }