ConsolePluginCommand.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. namespace OpenSim.Framework.Console
  29. {
  30. public delegate void ConsoleCommand(string[] comParams);
  31. /// <summary>
  32. /// Holder object for a new console plugin command
  33. ///
  34. /// Override the methods like Run and IsHelpfull (but the defaults might work ok.)
  35. /// </summary>
  36. public class ConsolePluginCommand
  37. {
  38. /// <summary>
  39. /// command delegate used in running
  40. /// </summary>
  41. private ConsoleCommand m_commandDelegate;
  42. /// <summary>
  43. /// help text displayed
  44. /// </summary>
  45. private string m_helpText;
  46. /// <summary>
  47. /// command in the form of "showme new commands"
  48. /// </summary>
  49. private string[] m_cmdText;
  50. /// <summary>
  51. /// Construct a new ConsolePluginCommand
  52. ///
  53. /// for use with OpenSim.RegisterConsolePluginCommand(myCmd);
  54. ///
  55. /// </summary>
  56. /// <param name="command">in the form of "showme new commands"</param>
  57. /// <param name="dlg">ommand delegate used in running</param>
  58. /// <param name="help">the text displayed in "help showme new commands"</param>
  59. public ConsolePluginCommand(string command, ConsoleCommand dlg, string help)
  60. {
  61. m_cmdText = command.Split();
  62. m_commandDelegate = dlg;
  63. m_helpText = help;
  64. }
  65. /// <summary>
  66. /// Returns the match length this command has upon the 'cmdWithParams'
  67. /// At least a higher number for "show plugin status" then "show" would return
  68. /// This is used to have multi length command verbs
  69. ///
  70. /// @see OopenSim.RunPluginCommands
  71. /// It will only run the one with the highest number
  72. ///
  73. /// </summary>
  74. public int matchLength(string cmdWithParams)
  75. {
  76. // QUESTION: have a case insensitive flag?
  77. cmdWithParams = cmdWithParams.ToLower().Trim();
  78. string matchText = String.Join(" ",m_cmdText).ToLower().Trim();
  79. if (cmdWithParams.StartsWith(matchText))
  80. {
  81. // QUESTION Instead return cmdText.Length; ?
  82. return matchText.Length;
  83. }
  84. return 0;
  85. }
  86. /// <summary>
  87. /// Run the delegate the incomming string may contain the command, if so, it is chopped off the cmdParams[]
  88. /// </summary>
  89. public void Run(string cmd, string[] cmdParams)
  90. {
  91. int skipParams = 0;
  92. if (m_cmdText.Length > 1)
  93. {
  94. int currentParam = 1;
  95. while (currentParam < m_cmdText.Length)
  96. {
  97. if (cmdParams[skipParams].ToLower().Equals(m_cmdText[currentParam].ToLower()))
  98. {
  99. skipParams++;
  100. }
  101. currentParam++;
  102. }
  103. }
  104. string[] sendCmdParams = cmdParams;
  105. if (skipParams > 0)
  106. {
  107. sendCmdParams = new string[cmdParams.Length-skipParams];
  108. for (int i=0;i<sendCmdParams.Length;i++) {
  109. sendCmdParams[i] = cmdParams[skipParams++];
  110. }
  111. }
  112. m_commandDelegate(sendCmdParams);//.Trim().Split(new char[] { ' ' }));
  113. }
  114. /// <summary>
  115. /// Shows help information on the console's Notice method
  116. /// </summary>
  117. public void ShowHelp(ConsoleBase console)
  118. {
  119. console.Output(String.Join(" ", m_cmdText) + " - " + m_helpText + "\n");
  120. }
  121. /// <summary>
  122. /// return true if the ShowHelp(..) method might be helpfull
  123. /// </summary>
  124. public bool IsHelpfull(string cmdWithParams)
  125. {
  126. cmdWithParams = cmdWithParams.ToLower();
  127. return cmdWithParams.Contains(String.Join(" ", m_cmdText).ToLower()) || m_helpText.ToLower().Contains(cmdWithParams);
  128. }
  129. }
  130. }