Command.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 OpenSim.Region.Framework.Interfaces;
  30. using OpenMetaverse;
  31. namespace OpenSim.Region.CoreModules.Framework.InterfaceCommander
  32. {
  33. /// <summary>
  34. /// A single function call encapsulated in a class which enforces arguments when passing around as Object[]'s.
  35. /// Used for console commands and script API generation
  36. /// </summary>
  37. public class Command : ICommand
  38. {
  39. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private List<CommandArgument> m_args = new List<CommandArgument>();
  41. private Action<object[]> m_command;
  42. private string m_help;
  43. private string m_name;
  44. private CommandIntentions m_intentions; //A permission type system could implement this and know what a command intends on doing.
  45. public Command(string name, CommandIntentions intention, Action<Object[]> command, string help)
  46. {
  47. m_name = name;
  48. m_command = command;
  49. m_help = help;
  50. m_intentions = intention;
  51. }
  52. #region ICommand Members
  53. public void AddArgument(string name, string helptext, string type)
  54. {
  55. m_args.Add(new CommandArgument(name, helptext, type));
  56. }
  57. public string Name
  58. {
  59. get { return m_name; }
  60. }
  61. public CommandIntentions Intentions
  62. {
  63. get { return m_intentions; }
  64. }
  65. public string Help
  66. {
  67. get { return m_help; }
  68. }
  69. public Dictionary<string, string> Arguments
  70. {
  71. get
  72. {
  73. Dictionary<string, string> tmp = new Dictionary<string, string>();
  74. foreach (CommandArgument arg in m_args)
  75. {
  76. tmp.Add(arg.Name, arg.ArgumentType);
  77. }
  78. return tmp;
  79. }
  80. }
  81. public string ShortHelp()
  82. {
  83. string help = m_name;
  84. foreach (CommandArgument arg in m_args)
  85. {
  86. help += " <" + arg.Name + ">";
  87. }
  88. return help;
  89. }
  90. public void ShowConsoleHelp()
  91. {
  92. Console.WriteLine("== " + Name + " ==");
  93. Console.WriteLine(m_help);
  94. Console.WriteLine("= Parameters =");
  95. foreach (CommandArgument arg in m_args)
  96. {
  97. Console.WriteLine("* " + arg.Name + " (" + arg.ArgumentType + ")");
  98. Console.WriteLine("\t" + arg.HelpText);
  99. }
  100. }
  101. public void Run(Object[] args)
  102. {
  103. Object[] cleanArgs = new Object[m_args.Count];
  104. if (args.Length < cleanArgs.Length)
  105. {
  106. Console.WriteLine("ERROR: Missing " + (cleanArgs.Length - args.Length) + " argument(s)");
  107. ShowConsoleHelp();
  108. return;
  109. }
  110. if (args.Length > cleanArgs.Length)
  111. {
  112. Console.WriteLine("ERROR: Too many arguments for this command. Type '<module> <command> help' for help.");
  113. return;
  114. }
  115. int i = 0;
  116. foreach (Object arg in args)
  117. {
  118. if (string.IsNullOrEmpty(arg.ToString()))
  119. {
  120. Console.WriteLine("ERROR: Empty arguments are not allowed");
  121. return;
  122. }
  123. try
  124. {
  125. switch (m_args[i].ArgumentType)
  126. {
  127. case "String":
  128. m_args[i].ArgumentValue = arg.ToString();
  129. break;
  130. case "Integer":
  131. m_args[i].ArgumentValue = Int32.Parse(arg.ToString());
  132. break;
  133. case "Double":
  134. m_args[i].ArgumentValue = Double.Parse(arg.ToString(), OpenSim.Framework.Culture.NumberFormatInfo);
  135. break;
  136. case "Boolean":
  137. m_args[i].ArgumentValue = Boolean.Parse(arg.ToString());
  138. break;
  139. case "UUID":
  140. m_args[i].ArgumentValue = UUID.Parse(arg.ToString());
  141. break;
  142. default:
  143. Console.WriteLine("ERROR: Unknown desired type for argument " + m_args[i].Name + " on command " + m_name);
  144. break;
  145. }
  146. }
  147. catch (FormatException)
  148. {
  149. Console.WriteLine("ERROR: Argument number " + (i + 1) +
  150. " (" + m_args[i].Name + ") must be a valid " +
  151. m_args[i].ArgumentType.ToLower() + ".");
  152. return;
  153. }
  154. cleanArgs[i] = m_args[i].ArgumentValue;
  155. i++;
  156. }
  157. m_command.Invoke(cleanArgs);
  158. }
  159. #endregion
  160. }
  161. /// <summary>
  162. /// A single command argument, contains name, type and at runtime, value.
  163. /// </summary>
  164. public class CommandArgument
  165. {
  166. private string m_help;
  167. private string m_name;
  168. private string m_type;
  169. private Object m_val;
  170. public CommandArgument(string name, string help, string type)
  171. {
  172. m_name = name;
  173. m_help = help;
  174. m_type = type;
  175. }
  176. public string Name
  177. {
  178. get { return m_name; }
  179. }
  180. public string HelpText
  181. {
  182. get { return m_help; }
  183. }
  184. public string ArgumentType
  185. {
  186. get { return m_type; }
  187. }
  188. public Object ArgumentValue
  189. {
  190. get { return m_val; }
  191. set { m_val = value; }
  192. }
  193. }
  194. }