PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Aurora/Framework/Utilities/Util.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 3052 lines | 2363 code | 332 blank | 357 comment | 421 complexity | 19977b450cf6d5745361c032fee389e9 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Diagnostics;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.IO.Compression;
  35. using System.Linq;
  36. using System.Net;
  37. using System.Net.NetworkInformation;
  38. using System.Net.Sockets;
  39. using System.Reflection;
  40. using System.Reflection.Emit;
  41. using System.Runtime.Serialization;
  42. using System.Runtime.Serialization.Formatters.Binary;
  43. using System.Security.Cryptography;
  44. using System.Text;
  45. using System.Text.RegularExpressions;
  46. using System.Threading;
  47. using System.Windows.Forms;
  48. using System.Xml;
  49. using Amib.Threading;
  50. using Nini.Config;
  51. using OpenMetaverse;
  52. using OpenMetaverse.StructuredData;
  53. using ProtoBuf;
  54. using ProtoBuf.Meta;
  55. using ReaderWriterLockSlim = System.Threading.ReaderWriterLockSlim;
  56. namespace Aurora.Framework
  57. {
  58. /// <summary>
  59. /// The method used by Util.FireAndForget for asynchronously firing events
  60. /// </summary>
  61. public enum FireAndForgetMethod
  62. {
  63. UnsafeQueueUserWorkItem,
  64. QueueUserWorkItem,
  65. BeginInvoke,
  66. SmartThreadPool,
  67. Thread
  68. }
  69. public enum RuntimeEnvironment
  70. {
  71. NET,
  72. WinMono,
  73. Mono
  74. }
  75. /// <summary>
  76. /// Miscellaneous utility functions
  77. /// </summary>
  78. public class Util
  79. {
  80. private static uint nextXferID = 5000;
  81. private static readonly Random randomClass = new Random();
  82. // Get a list of invalid file characters (OS dependent)
  83. private static readonly string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]";
  84. private static readonly string regexInvalidPathChars = "[" + new String(Path.GetInvalidPathChars()) + "]";
  85. private static readonly object XferLock = new object();
  86. /// <summary>
  87. /// Thread pool used for Util.FireAndForget if
  88. /// FireAndForgetMethod.SmartThreadPool is used
  89. /// </summary>
  90. private static SmartThreadPool m_ThreadPool;
  91. private static volatile bool m_threadPoolRunning;
  92. // Unix-epoch starts at January 1st 1970, 00:00:00 UTC. And all our times in the server are (or at least should be) in UTC.
  93. public static readonly DateTime UnixEpoch =
  94. DateTime.ParseExact("1970-01-01 00:00:00 +0", "yyyy-MM-dd hh:mm:ss z", DateTimeFormatInfo.InvariantInfo).
  95. ToUniversalTime();
  96. public static readonly Regex UUIDPattern
  97. = new Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
  98. public static FireAndForgetMethod FireAndForgetMethod = FireAndForgetMethod.SmartThreadPool;
  99. static Util()
  100. {
  101. RuntimeTypeModel.Default.Add(typeof(UUID), false)
  102. .SetSurrogate(typeof(UUIDSurrogate));
  103. RuntimeTypeModel.Default.Add(typeof(IPEndPoint), false)
  104. .SetSurrogate(typeof(IPEndPointSurrogate));
  105. RuntimeTypeModel.Default.Add(typeof(OSD), false)
  106. .SetSurrogate(typeof(OSDSurrogate));
  107. RuntimeTypeModel.Default.Add(typeof(OSDMap), false)
  108. .SetSurrogate(typeof(OSDMapSurrogate));
  109. RuntimeTypeModel.Default.Add(typeof(Vector3), false)
  110. .SetSurrogate(typeof(Vector3Surrogate));
  111. RuntimeTypeModel.Default.Add(typeof(Quaternion), false)
  112. .SetSurrogate(typeof(QuaternionSurrogate));
  113. RuntimeTypeModel.Default.Add(typeof(ParcelManager.ParcelAccessEntry), false)
  114. .SetSurrogate(typeof(ParcelAccessEntrySurrogate));
  115. RuntimeTypeModel.Default.Add(typeof(MediaEntry), false)
  116. .SetSurrogate(typeof(MediaEntrySurrogate));
  117. RuntimeTypeModel.Default.Add(typeof(System.Drawing.Color), false)
  118. .SetSurrogate(typeof(ColorSurrogate));
  119. }
  120. #region Protobuf helpers
  121. [ProtoContract]
  122. class UUIDSurrogate
  123. {
  124. [ProtoMember(1)]
  125. public string ID;
  126. // protobuf-net wants an implicit or explicit operator between the types
  127. public static implicit operator UUID(UUIDSurrogate value)
  128. {
  129. return UUID.Parse(value.ID);
  130. }
  131. public static implicit operator UUIDSurrogate(UUID value)
  132. {
  133. return new UUIDSurrogate
  134. {
  135. ID = value.ToString()
  136. };
  137. }
  138. }
  139. [ProtoContract]
  140. class Vector3Surrogate
  141. {
  142. [ProtoMember(1)]
  143. public float X;
  144. [ProtoMember(2)]
  145. public float Y;
  146. [ProtoMember(3)]
  147. public float Z;
  148. // protobuf-net wants an implicit or explicit operator between the types
  149. public static implicit operator Vector3(Vector3Surrogate value)
  150. {
  151. return new Vector3(value.X, value.Y, value.Z);
  152. }
  153. public static implicit operator Vector3Surrogate(Vector3 value)
  154. {
  155. return new Vector3Surrogate()
  156. {
  157. X = value.X,
  158. Y = value.Y,
  159. Z = value.Z
  160. };
  161. }
  162. }
  163. [ProtoContract]
  164. class QuaternionSurrogate
  165. {
  166. [ProtoMember(1)]
  167. public float X;
  168. [ProtoMember(2)]
  169. public float Y;
  170. [ProtoMember(3)]
  171. public float Z;
  172. [ProtoMember(4)]
  173. public float W;
  174. // protobuf-net wants an implicit or explicit operator between the types
  175. public static implicit operator Quaternion(QuaternionSurrogate value)
  176. {
  177. return new Quaternion(value.X, value.Y, value.Z, value.W);
  178. }
  179. public static implicit operator QuaternionSurrogate(Quaternion value)
  180. {
  181. return new QuaternionSurrogate()
  182. {
  183. X = value.X,
  184. Y = value.Y,
  185. Z = value.Z,
  186. W = value.W
  187. };
  188. }
  189. }
  190. [ProtoContract]
  191. class IPEndPointSurrogate
  192. {
  193. [ProtoMember(1)]
  194. public string IPAddr;
  195. [ProtoMember(2)]
  196. public int Port;
  197. // protobuf-net wants an implicit or explicit operator between the types
  198. public static implicit operator IPEndPoint(IPEndPointSurrogate value)
  199. {
  200. return value == null ? null : new IPEndPoint(IPAddress.Parse(value.IPAddr), value.Port);
  201. }
  202. public static implicit operator IPEndPointSurrogate(IPEndPoint value)
  203. {
  204. return value == null ? null : new IPEndPointSurrogate
  205. {
  206. IPAddr = value.Address.ToString(),
  207. Port = value.Port
  208. };
  209. }
  210. }
  211. [ProtoContract]
  212. class OSDSurrogate
  213. {
  214. [ProtoMember(1)]
  215. public string str;
  216. // protobuf-net wants an implicit or explicit operator between the types
  217. public static implicit operator OSD(OSDSurrogate value)
  218. {
  219. return value.str == "" ? null : OSDParser.DeserializeJson(value.str);
  220. }
  221. public static implicit operator OSDSurrogate(OSD value)
  222. {
  223. return new OSDSurrogate
  224. {
  225. str = value == null ? "" : OSDParser.SerializeJsonString(value)
  226. };
  227. }
  228. }
  229. [ProtoContract]
  230. class OSDMapSurrogate
  231. {
  232. [ProtoMember(1)]
  233. public string str;
  234. // protobuf-net wants an implicit or explicit operator between the types
  235. public static implicit operator OSDMap(OSDMapSurrogate value)
  236. {
  237. return value.str == "" ? null : (OSDMap)OSDParser.DeserializeJson(value.str);
  238. }
  239. public static implicit operator OSDMapSurrogate(OSDMap value)
  240. {
  241. return new OSDMapSurrogate
  242. {
  243. str = value == null ? "" : OSDParser.SerializeJsonString(value)
  244. };
  245. }
  246. }
  247. [ProtoContract]
  248. class ParcelAccessEntrySurrogate
  249. {
  250. [ProtoMember(1)]
  251. public UUID AgentID;
  252. [ProtoMember(2)]
  253. public AccessList Flags;
  254. [ProtoMember(3)]
  255. public DateTime Time;
  256. // protobuf-net wants an implicit or explicit operator between the types
  257. public static implicit operator ParcelManager.ParcelAccessEntry(ParcelAccessEntrySurrogate value)
  258. {
  259. return new ParcelManager.ParcelAccessEntry() { AgentID = value.AgentID, Flags = value.Flags, Time = value.Time };
  260. }
  261. public static implicit operator ParcelAccessEntrySurrogate(ParcelManager.ParcelAccessEntry value)
  262. {
  263. return new ParcelAccessEntrySurrogate
  264. {
  265. AgentID = value.AgentID,
  266. Flags = value.Flags,
  267. Time = value.Time
  268. };
  269. }
  270. }
  271. [ProtoContract]
  272. class MediaEntrySurrogate
  273. {
  274. [ProtoMember(1)]
  275. public OSD info;
  276. // protobuf-net wants an implicit or explicit operator between the types
  277. public static implicit operator MediaEntry(MediaEntrySurrogate value)
  278. {
  279. return value.info == null ? null : MediaEntry.FromOSD(value.info);
  280. }
  281. public static implicit operator MediaEntrySurrogate(MediaEntry value)
  282. {
  283. return new MediaEntrySurrogate
  284. {
  285. info = value == null ? null : value.GetOSD()
  286. };
  287. }
  288. }
  289. [ProtoContract]
  290. class ColorSurrogate
  291. {
  292. [ProtoMember(1)]
  293. public int A;
  294. [ProtoMember(2)]
  295. public int R;
  296. [ProtoMember(3)]
  297. public int G;
  298. [ProtoMember(4)]
  299. public int B;
  300. // protobuf-net wants an implicit or explicit operator between the types
  301. public static implicit operator System.Drawing.Color(ColorSurrogate value)
  302. {
  303. return System.Drawing.Color.FromArgb(value.A, value.R, value.G, value.B);
  304. }
  305. public static implicit operator ColorSurrogate(System.Drawing.Color value)
  306. {
  307. return new ColorSurrogate
  308. {
  309. A = value.A,
  310. R = value.R,
  311. G = value.G,
  312. B = value.B
  313. };
  314. }
  315. }
  316. #endregion
  317. public static string ConvertToString(List<string> list)
  318. {
  319. StringBuilder builder = new StringBuilder();
  320. foreach (string val in list)
  321. {
  322. builder.Append(val + ",");
  323. }
  324. return builder.ToString();
  325. }
  326. public static string ConvertToString(Dictionary<string, object> list)
  327. {
  328. StringBuilder builder = new StringBuilder();
  329. foreach (var val in list)
  330. {
  331. builder.Append(val.Key + "=" + val.Value.ToString() + ",");
  332. }
  333. return builder.ToString();
  334. }
  335. //wab - Added for debugging ease
  336. public static string ConvertToString(OSDMap values, string lineStart)
  337. {
  338. if (string.IsNullOrEmpty(lineStart)) lineStart = "\t";
  339. StringBuilder builder = new StringBuilder ();
  340. String[] keys = new String[values.Count];
  341. values.Keys.CopyTo (keys, 0);
  342. foreach (String key in keys) {
  343. Object val = values [key];
  344. if (val == null)
  345. builder.AppendFormat("{0}{1}=null\n", lineStart, key);
  346. else if (val is OSDMap)
  347. builder.AppendFormat("{0}{1}=...\n{2}", lineStart, key, ConvertToString((OSDMap)val, "\t\t"));
  348. else
  349. builder.AppendFormat("{0}{1}={2}\n", lineStart, key, val.ToString());
  350. }
  351. return builder.ToString();
  352. }
  353. public static List<string> ConvertToList(string listAsString)
  354. {
  355. //Do both , and " " so that it removes any annoying spaces in the string added by users
  356. List<string> value =
  357. new List<string>(listAsString.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries));
  358. return value;
  359. }
  360. public static Dictionary<string, string> ConvertToDictionary(string listAsString)
  361. {
  362. //Do both , and " " so that it removes any annoying spaces in the string added by users
  363. List<string> value =
  364. new List<string>(listAsString.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries));
  365. Dictionary<string, string> dict = new Dictionary<string, string>();
  366. foreach (var v in value)
  367. {
  368. var split = v.Split('=');
  369. dict.Add(split[0], split[1]);
  370. }
  371. return dict;
  372. }
  373. public static string BuildYMDDateString(DateTime time)
  374. {
  375. return time.Year + "-" + time.Month + "-" + time.Day;
  376. }
  377. /// <summary>
  378. /// Gets the name of the directory where the current running executable
  379. /// is located
  380. /// </summary>
  381. /// <returns>Filesystem path to the directory containing the current
  382. /// executable</returns>
  383. public static string ExecutingDirectory()
  384. {
  385. return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  386. }
  387. /// <summary>
  388. /// Copy data from one stream to another, leaving the read position of both streams at the beginning.
  389. /// </summary>
  390. /// <param name='inputStream'>
  391. /// Input stream. Must be seekable.
  392. /// </param>
  393. /// <exception cref='ArgumentException'>
  394. /// Thrown if the input stream is not seekable.
  395. /// </exception>
  396. public static Stream Copy(Stream inputStream)
  397. {
  398. if (!inputStream.CanSeek)
  399. throw new ArgumentException("Util.Copy(Stream inputStream) must receive an inputStream that can seek");
  400. const int readSize = 256;
  401. byte[] buffer = new byte[readSize];
  402. MemoryStream ms = new MemoryStream();
  403. int count = inputStream.Read(buffer, 0, readSize);
  404. while (count > 0)
  405. {
  406. ms.Write(buffer, 0, count);
  407. count = inputStream.Read(buffer, 0, readSize);
  408. }
  409. ms.Position = 0;
  410. inputStream.Position = 0;
  411. return ms;
  412. }
  413. /// <summary>
  414. /// Linear interpolates B<->C using percent A
  415. /// </summary>
  416. /// <param name = "a"></param>
  417. /// <param name = "b"></param>
  418. /// <param name = "c"></param>
  419. /// <returns></returns>
  420. public static double lerp(double a, double b, double c)
  421. {
  422. return (b * a) + (c * (1 - a));
  423. }
  424. /// <summary>
  425. /// Bilinear Interpolate, see Lerp but for 2D using 'percents' X & Y.
  426. /// Layout:
  427. /// A B
  428. /// C D
  429. /// A<->C = Y
  430. /// C<->D = X
  431. /// </summary>
  432. /// <param name = "x"></param>
  433. /// <param name = "y"></param>
  434. /// <param name = "a"></param>
  435. /// <param name = "b"></param>
  436. /// <param name = "c"></param>
  437. /// <param name = "d"></param>
  438. /// <returns></returns>
  439. public static double lerp2D(double x, double y, double a, double b, double c, double d)
  440. {
  441. return lerp(y, lerp(x, a, b), lerp(x, c, d));
  442. }
  443. public static Encoding UTF8 = Encoding.UTF8;
  444. /// <value>
  445. /// Well known UUID for the blank texture used in the Linden SL viewer version 1.20 (and hopefully onwards)
  446. /// </value>
  447. public static UUID BLANK_TEXTURE_UUID = new UUID("5748decc-f629-461c-9a36-a35a221fe21f");
  448. #region Vector Equations
  449. /// <summary>
  450. /// Get the distance between two 3d vectors
  451. /// </summary>
  452. /// <param name = "a">A 3d vector</param>
  453. /// <param name = "b">A 3d vector</param>
  454. /// <returns>The distance between the two vectors</returns>
  455. public static double GetDistanceTo(Vector3 a, Vector3 b)
  456. {
  457. float dx = a.X - b.X;
  458. float dy = a.Y - b.Y;
  459. float dz = a.Z - b.Z;
  460. return Math.Sqrt(dx * dx + dy * dy + dz * dz);
  461. }
  462. /// <summary>
  463. /// Get the distance between two 3d vectors (excluding Z)
  464. /// </summary>
  465. /// <param name = "a">A 3d vector</param>
  466. /// <param name = "b">A 3d vector</param>
  467. /// <returns>The distance between the two vectors</returns>
  468. public static double GetFlatDistanceTo(Vector3 a, Vector3 b)
  469. {
  470. float dx = a.X - b.X;
  471. float dy = a.Y - b.Y;
  472. return Math.Sqrt(dx * dx + dy * dy);
  473. }
  474. /// <summary>
  475. /// Returns true if the distance beween A and B is less than amount. Significantly faster than GetDistanceTo since it eliminates the Sqrt.
  476. /// </summary>
  477. /// <param name = "a"></param>
  478. /// <param name = "b"></param>
  479. /// <param name = "amount"></param>
  480. /// <returns></returns>
  481. public static bool DistanceLessThan(Vector3 a, Vector3 b, double amount)
  482. {
  483. float dx = a.X - b.X;
  484. float dy = a.Y - b.Y;
  485. float dz = a.Z - b.Z;
  486. return (dx * dx + dy * dy + dz * dz) < (amount * amount);
  487. }
  488. /// <summary>
  489. /// Get the magnitude of a 3d vector
  490. /// </summary>
  491. /// <param name = "a">A 3d vector</param>
  492. /// <returns>The magnitude of the vector</returns>
  493. public static double GetMagnitude(Vector3 a)
  494. {
  495. return Math.Sqrt((a.X * a.X) + (a.Y * a.Y) + (a.Z * a.Z));
  496. }
  497. /// <summary>
  498. /// Get a normalized form of a 3d vector
  499. /// The vector paramater cannot be <0,0,0>
  500. /// </summary>
  501. /// <param name = "a">A 3d vector</param>
  502. /// <returns>A new vector which is normalized form of the vector</returns>
  503. public static Vector3 GetNormalizedVector(Vector3 a)
  504. {
  505. if (IsZeroVector(a))
  506. throw new ArgumentException("Vector paramater cannot be a zero vector.");
  507. float Mag = (float)GetMagnitude(a);
  508. return new Vector3(a.X / Mag, a.Y / Mag, a.Z / Mag);
  509. }
  510. /// <summary>
  511. /// Returns if a vector is a zero vector (has all zero components)
  512. /// </summary>
  513. /// <returns></returns>
  514. public static bool IsZeroVector(Vector3 v)
  515. {
  516. if (v.X == 0 && v.Y == 0 && v.Z == 0)
  517. {
  518. return true;
  519. }
  520. return false;
  521. }
  522. # endregion
  523. public static Quaternion Axes2Rot(Vector3 fwd, Vector3 left, Vector3 up)
  524. {
  525. float s;
  526. float tr = (float)(fwd.X + left.Y + up.Z + 1.0);
  527. if (tr >= 1.0)
  528. {
  529. s = (float)(0.5 / Math.Sqrt(tr));
  530. return new Quaternion(
  531. (left.Z - up.Y) * s,
  532. (up.X - fwd.Z) * s,
  533. (fwd.Y - left.X) * s,
  534. (float)0.25 / s);
  535. }
  536. else
  537. {
  538. float max = (left.Y > up.Z) ? left.Y : up.Z;
  539. if (max < fwd.X)
  540. {
  541. s = (float)(Math.Sqrt(fwd.X - (left.Y + up.Z) + 1.0));
  542. float x = (float)(s * 0.5);
  543. s = (float)(0.5 / s);
  544. return new Quaternion(
  545. x,
  546. (fwd.Y + left.X) * s,
  547. (up.X + fwd.Z) * s,
  548. (left.Z - up.Y) * s);
  549. }
  550. else if (max == left.Y)
  551. {
  552. s = (float)(Math.Sqrt(left.Y - (up.Z + fwd.X) + 1.0));
  553. float y = (float)(s * 0.5);
  554. s = (float)(0.5 / s);
  555. return new Quaternion(
  556. (fwd.Y + left.X) * s,
  557. y,
  558. (left.Z + up.Y) * s,
  559. (up.X - fwd.Z) * s);
  560. }
  561. else
  562. {
  563. s = (float)(Math.Sqrt(up.Z - (fwd.X + left.Y) + 1.0));
  564. float z = (float)(s * 0.5);
  565. s = (float)(0.5 / s);
  566. return new Quaternion(
  567. (up.X + fwd.Z) * s,
  568. (left.Z + up.Y) * s,
  569. z,
  570. (fwd.Y - left.X) * s);
  571. }
  572. }
  573. }
  574. public static Random RandomClass
  575. {
  576. get { return randomClass; }
  577. }
  578. public static T Clamp<T>(T x, T min, T max)
  579. where T : IComparable<T>
  580. {
  581. return x.CompareTo(max) > 0
  582. ? max
  583. : x.CompareTo(min) < 0
  584. ? min
  585. : x;
  586. }
  587. public static uint GetNextXferID()
  588. {
  589. uint id = 0;
  590. lock (XferLock)
  591. {
  592. id = nextXferID;
  593. nextXferID++;
  594. }
  595. return id;
  596. }
  597. ///<summary>
  598. /// Debug utility function to convert unbroken strings of XML into something human readable for occasional debugging purposes.
  599. ///
  600. /// Please don't delete me even if I appear currently unused!
  601. ///</summary>
  602. ///<param name = "rawXml"></param>
  603. ///<returns></returns>
  604. public static string GetFormattedXml(string rawXml)
  605. {
  606. XmlDocument xd = new XmlDocument();
  607. xd.LoadXml(rawXml);
  608. StringBuilder sb = new StringBuilder();
  609. StringWriter sw = new StringWriter(sb);
  610. XmlTextWriter xtw = new XmlTextWriter(sw) { Formatting = Formatting.Indented };
  611. try
  612. {
  613. xd.WriteTo(xtw);
  614. }
  615. finally
  616. {
  617. xtw.Close();
  618. }
  619. return sb.ToString();
  620. }
  621. public static bool IsEnvironmentSupported(ref string reason)
  622. {
  623. // Must have .NET 2.0 (Generics / libsl)
  624. if (Environment.Version.Major < 2)
  625. {
  626. reason = ".NET 1.0/1.1 lacks components that are used by Aurora";
  627. return false;
  628. }
  629. // Windows 95/98/ME are unsupported
  630. if (Environment.OSVersion.Platform == PlatformID.Win32Windows ||
  631. Environment.OSVersion.Platform == PlatformID.Win32S ||
  632. Environment.OSVersion.Platform == PlatformID.WinCE)
  633. {
  634. reason = "Windows 95/98/ME will not run Aurora";
  635. return false;
  636. }
  637. // Windows 2000 / Pre-SP2 XP
  638. if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
  639. Environment.OSVersion.Version.Major == 5 &&
  640. Environment.OSVersion.Version.Minor == 0)
  641. {
  642. reason = "Please update to Windows XP Service Pack 2 or Server 2003 with .NET 3.5 installed";
  643. return false;
  644. }
  645. if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
  646. Environment.Version.Major < 4 &&
  647. Environment.Version.Build < 50727) //.net 3.5
  648. {
  649. reason = ".NET versions before 3.5 lack components that are used by Aurora";
  650. return false;
  651. }
  652. return true;
  653. }
  654. public static bool IsLinux
  655. {
  656. get
  657. {
  658. int p = (int)Environment.OSVersion.Platform;
  659. return (p == 4) || (p == 6) || (p == 128);
  660. }
  661. }
  662. public static int UnixTimeSinceEpoch()
  663. {
  664. return ToUnixTime(DateTime.UtcNow);
  665. }
  666. public static int ToUnixTime(DateTime stamp)
  667. {
  668. TimeSpan t = stamp.ToUniversalTime() - UnixEpoch;
  669. return (int)t.TotalSeconds;
  670. }
  671. public static DateTime ToDateTime(ulong seconds)
  672. {
  673. DateTime epoch = UnixEpoch;
  674. return epoch.AddSeconds(seconds);
  675. }
  676. public static DateTime ToDateTime(int seconds)
  677. {
  678. DateTime epoch = UnixEpoch;
  679. return epoch.AddSeconds(seconds);
  680. }
  681. /// <summary>
  682. /// Return an md5 hash of the given string
  683. /// </summary>
  684. /// <param name = "data"></param>
  685. /// <returns></returns>
  686. public static string Md5Hash(string data)
  687. {
  688. byte[] dataMd5 = ComputeMD5Hash(data);
  689. StringBuilder sb = new StringBuilder();
  690. foreach (byte t in dataMd5)
  691. sb.AppendFormat("{0:x2}", t);
  692. return sb.ToString();
  693. }
  694. private static byte[] ComputeMD5Hash(string data)
  695. {
  696. MD5 md5 = MD5.Create();
  697. return md5.ComputeHash(Encoding.Default.GetBytes(data));
  698. }
  699. /// <summary>
  700. /// Return an SHA1 hash of the given string
  701. /// </summary>
  702. /// <param name = "data"></param>
  703. /// <returns></returns>
  704. public static string SHA1Hash(string data)
  705. {
  706. byte[] hash = ComputeSHA1Hash(data);
  707. return BitConverter.ToString(hash).Replace("-", String.Empty);
  708. }
  709. private static byte[] ComputeSHA1Hash(string src)
  710. {
  711. SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
  712. return SHA1.ComputeHash(Encoding.Default.GetBytes(src));
  713. }
  714. public static int fast_distance2d(int x, int y)
  715. {
  716. x = Math.Abs(x);
  717. y = Math.Abs(y);
  718. int min = Math.Min(x, y);
  719. return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
  720. }
  721. // Inclusive, within range test (true if equal to the endpoints)
  722. public static bool InRange<T>(T x, T min, T max)
  723. where T : IComparable<T>
  724. {
  725. return x.CompareTo(max) <= 0 && x.CompareTo(min) >= 0;
  726. }
  727. // Clamp the maximum magnitude of a vector
  728. public static Vector3 ClampV(Vector3 x, float max)
  729. {
  730. Vector3 ret = x;
  731. float lenSq = x.LengthSquared();
  732. if (lenSq > (max * max))
  733. {
  734. x = x / x.Length() * max;
  735. }
  736. return x;
  737. }
  738. public static string FieldToString(byte[] bytes)
  739. {
  740. return FieldToString(bytes, String.Empty);
  741. }
  742. /// <summary>
  743. /// Convert a variable length field (byte array) to a string, with a
  744. /// field name prepended to each line of the output
  745. /// </summary>
  746. /// <remarks>
  747. /// If the byte array has unprintable characters in it, a
  748. /// hex dump will be put in the string instead
  749. /// </remarks>
  750. /// <param name = "bytes">The byte array to convert to a string</param>
  751. /// <param name = "fieldName">A field name to prepend to each line of output</param>
  752. /// <returns>An ASCII string or a string containing a hex dump, minus
  753. /// the null terminator</returns>
  754. public static string FieldToString(byte[] bytes, string fieldName)
  755. {
  756. // Check for a common case
  757. if (bytes.Length == 0) return String.Empty;
  758. StringBuilder output = new StringBuilder();
  759. #if (!ISWIN)
  760. bool printable = true;
  761. foreach (byte t in bytes)
  762. {
  763. if ((t < 0x20 || t > 0x7E) && t != 0x09 && t != 0x0D && t != 0x0A && t != 0x00)
  764. {
  765. printable = false;
  766. break;
  767. }
  768. }
  769. #else
  770. bool printable = bytes.All(t => (t >= 0x20 && t <= 0x7E) || t == 0x09 || t == 0x0D || t == 0x0A || t == 0x00);
  771. #endif
  772. if (printable)
  773. {
  774. if (fieldName.Length > 0)
  775. {
  776. output.Append(fieldName);
  777. output.Append(": ");
  778. }
  779. output.Append(CleanString(UTF8.GetString(bytes, 0, bytes.Length - 1)));
  780. }
  781. else
  782. {
  783. for (int i = 0; i < bytes.Length; i += 16)
  784. {
  785. if (i != 0)
  786. output.Append(Environment.NewLine);
  787. if (fieldName.Length > 0)
  788. {
  789. output.Append(fieldName);
  790. output.Append(": ");
  791. }
  792. for (int j = 0; j < 16; j++)
  793. {
  794. output.Append((i + j) < bytes.Length ? String.Format("{0:X2} ", bytes[i + j]) : " ");
  795. }
  796. for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
  797. {
  798. if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
  799. output.Append((char)bytes[i + j]);
  800. else
  801. output.Append(".");
  802. }
  803. }
  804. }
  805. return output.ToString();
  806. }
  807. /// <summary>
  808. /// Removes all invalid path chars (OS dependent)
  809. /// </summary>
  810. /// <param name = "path">path</param>
  811. /// <returns>safe path</returns>
  812. public static string safePath(string path)
  813. {
  814. return Regex.Replace(path, regexInvalidPathChars, String.Empty);
  815. }
  816. /// <summary>
  817. /// Removes all invalid filename chars (OS dependent)
  818. /// </summary>
  819. /// <param name = "path">filename</param>
  820. /// <returns>safe filename</returns>
  821. public static string safeFileName(string filename)
  822. {
  823. return Regex.Replace(filename, regexInvalidFileChars, String.Empty);
  824. ;
  825. }
  826. //
  827. // directory locations
  828. //
  829. public static string homeDir()
  830. {
  831. string temp;
  832. // string personal=(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
  833. // temp = Path.Combine(personal,".OpenSim");
  834. temp = ".";
  835. return temp;
  836. }
  837. public static string configDir()
  838. {
  839. return ".";
  840. }
  841. public static string dataDir()
  842. {
  843. return ".";
  844. }
  845. public static string logDir()
  846. {
  847. return ".";
  848. }
  849. // From: http://coercedcode.blogspot.com/2008/03/c-generate-unique-filenames-within.html
  850. public static string GetUniqueFilename(string FileName)
  851. {
  852. int count = 0;
  853. string Name;
  854. if (File.Exists(FileName))
  855. {
  856. FileInfo f = new FileInfo(FileName);
  857. Name = !String.IsNullOrEmpty(f.Extension) ? f.FullName.Substring(0, f.FullName.LastIndexOf('.')) : f.FullName;
  858. while (File.Exists(FileName))
  859. {
  860. count++;
  861. FileName = Name + count + f.Extension;
  862. }
  863. }
  864. return FileName;
  865. }
  866. // Nini (config) related Methods
  867. public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
  868. {
  869. if (!File.Exists(fileName))
  870. {
  871. //create new file
  872. }
  873. XmlConfigSource config = new XmlConfigSource(fileName);
  874. AddDataRowToConfig(config, row);
  875. config.Save();
  876. return config;
  877. }
  878. public static void AddDataRowToConfig(IConfigSource config, DataRow row)
  879. {
  880. config.Configs.Add((string)row[0]);
  881. for (int i = 0; i < row.Table.Columns.Count; i++)
  882. {
  883. config.Configs[(string)row[0]].Set(row.Table.Columns[i].ColumnName, row[i]);
  884. }
  885. }
  886. public static float Clip(float x, float min, float max)
  887. {
  888. return Math.Min(Math.Max(x, min), max);
  889. }
  890. public static int Clip(int x, int min, int max)
  891. {
  892. return Math.Min(Math.Max(x, min), max);
  893. }
  894. /// <summary>
  895. /// Convert an UUID to a raw uuid string. Right now this is a string without hyphens.
  896. /// </summary>
  897. /// <param name = "UUID"></param>
  898. /// <returns></returns>
  899. public static String ToRawUuidString(UUID UUID)
  900. {
  901. return UUID.Guid.ToString("n");
  902. }
  903. public static string CleanString(string input)
  904. {
  905. if (input.Length == 0)
  906. return input;
  907. int clip = input.Length;
  908. // Test for ++ string terminator
  909. int pos = input.IndexOf("\0");
  910. if (pos != -1 && pos < clip)
  911. clip = pos;
  912. // Test for CR
  913. pos = input.IndexOf("\r");
  914. if (pos != -1 && pos < clip)
  915. clip = pos;
  916. // Test for LF
  917. pos = input.IndexOf("\n");
  918. if (pos != -1 && pos < clip)
  919. clip = pos;
  920. // Truncate string before first end-of-line character found
  921. return input.Substring(0, clip);
  922. }
  923. /// <summary>
  924. /// returns the contents of /etc/issue on Unix Systems
  925. /// Use this for where it's absolutely necessary to implement platform specific stuff
  926. /// </summary>
  927. /// <returns></returns>
  928. public static string ReadEtcIssue()
  929. {
  930. try
  931. {
  932. StreamReader sr = new StreamReader("/etc/issue.net");
  933. string issue = sr.ReadToEnd();
  934. sr.Close();
  935. return issue;
  936. }
  937. catch (Exception)
  938. {
  939. return "";
  940. }
  941. }
  942. public static void SerializeToFile(string filename, Object obj)
  943. {
  944. IFormatter formatter = new BinaryFormatter();
  945. Stream stream = null;
  946. try
  947. {
  948. stream = new FileStream(
  949. filename, FileMode.Create,
  950. FileAccess.Write, FileShare.None);
  951. formatter.Serialize(stream, obj);
  952. }
  953. catch (Exception e)
  954. {
  955. MainConsole.Instance.Error(e.ToString());
  956. }
  957. finally
  958. {
  959. if (stream != null)
  960. {
  961. stream.Close();
  962. }
  963. }
  964. }
  965. public static Object DeserializeFromFile(string filename)
  966. {
  967. IFormatter formatter = new BinaryFormatter();
  968. Stream stream = null;
  969. Object ret = null;
  970. try
  971. {
  972. stream = new FileStream(
  973. filename, FileMode.Open,
  974. FileAccess.Read, FileShare.None);
  975. ret = formatter.Deserialize(stream);
  976. }
  977. catch (Exception e)
  978. {
  979. MainConsole.Instance.Error(e.ToString());
  980. }
  981. finally
  982. {
  983. if (stream != null)
  984. {
  985. stream.Close();
  986. }
  987. }
  988. return ret;
  989. }
  990. public static void Compress7ZipFile(string path, string destination)
  991. {
  992. ProcessStartInfo p = new ProcessStartInfo();
  993. string pa = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Util)).CodeBase);
  994. if (pa != null)
  995. {
  996. p.FileName = Path.Combine(pa, "7za.exe");
  997. p.Arguments = "a -y -tgzip \"" + destination + "\" \"" + path + "\" -mx=9";
  998. p.WindowStyle = ProcessWindowStyle.Hidden;
  999. Process x = Process.Start(p);
  1000. x.WaitForExit();
  1001. }
  1002. }
  1003. public static void UnCompress7ZipFile(string path, string destination)
  1004. {
  1005. ProcessStartInfo p = new ProcessStartInfo();
  1006. string pa = Path.GetDirectoryName(Assembly.GetAssembly(typeof(Util)).CodeBase);
  1007. if (pa != null)
  1008. {
  1009. p.FileName = Path.Combine(pa, "7za.exe");
  1010. p.Arguments = "e -y \"" + path + "\" -o\"" + destination + "\" -mx=9";
  1011. p.WindowStyle = ProcessWindowStyle.Hidden;
  1012. Process x = Process.Start(p);
  1013. x.WaitForExit();
  1014. }
  1015. }
  1016. public static string Compress(string text)
  1017. {
  1018. byte[] buffer = UTF8.GetBytes(text);
  1019. MemoryStream memory = new MemoryStream();
  1020. using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true))
  1021. {
  1022. compressor.Write(buffer, 0, buffer.Length);
  1023. }
  1024. memory.Position = 0;
  1025. byte[] compressed = new byte[memory.Length];
  1026. memory.Read(compressed, 0, compressed.Length);
  1027. byte[] compressedBuffer = new byte[compressed.Length + 4];
  1028. Buffer.BlockCopy(compressed, 0, compressedBuffer, 4, compressed.Length);
  1029. Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressedBuffer, 0, 4);
  1030. return Convert.ToBase64String(compressedBuffer);
  1031. }
  1032. public static byte[] CompressBytes(byte[] buffer)
  1033. {
  1034. MemoryStream memory = new MemoryStream();
  1035. using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true))
  1036. {
  1037. compressor.Write(buffer, 0, buffer.Length);
  1038. }
  1039. memory.Position = 0;
  1040. byte[] compressed = new byte[memory.Length];
  1041. memory.Read(compressed, 0, compressed.Length);
  1042. byte[] compressedBuffer = new byte[compressed.Length + 4];
  1043. Buffer.BlockCopy(compressed, 0, compressedBuffer, 4, compressed.Length);
  1044. Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressedBuffer, 0, 4);
  1045. return compressedBuffer;
  1046. }
  1047. public static string Decompress(string compressedText)
  1048. {
  1049. byte[] compressedBuffer = Convert.FromBase64String(compressedText);
  1050. using (MemoryStream memory = new MemoryStream())
  1051. {
  1052. int msgLength = BitConverter.ToInt32(compressedBuffer, 0);
  1053. memory.Write(compressedBuffer, 4, compressedBuffer.Length - 4);
  1054. byte[] buffer = new byte[msgLength];
  1055. memory.Position = 0;
  1056. using (GZipStream decompressor = new GZipStream(memory, CompressionMode.Decompress))
  1057. {
  1058. decompressor.Read(buffer, 0, buffer.Length);
  1059. }
  1060. return UTF8.GetString(buffer);
  1061. }
  1062. }
  1063. public static Stream DecompressStream(Stream compressedStream)
  1064. {
  1065. byte[] compressedBuffer = ReadToEnd(compressedStream);
  1066. using (MemoryStream memory = new MemoryStream())
  1067. {
  1068. int msgLength = BitConverter.ToInt32(compressedBuffer, 0);
  1069. memory.Write(compressedBuffer, 4, compressedBuffer.Length - 4);
  1070. byte[] buffer = new byte[msgLength];
  1071. memory.Position = 0;
  1072. using (GZipStream decompressor = new GZipStream(memory, CompressionMode.Decompress))
  1073. {
  1074. decompressor.Read(buffer, 0, buffer.Length);
  1075. }
  1076. return new MemoryStream(buffer);
  1077. }
  1078. }
  1079. public static byte[] ReadToEnd(System.IO.Stream stream)
  1080. {
  1081. byte[] readBuffer = new byte[4096];
  1082. int totalBytesRead = 0;
  1083. int bytesRead;
  1084. while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
  1085. {
  1086. totalBytesRead += bytesRead;
  1087. if (totalBytesRead == readBuffer.Length)
  1088. {
  1089. int nextByte = stream.ReadByte();
  1090. if (nextByte != -1)
  1091. {
  1092. byte[] temp = new byte[readBuffer.Length * 2];
  1093. Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
  1094. Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
  1095. readBuffer = temp;
  1096. totalBytesRead++;
  1097. }
  1098. }
  1099. }
  1100. byte[] buffer = readBuffer;
  1101. if (readBuffer.Length != totalBytesRead)
  1102. {
  1103. buffer = new byte[totalBytesRead];
  1104. Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
  1105. }
  1106. return buffer;
  1107. }
  1108. /// <summary>
  1109. /// Converts a byte array in big endian order into an ulong.
  1110. /// </summary>
  1111. /// <param name = "bytes">
  1112. /// The array of bytes
  1113. /// </param>
  1114. /// <returns>
  1115. /// The extracted ulong
  1116. /// </returns>
  1117. public static ulong BytesToUInt64Big(byte[] bytes)
  1118. {
  1119. if (bytes.Length < 8) return 0;
  1120. return ((ulong)bytes[0] << 56) | ((ulong)bytes[1] << 48) | ((ulong)bytes[2] << 40) |
  1121. ((ulong)bytes[3] << 32) |
  1122. ((ulong)bytes[4] << 24) | ((ulong)bytes[5] << 16) | ((ulong)bytes[6] << 8) | bytes[7];
  1123. }
  1124. // used for RemoteParcelRequest (for "About Landmark")
  1125. public static UUID BuildFakeParcelID(ulong regionHandle, uint x, uint y)
  1126. {
  1127. byte[] bytes =
  1128. {
  1129. (byte) regionHandle, (byte) (regionHandle >> 8), (byte) (regionHandle >> 16),
  1130. (byte) (regionHandle >> 24),
  1131. (byte) (regionHandle >> 32), (byte) (regionHandle >> 40), (byte) (regionHandle >> 48),
  1132. (byte) (regionHandle << 56),
  1133. (byte) x, (byte) (x >> 8), 0, 0,
  1134. (byte) y, (byte) (y >> 8), 0, 0
  1135. };
  1136. return new UUID(bytes, 0);
  1137. }
  1138. public static UUID BuildFakeParcelID(ulong regionHandle, uint x, uint y, uint z)
  1139. {
  1140. byte[] bytes =
  1141. {
  1142. (byte) regionHandle, (byte) (regionHandle >> 8), (byte) (regionHandle >> 16),
  1143. (byte) (regionHandle >> 24),
  1144. (byte) (regionHandle >> 32), (byte) (regionHandle >> 40), (byte) (regionHandle >> 48),
  1145. (byte) (regionHandle << 56),
  1146. (byte) x, (byte) (x >> 8), (byte) z, (byte) (z >> 8),
  1147. (byte) y, (byte) (y >> 8), 0, 0
  1148. };
  1149. return new UUID(bytes, 0);
  1150. }
  1151. public static void ParseFakeParcelID(UUID parcelID, out ulong regionHandle, out uint x, out uint y)
  1152. {
  1153. byte[] bytes = parcelID.GetBytes();
  1154. regionHandle = Utils.BytesToUInt64(bytes);
  1155. x = Utils.BytesToUInt(bytes, 8) & 0xffff;
  1156. y = Utils.BytesToUInt(bytes, 12) & 0xffff;
  1157. }
  1158. public static void ParseFakeParcelID(UUID parcelID, out ulong regionHandle, out uint x, out uint y, out uint z)
  1159. {
  1160. byte[] bytes = parcelID.GetBytes();
  1161. regionHandle = Utils.BytesToUInt64(bytes);
  1162. x = Utils.BytesToUInt(bytes, 8) & 0xffff;
  1163. z = (Utils.BytesToUInt(bytes, 8) & 0xffff0000) >> 16;
  1164. y = Utils.BytesToUInt(bytes, 12) & 0xffff;
  1165. }
  1166. public static void FakeParcelIDToGlobalPosition(UUID parcelID, out uint x, out uint y)
  1167. {
  1168. ulong regionHandle;
  1169. uint rx, ry;
  1170. ParseFakeParcelID(parcelID, out regionHandle, out x, out y);
  1171. Utils.LongToUInts(regionHandle, out rx, out ry);
  1172. x += rx;
  1173. y += ry;
  1174. }
  1175. /// <summary>
  1176. /// Get operating system information if available. Returns only the first 45 characters of information
  1177. /// </summary>
  1178. /// <returns>
  1179. /// Operating system information. Returns an empty string if none was available.
  1180. /// </returns>
  1181. public static string GetOperatingSystemInformation()
  1182. {
  1183. string os = String.Empty;
  1184. os = Environment.OSVersion.Platform != PlatformID.Unix ? Environment.OSVersion.ToString() : ReadEtcIssue();
  1185. if (os.Length > 45)
  1186. {
  1187. os = os.Substring(0, 45);
  1188. }
  1189. return os;
  1190. }
  1191. public static string GetRuntimeInformation()
  1192. {
  1193. string ru = String.Empty;
  1194. if (Environment.OSVersion.Platform == PlatformID.Unix)
  1195. ru = "Unix/Mono";
  1196. else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
  1197. ru = "OSX/Mono";
  1198. else
  1199. {
  1200. ru = Type.GetType("Mono.Runtime") != null ? "Win/Mono" : "Win/.NET";
  1201. }
  1202. return ru;
  1203. }
  1204. public static RuntimeEnvironment GetRuntimeEnvironment()
  1205. {
  1206. if (Environment.OSVersion.Platform == PlatformID.Unix)
  1207. return RuntimeEnvironment.Mono;
  1208. else if (Environment.OSVersion.Platform == PlatformID.MacOSX)
  1209. return RuntimeEnvironment.Mono;
  1210. else
  1211. {
  1212. return Type.GetType("Mono.Runtime") != null ? RuntimeEnvironment.WinMono :
  1213. RuntimeEnvironment.NET;
  1214. }
  1215. }
  1216. /// <summary>
  1217. /// Is the given string a UUID?
  1218. /// </summary>
  1219. /// <param name = "s"></param>
  1220. /// <returns></returns>
  1221. public static bool isUUID(string s)
  1222. {
  1223. return UUIDPattern.IsMatch(s);
  1224. }
  1225. public static string GetDisplayConnectionString(string connectionString)
  1226. {
  1227. int passPosition = 0;
  1228. int passEndPosition = 0;
  1229. string displayConnectionString = null;
  1230. // hide the password in the connection string

Large files files are truncated, but you can click here to view the full file