Commander.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. namespace OpenSim.Region.CoreModules.Framework.InterfaceCommander
  35. {
  36. /// <summary>
  37. /// A class to enable modules to register console and script commands, which enforces typing and valid input.
  38. /// </summary>
  39. public class Commander : ICommander
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. /// <value>
  43. /// Used in runtime class generation
  44. /// </summary>
  45. private string m_generatedApiClassName;
  46. public string Name
  47. {
  48. get { return m_name; }
  49. }
  50. private string m_name;
  51. public string Help
  52. {
  53. get
  54. {
  55. StringBuilder sb = new StringBuilder();
  56. sb.AppendLine("=== " + m_name + " ===");
  57. foreach (ICommand com in m_commands.Values)
  58. {
  59. sb.AppendLine("* " + Name + " " + com.Name + " - " + com.Help);
  60. }
  61. return sb.ToString();
  62. }
  63. }
  64. /// <summary>
  65. /// Constructor
  66. /// </summary>
  67. /// <param name="name"></param>
  68. public Commander(string name)
  69. {
  70. m_name = name;
  71. m_generatedApiClassName = m_name[0].ToString().ToUpper();
  72. if (m_name.Length > 1)
  73. m_generatedApiClassName += m_name.Substring(1);
  74. }
  75. public Dictionary<string, ICommand> Commands
  76. {
  77. get { return m_commands; }
  78. }
  79. private Dictionary<string, ICommand> m_commands = new Dictionary<string, ICommand>();
  80. #region ICommander Members
  81. public void RegisterCommand(string commandName, ICommand command)
  82. {
  83. m_commands[commandName] = command;
  84. }
  85. /// <summary>
  86. /// Generates a runtime C# class which can be compiled and inserted via reflection to enable modules to register new script commands
  87. /// </summary>
  88. /// <returns>Returns C# source code to create a binding</returns>
  89. public string GenerateRuntimeAPI()
  90. {
  91. string classSrc = "\n\tpublic class " + m_generatedApiClassName + " {\n";
  92. foreach (ICommand com in m_commands.Values)
  93. {
  94. classSrc += "\tpublic void " + EscapeRuntimeAPICommand(com.Name) + "( ";
  95. foreach (KeyValuePair<string, string> arg in com.Arguments)
  96. {
  97. classSrc += arg.Value + " " + Util.Md5Hash(arg.Key) + ",";
  98. }
  99. classSrc = classSrc.Remove(classSrc.Length - 1); // Delete the last comma
  100. classSrc += " )\n\t{\n";
  101. classSrc += "\t\tObject[] args = new Object[" + com.Arguments.Count.ToString() + "];\n";
  102. int i = 0;
  103. foreach (KeyValuePair<string, string> arg in com.Arguments)
  104. {
  105. classSrc += "\t\targs[" + i.ToString() + "] = " + Util.Md5Hash(arg.Key) + " " + ";\n";
  106. i++;
  107. }
  108. classSrc += "\t\tGetCommander(\"" + m_name + "\").Run(\"" + com.Name + "\", args);\n";
  109. classSrc += "\t}\n";
  110. }
  111. classSrc += "}\n";
  112. return classSrc;
  113. }
  114. /// <summary>
  115. /// Runs a specified function with attached arguments
  116. /// *** <b>DO NOT CALL DIRECTLY.</b> ***
  117. /// Call ProcessConsoleCommand instead if handling human input.
  118. /// </summary>
  119. /// <param name="function">The function name to call</param>
  120. /// <param name="args">The function parameters</param>
  121. public void Run(string function, object[] args)
  122. {
  123. m_commands[function].Run(args);
  124. }
  125. public void ProcessConsoleCommand(string function, string[] args)
  126. {
  127. if (m_commands.ContainsKey(function))
  128. {
  129. if (args.Length > 0 && args[0] == "help")
  130. {
  131. m_commands[function].ShowConsoleHelp();
  132. }
  133. else
  134. {
  135. m_commands[function].Run(args);
  136. }
  137. }
  138. else
  139. {
  140. if (function == "api")
  141. {
  142. m_log.Info(GenerateRuntimeAPI());
  143. }
  144. else
  145. {
  146. if (function != "help")
  147. Console.WriteLine("ERROR: Invalid command - No such command exists");
  148. Console.Write(Help);
  149. }
  150. }
  151. }
  152. #endregion
  153. private string EscapeRuntimeAPICommand(string command)
  154. {
  155. command = command.Replace('-', '_');
  156. StringBuilder tmp = new StringBuilder(command);
  157. tmp[0] = tmp[0].ToString().ToUpper().ToCharArray()[0];
  158. return tmp.ToString();
  159. }
  160. }
  161. }