ConsoleUtil.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 minimum vector input from the console to an OpenMetaverse.Vector3
  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 TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i)
  145. {
  146. if (!int.TryParse(rawConsoleInt, out i))
  147. {
  148. if (console != null)
  149. console.OutputFormat("ERROR: {0} is not a valid integer", rawConsoleInt);
  150. return false;
  151. }
  152. return true;
  153. }
  154. /// <summary>
  155. /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3
  156. /// </summary>
  157. /// <param name='rawConsoleVector'>/param>
  158. /// <param name='vector'></param>
  159. /// <returns></returns>
  160. public static bool TryParseConsoleMinVector(string rawConsoleVector, out Vector3 vector)
  161. {
  162. return TryParseConsoleVector(rawConsoleVector, c => float.MinValue.ToString(), out vector);
  163. }
  164. /// <summary>
  165. /// Convert a maximum vector input from the console to an OpenMetaverse.Vector3
  166. /// </summary>
  167. /// <param name='rawConsoleVector'>/param>
  168. /// <param name='vector'></param>
  169. /// <returns></returns>
  170. public static bool TryParseConsoleMaxVector(string rawConsoleVector, out Vector3 vector)
  171. {
  172. return TryParseConsoleVector(rawConsoleVector, c => float.MaxValue.ToString(), out vector);
  173. }
  174. /// <summary>
  175. /// Convert a vector input from the console to an OpenMetaverse.Vector3
  176. /// </summary>
  177. /// <param name='rawConsoleVector'>
  178. /// A string in the form <x>,<y>,<z> where there is no space between values.
  179. /// Any component can be missing (e.g. ,,40). blankComponentFunc is invoked to replace the blank with a suitable value
  180. /// Also, if the blank component is at the end, then the comma can be missed off entirely (e.g. 40,30 or 40)
  181. /// The strings "~" and "-~" are valid in components. The first substitutes float.MaxValue whilst the second is float.MinValue
  182. /// Other than that, component values must be numeric.
  183. /// </param>
  184. /// <param name='blankComponentFunc'></param>
  185. /// <param name='vector'></param>
  186. /// <returns></returns>
  187. public static bool TryParseConsoleVector(
  188. string rawConsoleVector, Func<string, string> blankComponentFunc, out Vector3 vector)
  189. {
  190. List<string> components = rawConsoleVector.Split(VectorSeparatorChars).ToList();
  191. if (components.Count < 1 || components.Count > 3)
  192. {
  193. vector = Vector3.Zero;
  194. return false;
  195. }
  196. for (int i = components.Count; i < 3; i++)
  197. components.Add("");
  198. List<string> semiDigestedComponents
  199. = components.ConvertAll<string>(
  200. c =>
  201. {
  202. if (c == "")
  203. return blankComponentFunc.Invoke(c);
  204. else if (c == MaxRawConsoleVectorValue)
  205. return float.MaxValue.ToString();
  206. else if (c == MinRawConsoleVectorValue)
  207. return float.MinValue.ToString();
  208. else
  209. return c;
  210. });
  211. string semiDigestedConsoleVector = string.Join(VectorSeparator, semiDigestedComponents.ToArray());
  212. // m_log.DebugFormat("[CONSOLE UTIL]: Parsing {0} into OpenMetaverse.Vector3", semiDigestedConsoleVector);
  213. return Vector3.TryParse(semiDigestedConsoleVector, out vector);
  214. }
  215. }
  216. }