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);
}
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 )
fn( ee );
foreach (var ee in e)
fn(ee);
}
public static IEnumerable<string> GetNamespaces(this Assembly a)
@@ -75,16 +75,16 @@ namespace OpenRA
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
{
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
{
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>
@@ -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());

View File

@@ -19,13 +19,23 @@ namespace OpenRA.Utility
{
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)
{
argCallbacks = new Dictionary<string, ArgCallback>()
actions = new Dictionary<string, Action<string[]>>()
{
{ "--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>(T def, Func<T> f)
{
try { return f(); }
catch { return def; }
}
}
}