You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace IPA.Utilities
  5. {
  6. /// <summary>
  7. /// A class providing static utility functions that in any other language would just *exist*.
  8. /// </summary>
  9. public static class Utils
  10. {
  11. /// <summary>
  12. /// Converts a hex string to a byte array.
  13. /// </summary>
  14. /// <param name="hex">the hex stream</param>
  15. /// <returns>the corresponding byte array</returns>
  16. public static byte[] StringToByteArray(string hex)
  17. {
  18. int numberChars = hex.Length;
  19. byte[] bytes = new byte[numberChars / 2];
  20. for (int i = 0; i < numberChars; i += 2)
  21. bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  22. return bytes;
  23. }
  24. /// <summary>
  25. /// Converts a byte array to a hex string.
  26. /// </summary>
  27. /// <param name="ba">the byte array</param>
  28. /// <returns>the hex form of the array</returns>
  29. public static string ByteArrayToString(byte[] ba)
  30. {
  31. StringBuilder hex = new StringBuilder(ba.Length * 2);
  32. foreach (byte b in ba)
  33. hex.AppendFormat("{0:x2}", b);
  34. return hex.ToString();
  35. }
  36. // Copyright (c) 2008-2013 Hafthor Stefansson
  37. // Distributed under the MIT/X11 software license
  38. // Ref: http://www.opensource.org/licenses/mit-license.php.
  39. // From: https://stackoverflow.com/a/8808245/3117125
  40. /// <summary>
  41. /// Uses unsafe code to compare 2 byte arrays quickly.
  42. /// </summary>
  43. /// <param name="a1">array 1</param>
  44. /// <param name="a2">array 2</param>
  45. /// <returns>whether or not they are byte-for-byte equal</returns>
  46. public static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
  47. {
  48. if (a1 == a2) return true;
  49. if (a1 == null || a2 == null || a1.Length != a2.Length)
  50. return false;
  51. fixed (byte* p1 = a1, p2 = a2)
  52. {
  53. byte* x1 = p1, x2 = p2;
  54. int l = a1.Length;
  55. for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
  56. if (*((long*)x1) != *((long*)x2)) return false;
  57. if ((l & 4) != 0) { if (*((int*)x1) != *((int*)x2)) return false; x1 += 4; x2 += 4; }
  58. if ((l & 2) != 0) { if (*((short*)x1) != *((short*)x2)) return false; x1 += 2; x2 += 2; }
  59. if ((l & 1) != 0) if (*x1 != *x2) return false;
  60. return true;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets a path relative to the provided folder.
  65. /// </summary>
  66. /// <param name="file">the file to relativize</param>
  67. /// <param name="folder">the source folder</param>
  68. /// <returns>a path to get from <paramref name="folder"/> to <paramref name="file"/></returns>
  69. public static string GetRelativePath(string file, string folder)
  70. {
  71. Uri pathUri = new Uri(file);
  72. // Folders must end in a slash
  73. if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
  74. {
  75. folder += Path.DirectorySeparatorChar;
  76. }
  77. Uri folderUri = new Uri(folder);
  78. return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
  79. }
  80. /// <summary>
  81. /// Copies all files from <paramref name="source"/> to <paramref name="target"/>.
  82. /// </summary>
  83. /// <param name="source">the source directory</param>
  84. /// <param name="target">the destination directory</param>
  85. /// <param name="appendFileName">the filename of the file to append together</param>
  86. /// <param name="onCopyException">a delegate called when there is an error copying. Return true to keep going.</param>
  87. public static void CopyAll(DirectoryInfo source, DirectoryInfo target, string appendFileName = "",
  88. Func<Exception, FileInfo, bool> onCopyException = null)
  89. {
  90. if (source.FullName.ToLower() == target.FullName.ToLower())
  91. {
  92. return;
  93. }
  94. // Check if the target directory exists, if not, create it.
  95. if (Directory.Exists(target.FullName) == false)
  96. {
  97. Directory.CreateDirectory(target.FullName);
  98. }
  99. // Copy each file into it's new directory.
  100. foreach (FileInfo fi in source.GetFiles())
  101. {
  102. try
  103. {
  104. if (fi.Name == appendFileName)
  105. File.AppendAllLines(Path.Combine(target.ToString(), fi.Name), File.ReadAllLines(fi.FullName));
  106. else
  107. fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
  108. }
  109. catch (Exception e)
  110. {
  111. var keepOn = onCopyException?.Invoke(e, fi);
  112. if (!keepOn.Unwrap())
  113. throw;
  114. }
  115. }
  116. // Copy each subdirectory using recursion.
  117. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  118. {
  119. DirectoryInfo nextTargetSubDir =
  120. target.CreateSubdirectory(diSourceSubDir.Name);
  121. CopyAll(diSourceSubDir, nextTargetSubDir, appendFileName, onCopyException);
  122. }
  123. }
  124. /// <summary>
  125. /// Whether you can safely use <see cref="DateTime.Now"/> without Mono throwing a fit.
  126. /// </summary>
  127. /// <value><see langword="true"/> if you can use <see cref="DateTime.Now"/> safely, <see langword="false"/> otherwise</value>
  128. public static bool CanUseDateTimeNowSafely { get; private set; } = true;
  129. private static bool DateTimeSafetyUnknown = true;
  130. private static ulong UnsafeAdvanceTicks = 1;
  131. /// <summary>
  132. /// Gets the current <see cref="DateTime"/> if supported, otherwise, if Mono would throw a fit,
  133. /// returns <see cref="DateTime.MinValue"/> plus some value, such that each time it is called
  134. /// the value will be greater than the previous result. Not suitable for timing.
  135. /// </summary>
  136. /// <returns>the current <see cref="DateTime"/> if supported, otherwise some indeterminant increasing value.</returns>
  137. public static DateTime CurrentTime()
  138. {
  139. if (DateTimeSafetyUnknown)
  140. {
  141. DateTime time = DateTime.MinValue;
  142. try
  143. {
  144. time = DateTime.Now;
  145. }
  146. catch (TimeZoneNotFoundException)
  147. { // Mono did a fucky wucky and we need to avoid this call
  148. CanUseDateTimeNowSafely = false;
  149. }
  150. DateTimeSafetyUnknown = false;
  151. return time;
  152. }
  153. else
  154. {
  155. if (CanUseDateTimeNowSafely) return DateTime.Now;
  156. else return DateTime.MinValue.AddTicks((long)UnsafeAdvanceTicks++); // return MinValue as a fallback
  157. }
  158. }
  159. /// <summary>
  160. /// Compares a pair of <see cref="SemVer.Version"/>s ignoring both the prerelease and build fields.
  161. /// </summary>
  162. /// <param name="l">the left value</param>
  163. /// <param name="r">the right value</param>
  164. /// <returns>-1 if l is less than r, 0 if they are equal in the numeric portion, or 1 if l is greater than r</returns>
  165. public static int VersionCompareNoPrerelease(SemVer.Version l, SemVer.Version r)
  166. {
  167. var cmpVal = l.Major - r.Major;
  168. if (cmpVal != 0) return cmpVal;
  169. cmpVal = l.Minor - r.Minor;
  170. if (cmpVal != 0) return cmpVal;
  171. cmpVal = l.Patch - r.Patch;
  172. return cmpVal;
  173. }
  174. }
  175. }