diff options
Diffstat (limited to 'OpenSim/Framework/Console/ConsoleUtil.cs')
-rw-r--r-- | OpenSim/Framework/Console/ConsoleUtil.cs | 387 |
1 files changed, 387 insertions, 0 deletions
diff --git a/OpenSim/Framework/Console/ConsoleUtil.cs b/OpenSim/Framework/Console/ConsoleUtil.cs new file mode 100644 index 0000000..44f6dc1 --- /dev/null +++ b/OpenSim/Framework/Console/ConsoleUtil.cs | |||
@@ -0,0 +1,387 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Linq; | ||
32 | using System.Reflection; | ||
33 | using log4net; | ||
34 | using OpenMetaverse; | ||
35 | |||
36 | namespace OpenSim.Framework.Console | ||
37 | { | ||
38 | public class ConsoleUtil | ||
39 | { | ||
40 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
41 | |||
42 | public const int LocalIdNotFound = 0; | ||
43 | |||
44 | /// <summary> | ||
45 | /// Used by modules to display stock co-ordinate help, though possibly this should be under some general section | ||
46 | /// rather than in each help summary. | ||
47 | /// </summary> | ||
48 | public const string CoordHelp | ||
49 | = @"Each component of the coord is comma separated. There must be no spaces between the commas. | ||
50 | If you don't care about the z component you can simply omit it. | ||
51 | If you don't care about the x or y components then you can leave them blank (though a comma is still required) | ||
52 | If you want to specify the maximum value of a component then you can use ~ instead of a number | ||
53 | If you want to specify the minimum value of a component then you can use -~ instead of a number | ||
54 | e.g. | ||
55 | show object pos 20,20,20 to 40,40,40 | ||
56 | delete object pos 20,20 to 40,40 | ||
57 | show object pos ,20,20 to ,40,40 | ||
58 | delete object pos ,,30 to ,,~ | ||
59 | show object pos ,,-~ to ,,30"; | ||
60 | |||
61 | public const string MinRawConsoleVectorValue = "-~"; | ||
62 | public const string MaxRawConsoleVectorValue = "~"; | ||
63 | |||
64 | public const string VectorSeparator = ","; | ||
65 | public static char[] VectorSeparatorChars = VectorSeparator.ToCharArray(); | ||
66 | |||
67 | /// <summary> | ||
68 | /// Check if the given file path exists. | ||
69 | /// </summary> | ||
70 | /// <remarks>If not, warning is printed to the given console.</remarks> | ||
71 | /// <returns>true if the file does not exist, false otherwise.</returns> | ||
72 | /// <param name='console'></param> | ||
73 | /// <param name='path'></param> | ||
74 | public static bool CheckFileDoesNotExist(ICommandConsole console, string path) | ||
75 | { | ||
76 | if (File.Exists(path)) | ||
77 | { | ||
78 | console.OutputFormat("File {0} already exists. Please move or remove it.", path); | ||
79 | return false; | ||
80 | } | ||
81 | |||
82 | return true; | ||
83 | } | ||
84 | |||
85 | /// <summary> | ||
86 | /// Try to parse a console UUID from the console. | ||
87 | /// </summary> | ||
88 | /// <remarks> | ||
89 | /// Will complain to the console if parsing fails. | ||
90 | /// </remarks> | ||
91 | /// <returns></returns> | ||
92 | /// <param name='console'>If null then no complaint is printed.</param> | ||
93 | /// <param name='rawUuid'></param> | ||
94 | /// <param name='uuid'></param> | ||
95 | public static bool TryParseConsoleUuid(ICommandConsole console, string rawUuid, out UUID uuid) | ||
96 | { | ||
97 | if (!UUID.TryParse(rawUuid, out uuid)) | ||
98 | { | ||
99 | if (console != null) | ||
100 | console.OutputFormat("ERROR: {0} is not a valid uuid", rawUuid); | ||
101 | |||
102 | return false; | ||
103 | } | ||
104 | |||
105 | return true; | ||
106 | } | ||
107 | |||
108 | public static bool TryParseConsoleLocalId(ICommandConsole console, string rawLocalId, out uint localId) | ||
109 | { | ||
110 | if (!uint.TryParse(rawLocalId, out localId)) | ||
111 | { | ||
112 | if (console != null) | ||
113 | console.OutputFormat("ERROR: {0} is not a valid local id", localId); | ||
114 | |||
115 | return false; | ||
116 | } | ||
117 | |||
118 | if (localId == 0) | ||
119 | { | ||
120 | if (console != null) | ||
121 | console.OutputFormat("ERROR: {0} is not a valid local id - it must be greater than 0", localId); | ||
122 | |||
123 | return false; | ||
124 | } | ||
125 | |||
126 | return true; | ||
127 | } | ||
128 | |||
129 | /// <summary> | ||
130 | /// Tries to parse the input as either a UUID or a local ID. | ||
131 | /// </summary> | ||
132 | /// <returns>true if parsing succeeded, false otherwise.</returns> | ||
133 | /// <param name='console'></param> | ||
134 | /// <param name='rawId'></param> | ||
135 | /// <param name='uuid'></param> | ||
136 | /// <param name='localId'> | ||
137 | /// Will be set to ConsoleUtil.LocalIdNotFound if parsing result was a UUID or no parse succeeded. | ||
138 | /// </param> | ||
139 | public static bool TryParseConsoleId(ICommandConsole console, string rawId, out UUID uuid, out uint localId) | ||
140 | { | ||
141 | if (TryParseConsoleUuid(null, rawId, out uuid)) | ||
142 | { | ||
143 | localId = LocalIdNotFound; | ||
144 | return true; | ||
145 | } | ||
146 | |||
147 | if (TryParseConsoleLocalId(null, rawId, out localId)) | ||
148 | { | ||
149 | return true; | ||
150 | } | ||
151 | |||
152 | if (console != null) | ||
153 | console.OutputFormat("ERROR: {0} is not a valid UUID or local id", rawId); | ||
154 | |||
155 | return false; | ||
156 | } | ||
157 | |||
158 | /// <summary> | ||
159 | /// Convert a console input to a bool, automatically complaining if a console is given. | ||
160 | /// </summary> | ||
161 | /// <param name='console'>Can be null if no console is available.</param> | ||
162 | /// <param name='rawConsoleVector'>/param> | ||
163 | /// <param name='vector'></param> | ||
164 | /// <returns></returns> | ||
165 | public static bool TryParseConsoleBool(ICommandConsole console, string rawConsoleString, out bool b) | ||
166 | { | ||
167 | if (!bool.TryParse(rawConsoleString, out b)) | ||
168 | { | ||
169 | if (console != null) | ||
170 | console.OutputFormat("ERROR: {0} is not a true or false value", rawConsoleString); | ||
171 | |||
172 | return false; | ||
173 | } | ||
174 | |||
175 | return true; | ||
176 | } | ||
177 | |||
178 | /// <summary> | ||
179 | /// Convert a console input to an int, automatically complaining if a console is given. | ||
180 | /// </summary> | ||
181 | /// <param name='console'>Can be null if no console is available.</param> | ||
182 | /// <param name='rawConsoleInt'>/param> | ||
183 | /// <param name='i'></param> | ||
184 | /// <returns></returns> | ||
185 | public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i) | ||
186 | { | ||
187 | if (!int.TryParse(rawConsoleInt, out i)) | ||
188 | { | ||
189 | if (console != null) | ||
190 | console.OutputFormat("ERROR: {0} is not a valid integer", rawConsoleInt); | ||
191 | |||
192 | return false; | ||
193 | } | ||
194 | |||
195 | return true; | ||
196 | } | ||
197 | |||
198 | /// <summary> | ||
199 | /// Convert a console input to a float, automatically complaining if a console is given. | ||
200 | /// </summary> | ||
201 | /// <param name='console'>Can be null if no console is available.</param> | ||
202 | /// <param name='rawConsoleInput'>/param> | ||
203 | /// <param name='i'></param> | ||
204 | /// <returns></returns> | ||
205 | public static bool TryParseConsoleFloat(ICommandConsole console, string rawConsoleInput, out float i) | ||
206 | { | ||
207 | if (!float.TryParse(rawConsoleInput, out i)) | ||
208 | { | ||
209 | if (console != null) | ||
210 | console.OutputFormat("ERROR: {0} is not a valid float", rawConsoleInput); | ||
211 | |||
212 | return false; | ||
213 | } | ||
214 | |||
215 | return true; | ||
216 | } | ||
217 | |||
218 | /// <summary> | ||
219 | /// Convert a console input to a double, automatically complaining if a console is given. | ||
220 | /// </summary> | ||
221 | /// <param name='console'>Can be null if no console is available.</param> | ||
222 | /// <param name='rawConsoleInput'>/param> | ||
223 | /// <param name='i'></param> | ||
224 | /// <returns></returns> | ||
225 | public static bool TryParseConsoleDouble(ICommandConsole console, string rawConsoleInput, out double i) | ||
226 | { | ||
227 | if (!double.TryParse(rawConsoleInput, out i)) | ||
228 | { | ||
229 | if (console != null) | ||
230 | console.OutputFormat("ERROR: {0} is not a valid double", rawConsoleInput); | ||
231 | |||
232 | return false; | ||
233 | } | ||
234 | |||
235 | return true; | ||
236 | } | ||
237 | |||
238 | /// <summary> | ||
239 | /// Convert a console integer to a natural int, automatically complaining if a console is given. | ||
240 | /// </summary> | ||
241 | /// <param name='console'>Can be null if no console is available.</param> | ||
242 | /// <param name='rawConsoleInt'>/param> | ||
243 | /// <param name='i'></param> | ||
244 | /// <returns></returns> | ||
245 | public static bool TryParseConsoleNaturalInt(ICommandConsole console, string rawConsoleInt, out int i) | ||
246 | { | ||
247 | if (TryParseConsoleInt(console, rawConsoleInt, out i)) | ||
248 | { | ||
249 | if (i < 0) | ||
250 | { | ||
251 | if (console != null) | ||
252 | console.OutputFormat("ERROR: {0} is not a positive integer", rawConsoleInt); | ||
253 | |||
254 | return false; | ||
255 | } | ||
256 | |||
257 | return true; | ||
258 | } | ||
259 | |||
260 | return false; | ||
261 | } | ||
262 | |||
263 | /// <summary> | ||
264 | /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3 | ||
265 | /// </summary> | ||
266 | /// <param name='rawConsoleVector'>/param> | ||
267 | /// <param name='vector'></param> | ||
268 | /// <returns></returns> | ||
269 | public static bool TryParseConsoleMinVector(string rawConsoleVector, out Vector3 vector) | ||
270 | { | ||
271 | return TryParseConsoleVector(rawConsoleVector, c => float.MinValue.ToString(), out vector); | ||
272 | } | ||
273 | |||
274 | /// <summary> | ||
275 | /// Convert a maximum vector input from the console to an OpenMetaverse.Vector3 | ||
276 | /// </summary> | ||
277 | /// <param name='rawConsoleVector'>/param> | ||
278 | /// <param name='vector'></param> | ||
279 | /// <returns></returns> | ||
280 | public static bool TryParseConsoleMaxVector(string rawConsoleVector, out Vector3 vector) | ||
281 | { | ||
282 | return TryParseConsoleVector(rawConsoleVector, c => float.MaxValue.ToString(), out vector); | ||
283 | } | ||
284 | |||
285 | /// <summary> | ||
286 | /// Convert a vector input from the console to an OpenMetaverse.Vector3 | ||
287 | /// </summary> | ||
288 | /// <param name='rawConsoleVector'> | ||
289 | /// A string in the form <x>,<y>,<z> where there is no space between values. | ||
290 | /// Any component can be missing (e.g. ,,40). blankComponentFunc is invoked to replace the blank with a suitable value | ||
291 | /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40,30 or 40) | ||
292 | /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue | ||
293 | /// Other than that, component values must be numeric. | ||
294 | /// </param> | ||
295 | /// <param name='blankComponentFunc'> | ||
296 | /// Behaviour if component is blank. If null then conversion fails on a blank component. | ||
297 | /// </param> | ||
298 | /// <param name='vector'></param> | ||
299 | /// <returns></returns> | ||
300 | public static bool TryParseConsoleVector( | ||
301 | string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector) | ||
302 | { | ||
303 | return Vector3.TryParse(CookVector(rawConsoleVector, 3, blankComponentFunc), out vector); | ||
304 | } | ||
305 | |||
306 | /// <summary> | ||
307 | /// Convert a vector input from the console to an OpenMetaverse.Vector2 | ||
308 | /// </summary> | ||
309 | /// <param name='rawConsoleVector'> | ||
310 | /// A string in the form <x>,<y> where there is no space between values. | ||
311 | /// Any component can be missing (e.g. ,40). blankComponentFunc is invoked to replace the blank with a suitable value | ||
312 | /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40) | ||
313 | /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue | ||
314 | /// Other than that, component values must be numeric. | ||
315 | /// </param> | ||
316 | /// <param name='blankComponentFunc'> | ||
317 | /// Behaviour if component is blank. If null then conversion fails on a blank component. | ||
318 | /// </param> | ||
319 | /// <param name='vector'></param> | ||
320 | /// <returns></returns> | ||
321 | public static bool TryParseConsole2DVector( | ||
322 | string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector2 vector) | ||
323 | { | ||
324 | // We don't use Vector2.TryParse() for now because for some reason it expects an input with 3 components | ||
325 | // rather than 2. | ||
326 | string cookedVector = CookVector(rawConsoleVector, 2, blankComponentFunc); | ||
327 | |||
328 | if (cookedVector == null) | ||
329 | { | ||
330 | vector = Vector2.Zero; | ||
331 | |||
332 | return false; | ||
333 | } | ||
334 | else | ||
335 | { | ||
336 | string[] cookedComponents = cookedVector.Split(VectorSeparatorChars); | ||
337 | |||
338 | vector = new Vector2(float.Parse(cookedComponents[0]), float.Parse(cookedComponents[1])); | ||
339 | |||
340 | return true; | ||
341 | } | ||
342 | |||
343 | //return Vector2.TryParse(CookVector(rawConsoleVector, 2, blankComponentFunc), out vector); | ||
344 | } | ||
345 | |||
346 | /// <summary> | ||
347 | /// Convert a raw console vector into a vector that can be be parsed by the relevant OpenMetaverse.TryParse() | ||
348 | /// </summary> | ||
349 | /// <param name='rawConsoleVector'></param> | ||
350 | /// <param name='dimensions'></param> | ||
351 | /// <param name='blankComponentFunc'></param> | ||
352 | /// <returns>null if conversion was not possible</returns> | ||
353 | private static string CookVector( | ||
354 | string rawConsoleVector, int dimensions, Func<string, string> blankComponentFunc) | ||
355 | { | ||
356 | List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList(); | ||
357 | |||
358 | if (components.Count < 1 || components.Count > dimensions) | ||
359 | return null; | ||
360 | |||
361 | if (components.Count < dimensions) | ||
362 | { | ||
363 | if (blankComponentFunc == null) | ||
364 | return null; | ||
365 | else | ||
366 | for (int i = components.Count; i < dimensions; i++) | ||
367 | components.Add(""); | ||
368 | } | ||
369 | |||
370 | List<string> cookedComponents | ||
371 | = components.ConvertAll<string>( | ||
372 | c => | ||
373 | { | ||
374 | if (c == "") | ||
375 | return blankComponentFunc.Invoke(c); | ||
376 | else if (c == MaxRawConsoleVectorValue) | ||
377 | return float.MaxValue.ToString(); | ||
378 | else if (c == MinRawConsoleVectorValue) | ||
379 | return float.MinValue.ToString(); | ||
380 | else | ||
381 | return c; | ||
382 | }); | ||
383 | |||
384 | return string.Join(VectorSeparator, cookedComponents.ToArray()); | ||
385 | } | ||
386 | } | ||
387 | } \ No newline at end of file | ||