Command.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "Float":
  134. m_args[i].ArgumentValue = float.Parse(arg.ToString(), OpenSim.Framework.Culture.NumberFormatInfo);
  135. break;
  136. case "Double":
  137. m_args[i].ArgumentValue = Double.Parse(arg.ToString(), OpenSim.Framework.Culture.NumberFormatInfo);
  138. break;
  139. case "Boolean":
  140. m_args[i].ArgumentValue = Boolean.Parse(arg.ToString());
  141. break;
  142. case "UUID":
  143. m_args[i].ArgumentValue = UUID.Parse(arg.ToString());
  144. break;
  145. default:
  146. Console.WriteLine("ERROR: Unknown desired type for argument " + m_args[i].Name + " on command " + m_name);
  147. break;
  148. }
  149. }
  150. catch (FormatException)
  151. {
  152. Console.WriteLine("ERROR: Argument number " + (i + 1) +
  153. " (" + m_args[i].Name + ") must be a valid " +
  154. m_args[i].ArgumentType.ToLower() + ".");
  155. return;
  156. }
  157. cleanArgs[i] = m_args[i].ArgumentValue;
  158. i++;
  159. }
  160. m_command.Invoke(cleanArgs);
  161. }
  162. #endregion
  163. }
  164. /// <summary>
  165. /// A single command argument, contains name, type and at runtime, value.
  166. /// </summary>
  167. public class CommandArgument
  168. {
  169. private string m_help;
  170. private string m_name;
  171. private string m_type;
  172. private Object m_val;
  173. public CommandArgument(string name, string help, string type)
  174. {
  175. m_name = name;
  176. m_help = help;
  177. m_type = type;
  178. }
  179. public string Name
  180. {
  181. get { return m_name; }
  182. }
  183. public string HelpText
  184. {
  185. get { return m_help; }
  186. }
  187. public string ArgumentType
  188. {
  189. get { return m_type; }
  190. }
  191. public Object ArgumentValue
  192. {
  193. get { return m_val; }
  194. set { m_val = value; }
  195. }
  196. }
  197. }