diff options
author | UbitUmarov | 2015-09-01 14:54:35 +0100 |
---|---|---|
committer | UbitUmarov | 2015-09-01 14:54:35 +0100 |
commit | 371c9dd2af01a2e7422ec901ee1f80757284a78c (patch) | |
tree | 058d2a513cacb12efcce0c0df0ae14ad135dbfe2 /OpenSim/Framework/Util.cs | |
parent | remove lixo (diff) | |
parent | dont change camera on crossings (diff) | |
download | opensim-SC-371c9dd2af01a2e7422ec901ee1f80757284a78c.zip opensim-SC-371c9dd2af01a2e7422ec901ee1f80757284a78c.tar.gz opensim-SC-371c9dd2af01a2e7422ec901ee1f80757284a78c.tar.bz2 opensim-SC-371c9dd2af01a2e7422ec901ee1f80757284a78c.tar.xz |
bad merge?
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r-- | OpenSim/Framework/Util.cs | 253 |
1 files changed, 222 insertions, 31 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index a5f798d..eb3526a 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs | |||
@@ -61,6 +61,15 @@ namespace OpenSim.Framework | |||
61 | public enum PermissionMask : uint | 61 | public enum PermissionMask : uint |
62 | { | 62 | { |
63 | None = 0, | 63 | None = 0, |
64 | |||
65 | // folded perms | ||
66 | foldedTransfer = 1, | ||
67 | foldedModify = 1 << 1, | ||
68 | foldedCopy = 1 << 2, | ||
69 | |||
70 | foldedMask = 0x07, | ||
71 | |||
72 | // | ||
64 | Transfer = 1 << 13, | 73 | Transfer = 1 << 13, |
65 | Modify = 1 << 14, | 74 | Modify = 1 << 14, |
66 | Copy = 1 << 15, | 75 | Copy = 1 << 15, |
@@ -267,14 +276,12 @@ namespace OpenSim.Framework | |||
267 | /// </summary> | 276 | /// </summary> |
268 | /// <param name="a">A 3d vector</param> | 277 | /// <param name="a">A 3d vector</param> |
269 | /// <returns>A new vector which is normalized form of the vector</returns> | 278 | /// <returns>A new vector which is normalized form of the vector</returns> |
270 | /// <remarks>The vector paramater cannot be <0,0,0></remarks> | 279 | |
271 | public static Vector3 GetNormalizedVector(Vector3 a) | 280 | public static Vector3 GetNormalizedVector(Vector3 a) |
272 | { | 281 | { |
273 | if (IsZeroVector(a)) | 282 | Vector3 v = new Vector3(a.X, a.Y, a.Z); |
274 | throw new ArgumentException("Vector paramater cannot be a zero vector."); | 283 | v.Normalize(); |
275 | 284 | return v; | |
276 | float Mag = (float) GetMagnitude(a); | ||
277 | return new Vector3(a.X / Mag, a.Y / Mag, a.Z / Mag); | ||
278 | } | 285 | } |
279 | 286 | ||
280 | /// <summary> | 287 | /// <summary> |
@@ -641,19 +648,25 @@ namespace OpenSim.Framework | |||
641 | /// </summary> | 648 | /// </summary> |
642 | /// <param name="data"></param> | 649 | /// <param name="data"></param> |
643 | /// <returns></returns> | 650 | /// <returns></returns> |
651 | |||
644 | public static string Md5Hash(string data) | 652 | public static string Md5Hash(string data) |
645 | { | 653 | { |
646 | byte[] dataMd5 = ComputeMD5Hash(data); | 654 | return Md5Hash(data, Encoding.Default); |
655 | } | ||
656 | |||
657 | public static string Md5Hash(string data, Encoding encoding) | ||
658 | { | ||
659 | byte[] dataMd5 = ComputeMD5Hash(data, encoding); | ||
647 | StringBuilder sb = new StringBuilder(); | 660 | StringBuilder sb = new StringBuilder(); |
648 | for (int i = 0; i < dataMd5.Length; i++) | 661 | for (int i = 0; i < dataMd5.Length; i++) |
649 | sb.AppendFormat("{0:x2}", dataMd5[i]); | 662 | sb.AppendFormat("{0:x2}", dataMd5[i]); |
650 | return sb.ToString(); | 663 | return sb.ToString(); |
651 | } | 664 | } |
652 | 665 | ||
653 | private static byte[] ComputeMD5Hash(string data) | 666 | private static byte[] ComputeMD5Hash(string data, Encoding encoding) |
654 | { | 667 | { |
655 | MD5 md5 = MD5.Create(); | 668 | MD5 md5 = MD5.Create(); |
656 | return md5.ComputeHash(Encoding.Default.GetBytes(data)); | 669 | return md5.ComputeHash(encoding.GetBytes(data)); |
657 | } | 670 | } |
658 | 671 | ||
659 | /// <summary> | 672 | /// <summary> |
@@ -661,6 +674,12 @@ namespace OpenSim.Framework | |||
661 | /// </summary> | 674 | /// </summary> |
662 | /// <param name="data"></param> | 675 | /// <param name="data"></param> |
663 | /// <returns></returns> | 676 | /// <returns></returns> |
677 | |||
678 | public static string SHA1Hash(string data, Encoding enc) | ||
679 | { | ||
680 | return SHA1Hash(enc.GetBytes(data)); | ||
681 | } | ||
682 | |||
664 | public static string SHA1Hash(string data) | 683 | public static string SHA1Hash(string data) |
665 | { | 684 | { |
666 | return SHA1Hash(Encoding.Default.GetBytes(data)); | 685 | return SHA1Hash(Encoding.Default.GetBytes(data)); |
@@ -714,17 +733,26 @@ namespace OpenSim.Framework | |||
714 | /// <param name="oldy">Old region y-coord</param> | 733 | /// <param name="oldy">Old region y-coord</param> |
715 | /// <param name="newy">New region y-coord</param> | 734 | /// <param name="newy">New region y-coord</param> |
716 | /// <returns></returns> | 735 | /// <returns></returns> |
717 | public static bool IsOutsideView(float drawdist, uint oldx, uint newx, uint oldy, uint newy) | 736 | public static bool IsOutsideView(float drawdist, uint oldx, uint newx, uint oldy, uint newy, |
737 | int oldsizex, int oldsizey, int newsizex, int newsizey) | ||
718 | { | 738 | { |
719 | int dd = (int)((drawdist + Constants.RegionSize - 1) / Constants.RegionSize); | 739 | // we still need to make sure we see new region 1stNeighbors |
720 | 740 | ||
721 | int startX = (int)oldx - dd; | 741 | oldx *= Constants.RegionSize; |
722 | int startY = (int)oldy - dd; | 742 | newx *= Constants.RegionSize; |
743 | if (oldx + oldsizex + drawdist < newx) | ||
744 | return true; | ||
745 | if (newx + newsizex + drawdist < oldx) | ||
746 | return true; | ||
723 | 747 | ||
724 | int endX = (int)oldx + dd; | 748 | oldy *= Constants.RegionSize; |
725 | int endY = (int)oldy + dd; | 749 | newy *= Constants.RegionSize; |
750 | if (oldy + oldsizey + drawdist < newy) | ||
751 | return true; | ||
752 | if (newy + newsizey + drawdist< oldy) | ||
753 | return true; | ||
726 | 754 | ||
727 | return (newx < startX || endX < newx || newy < startY || endY < newy); | 755 | return false; |
728 | } | 756 | } |
729 | 757 | ||
730 | public static string FieldToString(byte[] bytes) | 758 | public static string FieldToString(byte[] bytes) |
@@ -805,6 +833,16 @@ namespace OpenSim.Framework | |||
805 | } | 833 | } |
806 | 834 | ||
807 | /// <summary> | 835 | /// <summary> |
836 | /// Converts a URL to a IPAddress | ||
837 | /// </summary> | ||
838 | /// <param name="url">URL Standard Format</param> | ||
839 | /// <returns>A resolved IP Address</returns> | ||
840 | public static IPAddress GetHostFromURL(string url) | ||
841 | { | ||
842 | return GetHostFromDNS(url.Split(new char[] {'/', ':'})[3]); | ||
843 | } | ||
844 | |||
845 | /// <summary> | ||
808 | /// Returns a IP address from a specified DNS, favouring IPv4 addresses. | 846 | /// Returns a IP address from a specified DNS, favouring IPv4 addresses. |
809 | /// </summary> | 847 | /// </summary> |
810 | /// <param name="dnsAddress">DNS Hostname</param> | 848 | /// <param name="dnsAddress">DNS Hostname</param> |
@@ -1065,6 +1103,25 @@ namespace OpenSim.Framework | |||
1065 | } | 1103 | } |
1066 | } | 1104 | } |
1067 | 1105 | ||
1106 | public static string GetConfigVarWithDefaultSection(IConfigSource config, string varname, string section) | ||
1107 | { | ||
1108 | // First, check the Startup section, the default section | ||
1109 | IConfig cnf = config.Configs["Startup"]; | ||
1110 | if (cnf == null) | ||
1111 | return string.Empty; | ||
1112 | string val = cnf.GetString(varname, string.Empty); | ||
1113 | |||
1114 | // Then check for an overwrite of the default in the given section | ||
1115 | if (!string.IsNullOrEmpty(section)) | ||
1116 | { | ||
1117 | cnf = config.Configs[section]; | ||
1118 | if (cnf != null) | ||
1119 | val = cnf.GetString(varname, val); | ||
1120 | } | ||
1121 | |||
1122 | return val; | ||
1123 | } | ||
1124 | |||
1068 | /// <summary> | 1125 | /// <summary> |
1069 | /// Gets the value of a configuration variable by looking into | 1126 | /// Gets the value of a configuration variable by looking into |
1070 | /// multiple sections in order. The latter sections overwrite | 1127 | /// multiple sections in order. The latter sections overwrite |
@@ -1388,6 +1445,46 @@ namespace OpenSim.Framework | |||
1388 | return ret; | 1445 | return ret; |
1389 | } | 1446 | } |
1390 | 1447 | ||
1448 | public static string Compress(string text) | ||
1449 | { | ||
1450 | byte[] buffer = Util.UTF8.GetBytes(text); | ||
1451 | MemoryStream memory = new MemoryStream(); | ||
1452 | using (GZipStream compressor = new GZipStream(memory, CompressionMode.Compress, true)) | ||
1453 | { | ||
1454 | compressor.Write(buffer, 0, buffer.Length); | ||
1455 | } | ||
1456 | |||
1457 | memory.Position = 0; | ||
1458 | |||
1459 | byte[] compressed = new byte[memory.Length]; | ||
1460 | memory.Read(compressed, 0, compressed.Length); | ||
1461 | |||
1462 | byte[] compressedBuffer = new byte[compressed.Length + 4]; | ||
1463 | Buffer.BlockCopy(compressed, 0, compressedBuffer, 4, compressed.Length); | ||
1464 | Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, compressedBuffer, 0, 4); | ||
1465 | return Convert.ToBase64String(compressedBuffer); | ||
1466 | } | ||
1467 | |||
1468 | public static string Decompress(string compressedText) | ||
1469 | { | ||
1470 | byte[] compressedBuffer = Convert.FromBase64String(compressedText); | ||
1471 | using (MemoryStream memory = new MemoryStream()) | ||
1472 | { | ||
1473 | int msgLength = BitConverter.ToInt32(compressedBuffer, 0); | ||
1474 | memory.Write(compressedBuffer, 4, compressedBuffer.Length - 4); | ||
1475 | |||
1476 | byte[] buffer = new byte[msgLength]; | ||
1477 | |||
1478 | memory.Position = 0; | ||
1479 | using (GZipStream decompressor = new GZipStream(memory, CompressionMode.Decompress)) | ||
1480 | { | ||
1481 | decompressor.Read(buffer, 0, buffer.Length); | ||
1482 | } | ||
1483 | |||
1484 | return Util.UTF8.GetString(buffer); | ||
1485 | } | ||
1486 | } | ||
1487 | |||
1391 | /// <summary> | 1488 | /// <summary> |
1392 | /// Copy data from one stream to another, leaving the read position of both streams at the beginning. | 1489 | /// Copy data from one stream to another, leaving the read position of both streams at the beginning. |
1393 | /// </summary> | 1490 | /// </summary> |
@@ -1524,19 +1621,19 @@ namespace OpenSim.Framework | |||
1524 | { | 1621 | { |
1525 | string os = String.Empty; | 1622 | string os = String.Empty; |
1526 | 1623 | ||
1527 | if (Environment.OSVersion.Platform != PlatformID.Unix) | 1624 | // if (Environment.OSVersion.Platform != PlatformID.Unix) |
1528 | { | 1625 | // { |
1529 | os = Environment.OSVersion.ToString(); | 1626 | // os = Environment.OSVersion.ToString(); |
1530 | } | 1627 | // } |
1531 | else | 1628 | // else |
1532 | { | 1629 | // { |
1533 | os = ReadEtcIssue(); | 1630 | // os = ReadEtcIssue(); |
1534 | } | 1631 | // } |
1535 | 1632 | // | |
1536 | if (os.Length > 45) | 1633 | // if (os.Length > 45) |
1537 | { | 1634 | // { |
1538 | os = os.Substring(0, 45); | 1635 | // os = os.Substring(0, 45); |
1539 | } | 1636 | // } |
1540 | 1637 | ||
1541 | return os; | 1638 | return os; |
1542 | } | 1639 | } |
@@ -1591,6 +1688,69 @@ namespace OpenSim.Framework | |||
1591 | return displayConnectionString; | 1688 | return displayConnectionString; |
1592 | } | 1689 | } |
1593 | 1690 | ||
1691 | public static T ReadSettingsFromIniFile<T>(IConfig config, T settingsClass) | ||
1692 | { | ||
1693 | Type settingsType = settingsClass.GetType(); | ||
1694 | |||
1695 | FieldInfo[] fieldInfos = settingsType.GetFields(); | ||
1696 | foreach (FieldInfo fieldInfo in fieldInfos) | ||
1697 | { | ||
1698 | if (!fieldInfo.IsStatic) | ||
1699 | { | ||
1700 | if (fieldInfo.FieldType == typeof(System.String)) | ||
1701 | { | ||
1702 | fieldInfo.SetValue(settingsClass, config.Get(fieldInfo.Name, (string)fieldInfo.GetValue(settingsClass))); | ||
1703 | } | ||
1704 | else if (fieldInfo.FieldType == typeof(System.Boolean)) | ||
1705 | { | ||
1706 | fieldInfo.SetValue(settingsClass, config.GetBoolean(fieldInfo.Name, (bool)fieldInfo.GetValue(settingsClass))); | ||
1707 | } | ||
1708 | else if (fieldInfo.FieldType == typeof(System.Int32)) | ||
1709 | { | ||
1710 | fieldInfo.SetValue(settingsClass, config.GetInt(fieldInfo.Name, (int)fieldInfo.GetValue(settingsClass))); | ||
1711 | } | ||
1712 | else if (fieldInfo.FieldType == typeof(System.Single)) | ||
1713 | { | ||
1714 | fieldInfo.SetValue(settingsClass, config.GetFloat(fieldInfo.Name, (float)fieldInfo.GetValue(settingsClass))); | ||
1715 | } | ||
1716 | else if (fieldInfo.FieldType == typeof(System.UInt32)) | ||
1717 | { | ||
1718 | fieldInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(fieldInfo.Name, ((uint)fieldInfo.GetValue(settingsClass)).ToString()))); | ||
1719 | } | ||
1720 | } | ||
1721 | } | ||
1722 | |||
1723 | PropertyInfo[] propertyInfos = settingsType.GetProperties(); | ||
1724 | foreach (PropertyInfo propInfo in propertyInfos) | ||
1725 | { | ||
1726 | if ((propInfo.CanRead) && (propInfo.CanWrite)) | ||
1727 | { | ||
1728 | if (propInfo.PropertyType == typeof(System.String)) | ||
1729 | { | ||
1730 | propInfo.SetValue(settingsClass, config.Get(propInfo.Name, (string)propInfo.GetValue(settingsClass, null)), null); | ||
1731 | } | ||
1732 | else if (propInfo.PropertyType == typeof(System.Boolean)) | ||
1733 | { | ||
1734 | propInfo.SetValue(settingsClass, config.GetBoolean(propInfo.Name, (bool)propInfo.GetValue(settingsClass, null)), null); | ||
1735 | } | ||
1736 | else if (propInfo.PropertyType == typeof(System.Int32)) | ||
1737 | { | ||
1738 | propInfo.SetValue(settingsClass, config.GetInt(propInfo.Name, (int)propInfo.GetValue(settingsClass, null)), null); | ||
1739 | } | ||
1740 | else if (propInfo.PropertyType == typeof(System.Single)) | ||
1741 | { | ||
1742 | propInfo.SetValue(settingsClass, config.GetFloat(propInfo.Name, (float)propInfo.GetValue(settingsClass, null)), null); | ||
1743 | } | ||
1744 | if (propInfo.PropertyType == typeof(System.UInt32)) | ||
1745 | { | ||
1746 | propInfo.SetValue(settingsClass, Convert.ToUInt32(config.Get(propInfo.Name, ((uint)propInfo.GetValue(settingsClass, null)).ToString())), null); | ||
1747 | } | ||
1748 | } | ||
1749 | } | ||
1750 | |||
1751 | return settingsClass; | ||
1752 | } | ||
1753 | |||
1594 | public static string Base64ToString(string str) | 1754 | public static string Base64ToString(string str) |
1595 | { | 1755 | { |
1596 | Decoder utf8Decode = Encoding.UTF8.GetDecoder(); | 1756 | Decoder utf8Decode = Encoding.UTF8.GetDecoder(); |
@@ -1645,7 +1805,7 @@ namespace OpenSim.Framework | |||
1645 | 1805 | ||
1646 | public static Guid GetHashGuid(string data, string salt) | 1806 | public static Guid GetHashGuid(string data, string salt) |
1647 | { | 1807 | { |
1648 | byte[] hash = ComputeMD5Hash(data + salt); | 1808 | byte[] hash = ComputeMD5Hash(data + salt, Encoding.Default); |
1649 | 1809 | ||
1650 | //string s = BitConverter.ToString(hash); | 1810 | //string s = BitConverter.ToString(hash); |
1651 | 1811 | ||
@@ -1792,6 +1952,32 @@ namespace OpenSim.Framework | |||
1792 | return found.ToArray(); | 1952 | return found.ToArray(); |
1793 | } | 1953 | } |
1794 | 1954 | ||
1955 | public static string ServerURI(string uri) | ||
1956 | { | ||
1957 | if (uri == string.Empty) | ||
1958 | return string.Empty; | ||
1959 | |||
1960 | // Get rid of eventual slashes at the end | ||
1961 | uri = uri.TrimEnd('/'); | ||
1962 | |||
1963 | IPAddress ipaddr1 = null; | ||
1964 | string port1 = ""; | ||
1965 | try | ||
1966 | { | ||
1967 | ipaddr1 = Util.GetHostFromURL(uri); | ||
1968 | } | ||
1969 | catch { } | ||
1970 | |||
1971 | try | ||
1972 | { | ||
1973 | port1 = uri.Split(new char[] { ':' })[2]; | ||
1974 | } | ||
1975 | catch { } | ||
1976 | |||
1977 | // We tried our best to convert the domain names to IP addresses | ||
1978 | return (ipaddr1 != null) ? "http://" + ipaddr1.ToString() + ":" + port1 : uri; | ||
1979 | } | ||
1980 | |||
1795 | /// <summary> | 1981 | /// <summary> |
1796 | /// Convert a string to a byte format suitable for transport in an LLUDP packet. The output is truncated to 256 bytes if necessary. | 1982 | /// Convert a string to a byte format suitable for transport in an LLUDP packet. The output is truncated to 256 bytes if necessary. |
1797 | /// </summary> | 1983 | /// </summary> |
@@ -1970,6 +2156,11 @@ namespace OpenSim.Framework | |||
1970 | } | 2156 | } |
1971 | } | 2157 | } |
1972 | 2158 | ||
2159 | public static void FireAndForget(System.Threading.WaitCallback callback) | ||
2160 | { | ||
2161 | FireAndForget(callback, null); | ||
2162 | } | ||
2163 | |||
1973 | public static void InitThreadPool(int minThreads, int maxThreads) | 2164 | public static void InitThreadPool(int minThreads, int maxThreads) |
1974 | { | 2165 | { |
1975 | if (maxThreads < 2) | 2166 | if (maxThreads < 2) |
@@ -1986,7 +2177,7 @@ namespace OpenSim.Framework | |||
1986 | 2177 | ||
1987 | STPStartInfo startInfo = new STPStartInfo(); | 2178 | STPStartInfo startInfo = new STPStartInfo(); |
1988 | startInfo.ThreadPoolName = "Util"; | 2179 | startInfo.ThreadPoolName = "Util"; |
1989 | startInfo.IdleTimeout = 2000; | 2180 | startInfo.IdleTimeout = 20000; |
1990 | startInfo.MaxWorkerThreads = maxThreads; | 2181 | startInfo.MaxWorkerThreads = maxThreads; |
1991 | startInfo.MinWorkerThreads = minThreads; | 2182 | startInfo.MinWorkerThreads = minThreads; |
1992 | 2183 | ||