29 using System.Collections.Generic;
30 using System.Reflection;
33 using OpenSim.Framework;
34 using OpenSim.Region.Framework.Interfaces;
36 namespace OpenSim.
Region.CoreModules.Framework.InterfaceCommander
43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 private string m_generatedApiClassName;
52 get {
return m_name; }
54 private string m_name;
60 StringBuilder sb =
new StringBuilder();
62 sb.AppendLine(
"=== " + m_name +
" ===");
64 foreach (
ICommand com
in m_commands.Values)
66 sb.AppendLine(
"* " + Name +
" " + com.Name +
" - " + com.Help);
80 m_generatedApiClassName = m_name[0].ToString().ToUpper();
82 if (m_name.Length > 1)
83 m_generatedApiClassName += m_name.Substring(1);
86 public Dictionary<string, ICommand> Commands
88 get {
return m_commands; }
90 private Dictionary<string, ICommand> m_commands =
new Dictionary<string, ICommand>();
92 #region ICommander Members
96 m_commands[commandName] = command;
105 string classSrc =
"\n\tpublic class " + m_generatedApiClassName +
" {\n";
106 foreach (
ICommand com
in m_commands.Values)
108 classSrc +=
"\tpublic void " + EscapeRuntimeAPICommand(com.
Name) +
"( ";
109 foreach (KeyValuePair<string, string> arg
in com.
Arguments)
111 classSrc += arg.Value +
" " + Util.Md5Hash(arg.Key) +
",";
113 classSrc = classSrc.Remove(classSrc.Length - 1);
114 classSrc +=
" )\n\t{\n";
115 classSrc +=
"\t\tObject[] args = new Object[" + com.Arguments.Count.ToString() +
"];\n";
117 foreach (KeyValuePair<string, string> arg
in com.
Arguments)
119 classSrc +=
"\t\targs[" + i.ToString() +
"] = " + Util.Md5Hash(arg.Key) +
" " +
";\n";
122 classSrc +=
"\t\tGetCommander(\"" + m_name +
"\").Run(\"" + com.Name +
"\", args);\n";
137 public void Run(
string function,
object[] args)
139 m_commands[
function].Run(args);
144 if (m_commands.ContainsKey(
function))
146 if (args.Length > 0 && args[0] ==
"help")
148 m_commands[
function].ShowConsoleHelp();
152 m_commands[
function].Run(args);
157 if (
function ==
"api")
159 m_log.Info(GenerateRuntimeAPI());
163 if (
function !=
"help")
164 Console.WriteLine(
"ERROR: Invalid command - No such command exists");
173 private string EscapeRuntimeAPICommand(
string command)
175 command = command.Replace(
'-',
'_');
176 StringBuilder tmp =
new StringBuilder(command);
177 tmp[0] = tmp[0].ToString().ToUpper().ToCharArray()[0];
179 return tmp.ToString();
void RegisterCommand(string commandName, ICommand command)
string GenerateRuntimeAPI()
Generates a runtime C# class which can be compiled and inserted via reflection to enable modules to r...
Commander(string name)
Constructor
void ProcessConsoleCommand(string function, string[] args)
Dictionary< string, string > Arguments
A class to enable modules to register console and script commands, which enforces typing and valid in...
void Run(string function, object[] args)
Runs a specified function with attached arguments *** DO NOT CALL DIRECTLY. *** Call ProcessConsoleCo...