aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Util.cs
diff options
context:
space:
mode:
authorlbsa712007-10-31 07:28:23 +0000
committerlbsa712007-10-31 07:28:23 +0000
commit064404ab409ddd0a3b25027a98582696295c46fd (patch)
treebd84ec2e23930f76e7cc81c8529ef71936c86dd9 /OpenSim/Framework/Util.cs
parentmade illogical bitwise operations logical (diff)
downloadopensim-SC-064404ab409ddd0a3b25027a98582696295c46fd.zip
opensim-SC-064404ab409ddd0a3b25027a98582696295c46fd.tar.gz
opensim-SC-064404ab409ddd0a3b25027a98582696295c46fd.tar.bz2
opensim-SC-064404ab409ddd0a3b25027a98582696295c46fd.tar.xz
* Moved OpenSim/Framework/General to OpenSim/Framework for great justice.
Diffstat (limited to 'OpenSim/Framework/Util.cs')
-rw-r--r--OpenSim/Framework/Util.cs371
1 files changed, 371 insertions, 0 deletions
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
new file mode 100644
index 0000000..c731561
--- /dev/null
+++ b/OpenSim/Framework/Util.cs
@@ -0,0 +1,371 @@
1/*
2* Copyright (c) Contributors, http://opensimulator.org/
3* See CONTRIBUTORS.TXT for a full list of copyright holders.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above copyright
10* notice, this list of conditions and the following disclaimer in the
11* documentation and/or other materials provided with the distribution.
12* * Neither the name of the OpenSim 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*/
28
29using System;
30using System.Collections.Generic;
31using System.Data;
32using System.IO;
33using System.Net;
34using System.Net.Sockets;
35using System.Security.Cryptography;
36using System.Text;
37using libsecondlife;
38using Nini.Config;
39
40namespace OpenSim.Framework
41{
42 public class Util
43 {
44 private static Random randomClass = new Random();
45 private static uint nextXferID = 5000;
46 private static object XferLock = new object();
47 private static Dictionary<LLUUID, string> capsURLS = new Dictionary<LLUUID, string>();
48
49
50 public static ulong UIntsToLong(uint X, uint Y)
51 {
52 return Helpers.UIntsToLong(X, Y);
53 }
54
55 public static Random RandomClass
56 {
57 get { return randomClass; }
58 }
59
60 public static uint GetNextXferID()
61 {
62 uint id = 0;
63 lock (XferLock)
64 {
65 id = nextXferID;
66 nextXferID++;
67 }
68 return id;
69 }
70
71 public Util()
72 {
73 }
74
75 public static string GetFileName(string file)
76 {
77 // Return just the filename on UNIX platforms
78 // TODO: this should be customisable with a prefix, but that's something to do later.
79 if (Environment.OSVersion.Platform == PlatformID.Unix)
80 {
81 return file;
82 }
83
84 // Return %APPDATA%/OpenSim/file for 2K/XP/NT/2K3/VISTA
85 // TODO: Switch this to System.Enviroment.SpecialFolders.ApplicationData
86 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
87 {
88 if (!Directory.Exists("%APPDATA%\\OpenSim\\"))
89 {
90 Directory.CreateDirectory("%APPDATA%\\OpenSim");
91 }
92
93 return "%APPDATA%\\OpenSim\\" + file;
94 }
95
96 // Catch all - covers older windows versions
97 // (but those probably wont work anyway)
98 return file;
99 }
100
101 public static bool IsEnvironmentSupported(ref string reason)
102 {
103 // Must have .NET 2.0 (Generics / libsl)
104 if (Environment.Version.Major < 2)
105 {
106 reason = ".NET 1.0/1.1 lacks components that is used by OpenSim";
107 return false;
108 }
109
110 // Windows 95/98/ME are unsupported
111 if (Environment.OSVersion.Platform == PlatformID.Win32Windows &&
112 Environment.OSVersion.Platform != PlatformID.Win32NT)
113 {
114 reason = "Windows 95/98/ME will not run OpenSim";
115 return false;
116 }
117
118 // Windows 2000 / Pre-SP2 XP
119 if (Environment.OSVersion.Version.Major == 5 && (
120 Environment.OSVersion.Version.Minor == 0))
121 {
122 reason = "Please update to Windows XP Service Pack 2 or Server2003";
123 return false;
124 }
125
126 return true;
127 }
128
129 public static int UnixTimeSinceEpoch()
130 {
131 TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
132 int timestamp = (int) t.TotalSeconds;
133 return timestamp;
134 }
135
136 public static string Md5Hash(string pass)
137 {
138 MD5 md5 = MD5CryptoServiceProvider.Create();
139 byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(pass));
140 StringBuilder sb = new StringBuilder();
141 for (int i = 0; i < dataMd5.Length; i++)
142 sb.AppendFormat("{0:x2}", dataMd5[i]);
143 return sb.ToString();
144 }
145
146 public static string GetRandomCapsPath()
147 {
148 LLUUID caps = LLUUID.Random();
149 string capsPath = caps.ToStringHyphenated();
150 capsPath = capsPath.Remove(capsPath.Length - 4, 4);
151 return capsPath;
152 }
153
154 public static int fast_distance2d(int x, int y)
155 {
156 x = Math.Abs(x);
157 y = Math.Abs(y);
158
159 int min = Math.Min(x, y);
160
161 return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
162 }
163
164 public static string FieldToString(byte[] bytes)
165 {
166 return FieldToString(bytes, String.Empty);
167 }
168
169 /// <summary>
170 /// Convert a variable length field (byte array) to a string, with a
171 /// field name prepended to each line of the output
172 /// </summary>
173 /// <remarks>If the byte array has unprintable characters in it, a
174 /// hex dump will be put in the string instead</remarks>
175 /// <param name="bytes">The byte array to convert to a string</param>
176 /// <param name="fieldName">A field name to prepend to each line of output</param>
177 /// <returns>An ASCII string or a string containing a hex dump, minus
178 /// the null terminator</returns>
179 public static string FieldToString(byte[] bytes, string fieldName)
180 {
181 // Check for a common case
182 if (bytes.Length == 0) return String.Empty;
183
184 StringBuilder output = new StringBuilder();
185 bool printable = true;
186
187 for (int i = 0; i < bytes.Length; ++i)
188 {
189 // Check if there are any unprintable characters in the array
190 if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
191 && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
192 {
193 printable = false;
194 break;
195 }
196 }
197
198 if (printable)
199 {
200 if (fieldName.Length > 0)
201 {
202 output.Append(fieldName);
203 output.Append(": ");
204 }
205
206 if (bytes[bytes.Length - 1] == 0x00)
207 output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
208 else
209 output.Append(UTF8Encoding.UTF8.GetString(bytes));
210 }
211 else
212 {
213 for (int i = 0; i < bytes.Length; i += 16)
214 {
215 if (i != 0)
216 output.Append(Environment.NewLine);
217 if (fieldName.Length > 0)
218 {
219 output.Append(fieldName);
220 output.Append(": ");
221 }
222
223 for (int j = 0; j < 16; j++)
224 {
225 if ((i + j) < bytes.Length)
226 output.Append(String.Format("{0:X2} ", bytes[i + j]));
227 else
228 output.Append(" ");
229 }
230
231 for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
232 {
233 if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
234 output.Append((char) bytes[i + j]);
235 else
236 output.Append(".");
237 }
238 }
239 }
240
241 return output.ToString();
242 }
243
244 /// <summary>
245 /// Returns a IP address from a specified DNS, favouring IPv4 addresses.
246 /// </summary>
247 /// <param name="dnsAddress">DNS Hostname</param>
248 /// <returns>An IP address, or null</returns>
249 public static IPAddress GetHostFromDNS(string dnsAddress)
250 {
251 // Is it already a valid IP? No need to look it up.
252 IPAddress ipa;
253 if (IPAddress.TryParse(dnsAddress, out ipa))
254 return ipa;
255
256 // Not an IP, lookup required
257 IPAddress[] hosts = Dns.GetHostEntry(dnsAddress).AddressList;
258
259 foreach (IPAddress host in hosts)
260 {
261 if (host.AddressFamily == AddressFamily.InterNetwork)
262 {
263 return host;
264 }
265 }
266
267 if (hosts.Length > 0)
268 return hosts[0];
269
270 return null;
271 }
272
273 public static IPAddress GetLocalHost()
274 {
275 string dnsAddress = "localhost";
276
277 IPAddress[] hosts = Dns.GetHostEntry(dnsAddress).AddressList;
278
279 foreach (IPAddress host in hosts)
280 {
281 if (!IPAddress.IsLoopback(host) && host.AddressFamily == AddressFamily.InterNetwork)
282 {
283 return host;
284 }
285 }
286
287 if (hosts.Length > 0)
288 return hosts[0];
289
290 return null;
291 }
292
293 //
294 // directory locations
295 //
296
297 public static string homeDir()
298 {
299 string temp;
300// string personal=(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
301// temp = Path.Combine(personal,".OpenSim");
302 temp = ".";
303 return temp;
304 }
305
306 public static string configDir()
307 {
308 string temp;
309 temp = ".";
310 return temp;
311 }
312
313 public static string dataDir()
314 {
315 string temp;
316 temp = ".";
317 return temp;
318 }
319
320 public static string logDir()
321 {
322 string temp;
323 temp = ".";
324 return temp;
325 }
326
327 public static string GetCapsURL(LLUUID userID)
328 {
329 if (capsURLS.ContainsKey(userID))
330 {
331 return capsURLS[userID];
332 }
333 return "";
334 }
335
336 public static void SetCapsURL(LLUUID userID, string url)
337 {
338 if (capsURLS.ContainsKey(userID))
339 {
340 capsURLS[userID] = url;
341 }
342 else
343 {
344 capsURLS.Add(userID, url);
345 }
346 }
347
348 // Nini (config) related Methods
349 public static IConfigSource ConvertDataRowToXMLConfig(DataRow row, string fileName)
350 {
351 if (!File.Exists(fileName))
352 {
353 //create new file
354 }
355 XmlConfigSource config = new XmlConfigSource(fileName);
356 AddDataRowToConfig(config, row);
357 config.Save();
358
359 return config;
360 }
361
362 public static void AddDataRowToConfig(IConfigSource config, DataRow row)
363 {
364 config.Configs.Add((string) row[0]);
365 for (int i = 0; i < row.Table.Columns.Count; i++)
366 {
367 config.Configs[(string) row[0]].Set(row.Table.Columns[i].ColumnName, row[i]);
368 }
369 }
370 }
371} \ No newline at end of file