Util.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using libsecondlife;
  5. using libsecondlife.Packets;
  6. namespace OpenSim.Framework.Utilities
  7. {
  8. public class Util
  9. {
  10. private static Random randomClass = new Random();
  11. private static uint nextXferID = 5000;
  12. private static object XferLock = new object();
  13. public static ulong UIntsToLong(uint X, uint Y)
  14. {
  15. return Helpers.UIntsToLong(X, Y);
  16. }
  17. public static Random RandomClass
  18. {
  19. get
  20. {
  21. return randomClass;
  22. }
  23. }
  24. public static uint GetNextXferID()
  25. {
  26. uint id = 0;
  27. lock(XferLock)
  28. {
  29. id = nextXferID;
  30. nextXferID++;
  31. }
  32. return id;
  33. }
  34. public static int fast_distance2d(int x, int y)
  35. {
  36. x = System.Math.Abs(x);
  37. y = System.Math.Abs(y);
  38. int min = System.Math.Min(x, y);
  39. return (x + y - (min >> 1) - (min >> 2) + (min >> 4));
  40. }
  41. public static string FieldToString(byte[] bytes)
  42. {
  43. return FieldToString(bytes, String.Empty);
  44. }
  45. /// <summary>
  46. /// Convert a variable length field (byte array) to a string, with a
  47. /// field name prepended to each line of the output
  48. /// </summary>
  49. /// <remarks>If the byte array has unprintable characters in it, a
  50. /// hex dump will be put in the string instead</remarks>
  51. /// <param name="bytes">The byte array to convert to a string</param>
  52. /// <param name="fieldName">A field name to prepend to each line of output</param>
  53. /// <returns>An ASCII string or a string containing a hex dump, minus
  54. /// the null terminator</returns>
  55. public static string FieldToString(byte[] bytes, string fieldName)
  56. {
  57. // Check for a common case
  58. if (bytes.Length == 0) return String.Empty;
  59. StringBuilder output = new StringBuilder();
  60. bool printable = true;
  61. for (int i = 0; i < bytes.Length; ++i)
  62. {
  63. // Check if there are any unprintable characters in the array
  64. if ((bytes[i] < 0x20 || bytes[i] > 0x7E) && bytes[i] != 0x09
  65. && bytes[i] != 0x0D && bytes[i] != 0x0A && bytes[i] != 0x00)
  66. {
  67. printable = false;
  68. break;
  69. }
  70. }
  71. if (printable)
  72. {
  73. if (fieldName.Length > 0)
  74. {
  75. output.Append(fieldName);
  76. output.Append(": ");
  77. }
  78. if (bytes[bytes.Length - 1] == 0x00)
  79. output.Append(UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length - 1));
  80. else
  81. output.Append(UTF8Encoding.UTF8.GetString(bytes));
  82. }
  83. else
  84. {
  85. for (int i = 0; i < bytes.Length; i += 16)
  86. {
  87. if (i != 0)
  88. output.Append(Environment.NewLine);
  89. if (fieldName.Length > 0)
  90. {
  91. output.Append(fieldName);
  92. output.Append(": ");
  93. }
  94. for (int j = 0; j < 16; j++)
  95. {
  96. if ((i + j) < bytes.Length)
  97. output.Append(String.Format("{0:X2} ", bytes[i + j]));
  98. else
  99. output.Append(" ");
  100. }
  101. for (int j = 0; j < 16 && (i + j) < bytes.Length; j++)
  102. {
  103. if (bytes[i + j] >= 0x20 && bytes[i + j] < 0x7E)
  104. output.Append((char)bytes[i + j]);
  105. else
  106. output.Append(".");
  107. }
  108. }
  109. }
  110. return output.ToString();
  111. }
  112. public Util()
  113. {
  114. }
  115. }
  116. }