fix some more stuff in utility

This commit is contained in:
Chris Forbes
2011-02-05 11:49:43 +13:00
parent 8d41f0d680
commit 6afbb5550a
2 changed files with 38 additions and 28 deletions

View File

@@ -24,10 +24,10 @@ namespace OpenRA
return string.Format(fmt, args); return string.Format(fmt, args);
} }
public static void Do<T>( this IEnumerable<T> e, Action<T> fn ) public static void Do<T>(this IEnumerable<T> e, Action<T> fn)
{ {
foreach( var ee in e ) foreach (var ee in e)
fn( ee ); fn(ee);
} }
public static IEnumerable<string> GetNamespaces(this Assembly a) public static IEnumerable<string> GetNamespaces(this Assembly a)
@@ -75,16 +75,16 @@ namespace OpenRA
return mi.GetCustomAttributes(typeof(T), true).Length != 0; return mi.GetCustomAttributes(typeof(T), true).Length != 0;
} }
public static T[] GetCustomAttributes<T>( this MemberInfo mi, bool inherit ) public static T[] GetCustomAttributes<T>(this MemberInfo mi, bool inherit)
where T : class where T : class
{ {
return (T[])mi.GetCustomAttributes( typeof( T ), inherit ); return (T[])mi.GetCustomAttributes(typeof(T), inherit);
} }
public static T[] GetCustomAttributes<T>( this ParameterInfo mi ) public static T[] GetCustomAttributes<T>(this ParameterInfo mi)
where T : class where T : class
{ {
return (T[])mi.GetCustomAttributes( typeof( T ), true ); return (T[])mi.GetCustomAttributes(typeof(T), true);
} }
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T> public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>

View File

@@ -19,13 +19,23 @@ namespace OpenRA.Utility
{ {
class Program class Program
{ {
delegate void ArgCallback(string[] args); static Dictionary<string, Action<string[]>> actions;
const int PipeBufferSize = 1024 * 1024;
static Dictionary<string, ArgCallback> 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) static void Main(string[] args)
{ {
argCallbacks = new Dictionary<string, ArgCallback>() actions = new Dictionary<string, Action<string[]>>()
{ {
{ "--extract-zip-inner", Command.ExtractZip }, { "--extract-zip-inner", Command.ExtractZip },
{ "--install-ra-packages-inner", Command.InstallRAPackages }, { "--install-ra-packages-inner", Command.InstallRAPackages },
@@ -44,28 +54,22 @@ namespace OpenRA.Utility
if (args.Length > 1 && i >= 0) if (args.Length > 1 && i >= 0)
{ {
piping = true; piping = true;
string pipename = args[i+1]; var pipename = args[i + 1];
NamedPipeServerStream pipe;
var id = WindowsIdentity.GetCurrent(); var pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1,
var principal = new WindowsPrincipal(id); PipeTransmissionMode.Byte, PipeOptions.None, PipeBufferSize, PipeBufferSize,
if (principal.IsInRole(WindowsBuiltInRole.Administrator)) MakePipeSecurity());
{
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);
pipe.WaitForConnection(); pipe.WaitForConnection();
Console.SetOut(new StreamWriter(pipe) { AutoFlush = true }); Console.SetOut(new StreamWriter(pipe) { AutoFlush = true });
} }
ArgCallback callback;
if (argCallbacks.TryGetValue(args[0], out callback)) var action = WithDefault( null, () => actions[args[0]]);
callback(args); if (action == null)
else
PrintUsage(); PrintUsage();
else
action(args);
if (piping) if (piping)
Console.Out.Close(); 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(" --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"); Console.WriteLine(" --settings-value SUPPORTDIR KEY Get value of KEY in SUPPORTDIR/settings.yaml");
} }
static T WithDefault<T>(T def, Func<T> f)
{
try { return f(); }
catch { return def; }
}
} }
} }