Command.cs 7.3 KB

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