aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Console/ConsoleUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Console/ConsoleUtil.cs')
-rw-r--r--OpenSim/Framework/Console/ConsoleUtil.cs175
1 files changed, 157 insertions, 18 deletions
diff --git a/OpenSim/Framework/Console/ConsoleUtil.cs b/OpenSim/Framework/Console/ConsoleUtil.cs
index dff956a..44f6dc1 100644
--- a/OpenSim/Framework/Console/ConsoleUtil.cs
+++ b/OpenSim/Framework/Console/ConsoleUtil.cs
@@ -49,14 +49,14 @@ namespace OpenSim.Framework.Console
49 = @"Each component of the coord is comma separated. There must be no spaces between the commas. 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. 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) 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 maxmimum value of a component then you can use ~ instead of a number 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 53 If you want to specify the minimum value of a component then you can use -~ instead of a number
54 e.g. 54 e.g.
55 delete object pos 20,20,20 to 40,40,40 55 show object pos 20,20,20 to 40,40,40
56 delete object pos 20,20 to 40,40 56 delete object pos 20,20 to 40,40
57 delete object pos ,20,20 to ,40,40 57 show object pos ,20,20 to ,40,40
58 delete object pos ,,30 to ,,~ 58 delete object pos ,,30 to ,,~
59 delete object pos ,,-~ to ,,30"; 59 show object pos ,,-~ to ,,30";
60 60
61 public const string MinRawConsoleVectorValue = "-~"; 61 public const string MinRawConsoleVectorValue = "-~";
62 public const string MaxRawConsoleVectorValue = "~"; 62 public const string MaxRawConsoleVectorValue = "~";
@@ -156,12 +156,32 @@ namespace OpenSim.Framework.Console
156 } 156 }
157 157
158 /// <summary> 158 /// <summary>
159 /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3 159 /// Convert a console input to a bool, automatically complaining if a console is given.
160 /// </summary> 160 /// </summary>
161 /// <param name='console'>Can be null if no console is available.</param> 161 /// <param name='console'>Can be null if no console is available.</param>
162 /// <param name='rawConsoleVector'>/param> 162 /// <param name='rawConsoleVector'>/param>
163 /// <param name='vector'></param> 163 /// <param name='vector'></param>
164 /// <returns></returns> 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>
165 public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i) 185 public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i)
166 { 186 {
167 if (!int.TryParse(rawConsoleInt, out i)) 187 if (!int.TryParse(rawConsoleInt, out i))
@@ -174,6 +194,71 @@ namespace OpenSim.Framework.Console
174 194
175 return true; 195 return true;
176 } 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 }
177 262
178 /// <summary> 263 /// <summary>
179 /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3 264 /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3
@@ -207,24 +292,82 @@ namespace OpenSim.Framework.Console
207 /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue 292 /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue
208 /// Other than that, component values must be numeric. 293 /// Other than that, component values must be numeric.
209 /// </param> 294 /// </param>
210 /// <param name='blankComponentFunc'></param> 295 /// <param name='blankComponentFunc'>
296 /// Behaviour if component is blank. If null then conversion fails on a blank component.
297 /// </param>
211 /// <param name='vector'></param> 298 /// <param name='vector'></param>
212 /// <returns></returns> 299 /// <returns></returns>
213 public static bool TryParseConsoleVector( 300 public static bool TryParseConsoleVector(
214 string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector) 301 string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector)
215 { 302 {
216 List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList(); 303 return Vector3.TryParse(CookVector(rawConsoleVector, 3, blankComponentFunc), out vector);
217 304 }
218 if (components.Count < 1 || components.Count > 3) 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)
219 { 329 {
220 vector = Vector3.Zero; 330 vector = Vector2.Zero;
331
221 return false; 332 return false;
222 } 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();
223 357
224 for (int i = components.Count; i < 3; i++) 358 if (components.Count < 1 || components.Count > dimensions)
225 components.Add(""); 359 return null;
226 360
227 List<string> semiDigestedComponents 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
228 = components.ConvertAll<string>( 371 = components.ConvertAll<string>(
229 c => 372 c =>
230 { 373 {
@@ -238,11 +381,7 @@ namespace OpenSim.Framework.Console
238 return c; 381 return c;
239 }); 382 });
240 383
241 string semiDigestedConsoleVector = string.Join(VectorSeparator, semiDigestedComponents.ToArray()); 384 return string.Join(VectorSeparator, cookedComponents.ToArray());
242
243 // m_log.DebugFormat("[CONSOLE UTIL]: Parsing {0} into OpenMetaverse.Vector3", semiDigestedConsoleVector);
244
245 return Vector3.TryParse(semiDigestedConsoleVector, out vector);
246 } 385 }
247 } 386 }
248} \ No newline at end of file 387} \ No newline at end of file