PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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
  1231. passPosition = connectionString.IndexOf("password", StringComparison.OrdinalIgnoreCase);
  1232. passPosition = connectionString.IndexOf("=", passPosition);
  1233. if (passPosition < connectionString.Length)
  1234. passPosition += 1;
  1235. passEndPosition = connectionString.IndexOf(";", passPosition);
  1236. displayConnectionString = connectionString.Substring(0, passPosition);
  1237. displayConnectionString += "***";
  1238. displayConnectionString += connectionString.Substring(passEndPosition,
  1239. connectionString.Length - passEndPosition);
  1240. return displayConnectionString;
  1241. }
  1242. public static T ReadSettingsFromIniFile<T>(IConfig config, T settingsClass)
  1243. {
  1244. Type settingsType = settingsClass.GetType();
  1245. FieldInfo[] fieldInfos = settingsType.GetFields();
  1246. foreach (FieldInfo fieldInfo in fieldInfos)
  1247. {
  1248. if (!fieldInfo.IsStatic)
  1249. {
  1250. if (fieldInfo.FieldType == typeof (String))
  1251. {
  1252. fieldInfo.SetValue(settingsClass, config.Get(fieldInfo.Name, (string) fieldInfo.GetValue(settingsClass)));
  1253. }
  1254. else if (fieldInfo.FieldType == typeof (Boolean))
  1255. {
  1256. fieldInfo.SetValue(settingsClass, config.GetBoolean(fieldInfo.Name, (bool) fieldInfo.GetValue(settingsClass)));
  1257. }
  1258. else if (fieldInfo.FieldType == typeof (Int32))
  1259. {
  1260. fieldInfo.SetValue(settingsClass, config.GetInt(fieldInfo.Name, (int) fieldInfo.GetValue(settingsClass)));
  1261. }
  1262. else if (fieldInfo.FieldType == typeof (Single))
  1263. {
  1264. fieldInfo.SetValue(settingsClass, config.GetFloat(fieldInfo.Name, (float) fieldInfo.GetValue(settingsClass)));
  1265. }
  1266. else if (fieldInfo.FieldType == typeof (UInt32))
  1267. {
  1268. fieldInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(fieldInfo.Name, ((uint) fieldInfo.GetValue(settingsClass)).ToString())));
  1269. }
  1270. }
  1271. }
  1272. PropertyInfo[] propertyInfos = settingsType.GetProperties();
  1273. foreach (PropertyInfo propInfo in propertyInfos)
  1274. {
  1275. if ((propInfo.CanRead) && (propInfo.CanWrite))
  1276. {
  1277. if (propInfo.PropertyType == typeof (String))
  1278. {
  1279. propInfo.SetValue(settingsClass, config.Get(propInfo.Name, (string) propInfo.GetValue(settingsClass, null)), null);
  1280. }
  1281. else if (propInfo.PropertyType == typeof (Boolean))
  1282. {
  1283. propInfo.SetValue(settingsClass, config.GetBoolean(propInfo.Name, (bool) propInfo.GetValue(settingsClass, null)), null);
  1284. }
  1285. else if (propInfo.PropertyType == typeof (Int32))
  1286. {
  1287. propInfo.SetValue(settingsClass, config.GetInt(propInfo.Name, (int) propInfo.GetValue(settingsClass, null)), null);
  1288. }
  1289. else if (propInfo.PropertyType == typeof (Single))
  1290. {
  1291. propInfo.SetValue(settingsClass, config.GetFloat(propInfo.Name, (float) propInfo.GetValue(settingsClass, null)), null);
  1292. }
  1293. if (propInfo.PropertyType == typeof (UInt32))
  1294. {
  1295. propInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(propInfo.Name, ((uint) propInfo.GetValue(settingsClass, null)).ToString())), null);
  1296. }
  1297. }
  1298. }
  1299. return settingsClass;
  1300. }
  1301. public static string Base64ToString(string str)
  1302. {
  1303. UTF8Encoding encoder = new UTF8Encoding();
  1304. Decoder utf8Decode = encoder.GetDecoder();
  1305. byte[] todecode_byte = Convert.FromBase64String(str);
  1306. int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
  1307. char[] decoded_char = new char[charCount];
  1308. utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
  1309. string result = new String(decoded_char);
  1310. return result;
  1311. }
  1312. public static Guid GetHashGuid(string data, string salt)
  1313. {
  1314. byte[] hash = ComputeMD5Hash(data + salt);
  1315. //string s = BitConverter.ToString(hash);
  1316. Guid guid = new Guid(hash);
  1317. return guid;
  1318. }
  1319. public static byte ConvertMaturityToAccessLevel(uint maturity)
  1320. {
  1321. byte retVal = 0;
  1322. switch (maturity)
  1323. {
  1324. case 0: //PG
  1325. retVal = 13;
  1326. break;
  1327. case 1: //Mature
  1328. retVal = 21;
  1329. break;
  1330. case 2: // Adult
  1331. retVal = 42;
  1332. break;
  1333. }
  1334. return retVal;
  1335. }
  1336. public static uint ConvertAccessLevelToMaturity(byte maturity)
  1337. {
  1338. uint retVal = 0;
  1339. switch (maturity)
  1340. {
  1341. case 13: //PG
  1342. retVal = 0;
  1343. break;
  1344. case 21: //Mature
  1345. retVal = 1;
  1346. break;
  1347. case 42: // Adult
  1348. retVal = 2;
  1349. break;
  1350. }
  1351. return retVal;
  1352. }
  1353. /// <summary>
  1354. /// Produces an OSDMap from its string representation on a stream
  1355. /// </summary>
  1356. /// <param name = "data">The stream</param>
  1357. /// <param name = "length">The size of the data on the stream</param>
  1358. /// <returns>The OSDMap or an exception</returns>
  1359. public static OSDMap GetOSDMap(Stream stream, int length)
  1360. {
  1361. byte[] data = new byte[length];
  1362. stream.Read(data, 0, length);
  1363. string strdata = UTF8.GetString(data);
  1364. OSDMap args = null;
  1365. OSD buffer;
  1366. buffer = OSDParser.DeserializeJson(strdata);
  1367. if (buffer.Type == OSDType.Map)
  1368. {
  1369. args = (OSDMap)buffer;
  1370. return args;
  1371. }
  1372. return null;
  1373. }
  1374. public static OSDMap GetOSDMap(string data)
  1375. {
  1376. OSDMap args = null;
  1377. try
  1378. {
  1379. OSD buffer;
  1380. // We should pay attention to the content-type, but let's assume we know it's Json
  1381. buffer = OSDParser.DeserializeJson(data);
  1382. if (buffer.Type == OSDType.Map)
  1383. {
  1384. args = (OSDMap)buffer;
  1385. return args;
  1386. }
  1387. else
  1388. {
  1389. // uh?
  1390. MainConsole.Instance.Debug(("[UTILS]: Got OSD of unexpected type " + buffer.Type.ToString()));
  1391. return null;
  1392. }
  1393. }
  1394. catch (Exception ex)
  1395. {
  1396. MainConsole.Instance.Debug("[UTILS]: exception on GetOSDMap " + ex);
  1397. return null;
  1398. }
  1399. }
  1400. public static string[] Glob(string path)
  1401. {
  1402. string vol = String.Empty;
  1403. if (Path.VolumeSeparatorChar != Path.DirectorySeparatorChar)
  1404. {
  1405. string[] vcomps = path.Split(new[] { Path.VolumeSeparatorChar }, 2, StringSplitOptions.RemoveEmptyEntries);
  1406. if (vcomps.Length > 1)
  1407. {
  1408. path = vcomps[1];
  1409. vol = vcomps[0];
  1410. }
  1411. }
  1412. string[] comps = path.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  1413. StringSplitOptions.RemoveEmptyEntries);
  1414. // Glob
  1415. path = vol;
  1416. if (vol != String.Empty)
  1417. path += new String(new[] { Path.VolumeSeparatorChar, Path.DirectorySeparatorChar });
  1418. else
  1419. path = new String(new[] { Path.DirectorySeparatorChar });
  1420. List<string> paths = new List<string>();
  1421. List<string> found = new List<string>();
  1422. paths.Add(path);
  1423. int compIndex = -1;
  1424. foreach (string c in comps)
  1425. {
  1426. compIndex++;
  1427. List<string> addpaths = new List<string>();
  1428. foreach (string p in paths)
  1429. {
  1430. string[] dirs = Directory.GetDirectories(p, c);
  1431. if (dirs.Length != 0)
  1432. {
  1433. #if (!ISWIN)
  1434. foreach (string dir in dirs)
  1435. addpaths.Add(Path.Combine(path, dir));
  1436. #else
  1437. addpaths.AddRange(dirs.Select(dir => Path.Combine(path, dir)));
  1438. #endif
  1439. }
  1440. // Only add files if that is the last path component
  1441. if (compIndex == comps.Length - 1)
  1442. {
  1443. string[] files = Directory.GetFiles(p, c);
  1444. found.AddRange(files);
  1445. }
  1446. }
  1447. paths = addpaths;
  1448. }
  1449. return found.ToArray();
  1450. }
  1451. public static string[] GetSubFiles(string path)
  1452. {
  1453. string[] comps = path.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar },
  1454. StringSplitOptions.None);
  1455. List<string> paths = new List<string>();
  1456. string endFind = comps[comps.Length - 1];
  1457. string baseDir = "";
  1458. for (int i = 0; i < comps.Length - 1; i++)
  1459. {
  1460. if (i == comps.Length - 2)
  1461. baseDir += comps[i];
  1462. else
  1463. baseDir += comps[i] + Path.DirectorySeparatorChar;
  1464. }
  1465. paths.Add(baseDir);
  1466. #if (!ISWIN)
  1467. List<string> list = new List<string>();
  1468. foreach (string p in paths)
  1469. foreach (string file in Directory.GetFiles(p, endFind))
  1470. list.Add(file);
  1471. return list.ToArray();
  1472. #else
  1473. return paths.SelectMany(p => Directory.GetFiles(p, endFind)).ToArray();
  1474. #endif
  1475. }
  1476. public static byte[] StringToBytes256(string str, params object[] args)
  1477. {
  1478. return StringToBytes256(string.Format(str, args));
  1479. }
  1480. public static byte[] StringToBytes1024(string str, params object[] args)
  1481. {
  1482. return StringToBytes1024(string.Format(str, args));
  1483. }
  1484. public static byte[] StringToBytes256(string str)
  1485. {
  1486. if (String.IsNullOrEmpty(str))
  1487. {
  1488. return Utils.EmptyBytes;
  1489. }
  1490. if (str.Length > 254) str = str.Remove(254);
  1491. if (!str.EndsWith("\0"))
  1492. {
  1493. str += "\0";
  1494. }
  1495. // Because this is UTF-8 encoding and not ASCII, it's possible we
  1496. // might have gotten an oversized array even after the string trim
  1497. byte[] data = UTF8.GetBytes(str);
  1498. if (data.Length > 256)
  1499. {
  1500. Array.Resize(ref data, 256);
  1501. data[255] = 0;
  1502. }
  1503. return data;
  1504. }
  1505. public static byte[] StringToBytes1024(string str)
  1506. {
  1507. if (String.IsNullOrEmpty(str))
  1508. {
  1509. return Utils.EmptyBytes;
  1510. }
  1511. if (str.Length > 1023) str = str.Remove(1023);
  1512. if (!str.EndsWith("\0"))
  1513. {
  1514. str += "\0";
  1515. }
  1516. // Because this is UTF-8 encoding and not ASCII, it's possible we
  1517. // might have gotten an oversized array even after the string trim
  1518. byte[] data = UTF8.GetBytes(str);
  1519. if (data.Length > 1024)
  1520. {
  1521. Array.Resize(ref data, 1024);
  1522. data[1023] = 0;
  1523. }
  1524. return data;
  1525. }
  1526. #region FireAndForget Threading Pattern
  1527. /// <summary>
  1528. /// Created to work around a limitation in Mono with nested delegates
  1529. /// </summary>
  1530. private sealed class FireAndForgetWrapper
  1531. {
  1532. private static volatile FireAndForgetWrapper instance;
  1533. private static readonly object syncRoot = new Object();
  1534. public static FireAndForgetWrapper Instance
  1535. {
  1536. get
  1537. {
  1538. if (instance == null)
  1539. {
  1540. lock (syncRoot)
  1541. {
  1542. if (instance == null)
  1543. {
  1544. instance = new FireAndForgetWrapper();
  1545. }
  1546. }
  1547. }
  1548. return instance;
  1549. }
  1550. }
  1551. public void FireAndForget(WaitCallback callback)
  1552. {
  1553. callback.BeginInvoke(null, EndFireAndForget, callback);
  1554. }
  1555. public void FireAndForget(WaitCallback callback, object obj)
  1556. {
  1557. callback.BeginInvoke(obj, EndFireAndForget, callback);
  1558. }
  1559. private static void EndFireAndForget(IAsyncResult ar)
  1560. {
  1561. WaitCallback callback = (WaitCallback)ar.AsyncState;
  1562. try
  1563. {
  1564. callback.EndInvoke(ar);
  1565. }
  1566. catch (Exception ex)
  1567. {
  1568. MainConsole.Instance.Error("[UTIL]: Asynchronous method threw an exception: " + ex, ex);
  1569. }
  1570. ar.AsyncWaitHandle.Close();
  1571. }
  1572. }
  1573. public static void FireAndForget(WaitCallback callback)
  1574. {
  1575. FireAndForget(callback, null);
  1576. }
  1577. public static void InitThreadPool(int maxThreads)
  1578. {
  1579. if (maxThreads < 2)
  1580. throw new ArgumentOutOfRangeException("maxThreads", "maxThreads must be greater than 2");
  1581. if (m_ThreadPool != null)
  1582. throw new InvalidOperationException("SmartThreadPool is already initialized");
  1583. m_threadPoolRunning = true;
  1584. m_ThreadPool = new SmartThreadPool(2000, maxThreads, 2);
  1585. }
  1586. public static void CloseThreadPool()
  1587. {
  1588. if (FireAndForgetMethod == FireAndForgetMethod.SmartThreadPool &&
  1589. m_ThreadPool != null)
  1590. {
  1591. //This stops more tasks and threads from being started
  1592. m_threadPoolRunning = false;
  1593. m_ThreadPool.WaitForIdle(60 * 1000);
  1594. //Wait for the threads to be idle, but don't wait for more than a minute
  1595. //Destroy the threadpool now
  1596. m_ThreadPool.Dispose();
  1597. m_ThreadPool = null;
  1598. }
  1599. }
  1600. public static int FireAndForgetCount()
  1601. {
  1602. const int MAX_SYSTEM_THREADS = 200;
  1603. switch (FireAndForgetMethod)
  1604. {
  1605. case FireAndForgetMethod.UnsafeQueueUserWorkItem:
  1606. case FireAndForgetMethod.QueueUserWorkItem:
  1607. case FireAndForgetMethod.BeginInvoke:
  1608. int workerThreads, iocpThreads;
  1609. ThreadPool.GetAvailableThreads(out workerThreads, out iocpThreads);
  1610. return workerThreads;
  1611. case FireAndForgetMethod.SmartThreadPool:
  1612. if (m_ThreadPool == null || !m_threadPoolRunning)
  1613. return 0;
  1614. return m_ThreadPool.MaxThreads - m_ThreadPool.InUseThreads;
  1615. case FireAndForgetMethod.Thread:
  1616. return MAX_SYSTEM_THREADS - Process.GetCurrentProcess().Threads.Count;
  1617. default:
  1618. throw new NotImplementedException();
  1619. }
  1620. }
  1621. public static void FireAndForget(WaitCallback callback, object obj)
  1622. {
  1623. switch (FireAndForgetMethod)
  1624. {
  1625. case FireAndForgetMethod.UnsafeQueueUserWorkItem:
  1626. ThreadPool.UnsafeQueueUserWorkItem(callback, obj);
  1627. break;
  1628. case FireAndForgetMethod.QueueUserWorkItem:
  1629. ThreadPool.QueueUserWorkItem(callback, obj);
  1630. break;
  1631. case FireAndForgetMethod.BeginInvoke:
  1632. FireAndForgetWrapper wrapper = FireAndForgetWrapper.Instance;
  1633. wrapper.FireAndForget(callback, obj);
  1634. break;
  1635. case FireAndForgetMethod.SmartThreadPool:
  1636. if (m_ThreadPool == null)
  1637. InitThreadPool(15);
  1638. if (m_threadPoolRunning) //Check if the thread pool should be running
  1639. m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new[] { callback, obj });
  1640. break;
  1641. case FireAndForgetMethod.Thread:
  1642. Thread thread = new Thread(delegate(object o)
  1643. {
  1644. Culture.SetCurrentCulture();
  1645. callback(o);
  1646. });
  1647. thread.Start(obj);
  1648. break;
  1649. default:
  1650. throw new NotImplementedException();
  1651. }
  1652. }
  1653. private static object SmartThreadPoolCallback(object o)
  1654. {
  1655. object[] array = (object[])o;
  1656. WaitCallback callback = (WaitCallback)array[0];
  1657. object obj = array[1];
  1658. callback(obj);
  1659. return null;
  1660. }
  1661. #endregion FireAndForget Threading Pattern
  1662. /// <summary>
  1663. /// Environment.TickCount is an int but it counts all 32 bits so it goes positive
  1664. /// and negative every 24.9 days. This trims down TickCount so it doesn't wrap
  1665. /// for the callers.
  1666. /// This trims it to a 12 day interval so don't let your frame time get too long.
  1667. /// </summary>
  1668. /// <returns></returns>
  1669. public static Int32 EnvironmentTickCount()
  1670. {
  1671. return Environment.TickCount & EnvironmentTickCountMask;
  1672. }
  1673. public const Int32 EnvironmentTickCountMask = 0x3fffffff;
  1674. /// <summary>
  1675. /// Environment.TickCount is an int but it counts all 32 bits so it goes positive
  1676. /// and negative every 24.9 days. Subtracts the passed value (previously fetched by
  1677. /// 'EnvironmentTickCount()') and accounts for any wrapping.
  1678. /// </summary>
  1679. /// <returns>subtraction of passed prevValue from current Environment.TickCount</returns>
  1680. public static Int32 EnvironmentTickCountSubtract(Int32 prevValue)
  1681. {
  1682. Int32 diff = EnvironmentTickCount() - prevValue;
  1683. return (diff >= 0) ? diff : (diff + EnvironmentTickCountMask + 1);
  1684. }
  1685. /// <summary>
  1686. /// Environment.TickCount is an int but it counts all 32 bits so it goes positive
  1687. /// and negative every 24.9 days. Adds the passed value (previously fetched by
  1688. /// 'EnvironmentTickCount()') and accounts for any wrapping.
  1689. /// </summary>
  1690. /// <returns>addition of passed prevValue from current Environment.TickCount</returns>
  1691. public static Int32 EnvironmentTickCountAdd(Int32 ms)
  1692. {
  1693. Int32 add = EnvironmentTickCount() + ms;
  1694. return (add >= EnvironmentTickCountMask) ? add - EnvironmentTickCountMask : add;
  1695. }
  1696. /// <summary>
  1697. /// Prints the call stack at any given point. Useful for debugging.
  1698. /// </summary>
  1699. public static void PrintCallStack()
  1700. {
  1701. StackTrace stackTrace = new StackTrace(); // get call stack
  1702. StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
  1703. // write call stack method names
  1704. foreach (StackFrame stackFrame in stackFrames)
  1705. {
  1706. MainConsole.Instance.Debug(stackFrame.GetMethod().DeclaringType + "." + stackFrame.GetMethod().Name);
  1707. // write method name
  1708. }
  1709. }
  1710. public static OSDMap DictionaryToOSD(Dictionary<string, object> sendData)
  1711. {
  1712. OSDMap map = new OSDMap();
  1713. foreach (KeyValuePair<string, object> kvp in sendData)
  1714. {
  1715. if (kvp.Value is Dictionary<string, object>)
  1716. {
  1717. map[kvp.Key] = DictionaryToOSD(kvp.Value as Dictionary<string, object>);
  1718. }
  1719. else
  1720. {
  1721. OSD v = OSD.FromObject(kvp.Value);
  1722. OSD val = kvp.Value as OSD;
  1723. map[kvp.Key] = v.Type == OSDType.Unknown ? val : v;
  1724. }
  1725. }
  1726. return map;
  1727. }
  1728. public static Dictionary<string, object> OSDToDictionary(OSDMap map)
  1729. {
  1730. Dictionary<string, object> retVal = new Dictionary<string, object>();
  1731. foreach (string key in map.Keys)
  1732. {
  1733. retVal.Add(key, OSDToObject(map[key]));
  1734. }
  1735. return retVal;
  1736. }
  1737. public static OSD MakeOSD(object o, Type t)
  1738. {
  1739. if (o is OSD)
  1740. return (OSD)o;
  1741. if (o is System.Drawing.Image)
  1742. return OSDBinary.FromBinary(ImageToByteArray(o as System.Drawing.Image));
  1743. OSD oo;
  1744. if ((oo = OSD.FromObject(o)).Type != OSDType.Unknown)
  1745. return (OSD)oo;
  1746. if (o is IDataTransferable)
  1747. return ((IDataTransferable)o).ToOSD();
  1748. Type[] genericArgs = t.GetGenericArguments();
  1749. if (Util.IsInstanceOfGenericType(typeof(List<>), t))
  1750. {
  1751. OSDArray array = new OSDArray();
  1752. System.Collections.IList collection = (System.Collections.IList)o;
  1753. foreach (object item in collection)
  1754. {
  1755. array.Add(MakeOSD(item, genericArgs[0]));
  1756. }
  1757. return array;
  1758. }
  1759. else if (Util.IsInstanceOfGenericType(typeof(Dictionary<,>), t))
  1760. {
  1761. OSDMap array = new OSDMap();
  1762. System.Collections.IDictionary collection = (System.Collections.IDictionary)o;
  1763. foreach (System.Collections.DictionaryEntry item in collection)
  1764. {
  1765. array.Add(MakeOSD(item.Key, genericArgs[0]), MakeOSD(item.Value, genericArgs[1]));
  1766. }
  1767. return array;
  1768. }
  1769. if (t.BaseType == typeof(Enum))
  1770. return OSD.FromString(o.ToString());
  1771. return null;
  1772. }
  1773. public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
  1774. {
  1775. MemoryStream ms = new MemoryStream();
  1776. imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
  1777. return ms.ToArray();
  1778. }
  1779. public static System.Drawing.Image ByteArrayToImage(byte[] byteArrayIn)
  1780. {
  1781. MemoryStream ms = new MemoryStream(byteArrayIn);
  1782. System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
  1783. return returnImage;
  1784. }
  1785. private static object CreateInstance(Type type)
  1786. {
  1787. if (type == typeof(string))
  1788. return string.Empty;
  1789. else
  1790. return Activator.CreateInstance(type);
  1791. }
  1792. public static object OSDToObject(OSD o)
  1793. {
  1794. return OSDToObject(o, o.GetType());
  1795. }
  1796. public static object OSDToObject(OSD o, Type PossibleArrayType)
  1797. {
  1798. if (o.Type == OSDType.UUID || PossibleArrayType == typeof(UUID))
  1799. return o.AsUUID();
  1800. if (PossibleArrayType == typeof(string) || PossibleArrayType == typeof(OSDString) || PossibleArrayType.BaseType == typeof(Enum))
  1801. {
  1802. if (PossibleArrayType.BaseType == typeof(Enum))
  1803. return Enum.Parse(PossibleArrayType, o.AsString());
  1804. return o.AsString();
  1805. }
  1806. if (o.Type == OSDType.Array && PossibleArrayType == typeof(System.Drawing.Image))
  1807. return ByteArrayToImage(o.AsBinary());
  1808. if (o.Type == OSDType.Integer && PossibleArrayType == typeof(byte))
  1809. return (byte)o.AsInteger();
  1810. if (o.Type == OSDType.Integer || PossibleArrayType == typeof(int))
  1811. return o.AsInteger();
  1812. if (o.Type == OSDType.Binary || PossibleArrayType == typeof(byte[]))
  1813. return o.AsBinary();
  1814. if (o.Type == OSDType.Boolean || PossibleArrayType == typeof(bool))
  1815. return o.AsBoolean();
  1816. if (PossibleArrayType == typeof(Color4))
  1817. return o.AsColor4();
  1818. if (o.Type == OSDType.Date || PossibleArrayType == typeof(DateTime))
  1819. return o.AsDate();
  1820. if (PossibleArrayType == typeof(long))
  1821. return o.AsLong();
  1822. if (PossibleArrayType == typeof(Quaternion))
  1823. return o.AsQuaternion();
  1824. if (PossibleArrayType == typeof(float))
  1825. return (float)o.AsReal();
  1826. if (o.Type == OSDType.Real || PossibleArrayType == typeof(double))
  1827. return o.AsReal();
  1828. if (PossibleArrayType == typeof(uint))
  1829. return o.AsUInteger();
  1830. if (PossibleArrayType == typeof(ulong))
  1831. return o.AsULong();
  1832. if (o.Type == OSDType.URI || PossibleArrayType == typeof(Uri))
  1833. return o.AsUri();
  1834. if (PossibleArrayType == typeof(Vector2))
  1835. return o.AsVector2();
  1836. if (PossibleArrayType == typeof(Vector3))
  1837. return o.AsVector3();
  1838. if (PossibleArrayType == typeof(Vector3d))
  1839. return o.AsVector3d();
  1840. if (PossibleArrayType == typeof(Vector4))
  1841. return o.AsVector4();
  1842. if (PossibleArrayType == typeof(OSDMap))
  1843. return (OSDMap)o;
  1844. if (PossibleArrayType == typeof(OSDArray))
  1845. return (OSDArray)o;
  1846. if (o.Type == OSDType.Array)
  1847. {
  1848. OSDArray array = (OSDArray)o;
  1849. var possArrayType = Activator.CreateInstance(PossibleArrayType);
  1850. IList list = (IList)possArrayType;
  1851. Type t = PossibleArrayType.GetGenericArguments()[0];
  1852. if (t == typeof(UInt32))
  1853. return o.AsUInteger();
  1854. foreach (OSD oo in array)
  1855. {
  1856. list.Add(OSDToObject(oo, t));
  1857. }
  1858. return list;
  1859. }
  1860. var possType = Activator.CreateInstance(PossibleArrayType);
  1861. if (possType is IDataTransferable)
  1862. {
  1863. IDataTransferable data = (IDataTransferable)possType;
  1864. data.FromOSD((OSDMap)o);
  1865. return data;
  1866. }
  1867. else if (o.Type == OSDType.Map)
  1868. {
  1869. OSDMap array = (OSDMap)o;
  1870. var possArrayTypeB = Activator.CreateInstance(PossibleArrayType);
  1871. var list = (IDictionary)possArrayTypeB;
  1872. Type t = PossibleArrayType.GetGenericArguments()[1];
  1873. Type tt = PossibleArrayType.GetGenericArguments()[0];
  1874. foreach (KeyValuePair<string, OSD> oo in array)
  1875. {
  1876. list.Add(OSDToObject(oo.Key, tt), OSDToObject(oo.Value, t));
  1877. }
  1878. return list;
  1879. }
  1880. return null;
  1881. }
  1882. public static List<T> MakeList<T>(T itemOftype)
  1883. {
  1884. List<T> newList = new List<T>();
  1885. return newList;
  1886. }
  1887. public static Dictionary<A, B> MakeDictionary<A, B>(A itemOftypeA, B itemOfTypeB)
  1888. {
  1889. Dictionary<A, B> newList = new Dictionary<A, B>();
  1890. return newList;
  1891. }
  1892. public static void UlongToInts(ulong regionHandle, out int x, out int y)
  1893. {
  1894. uint xx, yy;
  1895. Utils.LongToUInts(regionHandle, out xx, out yy);
  1896. x = (int)xx;
  1897. y = (int)yy;
  1898. }
  1899. public static ulong IntsToUlong(int x, int y)
  1900. {
  1901. return Utils.UIntsToLong((uint)x, (uint)y);
  1902. }
  1903. public static string CombineParams(string[] commandParams, int pos)
  1904. {
  1905. string result = string.Empty;
  1906. for (int i = pos; i < commandParams.Length; i++)
  1907. {
  1908. result += commandParams[i] + " ";
  1909. }
  1910. result = result.Remove(result.Length - 1, 1); //Remove the trailing space
  1911. return result;
  1912. }
  1913. public static string CombineParams(string[] commandParams, int pos, int endPos)
  1914. {
  1915. string result = string.Empty;
  1916. for (int i = pos; i < endPos; i++)
  1917. {
  1918. result += commandParams[i] + " ";
  1919. }
  1920. return result.Substring(0, result.Length - 1);
  1921. }
  1922. public static string BasePathCombine(string p)
  1923. {
  1924. if (p == "")
  1925. return Application.StartupPath;
  1926. return Path.Combine(Application.StartupPath, p);
  1927. }
  1928. public static void GetReaderLock(ReaderWriterLockSlim l)
  1929. {
  1930. int i = 0;
  1931. while (i < 10) //Only try 10 times... 10s is way too much
  1932. {
  1933. try
  1934. {
  1935. l.TryEnterReadLock(100);
  1936. //Got it
  1937. return;
  1938. }
  1939. catch (ApplicationException)
  1940. {
  1941. // The reader lock request timed out. Try again
  1942. }
  1943. }
  1944. throw new ApplicationException("Could not retrieve read lock");
  1945. }
  1946. public static void ReleaseReaderLock(ReaderWriterLockSlim l)
  1947. {
  1948. l.ExitReadLock();
  1949. }
  1950. public static void GetWriterLock(ReaderWriterLockSlim l)
  1951. {
  1952. int i = 0;
  1953. while (i < 10) //Only try 10 times... 10s is way too much
  1954. {
  1955. try
  1956. {
  1957. l.TryEnterWriteLock(100);
  1958. //Got it
  1959. return;
  1960. }
  1961. catch (ApplicationException)
  1962. {
  1963. // The reader lock request timed out. Try again
  1964. }
  1965. }
  1966. throw new ApplicationException("Could not retrieve write lock");
  1967. }
  1968. public static void ReleaseWriterLock(ReaderWriterLockSlim l)
  1969. {
  1970. l.ExitWriteLock();
  1971. }
  1972. public static sbyte CheckMeshType(sbyte p)
  1973. {
  1974. if (p == (sbyte)AssetType.Mesh)
  1975. return (sbyte)AssetType.Texture;
  1976. return p;
  1977. }
  1978. public static bool IsInstanceOfGenericType(Type genericType, object instance)
  1979. {
  1980. Type type = instance.GetType();
  1981. return IsInstanceOfGenericType(genericType, type);
  1982. }
  1983. public static bool IsInstanceOfGenericType(Type genericType, Type type)
  1984. {
  1985. while (type != null)
  1986. {
  1987. if (type.IsGenericType &&
  1988. type.GetGenericTypeDefinition() == genericType)
  1989. {
  1990. return true;
  1991. }
  1992. type = type.BaseType;
  1993. }
  1994. return false;
  1995. }
  1996. // http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/68f7ca38-5cd1-411f-b8d4-e4f7a688bc03
  1997. // By: A Million Lemmings
  1998. public static string ConvertDecString(int dvalue)
  1999. {
  2000. string CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  2001. string retVal = string.Empty;
  2002. double value = Convert.ToDouble(dvalue);
  2003. do
  2004. {
  2005. double remainder = value - (26 * Math.Truncate(value / 26));
  2006. retVal = retVal + CHARS.Substring((int)remainder, 1);
  2007. value = Math.Truncate(value / 26);
  2008. } while (value > 0);
  2009. return retVal;
  2010. }
  2011. }
  2012. public static class StringUtils
  2013. {
  2014. public static string MySqlEscape(this string usString)
  2015. {
  2016. return MySqlEscape(usString, 0);
  2017. }
  2018. /// <summary>
  2019. /// Because Escaping the sql might cause it to go over the max length
  2020. /// DO NOT USE THIS ON JSON STRINGS!!! IT WILL BREAK THE DESERIALIZATION!!!
  2021. /// </summary>
  2022. /// <param name="usString"></param>
  2023. /// <param name="maxLength"></param>
  2024. /// <returns></returns>
  2025. public static string MySqlEscape(this string usString, int maxLength)
  2026. {
  2027. if (usString == null)
  2028. {
  2029. return null;
  2030. }
  2031. // SQL Encoding for MySQL Recommended here:
  2032. // http://au.php.net/manual/en/function.mysql-real-escape-string.php
  2033. // it escapes \r, \n, \x00, \x1a, baskslash, single quotes, and double quotes
  2034. string returnvalue = Regex.Replace(usString, @"[\r\n\x00\x1a\\'""]", @"\$0");
  2035. if ((maxLength != 0) && (returnvalue.Length > maxLength))
  2036. returnvalue = returnvalue.Substring(0, maxLength);
  2037. return returnvalue;
  2038. }
  2039. /// From http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx
  2040. /// <summary>
  2041. /// Generates a random string with the given length
  2042. /// </summary>
  2043. /// <param name = "size">Size of the string</param>
  2044. /// <param name = "lowerCase">If true, generate lowercase string</param>
  2045. /// <returns>Random string</returns>
  2046. public static string RandomString(int size, bool lowerCase)
  2047. {
  2048. string builder = "t";
  2049. int off = lowerCase ? 'a' : 'A';
  2050. int j;
  2051. for (int i = 0; i < size; i++)
  2052. {
  2053. j = Util.RandomClass.Next(25);
  2054. builder += (char)(j + off);
  2055. }
  2056. return builder;
  2057. }
  2058. public static string[] AlphanumericSort(List<string> list)
  2059. {
  2060. string[] nList = list.ToArray();
  2061. Array.Sort(nList, new AlphanumComparatorFast());
  2062. return nList;
  2063. }
  2064. public class AlphanumComparatorFast : IComparer
  2065. {
  2066. public int Compare(object x, object y)
  2067. {
  2068. string s1 = x as string;
  2069. if (s1 == null)
  2070. {
  2071. return 0;
  2072. }
  2073. string s2 = y as string;
  2074. if (s2 == null)
  2075. {
  2076. return 0;
  2077. }
  2078. int len1 = s1.Length;
  2079. int len2 = s2.Length;
  2080. int marker1 = 0;
  2081. int marker2 = 0;
  2082. // Walk through two the strings with two markers.
  2083. while (marker1 < len1 && marker2 < len2)
  2084. {
  2085. char ch1 = s1[marker1];
  2086. char ch2 = s2[marker2];
  2087. // Some buffers we can build up characters in for each chunk.
  2088. char[] space1 = new char[len1];
  2089. int loc1 = 0;
  2090. char[] space2 = new char[len2];
  2091. int loc2 = 0;
  2092. // Walk through all following characters that are digits or
  2093. // characters in BOTH strings starting at the appropriate marker.
  2094. // Collect char arrays.
  2095. do
  2096. {
  2097. space1[loc1++] = ch1;
  2098. marker1++;
  2099. if (marker1 < len1)
  2100. {
  2101. ch1 = s1[marker1];
  2102. }
  2103. else
  2104. {
  2105. break;
  2106. }
  2107. } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
  2108. do
  2109. {
  2110. space2[loc2++] = ch2;
  2111. marker2++;
  2112. if (marker2 < len2)
  2113. {
  2114. ch2 = s2[marker2];
  2115. }
  2116. else
  2117. {
  2118. break;
  2119. }
  2120. } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
  2121. // If we have collected numbers, compare them numerically.
  2122. // Otherwise, if we have strings, compare them alphabetically.
  2123. string str1 = new string(space1);
  2124. string str2 = new string(space2);
  2125. int result;
  2126. if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
  2127. {
  2128. int thisNumericChunk = int.Parse(str1);
  2129. int thatNumericChunk = int.Parse(str2);
  2130. result = thisNumericChunk.CompareTo(thatNumericChunk);
  2131. }
  2132. else
  2133. {
  2134. result = str1.CompareTo(str2);
  2135. }
  2136. if (result != 0)
  2137. {
  2138. return result;
  2139. }
  2140. }
  2141. return len1 - len2;
  2142. }
  2143. }
  2144. public static List<string> SizeSort(List<string> functionKeys, bool smallestToLargest)
  2145. {
  2146. functionKeys.Sort((a, b) =>
  2147. {
  2148. return a.Length.CompareTo(b.Length);
  2149. });
  2150. if (!smallestToLargest)
  2151. functionKeys.Reverse();//Flip the order then
  2152. return functionKeys;
  2153. }
  2154. }
  2155. public class NetworkUtils
  2156. {
  2157. private static bool m_noInternetConnection;
  2158. private static int m_nextInternetConnectionCheck;
  2159. //private static bool useLocalhostLoopback=false;
  2160. private static readonly ExpiringCache<string, IPAddress> m_dnsCache = new ExpiringCache<string, IPAddress>();
  2161. public static IPEndPoint ResolveEndPoint(string hostName, int port)
  2162. {
  2163. IPEndPoint endpoint = null;
  2164. // Old one defaults to IPv6
  2165. //return new IPEndPoint(Dns.GetHostAddresses(m_externalHostName)[0], m_internalEndPoint.Port);
  2166. IPAddress ia = null;
  2167. // If it is already an IP, don't resolve it - just return directly
  2168. if (IPAddress.TryParse(hostName, out ia))
  2169. {
  2170. endpoint = new IPEndPoint(ia, port);
  2171. return endpoint;
  2172. }
  2173. try
  2174. {
  2175. if (IPAddress.TryParse(hostName.Split(':')[0], out ia))
  2176. {
  2177. endpoint = new IPEndPoint(ia, port);
  2178. return endpoint;
  2179. }
  2180. }
  2181. catch
  2182. {
  2183. }
  2184. // Reset for next check
  2185. ia = null;
  2186. try
  2187. {
  2188. if (CheckInternetConnection())
  2189. {
  2190. foreach (IPAddress Adr in Dns.GetHostAddresses(hostName))
  2191. {
  2192. if (ia == null)
  2193. ia = Adr;
  2194. if (Adr.AddressFamily == AddressFamily.InterNetwork)
  2195. {
  2196. ia = Adr;
  2197. InternetSuccess();
  2198. break;
  2199. }
  2200. }
  2201. }
  2202. }
  2203. catch (SocketException e)
  2204. {
  2205. InternetFailure();
  2206. throw new Exception(
  2207. "Unable to resolve local hostname " + hostName + " innerException of type '" +
  2208. e + "' attached to this exception", e);
  2209. }
  2210. if (ia != null)
  2211. endpoint = new IPEndPoint(ia, port);
  2212. return endpoint;
  2213. }
  2214. public static bool CheckInternetConnection()
  2215. {
  2216. if (m_noInternetConnection)
  2217. {
  2218. if (Util.EnvironmentTickCount() > m_nextInternetConnectionCheck)
  2219. return true; //Try again
  2220. return false;
  2221. }
  2222. return true; //No issues
  2223. }
  2224. public static void InternetSuccess()
  2225. {
  2226. m_noInternetConnection = false;
  2227. }
  2228. public static void InternetFailure()
  2229. {
  2230. m_nextInternetConnectionCheck = Util.EnvironmentTickCountAdd(5 * 60 * 1000); /*5 mins*/
  2231. m_noInternetConnection = true;
  2232. }
  2233. /// <summary>
  2234. /// Gets the client IP address
  2235. /// </summary>
  2236. /// <param name = "xff"></param>
  2237. /// <returns></returns>
  2238. public static IPEndPoint GetClientIPFromXFF(string xff)
  2239. {
  2240. if (xff == string.Empty)
  2241. return null;
  2242. string[] parts = xff.Split(new[] { ',' });
  2243. if (parts.Length > 0)
  2244. {
  2245. try
  2246. {
  2247. return new IPEndPoint(IPAddress.Parse(parts[0]), 0);
  2248. }
  2249. catch (Exception e)
  2250. {
  2251. MainConsole.Instance.WarnFormat("[UTIL]: Exception parsing XFF header {0}: {1}", xff, e.Message);
  2252. }
  2253. }
  2254. return null;
  2255. }
  2256. public static string GetCallerIP(Hashtable req)
  2257. {
  2258. if (req.ContainsKey("headers"))
  2259. {
  2260. try
  2261. {
  2262. Hashtable headers = (Hashtable)req["headers"];
  2263. if (headers.ContainsKey("remote_addr") && headers["remote_addr"] != null)
  2264. return headers["remote_addr"].ToString();
  2265. if (headers.ContainsKey("Host") && headers["Host"] != null)
  2266. return headers["Host"].ToString().Split(':')[0];
  2267. }
  2268. catch (Exception e)
  2269. {
  2270. MainConsole.Instance.WarnFormat("[UTIL]: exception in GetCallerIP: {0}", e.Message);
  2271. }
  2272. }
  2273. return string.Empty;
  2274. }
  2275. /// <summary>
  2276. /// Attempts to resolve the loopback issue, but only works if this is run on the same network as the iPAddress
  2277. /// </summary>
  2278. /// <param name = "iPAddress"></param>
  2279. /// <param name = "clientIP"></param>
  2280. /// <returns></returns>
  2281. public static IPAddress ResolveAddressForClient(IPAddress iPAddress, IPEndPoint clientIP)
  2282. {
  2283. /*if (iPAddress == null)
  2284. return clientIP.Address;
  2285. if (iPAddress.Equals(clientIP.Address))
  2286. {
  2287. if (useLocalhostLoopback)
  2288. return IPAddress.Loopback;
  2289. if (iPAddress == IPAddress.Loopback)
  2290. return iPAddress; //Don't send something else if it is already on loopback
  2291. if (CheckInternetConnection())
  2292. {
  2293. #pragma warning disable 618
  2294. //The 'bad' way, only works for things on the same machine...
  2295. try
  2296. {
  2297. string hostName = Dns.GetHostName();
  2298. IPHostEntry ipEntry = Dns.GetHostByName(hostName);
  2299. #pragma warning restore 618
  2300. IPAddress[] addr = ipEntry.AddressList;
  2301. return addr[0]; //Loopback around! They are on the same connection
  2302. }
  2303. catch
  2304. {
  2305. InternetFailure(); //Something went wrong
  2306. }
  2307. }
  2308. }
  2309. return iPAddress;*/
  2310. return iPAddress;
  2311. }
  2312. public static bool IsLanIP(IPAddress address)
  2313. {
  2314. var interfaces = NetworkInterface.GetAllNetworkInterfaces();
  2315. foreach (var iface in interfaces)
  2316. {
  2317. var properties = iface.GetIPProperties();
  2318. foreach (var ifAddr in properties.UnicastAddresses)
  2319. {
  2320. if (ifAddr.IPv4Mask != null &&
  2321. ifAddr.Address.AddressFamily == AddressFamily.InterNetwork &&
  2322. CheckMask(ifAddr.Address, ifAddr.IPv4Mask, address))
  2323. return true;
  2324. }
  2325. }
  2326. return false;
  2327. }
  2328. private static bool CheckMask(IPAddress address, IPAddress mask, IPAddress target)
  2329. {
  2330. if (mask == null)
  2331. return false;
  2332. var ba = address.GetAddressBytes();
  2333. var bm = mask.GetAddressBytes();
  2334. var bb = target.GetAddressBytes();
  2335. if (ba.Length != bm.Length || bm.Length != bb.Length)
  2336. return false;
  2337. for (var i = 0; i < ba.Length; i++)
  2338. {
  2339. int m = bm[i];
  2340. int a = ba[i] & m;
  2341. int b = bb[i] & m;
  2342. if (a != b)
  2343. return false;
  2344. }
  2345. return true;
  2346. }
  2347. public static IPEndPoint ResolveAddressForClient(IPEndPoint iPAddress, IPEndPoint clientIP)
  2348. {
  2349. iPAddress.Address = ResolveAddressForClient(iPAddress.Address, clientIP);
  2350. return iPAddress;
  2351. }
  2352. /// <summary>
  2353. /// Returns a IP address from a specified DNS, favouring IPv4 addresses.
  2354. /// </summary>
  2355. /// <param name = "dnsAddress">DNS Hostname</param>
  2356. /// <returns>An IP address, or null</returns>
  2357. public static IPAddress GetHostFromDNS(string dnsAddress)
  2358. {
  2359. dnsAddress = dnsAddress.Replace("http://", "").Replace("https://", "");
  2360. if (dnsAddress.EndsWith("/"))
  2361. dnsAddress = dnsAddress.Remove(dnsAddress.Length - 1);
  2362. if (dnsAddress.Contains(":"))
  2363. dnsAddress = dnsAddress.Split(':')[0];
  2364. IPAddress ipa;
  2365. if (m_dnsCache.TryGetValue(dnsAddress, out ipa))
  2366. return ipa;
  2367. // Is it already a valid IP? No need to look it up.
  2368. if (IPAddress.TryParse(dnsAddress, out ipa))
  2369. {
  2370. m_dnsCache.Add(dnsAddress, ipa, 30 * 60 /*30mins*/);
  2371. return ipa;
  2372. }
  2373. try
  2374. {
  2375. if (IPAddress.TryParse(dnsAddress.Split(':')[0], out ipa))
  2376. {
  2377. m_dnsCache.Add(dnsAddress, ipa, 30 * 60 /*30mins*/);
  2378. return ipa;
  2379. }
  2380. }
  2381. catch
  2382. {
  2383. }
  2384. IPAddress[] hosts = null;
  2385. // Not an IP, lookup required
  2386. try
  2387. {
  2388. if (CheckInternetConnection())
  2389. {
  2390. hosts = Dns.GetHostEntry(dnsAddress).AddressList;
  2391. if (hosts != null)
  2392. InternetSuccess();
  2393. else
  2394. InternetFailure();
  2395. }
  2396. }
  2397. catch (Exception e)
  2398. {
  2399. MainConsole.Instance.WarnFormat("[UTIL]: An error occurred while resolving host name {0}, {1}", dnsAddress, e);
  2400. InternetFailure();
  2401. // Still going to throw the exception on for now, since this was what was happening in the first place
  2402. throw e;
  2403. }
  2404. if (hosts != null)
  2405. {
  2406. #if (!ISWIN)
  2407. foreach (IPAddress host in hosts)
  2408. {
  2409. if (host.AddressFamily == AddressFamily.InterNetwork)
  2410. {
  2411. m_dnsCache.Add(dnsAddress, host, 30*60 /*30mins*/);
  2412. return host;
  2413. }
  2414. }
  2415. #else
  2416. foreach (IPAddress host in hosts.Where(host => host.AddressFamily == AddressFamily.InterNetwork))
  2417. {
  2418. m_dnsCache.Add(dnsAddress, host, 30 * 60 /*30mins*/);
  2419. return host;
  2420. }
  2421. #endif
  2422. if (hosts.Length > 0)
  2423. {
  2424. m_dnsCache.Add(dnsAddress, hosts[0], 30 * 60 /*30mins*/);
  2425. return hosts[0];
  2426. }
  2427. }
  2428. return null;
  2429. }
  2430. public static Uri GetURI(string protocol, string hostname, int port, string path)
  2431. {
  2432. return new UriBuilder(protocol, hostname, port, path).Uri;
  2433. }
  2434. /// <summary>
  2435. /// Gets a list of all local system IP addresses
  2436. /// </summary>
  2437. /// <returns></returns>
  2438. public static IPAddress[] GetLocalHosts()
  2439. {
  2440. return Dns.GetHostAddresses(Dns.GetHostName());
  2441. }
  2442. public static IPAddress GetLocalHost()
  2443. {
  2444. IPAddress[] iplist = GetLocalHosts();
  2445. if (iplist.Length == 0) // No accessible external interfaces
  2446. {
  2447. if (CheckInternetConnection())
  2448. {
  2449. try
  2450. {
  2451. IPAddress[] loopback = Dns.GetHostAddresses("localhost");
  2452. IPAddress localhost = loopback[0];
  2453. InternetSuccess();
  2454. return localhost;
  2455. }
  2456. catch
  2457. {
  2458. InternetFailure();
  2459. }
  2460. }
  2461. }
  2462. #if (!ISWIN)
  2463. foreach (IPAddress host in iplist)
  2464. {
  2465. if (!IPAddress.IsLoopback(host) && host.AddressFamily == AddressFamily.InterNetwork)
  2466. {
  2467. return host;
  2468. }
  2469. }
  2470. #else
  2471. foreach (IPAddress host in iplist.Where(host => !IPAddress.IsLoopback(host) && host.AddressFamily == AddressFamily.InterNetwork))
  2472. {
  2473. return host;
  2474. }
  2475. #endif
  2476. if (iplist.Length > 0)
  2477. {
  2478. #if (!ISWIN)
  2479. foreach (IPAddress host in iplist)
  2480. {
  2481. if (host.AddressFamily == AddressFamily.InterNetwork)
  2482. {
  2483. return host;
  2484. }
  2485. }
  2486. #else
  2487. foreach (IPAddress host in iplist.Where(host => host.AddressFamily == AddressFamily.InterNetwork))
  2488. {
  2489. return host;
  2490. }
  2491. #endif
  2492. // Well all else failed...
  2493. return iplist[0];
  2494. }
  2495. return null;
  2496. }
  2497. #region Nested type: IPAddressRange
  2498. public class IPAddressRange
  2499. {
  2500. private readonly AddressFamily addressFamily;
  2501. private readonly byte[] lowerBytes;
  2502. private readonly byte[] upperBytes;
  2503. public IPAddressRange(IPAddress lower, IPAddress upper)
  2504. {
  2505. // Assert that lower.AddressFamily == upper.AddressFamily
  2506. this.addressFamily = lower.AddressFamily;
  2507. this.lowerBytes = lower.GetAddressBytes();
  2508. this.upperBytes = upper.GetAddressBytes();
  2509. }
  2510. public bool IsInRange(IPAddress address)
  2511. {
  2512. if (address.AddressFamily != addressFamily)
  2513. {
  2514. return false;
  2515. }
  2516. byte[] addressBytes = address.GetAddressBytes();
  2517. bool lowerBoundary = true, upperBoundary = true;
  2518. for (int i = 0;
  2519. i < this.lowerBytes.Length &&
  2520. (lowerBoundary || upperBoundary);
  2521. i++)
  2522. {
  2523. if ((lowerBoundary && addressBytes[i] < lowerBytes[i]) ||
  2524. (upperBoundary && addressBytes[i] > upperBytes[i]))
  2525. {
  2526. return false;
  2527. }
  2528. lowerBoundary &= (addressBytes[i] == lowerBytes[i]);
  2529. upperBoundary &= (addressBytes[i] == upperBytes[i]);
  2530. }
  2531. return true;
  2532. }
  2533. }
  2534. #endregion
  2535. }
  2536. public static class Extensions
  2537. {
  2538. public static List<T> ConvertAll<T>(this OSDArray array, Converter<OSD, T> converter)
  2539. {
  2540. List<OSD> list = new List<OSD>();
  2541. foreach (OSD o in array)
  2542. {
  2543. list.Add(o);
  2544. }
  2545. return list.ConvertAll<T>(converter);
  2546. }
  2547. public static Dictionary<string, T> ConvertMap<T>(this OSDMap array, Converter<OSD, T> converter)
  2548. {
  2549. Dictionary<string, T> map = new Dictionary<string, T>();
  2550. foreach (KeyValuePair<string, OSD> o in array)
  2551. {
  2552. map.Add(o.Key, converter(o.Value));
  2553. }
  2554. return map;
  2555. }
  2556. public static OSDArray ToOSDArray<T>(this List<T> array)
  2557. {
  2558. OSDArray list = new OSDArray();
  2559. foreach (object o in array)
  2560. {
  2561. OSD osd = Util.MakeOSD(o, o.GetType());
  2562. if (osd != null)
  2563. list.Add(osd);
  2564. }
  2565. return list;
  2566. }
  2567. public static OSDMap ToOSDMap<A,B>(this Dictionary<A, B> array)
  2568. {
  2569. OSDMap list = new OSDMap();
  2570. foreach (KeyValuePair<A, B> o in array)
  2571. {
  2572. OSD osd = Util.MakeOSD(o.Value, o.Value.GetType());
  2573. if (osd != null)
  2574. list.Add(o.Key.ToString(), osd);
  2575. }
  2576. return list;
  2577. }
  2578. /// <summary>
  2579. /// Comes from http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=14593
  2580. /// </summary>
  2581. /// <param name="method"></param>
  2582. /// <param name="parameters"></param>
  2583. /// <param name="invokeClass"></param>
  2584. /// <param name="invokeParameters"></param>
  2585. /// <returns></returns>
  2586. public static object FastInvoke(this MethodInfo method, ParameterInfo[] parameters, object invokeClass, object[] invokeParameters)
  2587. {
  2588. DynamicMethod dynamicMethod = new DynamicMethod(string.Empty,
  2589. typeof(object), new Type[] { typeof(object),
  2590. typeof(object[]) },
  2591. method.DeclaringType.Module);
  2592. ILGenerator il = dynamicMethod.GetILGenerator();
  2593. Type[] paramTypes = new Type[parameters.Length];
  2594. for (int i = 0; i < paramTypes.Length; i++)
  2595. {
  2596. paramTypes[i] = parameters[i].ParameterType;
  2597. }
  2598. LocalBuilder[] locals = new LocalBuilder[paramTypes.Length];
  2599. for (int i = 0; i < paramTypes.Length; i++)
  2600. {
  2601. locals[i] = il.DeclareLocal(paramTypes[i]);
  2602. }
  2603. for (int i = 0; i < paramTypes.Length; i++)
  2604. {
  2605. il.Emit(OpCodes.Ldarg_1);
  2606. EmitFastInt(il, i);
  2607. il.Emit(OpCodes.Ldelem_Ref);
  2608. EmitCastToReference(il, paramTypes[i]);
  2609. il.Emit(OpCodes.Stloc, locals[i]);
  2610. }
  2611. il.Emit(OpCodes.Ldarg_0);
  2612. for (int i = 0; i < paramTypes.Length; i++)
  2613. {
  2614. il.Emit(OpCodes.Ldloc, locals[i]);
  2615. }
  2616. il.EmitCall(OpCodes.Call, method, null);
  2617. if (method.ReturnType == typeof(void))
  2618. il.Emit(OpCodes.Ldnull);
  2619. else
  2620. EmitBoxIfNeeded(il, method.ReturnType);
  2621. il.Emit(OpCodes.Ret);
  2622. return dynamicMethod.Invoke(null, new object[2] {invokeClass, invokeParameters });
  2623. /*FastInvokeHandler invoder =
  2624. (FastInvokeHandler)dynamicMethod.CreateDelegate(
  2625. typeof(FastInvokeHandler));
  2626. return invoder;*/
  2627. }
  2628. private static void EmitCastToReference(ILGenerator il, System.Type type)
  2629. {
  2630. if (type.IsValueType)
  2631. {
  2632. il.Emit(OpCodes.Unbox_Any, type);
  2633. }
  2634. else
  2635. {
  2636. il.Emit(OpCodes.Castclass, type);
  2637. }
  2638. }
  2639. private static void EmitBoxIfNeeded(ILGenerator il, System.Type type)
  2640. {
  2641. if (type.IsValueType)
  2642. {
  2643. il.Emit(OpCodes.Box, type);
  2644. }
  2645. }
  2646. private static void EmitFastInt(ILGenerator il, int value)
  2647. {
  2648. switch (value)
  2649. {
  2650. case -1:
  2651. il.Emit(OpCodes.Ldc_I4_M1);
  2652. return;
  2653. case 0:
  2654. il.Emit(OpCodes.Ldc_I4_0);
  2655. return;
  2656. case 1:
  2657. il.Emit(OpCodes.Ldc_I4_1);
  2658. return;
  2659. case 2:
  2660. il.Emit(OpCodes.Ldc_I4_2);
  2661. return;
  2662. case 3:
  2663. il.Emit(OpCodes.Ldc_I4_3);
  2664. return;
  2665. case 4:
  2666. il.Emit(OpCodes.Ldc_I4_4);
  2667. return;
  2668. case 5:
  2669. il.Emit(OpCodes.Ldc_I4_5);
  2670. return;
  2671. case 6:
  2672. il.Emit(OpCodes.Ldc_I4_6);
  2673. return;
  2674. case 7:
  2675. il.Emit(OpCodes.Ldc_I4_7);
  2676. return;
  2677. case 8:
  2678. il.Emit(OpCodes.Ldc_I4_8);
  2679. return;
  2680. }
  2681. if (value > -129 && value < 128)
  2682. {
  2683. il.Emit(OpCodes.Ldc_I4_S, (SByte)value);
  2684. }
  2685. else
  2686. {
  2687. il.Emit(OpCodes.Ldc_I4, value);
  2688. }
  2689. }
  2690. }
  2691. public class AllScopeIDImpl : IDataTransferable
  2692. {
  2693. public UUID ScopeID = UUID.Zero;
  2694. public List<UUID> AllScopeIDs
  2695. {
  2696. get
  2697. {
  2698. List<UUID> ids = new List<UUID>();
  2699. if (!ids.Contains(ScopeID))
  2700. ids.Add(ScopeID);
  2701. return ids;
  2702. }
  2703. set
  2704. {
  2705. }
  2706. }
  2707. public static List<T> CheckScopeIDs<T>(List<UUID> scopeIDs, List<T> list) where T : AllScopeIDImpl
  2708. {
  2709. if (scopeIDs == null || scopeIDs.Count == 0 || scopeIDs.Contains(UUID.Zero))
  2710. return list;
  2711. return new List<T>(list.Where(r => scopeIDs.Any(s => r.AllScopeIDs.Contains(s)) || r.AllScopeIDs.Contains(UUID.Zero)));
  2712. }
  2713. public static T CheckScopeIDs<T>(List<UUID> scopeIDs, T l) where T : AllScopeIDImpl
  2714. {
  2715. if (l == null || scopeIDs == null || scopeIDs.Count == 0 || scopeIDs.Contains(UUID.Zero))
  2716. return l;
  2717. return (scopeIDs.Any(s => l.AllScopeIDs.Contains(s)) || l.AllScopeIDs.Contains(UUID.Zero)) ? l : null;
  2718. }
  2719. }
  2720. }