www.pudn.com > Wcell.rar > CommandConsole.cs


/************************************************************************* 
 * 
 *   file		: CommandConsole.cs 
 *   copyright		: (C) The WCell Team 
 *   email		: info@wcell.org 
 *   last changed	: $LastChangedDate: 2008-03-10 08:52:03 +0800 (星期一, 10 三月 2008) $ 
 *   last author	: $LastChangedBy: tobz $ 
 *   revision		: $Rev: 190 $ 
 * 
 *   This program is free software; you can redistribute it and/or modify 
 *   it under the terms of the GNU General Public License as published by 
 *   the Free Software Foundation; either version 2 of the License, or 
 *   (at your option) any later version. 
 * 
 *************************************************************************/ 
 
using System; 
using System.Collections.Generic; 
using System.Reflection; 
using WCell.RealmServerConsole.Commands; 
 
namespace WCell.RealmServerConsole 
{ 
    ///  
    /// Delegate for consome command methods 
    ///  
    /// the arguments of the command 
    public delegate void CommandDelegate(string[] arguments); 
 
    internal static class CommandConsole 
    { 
        private static Dictionary m_commands = new Dictionary(); 
 
        internal static void FindCommands() 
        { 
            foreach (Type typ in Assembly.GetExecutingAssembly().GetTypes()) 
            { 
                MethodInfo[] methods = typ.GetMethods(); 
 
                foreach (MethodInfo method in methods) 
                { 
                    ConsoleCommandAttribute[] attributes = (ConsoleCommandAttribute[]) 
                                                           method.GetCustomAttributes(typeof (ConsoleCommandAttribute), 
                                                                                      false); 
 
                    if (attributes.Length == 0) 
                        continue; 
 
                    CommandDelegate _delegate = 
                        (CommandDelegate) Delegate.CreateDelegate(typeof (CommandDelegate), method); 
 
                    m_commands.Add("--" + attributes[0].CommandString, _delegate); 
                } 
            } 
        } 
 
        internal static string GetCommandList() 
        { 
            string output = ""; 
 
            foreach (string command in m_commands.Keys) 
            { 
                output += ", " + command; 
            } 
 
            return output.Substring(2); 
        } 
 
        internal static void Run() 
        { 
            Console.WriteLine("Console ready. Type --help for help"); 
 
            string command; 
 
            while (true) 
            { 
                command = Console.ReadLine(); 
 
                lock (Console.Out) 
                { 
                    if (m_commands.ContainsKey(command)) 
                    { 
                        m_commands[command].Invoke(new string[] {}); 
                        continue; 
                    } 
 
                    string[] commandArguments = command.Split(new char[] {' '}); 
 
                    if (commandArguments.Length >= 2 && m_commands.ContainsKey(commandArguments[0])) 
                    { 
                        List args = new List(commandArguments); 
 
                        args.RemoveAt(0); 
 
                        m_commands[commandArguments[0]].Invoke(args.ToArray()); 
                        continue; 
                    } 
 
                    Console.WriteLine("The command '{0}' is not a recognized command. Please try again!", command); 
                } 
            } 
        } 
    } 
}