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