diff options
-rw-r--r-- | OpenSim/Framework/Console/ConsoleUtil.cs | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/OpenSim/Framework/Console/ConsoleUtil.cs b/OpenSim/Framework/Console/ConsoleUtil.cs new file mode 100644 index 0000000..a254be0 --- /dev/null +++ b/OpenSim/Framework/Console/ConsoleUtil.cs | |||
@@ -0,0 +1,111 @@ | |||
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.Linq; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using OpenMetaverse; | ||
34 | |||
35 | public class ConsoleUtil | ||
36 | { | ||
37 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
38 | |||
39 | public const string MinRawConsoleVectorValue = "-~"; | ||
40 | public const string MaxRawConsoleVectorValue = "~"; | ||
41 | |||
42 | public const string VectorSeparator = ","; | ||
43 | public static char[] VectorSeparatorChars = VectorSeparator.ToCharArray(); | ||
44 | |||
45 | /// <summary> | ||
46 | /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3 | ||
47 | /// </summary> | ||
48 | /// <param name='rawConsoleVector'>/param> | ||
49 | /// <param name='vector'></param> | ||
50 | /// <returns></returns> | ||
51 | public static bool TryParseConsoleMinVector(string rawConsoleVector, out Vector3 vector) | ||
52 | { | ||
53 | return TryParseConsoleVector(rawConsoleVector, c => float.MinValue.ToString(), out vector); | ||
54 | } | ||
55 | |||
56 | /// <summary> | ||
57 | /// Convert a maximum vector input from the console to an OpenMetaverse.Vector3 | ||
58 | /// </summary> | ||
59 | /// <param name='rawConsoleVector'>/param> | ||
60 | /// <param name='vector'></param> | ||
61 | /// <returns></returns> | ||
62 | public static bool TryParseConsoleMaxVector(string rawConsoleVector, out Vector3 vector) | ||
63 | { | ||
64 | return TryParseConsoleVector(rawConsoleVector, c => float.MaxValue.ToString(), out vector); | ||
65 | } | ||
66 | |||
67 | /// <summary> | ||
68 | /// Convert a vector input from the console to an OpenMetaverse.Vector3 | ||
69 | /// </summary> | ||
70 | /// <param name='rawConsoleVector'> | ||
71 | /// A string in the form <x>,<y>,<z> where there is no space between values. | ||
72 | /// Any component can be missing (e.g. ,,40). blankComponentFunc is invoked to replace the blank with a suitable value | ||
73 | /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40,30 or 40) | ||
74 | /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue | ||
75 | /// Other than that, component values must be numeric. | ||
76 | /// </param> | ||
77 | /// <param name='blankComponentFunc'></param> | ||
78 | /// <param name='vector'></param> | ||
79 | /// <returns></returns> | ||
80 | public static bool TryParseConsoleVector( | ||
81 | string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector) | ||
82 | { | ||
83 | List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList(); | ||
84 | |||
85 | if (components.Count < 1 || components.Count > 3) | ||
86 | return false; | ||
87 | |||
88 | for (int i = components.Count; i < 3; i++) | ||
89 | components.Add(""); | ||
90 | |||
91 | List<string> semiDigestedComponents | ||
92 | = components.ConvertAll<string>( | ||
93 | c => | ||
94 | { | ||
95 | if (c == "") | ||
96 | return blankComponentFunc.Invoke(c); | ||
97 | else if (c == MaxRawConsoleVectorValue) | ||
98 | return float.MaxValue.ToString(); | ||
99 | else if (c == MinRawConsoleVectorValue) | ||
100 | return float.MinValue.ToString(); | ||
101 | else | ||
102 | return c; | ||
103 | }); | ||
104 | |||
105 | string semiDigestedConsoleVector = string.Join(VectorSeparator, semiDigestedComponents.ToArray()); | ||
106 | |||
107 | m_log.DebugFormat("[CONSOLE UTIL]: Parsing {0} into OpenMetaverse.Vector3", semiDigestedConsoleVector); | ||
108 | |||
109 | return Vector3.TryParse(semiDigestedConsoleVector, out vector); | ||
110 | } | ||
111 | } \ No newline at end of file | ||