/Aurora/Framework/Utilities/Util.cs
C# | 3042 lines | 2328 code | 354 blank | 360 comment | 395 complexity | d4de62d4428e5e099577b3a9324ffbd4 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- /*
- * Copyright (c) Contributors, http://aurora-sim.org/, http://opensimulator.org/
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the Aurora-Sim Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
- using Amib.Threading;
- using Aurora.Framework.ConsoleFramework;
- using Aurora.Framework.Modules;
- using Nini.Config;
- using OpenMetaverse;
- using OpenMetaverse.StructuredData;
- using ProtoBuf;
- using ProtoBuf.Meta;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Net;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Windows.Forms;
- using System.Xml;
- using ReaderWriterLockSlim = System.Threading.ReaderWriterLockSlim;
-
- namespace Aurora.Framework.Utilities
- {
- /// <summary>
- /// The method used by Util.FireAndForget for asynchronously firing events
- /// </summary>
- public enum FireAndForgetMethod
- {
- UnsafeQueueUserWorkItem,
- QueueUserWorkItem,
- BeginInvoke,
- SmartThreadPool,
- Thread
- }
-
- public enum RuntimeEnvironment
- {
- NET,
- WinMono,
- Mono
- }
-
- /// <summary>
- /// Miscellaneous utility functions
- /// </summary>
- public class Util
- {
- private static uint nextXferID = 5000;
- private static readonly Random randomClass = new Random();
- // Get a list of invalid file characters (OS dependent)
- private static readonly string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]";
- private static readonly string regexInvalidPathChars = "[" + new String(Path.GetInvalidPathChars()) + "]";
- private static readonly object XferLock = new object();
-
- /// <summary>
- /// Thread pool used for Util.FireAndForget if
- /// FireAndForgetMethod.SmartThreadPool is used
- /// </summary>
- private static SmartThreadPool m_ThreadPool;
-
- private static volatile bool m_threadPoolRunning;
-
- // 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.
- public static readonly DateTime UnixEpoch =
- DateTime.ParseExact("1970-01-01 00:00:00 +0", "yyyy-MM-dd hh:mm:ss z", DateTimeFormatInfo.InvariantInfo).
- ToUniversalTime();
-
- public static readonly Regex UUIDPattern
- = 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}$");
-
- public static FireAndForgetMethod FireAndForgetMethod = FireAndForgetMethod.SmartThreadPool;
-
- static Util()
- {
- RuntimeTypeModel.Default.Add(typeof (UUID), false)
- .SetSurrogate(typeof (UUIDSurrogate));
- RuntimeTypeModel.Default.Add(typeof (IPEndPoint), false)
- .SetSurrogate(typeof (IPEndPointSurrogate));
- RuntimeTypeModel.Default.Add(typeof (OSD), false)
- .SetSurrogate(typeof (OSDSurrogate));
- RuntimeTypeModel.Default.Add(typeof (OSDMap), false)
- .SetSurrogate(typeof (OSDMapSurrogate));
- RuntimeTypeModel.Default.Add(typeof (Vector3), false)
- .SetSurrogate(typeof (Vector3Surrogate));
- RuntimeTypeModel.Default.Add(typeof (Quaternion), false)
- .SetSurrogate(typeof (QuaternionSurrogate));
- RuntimeTypeModel.Default.Add(typeof (ParcelManager.ParcelAccessEntry), false)
- .SetSurrogate(typeof (ParcelAccessEntrySurrogate));
- RuntimeTypeModel.Default.Add(typeof (MediaEntry), false)
- .SetSurrogate(typeof (MediaEntrySurrogate));
- RuntimeTypeModel.Default.Add(typeof (System.Drawing.Color), false)
- .SetSurrogate(typeof (ColorSurrogate));
- }
-
- #region Protobuf helpers
-
- [ProtoContract]
- private class UUIDSurrogate
- {
- [ProtoMember(1)] public string ID;
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator UUID(UUIDSurrogate value)
- {
- return UUID.Parse(value.ID);
- }
-
- public static implicit operator UUIDSurrogate(UUID value)
- {
- return new UUIDSurrogate
- {
- ID = value.ToString()
- };
- }
- }
-
- [ProtoContract]
- private class Vector3Surrogate
- {
- [ProtoMember(1)] public float X;
- [ProtoMember(2)] public float Y;
- [ProtoMember(3)] public float Z;
-
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator Vector3(Vector3Surrogate value)
- {
- return new Vector3(value.X, value.Y, value.Z);
- }
-
- public static implicit operator Vector3Surrogate(Vector3 value)
- {
- return new Vector3Surrogate()
- {
- X = value.X,
- Y = value.Y,
- Z = value.Z
- };
- }
- }
-
- [ProtoContract]
- private class QuaternionSurrogate
- {
- [ProtoMember(1)] public float X;
- [ProtoMember(2)] public float Y;
- [ProtoMember(3)] public float Z;
- [ProtoMember(4)] public float W;
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator Quaternion(QuaternionSurrogate value)
- {
- return new Quaternion(value.X, value.Y, value.Z, value.W);
- }
-
- public static implicit operator QuaternionSurrogate(Quaternion value)
- {
- return new QuaternionSurrogate()
- {
- X = value.X,
- Y = value.Y,
- Z = value.Z,
- W = value.W
- };
- }
- }
-
- [ProtoContract]
- private class IPEndPointSurrogate
- {
- [ProtoMember(1)] public string IPAddr;
- [ProtoMember(2)] public int Port;
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator IPEndPoint(IPEndPointSurrogate value)
- {
- return value == null ? null : new IPEndPoint(IPAddress.Parse(value.IPAddr), value.Port);
- }
-
- public static implicit operator IPEndPointSurrogate(IPEndPoint value)
- {
- return value == null
- ? null
- : new IPEndPointSurrogate
- {
- IPAddr = value.Address.ToString(),
- Port = value.Port
- };
- }
- }
-
- [ProtoContract]
- private class OSDSurrogate
- {
- [ProtoMember(1)] public string str;
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator OSD(OSDSurrogate value)
- {
- return value.str == "" ? null : OSDParser.DeserializeJson(value.str);
- }
-
- public static implicit operator OSDSurrogate(OSD value)
- {
- return new OSDSurrogate
- {
- str = value == null ? "" : OSDParser.SerializeJsonString(value)
- };
- }
- }
-
- [ProtoContract]
- private class OSDMapSurrogate
- {
- [ProtoMember(1)] public string str;
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator OSDMap(OSDMapSurrogate value)
- {
- return value.str == "" ? null : (OSDMap) OSDParser.DeserializeJson(value.str);
- }
-
- public static implicit operator OSDMapSurrogate(OSDMap value)
- {
- return new OSDMapSurrogate
- {
- str = value == null ? "" : OSDParser.SerializeJsonString(value)
- };
- }
- }
-
- [ProtoContract]
- private class ParcelAccessEntrySurrogate
- {
- [ProtoMember(1)] public UUID AgentID;
- [ProtoMember(2)] public AccessList Flags;
- [ProtoMember(3)] public DateTime Time;
-
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator ParcelManager.ParcelAccessEntry(ParcelAccessEntrySurrogate value)
- {
- return new ParcelManager.ParcelAccessEntry()
- {
- AgentID = value.AgentID,
- Flags = value.Flags,
- Time = value.Time
- };
- }
-
- public static implicit operator ParcelAccessEntrySurrogate(ParcelManager.ParcelAccessEntry value)
- {
- return new ParcelAccessEntrySurrogate
- {
- AgentID = value.AgentID,
- Flags = value.Flags,
- Time = value.Time
- };
- }
- }
-
- [ProtoContract]
- private class MediaEntrySurrogate
- {
- [ProtoMember(1)] public OSD info;
-
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator MediaEntry(MediaEntrySurrogate value)
- {
- return value.info == null ? null : MediaEntry.FromOSD(value.info);
- }
-
- public static implicit operator MediaEntrySurrogate(MediaEntry value)
- {
- return new MediaEntrySurrogate
- {
- info = value == null ? null : value.GetOSD()
- };
- }
- }
-
- [ProtoContract]
- private class ColorSurrogate
- {
- [ProtoMember(1)] public int A;
- [ProtoMember(2)] public int R;
- [ProtoMember(3)] public int G;
- [ProtoMember(4)] public int B;
-
- // protobuf-net wants an implicit or explicit operator between the types
- public static implicit operator System.Drawing.Color(ColorSurrogate value)
- {
- return System.Drawing.Color.FromArgb(value.A, value.R, value.G, value.B);
- }
-
- public static implicit operator ColorSurrogate(System.Drawing.Color value)
- {
- return new ColorSurrogate
- {
- A = value.A,
- R = value.R,
- G = value.G,
- B = value.B
- };
- }
- }
-
- #endregion
-
- public static string ConvertToString(List<string> list)
- {
- StringBuilder builder = new StringBuilder();
- foreach (string val in list)
- {
- builder.Append(val + ",");
- }
- return builder.ToString();
- }
-
- public static string ConvertToString(Dictionary<string, object> list)
- {
- StringBuilder builder = new StringBuilder();
- foreach (var val in list)
- {
- builder.Append(val.Key + "=" + val.Value.ToString() + ",");
- }
- return builder.ToString();
- }
-
- //wab - Added for debugging ease
- public static string ConvertToString(OSDMap values, string lineStart)
- {
- if (string.IsNullOrEmpty(lineStart)) lineStart = "\t";
- StringBuilder builder = new StringBuilder();
- String[] keys = new String[values.Count];
- values.Keys.CopyTo(keys, 0);
- foreach (String key in keys)
- {
- Object val = values[key];
- if (val == null)
- builder.AppendFormat("{0}{1}=null\n", lineStart, key);
- else if (val is OSDMap)
- builder.AppendFormat("{0}{1}=...\n{2}", lineStart, key, ConvertToString((OSDMap) val, "\t\t"));
- else
- builder.AppendFormat("{0}{1}={2}\n", lineStart, key, val.ToString());
- }
- return builder.ToString();
- }
-
- public static List<string> ConvertToList(string listAsString)
- {
- //Do both , and " " so that it removes any annoying spaces in the string added by users
- List<string> value =
- new List<string>(listAsString.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries));
- return value;
- }
-
- public static Dictionary<string, string> ConvertToDictionary(string listAsString)
- {
- //Do both , and " " so that it removes any annoying spaces in the string added by users
- List<string> value =
- new List<string>(listAsString.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries));
- Dictionary<string, string> dict = new Dictionary<string, string>();
- foreach (var v in value)
- {
- var split = v.Split('=');
- dict.Add(split[0], split[1]);
- }
- return dict;
- }
-
- public static string BuildYMDDateString(DateTime time)
- {
- return time.Year + "-" + time.Month + "-" + time.Day;
- }
-
- /// <summary>
- /// Gets the name of the directory where the current running executable
- /// is located
- /// </summary>
- /// <returns>
- /// Filesystem path to the directory containing the current
- /// executable
- /// </returns>
- public static string ExecutingDirectory()
- {
- return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- }
-
- /// <summary>
- /// Copy data from one stream to another, leaving the read position of both streams at the beginning.
- /// </summary>
- /// <param name='inputStream'>
- /// Input stream. Must be seekable.
- /// </param>
- /// <exception cref='ArgumentException'>
- /// Thrown if the input stream is not seekable.
- /// </exception>
- public static Stream Copy(Stream inputStream)
- {
- if (!inputStream.CanSeek)
- throw new ArgumentException("Util.Copy(Stream inputStream) must receive an inputStream that can seek");
-
- const int readSize = 256;
- byte[] buffer = new byte[readSize];
- MemoryStream ms = new MemoryStream();
-
- int count = inputStream.Read(buffer, 0, readSize);
-
- while (count > 0)
- {
- ms.Write(buffer, 0, count);
- count = inputStream.Read(buffer, 0, readSize);
- }
-
- ms.Position = 0;
- inputStream.Position = 0;
-
- return ms;
- }
-
- /// <summary>
- /// Linear interpolates B<->C using percent A
- /// </summary>
- /// <param name="a"></param>
- /// <param name="b"></param>
- /// <param name="c"></param>
- /// <returns></returns>
- public static double lerp(double a, double b, double c)
- {
- return (b*a) + (c*(1 - a));
- }
-
- /// <summary>
- /// Bilinear Interpolate, see Lerp but for 2D using 'percents' X & Y.
- /// Layout:
- /// A B
- /// C D
- /// A<->C = Y
- /// C<->D = X
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="a"></param>
- /// <param name="b"></param>
- /// <param name="c"></param>
- /// <param name="d"></param>
- /// <returns></returns>
- public static double lerp2D(double x, double y, double a, double b, double c, double d)
- {
- return lerp(y, lerp(x, a, b), lerp(x, c, d));
- }
-
-
- public static Encoding UTF8 = Encoding.UTF8;
-
- /// <value>
- /// Well known UUID for the blank texture used in the Linden SL viewer version 1.20 (and hopefully onwards)
- /// </value>
- public static UUID BLANK_TEXTURE_UUID = new UUID("5748decc-f629-461c-9a36-a35a221fe21f");
-
- #region Vector Equations
-
- /// <summary>
- /// Get the distance between two 3d vectors
- /// </summary>
- /// <param name="a">A 3d vector</param>
- /// <param name="b">A 3d vector</param>
- /// <returns>The distance between the two vectors</returns>
- public static double GetDistanceTo(Vector3 a, Vector3 b)
- {
- float dx = a.X - b.X;
- float dy = a.Y - b.Y;
- float dz = a.Z - b.Z;
- return Math.Sqrt(dx*dx + dy*dy + dz*dz);
- }
-
- /// <summary>
- /// Get the distance between two 3d vectors (excluding Z)
- /// </summary>
- /// <param name="a">A 3d vector</param>
- /// <param name="b">A 3d vector</param>
- /// <returns>The distance between the two vectors</returns>
- public static double GetFlatDistanceTo(Vector3 a, Vector3 b)
- {
- float dx = a.X - b.X;
- float dy = a.Y - b.Y;
- return Math.Sqrt(dx*dx + dy*dy);
- }
-
- /// <summary>
- /// Returns true if the distance beween A and B is less than amount. Significantly faster than GetDistanceTo since it eliminates the Sqrt.
- /// </summary>
- /// <param name="a"></param>
- /// <param name="b"></param>
- /// <param name="amount"></param>
- /// <returns></returns>
- public static bool DistanceLessThan(Vector3 a, Vector3 b, double amount)
- {
- float dx = a.X - b.X;
- float dy = a.Y - b.Y;
- float dz = a.Z - b.Z;
- return (dx*dx + dy*dy + dz*dz) < (amount*amount);
- }
-
- /// <summary>
- /// Get the magnitude of a 3d vector
- /// </summary>
- /// <param name="a">A 3d vector</param>
- /// <returns>The magnitude of the vector</returns>
- public static double GetMagnitude(Vector3 a)
- {
- return Math.Sqrt((a.X*a.X) + (a.Y*a.Y) + (a.Z*a.Z));
- }
-
- /// <summary>
- /// Get a normalized form of a 3d vector
- /// The vector paramater cannot be <0,0,0>
- /// </summary>
- /// <param name="a">A 3d vector</param>
- /// <returns>A new vector which is normalized form of the vector</returns>
- public static Vector3 GetNormalizedVector(Vector3 a)
- {
- if (IsZeroVector(a))
- throw new ArgumentException("Vector paramater cannot be a zero vector.");
-
- float Mag = (float) GetMagnitude(a);
- return new Vector3(a.X/Mag, a.Y/Mag, a.Z/Mag);
- }
-
- /// <summary>
- /// Returns if a vector is a zero vector (has all zero components)
- /// </summary>
- /// <returns></returns>
- public static bool IsZeroVector(Vector3 v)
- {
- if (v.X == 0 && v.Y == 0 && v.Z == 0)
- {
- return true;
- }
-
- return false;
- }
-
- # endregion
-
- public static Quaternion Axes2Rot(Vector3 fwd, Vector3 left, Vector3 up)
- {
- float s;
- float tr = (float) (fwd.X + left.Y + up.Z + 1.0);
-
- if (tr >= 1.0)
- {
- s = (float) (0.5/Math.Sqrt(tr));
- return new Quaternion(
- (left.Z - up.Y)*s,
- (up.X - fwd.Z)*s,
- (fwd.Y - left.X)*s,
- (float) 0.25/s);
- }
- else
- {
- float max = (left.Y > up.Z) ? left.Y : up.Z;
-
- if (max < fwd.X)
- {
- s = (float) (Math.Sqrt(fwd.X - (left.Y + up.Z) + 1.0));
- float x = (float) (s*0.5);
- s = (float) (0.5/s);
- return new Quaternion(
- x,
- (fwd.Y + left.X)*s,
- (up.X + fwd.Z)*s,
- (left.Z - up.Y)*s);
- }
- else if (max == left.Y)
- {
- s = (float) (Math.Sqrt(left.Y - (up.Z + fwd.X) + 1.0));
- float y = (float) (s*0.5);
- s = (float) (0.5/s);
- return new Quaternion(
- (fwd.Y + left.X)*s,
- y,
- (left.Z + up.Y)*s,
- (up.X - fwd.Z)*s);
- }
- else
- {
- s = (float) (Math.Sqrt(up.Z - (fwd.X + left.Y) + 1.0));
- float z = (float) (s*0.5);
- s = (float) (0.5/s);
- return new Quaternion(
- (up.X + fwd.Z)*s,
- (left.Z + up.Y)*s,
- z,
- (fwd.Y - left.X)*s);
- }
- }
- }
-
- public static Random RandomClass
- {
- get { return randomClass; }
- }
-
- public static T Clamp<T>(T x, T min, T max)
- where T : IComparable<T>
- {
- return x.CompareTo(max) > 0
- ? max
- : x.CompareTo(min) < 0
- ? min
- : x;
- }
-
- public static uint GetNextXferID()
- {
- uint id = 0;
- lock (XferLock)
- {
- id = nextXferID;
- nextXferID++;
- }
- return id;
- }
-
- /// <summary>
- /// Debug utility function to convert unbroken strings of XML into something human readable for occasional debugging purposes.
- /// Please don't delete me even if I appear currently unused!
- /// </summary>
- /// <param name="rawXml"></param>
- /// <returns></returns>
- public static string GetFormattedXml(string rawXml)
- {
- XmlDocument xd = new XmlDocument();
- xd.LoadXml(rawXml);
-
- StringBuilder sb = new StringBuilder();
- StringWriter sw = new StringWriter(sb);
-
- XmlTextWriter xtw = new XmlTextWriter(sw) {Formatting = Formatting.Indented};
-
- try
- {
- xd.WriteTo(xtw);
- }
- finally
- {
- xtw.Close();
- }
-
- return sb.ToString();
- }
-
- public static bool IsEnvironmentSupported(ref string reason)
- {
- // Must have .NET 2.0 (Generics / libsl)
- if (Environment.Version.Major < 2)
- {
- reason = ".NET 1.0/1.1 lacks components that are used by Aurora";
- return false;
- }
-
- // Windows 95/98/ME are unsupported
- if (Environment.OSVersion.Platform == PlatformID.Win32Windows ||
- Environment.OSVersion.Platform == PlatformID.Win32S ||
- Environment.OSVersion.Platform == PlatformID.WinCE)
- {
- reason = "Windows 95/98/ME will not run Aurora";
- return false;
- }
-
- // Windows 2000 / Pre-SP2 XP
- if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
- Environment.OSVersion.Version.Major == 5 &&
- Environment.OSVersion.Version.Minor == 0)
- {
- reason = "Please update to Windows XP Service Pack 2 or Server 2003 with .NET 3.5 installed";
- return false;
- }
-
- if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
- Environment.Version.Major < 4 &&
- Environment.Version.Build < 50727) //.net 3.5
- {
- reason = ".NET versions before 3.5 lack components that are used by Aurora";
- return false;
- }
-
- return true;
- }
-
- public static bool IsLinux
- {
- get
- {
- int p = (int) Environment.OSVersion.Platform;
- return (p == 4) || (p == 6) || (p == 128);
- }
- }
-
- public static int UnixTimeSinceEpoch()
- {
- return ToUnixTime(DateTime.UtcNow);
- }
-
- public static int ToUnixTime(DateTime stamp)
- {
- TimeSpan t = stamp.ToUniversalTime() - UnixEpoch;
- return (int) t.TotalSeconds;
- }
-
- public static DateTime ToDateTime(ulong seconds)
- {
- DateTime epoch = UnixEpoch;
- return epoch.AddSeconds(seconds);
- }
-
- public static DateTime ToDateTime(int seconds)
- {
- DateTime epoch = UnixEpoch;
- return epoch.AddSeconds(seconds);
- }
-
- /// <summary>
- /// Return an md5 hash of the given string
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static string Md5Hash(string data)
- {
- byte[] dataMd5 = ComputeMD5Hash(data);
- StringBuilder sb = new StringBuilder();
- foreach (byte t in dataMd5)
- sb.AppendFormat("{0:x2}", t);
- return sb.ToString();
- }
-
- private static byte[] ComputeMD5Hash(string data)
- {
- MD5 md5 = MD5.Create();
- return md5.ComputeHash(Encoding.Default.GetBytes(data));
- }
-
- /// <summary>
- /// Return an SHA1 hash of the given string
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static string SHA1Hash(string data)
- {
- byte[] hash = ComputeSHA1Hash(data);
- return BitConverter.ToString(hash).Replace("-", String.Empty);
- }
-
- private static byte[] ComputeSHA1Hash(string src)
- {
- SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
- return SHA1.ComputeHash(Encoding.Default.GetBytes(src));
- }
-
- public static int fast_distance2d(int x, int y)
- {
- x = Math.Abs(x);
- y = Math.Abs(y);
-
- int min = Math.Min(x, y);
-
- return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
- }
-
- // Inclusive, within range test (true if equal to the endpoints)
- public static bool InRange<T>(T x, T min, T max)
- where T : IComparable<T>
- {
- return x.CompareTo(max) <= 0 && x.CompareTo(min) >= 0;
- }
-
- // Clamp the maximum magnitude of a vector
- public static Vector3 ClampV(Vector3 x, float max)
- {
- Vector3 ret = x;
- float lenSq = x.LengthSquared();
- if (lenSq > (max*max))
- {
- x = x/x.Length()*max;
- }
- return x;
- }
-
- public static string FieldToString(byte[] bytes)
- {
- return FieldToString(bytes, String.Empty);
- }
-
- /// <summary>
- /// Convert a variable length field (byte array) to a string, with a
- /// field name prepended to each line of the output
- /// </summary>
- /// <remarks>
- /// If the byte array has unprintable characters in it, a
- /// hex dump will be put in the string instead
- /// </remarks>
- /// <param name="bytes">The byte array to convert to a string</param>
- /// <param name="fieldName">A field name to prepend to each line of output</param>
- /// <returns>
- /// An ASCII string or a string containing a hex dump, minus
- /// the null terminator
- /// </returns>
- public static string FieldToString(byte[] bytes, string fieldName)
- {
- // Check for a common case
- if (bytes.Length == 0) return String.Empty;
-
- StringBuilder output = new StringBuilder();
-
- bool printable = bytes.All(t => (t >= 0x20 && t <= 0x7E) || t == 0x09 || t == 0x0D || t == 0x0A || t == 0x00);
-
- if (printable)
- {
- if (fieldName.Length > 0)
- {
- output.Append(fieldName);
- output.Append(": ");
- }
-
- output.Append(CleanString(UTF8.GetString(bytes, 0, bytes.Length - 1)));
- }
- else
- {
- for (int i = 0; i < bytes.Length; i += 16)
- {
- if (i != 0)
- output.Append(Environment.NewLine);
- if (fieldName.Length > 0)
- {
- output.Append(fieldName);
- output.Append(": ");
- }
-
- for (int j = 0; j < 16; j++)
- {
- output.Append((i + j) < bytes.Length ? String.Format("{0:X2} ", bytes[i + j]) : " ");
- }
-
- for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
- {
- if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
- output.Append((char) bytes[i + j]);
- else
- output.Append(".");
- }
- }
- }
-
- return output.ToString();
- }
-
- /// <summary>
- /// Removes all invalid path chars (OS dependent)
- /// </summary>
- /// <param name="path">path</param>
- /// <returns>safe path</returns>
- public static string safePath(string path)
- {
- return Regex.Replace(path, regexInvalidPathChars, String.Empty);
- }
-
- /// <summary>
- /// Removes all invalid filename chars (OS dependent)
- /// </summary>
- /// <param name="filename">filename</param>
- /// <returns>safe filename</returns>
- public static string safeFileName(string filename)
- {
- return Regex.Replace(filename, regexInvalidFileChars, String.Empty);
- }
-
- //
- // directory locations
- //
-
- public static string homeDir()
- {
- string temp;
- // string personal=(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
- // temp = Path.Combine(personal,".OpenSim");
- temp = ".";
- return temp;
- }
-
- public static string configDir()
- {
- return ".";
- }
-
- public static string dataDir()
- {
- return ".";
- }
-
- public static string logDir()
- {
- return ".";
- }
-
- // From: http://coercedcode.blogspot.com/2008/03/c-generate-unique-filenames-within.html
- public static string GetUniqueFilename(string FileName)
- {
- int count = 0;
- string Name;
-
- if (File.Exists(FileName))
- {
- FileInfo f = new FileInfo(FileName);
-
- Name = !String.IsNullOrEmpty(f.Extension)
- ? f.FullName.Substring(0, f.FullName.LastIndexOf('.'))
- : f.FullName;
-
- while (File.Exists(FileName))
- {
- count++;
- FileName = Name + count + f.Extension;
- }
- }
- return FileName;
- }
-
- // Nini (config) related Methods
- public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
- {
- if (!File.Exists(fileName))
- {
- //create new file
- }
- XmlConfigSource config = new XmlConfigSource(fileName);
- AddDataRowToConfig(config, row);
- config.Save();
-
- return config;
- }
-
- public static void AddDataRowToConfig(IConfigSource config, DataRow row)
- {
- config.Configs.Add((string) row[0]);
- for (int i = 0; i < row.Table.Columns.Count; i++)
- {
- config.Configs[(string) row[0]].Set(row.Table.Columns[i].ColumnName, row[i]);
- }
- }
-
- public static float Clip(float x, float min, float max)
- {
- return Math.Min(Math.Max(x, min), max);
- }
-
- public static int Clip(int x, int min, int max)
- {
- return Math.Min(Math.Max(x, min), max);
- }
-
- /// <summary>
- /// Convert an UUID to a raw uuid string. Right now this is a string without hyphens.
- /// </summary>
- /// <param name="UUID"></param>
- /// <returns></returns>
- public static String ToRawUuidString(UUID UUID)
- {
- return UUID.Guid.ToString("n");
- }
-
- public static string CleanString(string input)
- {
- if (input.Length == 0)
- return input;
-
- int clip = input.Length;
-
- // Test for ++ string terminator
- int pos = input.IndexOf("\0");
- if (pos != -1 && pos < clip)
- clip = pos;
-
- // Test for CR
- pos = input.IndexOf("\r");
- if (pos != -1 && pos < clip)
- clip = pos;
-
- // Test for LF
- pos = input.IndexOf("\n");
- if (pos != -1 && pos < clip)
- clip = pos;
-
- // Truncate string before first end-of-line character found
- return input.Substring(0, clip);
- }
-
- /// <summary>
- /// returns the contents of /etc/issue on Unix Systems
- /// Use this for where it's absolutely necessary to implement platform specific stuff
- /// </summary>
- /// <returns></returns>
- public static string ReadEtcIssue()
- {
- try
- {
- StreamReader sr = new StreamReader("/etc/issue.net");
- string issue = sr.ReadToEnd();
- sr.Close();
- return issue;
- }
- catch (Exception)
- {
- return "";
- }
- }
-
- public static void SerializeToFile(string filename, Object obj)
- {
- IFormatter formatter = new BinaryFormatter();
- Stream stream = null;
-
- try
- {
- stream = new FileStream(
- filename, FileMode.Create,
- FileAccess.Write, FileShare.None);
-
- formatter.Serialize(stream, obj);
- }
- catch (Exception e)
- {
- MainConsole.Instance.Error(e.ToString());
- }
- finally
- {
- if (stream != null)
- {
- stream.Close();
- }
- }
- }
-
- public static Object DeserializeFromFile(string filename)
- {
- IFormatter formatter = new BinaryFormatter();
- Stream stream = null;
- Object ret = null;
-
- try
- {
- stream = new FileStream(
- filename, FileMode.Open,
- FileAccess.Read, FileShare.None);
-
- ret = formatter.Deserialize(stream);
- }
- catch (Exception e)
- {
- MainConsole.Instance.Error(e.ToString());
- }
- finally
- {
- if (stream != null)
- {
- stream.Close();
- }
- }
-
- return ret;
- }
-
- public static void Compress7ZipFile(string path, string destination)
- {
- ProcessStartInfo p = new ProcessStartInfo();
- string pa = Path.GetDirectoryName(Assembly.GetAssembly(typeof (Util)).CodeBase);
- if (pa != null)
- {
- p.FileName = Path.Combine(pa, "7za.exe");
- p.Arguments = "a -y -tgzip \"" + destination + "\" \"" + path + "\" -mx=9";
- p.WindowStyle = ProcessWindowStyle.Hidden;
- Process x = Process.Start(p);
- x.WaitForExit();
- }
- }
-
- public static void UnCompress7ZipFile(string path, string destination)
- {
- ProcessStartInfo p = new ProcessStartInfo();
- string pa = Path.GetDirectoryName(Assembly.GetAssembly(typeof (Util)).CodeBase);
- if (pa != null)
- {
- p.FileName = Path.Combine(pa, "7za.exe");
- p.Arguments = "e -y \"" + path + "\" -o\"" + destination + "\" -mx=9";
- p.WindowStyle = ProcessWindowStyle.Hidden;
- Process x = Process.Start(p);
- x.WaitForExit();
- }
- }
-
- public static string Compress(string text)
- {
- byte[] buffer = UTF8.GetBytes(text);
- MemoryStream memory = new MemoryStream();
- using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true))
- {
- compressor.Write(buffer, 0, buffer.Length);
- }
-
- memory.Position = 0;
-
- byte[] compressed = new byte[memory.Length];
- memory.Read(compressed, 0, compressed.Length);
-
- byte[] compressedBuffer = new byte[compressed.Length + 4];
- Buffer.BlockCopy(compressed, 0, compressedBuffer, 4, compressed.Length);
- Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressedBuffer, 0, 4);
- return Convert.ToBase64String(compressedBuffer);
- }
-
- public static byte[] CompressBytes(byte[] buffer)
- {
- MemoryStream memory = new MemoryStream();
- using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true))
- {
- compressor.Write(buffer, 0, buffer.Length);
- }
-
- memory.Position = 0;
-
- byte[] compressed = new byte[memory.Length];
- memory.Read(compressed, 0, compressed.Length);
-
- byte[] compressedBuffer = new byte[compressed.Length + 4];
- Buffer.BlockCopy(compressed, 0, compressedBuffer, 4, compressed.Length);
- Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressedBuffer, 0, 4);
- return compressedBuffer;
- }
-
- public static string Decompress(string compressedText)
- {
- byte[] compressedBuffer = Convert.FromBase64String(compressedText);
- using (MemoryStream memory = new MemoryStream())
- {
- int msgLength = BitConverter.ToInt32(compressedBuffer, 0);
- memory.Write(compressedBuffer, 4, compressedBuffer.Length - 4);
-
- byte[] buffer = new byte[msgLength];
-
- memory.Position = 0;
- using (GZipStream decompressor = new GZipStream(memory, CompressionMode.Decompress))
- {
- decompressor.Read(buffer, 0, buffer.Length);
- }
-
- return UTF8.GetString(buffer);
- }
- }
-
- public static Stream DecompressStream(Stream compressedStream)
- {
- byte[] compressedBuffer = ReadToEnd(compressedStream);
- using (MemoryStream memory = new MemoryStream())
- {
- int msgLength = BitConverter.ToInt32(compressedBuffer, 0);
- memory.Write(compressedBuffer, 4, compressedBuffer.Length - 4);
-
- byte[] buffer = new byte[msgLength];
-
- memory.Position = 0;
- using (GZipStream decompressor = new GZipStream(memory, CompressionMode.Decompress))
- {
- decompressor.Read(buffer, 0, buffer.Length);
- }
-
- return new MemoryStream(buffer);
- }
- }
-
- public static byte[] ReadToEnd(System.IO.Stream stream)
- {
- byte[] readBuffer = new byte[4096];
-
- int totalBytesRead = 0;
- int bytesRead;
-
- while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
- {
- totalBytesRead += bytesRead;
-
- if (totalBytesRead == readBuffer.Length)
- {
- int nextByte = stream.ReadByte();
- if (nextByte != -1)
- {
- byte[] temp = new byte[readBuffer.Length*2];
- Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
- Buffer.SetByte(temp, totalBytesRead, (byte) nextByte);
- readBuffer = temp;
- totalBytesRead++;
- }
- }
- }
-
- byte[] buffer = readBuffer;
- if (readBuffer.Length != totalBytesRead)
- {
- buffer = new byte[totalBytesRead];
- Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
- }
- return buffer;
- }
-
- /// <summary>
- /// Converts a byte array in big endian order into an ulong.
- /// </summary>
- /// <param name="bytes">
- /// The array of bytes
- /// </param>
- /// <returns>
- /// The extracted ulong
- /// </returns>
- public static ulong BytesToUInt64Big(byte[] bytes)
- {
- if (bytes.Length < 8) return 0;
- return ((ulong) bytes[0] << 56) | ((ulong) bytes[1] << 48) | ((ulong) bytes[2] << 40) |
- ((ulong) bytes[3] << 32) |
- ((ulong) bytes[4] << 24) | ((ulong) bytes[5] << 16) | ((ulong) bytes[6] << 8) | bytes[7];
- }
-
- // used for RemoteParcelRequest (for "About Landmark")
- public static UUID BuildFakeParcelID(ulong regionHandle, uint x, uint y)
- {
- byte[] bytes =
- {
- (byte) regionHandle, (byte) (regionHandle >> 8), (byte) (regionHandle >> 16),
- (byte) (regionHandle >> 24),
- (byte) (regionHandle >> 32), (byte) (regionHandle >> 40), (byte) (regionHandle >> 48),
- (byte) (regionHandle << 56),
- (byte) x, (byte) (x >> 8), 0, 0,
- (byte) y, (byte) (y >> 8), 0, 0
- };
- return new UUID(bytes, 0);
- }
-
- public static UUID BuildFakeParcelID(ulong regionHandle, uint x, uint y, uint z)
- {
- byte[] bytes =
- {
- (byte) regionHandle, (byte) (regionHandle >> 8), (byte) (regionHandle >> 16),
- (byte) (regionHandle >> 24),
- (byte) (regionHandle >> 32), (byte) (regionHandle >> 40), (byte) (regionHandle >> 48),
- (byte) (regionHandle << 56),
- (byte) x, (byte) (x >> 8), (byte) z, (byte) (z >> 8),
- (byte) y, (byte) (y >> 8), 0, 0
- };
- return new UUID(bytes, 0);
- }
-
- public static void ParseFakeParcelID(UUID parcelID, out ulong regionHandle, out uint x, out uint y)
- {
- byte[] bytes = parcelID.GetBytes();
- regionHandle = Utils.BytesToUInt64(bytes);
- x = Utils.BytesToUInt(bytes, 8) & 0xffff;
- y = Utils.BytesToUInt(bytes, 12) & 0xffff;
- }
-
- public static void ParseFakeParcelID(UUID parcelID, out ulong regionHandle, out uint x, out uint y, out uint z)
- {
- byte[] bytes = parcelID.GetBytes();
- regionHandle = Utils.BytesToUInt64(bytes);
- x = Utils.BytesToUInt(bytes, 8) & 0xffff;
- z = (Utils.BytesToUInt(bytes, 8) & 0xffff0000) >> 16;
- y = Utils.BytesToUInt(bytes, 12) & 0xffff;
- }
-
- public static void FakeParcelIDToGlobalPosition(UUID parcelID, out uint x, out uint y)
- {
- ulong regionHandle;
- uint rx, ry;
-
- ParseFakeParcelID(parcelID, out regionHandle, out x, out y);
- Utils.LongToUInts(regionHandle, out rx, out ry);
-
- x += rx;
- y += ry;
- }
-
- /// <summary>
- /// Get operating system information if available. Returns only the first 45 characters of information
- /// </summary>
- /// <returns>
- /// Operating system information. Returns an empty string if none was available.
- /// </returns>
- public static string GetOperatingSystemInformation()
- {
- string os = String.Empty;
-
- os = Environment.OSVersion.Platform != PlatformID.Unix ? Environment.OSVersion.ToString() : ReadEtcIssue();
-
- if (os.Length > 45)
- {
- os = os.Substring(0, 45);
- }
-
- return os;
- }
-
- public static string GetRuntimeInformation()
- {
- string ru = String.Empty;
-
- switch (Environment.OSVersion.Platform)
- {
- case PlatformID.Unix:
- ru = "Unix/Mono";
- break;
- case PlatformID.MacOSX:
- ru = "OSX/Mono";
- break;
- default:
- ru = Type.GetType("Mono.Runtime") != null ? "Win/Mono" : "Win/.NET";
- break;
- }
-
- return ru;
- }
-
- public static RuntimeEnvironment GetRuntimeEnvironment()
- {
- switch (Environment.OSVersion.Platform)
- {
- case PlatformID.Unix:
- return RuntimeEnvironment.Mono;
- case PlatformID.MacOSX:
- return RuntimeEnvironment.Mono;
- default:
- return Type.GetType("Mono.Runtime") != null
- ? RuntimeEnvironment.WinMono
- : RuntimeEnvironment.NET;
- }
- }
-
- /// <summary>
- …
Large files files are truncated, but you can click here to view the full file