ConsoleUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Linq;
  31. using System.Reflection;
  32. using log4net;
  33. using OpenMetaverse;
  34. namespace OpenSim.Framework.Console
  35. {
  36. public class ConsoleUtil
  37. {
  38. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. public const int LocalIdNotFound = 0;
  40. /// <summary>
  41. /// Used by modules to display stock co-ordinate help, though possibly this should be under some general section
  42. /// rather than in each help summary.
  43. /// </summary>
  44. public const string CoordHelp
  45. = @"Each component of the coord is comma separated. There must be no spaces between the commas.
  46. If you don't care about the z component you can simply omit it.
  47. If you don't care about the x or y components then you can leave them blank (though a comma is still required)
  48. If you want to specify the maximum value of a component then you can use ~ instead of a number
  49. If you want to specify the minimum value of a component then you can use -~ instead of a number
  50. e.g.
  51. show object pos 20,20,20 to 40,40,40
  52. delete object pos 20,20 to 40,40
  53. show object pos ,20,20 to ,40,40
  54. delete object pos ,,30 to ,,~
  55. show object pos ,,-~ to ,,30";
  56. public const string MinRawConsoleVectorValue = "-~";
  57. public const string MaxRawConsoleVectorValue = "~";
  58. public const string VectorSeparator = ",";
  59. public static char[] VectorSeparatorChars = VectorSeparator.ToCharArray();
  60. /// <summary>
  61. /// Check if the given file path exists.
  62. /// </summary>
  63. /// <remarks>If not, warning is printed to the given console.</remarks>
  64. /// <returns>true if the file does not exist, false otherwise.</returns>
  65. /// <param name='console'></param>
  66. /// <param name='path'></param>
  67. public static bool CheckFileDoesNotExist(ICommandConsole console, string path)
  68. {
  69. if (File.Exists(path))
  70. {
  71. console.OutputFormat("File {0} already exists. Please move or remove it.", path);
  72. return false;
  73. }
  74. return true;
  75. }
  76. /// <summary>
  77. /// Try to parse a console UUID from the console.
  78. /// </summary>
  79. /// <remarks>
  80. /// Will complain to the console if parsing fails.
  81. /// </remarks>
  82. /// <returns></returns>
  83. /// <param name='console'>If null then no complaint is printed.</param>
  84. /// <param name='rawUuid'></param>
  85. /// <param name='uuid'></param>
  86. public static bool TryParseConsoleUuid(ICommandConsole console, string rawUuid, out UUID uuid)
  87. {
  88. if (!UUID.TryParse(rawUuid, out uuid))
  89. {
  90. if (console != null)
  91. console.OutputFormat("ERROR: {0} is not a valid uuid", rawUuid);
  92. return false;
  93. }
  94. return true;
  95. }
  96. public static bool TryParseConsoleLocalId(ICommandConsole console, string rawLocalId, out uint localId)
  97. {
  98. if (!uint.TryParse(rawLocalId, out localId))
  99. {
  100. if (console != null)
  101. console.OutputFormat("ERROR: {0} is not a valid local id", localId);
  102. return false;
  103. }
  104. if (localId == 0)
  105. {
  106. if (console != null)
  107. console.OutputFormat("ERROR: {0} is not a valid local id - it must be greater than 0", localId);
  108. return false;
  109. }
  110. return true;
  111. }
  112. /// <summary>
  113. /// Tries to parse the input as either a UUID or a local ID.
  114. /// </summary>
  115. /// <returns>true if parsing succeeded, false otherwise.</returns>
  116. /// <param name='console'></param>
  117. /// <param name='rawId'></param>
  118. /// <param name='uuid'></param>
  119. /// <param name='localId'>
  120. /// Will be set to ConsoleUtil.LocalIdNotFound if parsing result was a UUID or no parse succeeded.
  121. /// </param>
  122. public static bool TryParseConsoleId(ICommandConsole console, string rawId, out UUID uuid, out uint localId)
  123. {
  124. if (TryParseConsoleUuid(null, rawId, out uuid))
  125. {
  126. localId = LocalIdNotFound;
  127. return true;
  128. }
  129. if (TryParseConsoleLocalId(null, rawId, out localId))
  130. {
  131. return true;
  132. }
  133. if (console != null)
  134. console.OutputFormat("ERROR: {0} is not a valid UUID or local id", rawId);
  135. return false;
  136. }
  137. /// <summary>
  138. /// Convert a console integer to an int, automatically complaining if a console is given.
  139. /// </summary>
  140. /// <param name='console'>Can be null if no console is available.</param>
  141. /// <param name='rawConsoleVector'>/param>
  142. /// <param name='vector'></param>
  143. /// <returns></returns>
  144. public static bool TryParseConsoleBool(ICommandConsole console, string rawConsoleString, out bool b)
  145. {
  146. if (!bool.TryParse(rawConsoleString, out b))
  147. {
  148. if (console != null)
  149. console.OutputFormat("ERROR: {0} is not a true or false value", rawConsoleString);
  150. return false;
  151. }
  152. return true;
  153. }
  154. /// <summary>
  155. /// Convert a console integer to an int, automatically complaining if a console is given.
  156. /// </summary>
  157. /// <param name='console'>Can be null if no console is available.</param>
  158. /// <param name='rawConsoleInt'>/param>
  159. /// <param name='i'></param>
  160. /// <returns></returns>
  161. public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i)
  162. {
  163. if (!int.TryParse(rawConsoleInt, out i))
  164. {
  165. if (console != null)
  166. console.OutputFormat("ERROR: {0} is not a valid integer", rawConsoleInt);
  167. return false;
  168. }
  169. return true;
  170. }
  171. /// <summary>
  172. /// Convert a console integer to a natural int, automatically complaining if a console is given.
  173. /// </summary>
  174. /// <param name='console'>Can be null if no console is available.</param>
  175. /// <param name='rawConsoleInt'>/param>
  176. /// <param name='i'></param>
  177. /// <returns></returns>
  178. public static bool TryParseConsoleNaturalInt(ICommandConsole console, string rawConsoleInt, out int i)
  179. {
  180. if (TryParseConsoleInt(console, rawConsoleInt, out i))
  181. {
  182. if (i < 0)
  183. {
  184. if (console != null)
  185. console.OutputFormat("ERROR: {0} is not a positive integer", rawConsoleInt);
  186. return false;
  187. }
  188. return true;
  189. }
  190. return false;
  191. }
  192. /// <summary>
  193. /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3
  194. /// </summary>
  195. /// <param name='rawConsoleVector'>/param>
  196. /// <param name='vector'></param>
  197. /// <returns></returns>
  198. public static bool TryParseConsoleMinVector(string rawConsoleVector, out Vector3 vector)
  199. {
  200. return TryParseConsoleVector(rawConsoleVector, c => float.MinValue.ToString(), out vector);
  201. }
  202. /// <summary>
  203. /// Convert a maximum vector input from the console to an OpenMetaverse.Vector3
  204. /// </summary>
  205. /// <param name='rawConsoleVector'>/param>
  206. /// <param name='vector'></param>
  207. /// <returns></returns>
  208. public static bool TryParseConsoleMaxVector(string rawConsoleVector, out Vector3 vector)
  209. {
  210. return TryParseConsoleVector(rawConsoleVector, c => float.MaxValue.ToString(), out vector);
  211. }
  212. /// <summary>
  213. /// Convert a vector input from the console to an OpenMetaverse.Vector3
  214. /// </summary>
  215. /// <param name='rawConsoleVector'>
  216. /// A string in the form <x>,<y>,<z> where there is no space between values.
  217. /// Any component can be missing (e.g. ,,40). blankComponentFunc is invoked to replace the blank with a suitable value
  218. /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40,30 or 40)
  219. /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue
  220. /// Other than that, component values must be numeric.
  221. /// </param>
  222. /// <param name='blankComponentFunc'></param>
  223. /// <param name='vector'></param>
  224. /// <returns></returns>
  225. public static bool TryParseConsoleVector(
  226. string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector)
  227. {
  228. List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList();
  229. if (components.Count < 1 || components.Count > 3)
  230. {
  231. vector = Vector3.Zero;
  232. return false;
  233. }
  234. for (int i = components.Count; i < 3; i++)
  235. components.Add("");
  236. List<string> semiDigestedComponents
  237. = components.ConvertAll<string>(
  238. c =>
  239. {
  240. if (c == "")
  241. return blankComponentFunc.Invoke(c);
  242. else if (c == MaxRawConsoleVectorValue)
  243. return float.MaxValue.ToString();
  244. else if (c == MinRawConsoleVectorValue)
  245. return float.MinValue.ToString();
  246. else
  247. return c;
  248. });
  249. string semiDigestedConsoleVector = string.Join(VectorSeparator, semiDigestedComponents.ToArray());
  250. // m_log.DebugFormat("[CONSOLE UTIL]: Parsing {0} into OpenMetaverse.Vector3", semiDigestedConsoleVector);
  251. return Vector3.TryParse(semiDigestedConsoleVector, out vector);
  252. }
  253. }
  254. }