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.

125 lines
4.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IPA.Utilities
  8. {
  9. /// <summary>
  10. /// A class providing static utility functions that in any other language would just *exist*.
  11. /// </summary>
  12. public static class LoneFunctions
  13. {
  14. /// <summary>
  15. /// Converts a hex string to a byte array.
  16. /// </summary>
  17. /// <param name="hex">the hex stream</param>
  18. /// <returns>the corresponding byte array</returns>
  19. public static byte[] StringToByteArray(string hex)
  20. {
  21. int NumberChars = hex.Length;
  22. byte[] bytes = new byte[NumberChars / 2];
  23. for (int i = 0; i < NumberChars; i += 2)
  24. bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  25. return bytes;
  26. }
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. /// <param name="ba"></param>
  31. /// <returns></returns>
  32. public static string ByteArrayToString(byte[] ba)
  33. {
  34. StringBuilder hex = new StringBuilder(ba.Length * 2);
  35. foreach (byte b in ba)
  36. hex.AppendFormat("{0:x2}", b);
  37. return hex.ToString();
  38. }
  39. // Copyright (c) 2008-2013 Hafthor Stefansson
  40. // Distributed under the MIT/X11 software license
  41. // Ref: http://www.opensource.org/licenses/mit-license.php.
  42. // From: https://stackoverflow.com/a/8808245/3117125
  43. /// <summary>
  44. /// Uses unsafe code to compare 2 byte arrays quickly.
  45. /// </summary>
  46. /// <param name="a1">array 1</param>
  47. /// <param name="a2">array 2</param>
  48. /// <returns>whether or not they are byte-for-byte equal</returns>
  49. public static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
  50. {
  51. if (a1 == a2) return true;
  52. if (a1 == null || a2 == null || a1.Length != a2.Length)
  53. return false;
  54. fixed (byte* p1 = a1, p2 = a2)
  55. {
  56. byte* x1 = p1, x2 = p2;
  57. int l = a1.Length;
  58. for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
  59. if (*((long*)x1) != *((long*)x2)) return false;
  60. if ((l & 4) != 0) { if (*((int*)x1) != *((int*)x2)) return false; x1 += 4; x2 += 4; }
  61. if ((l & 2) != 0) { if (*((short*)x1) != *((short*)x2)) return false; x1 += 2; x2 += 2; }
  62. if ((l & 1) != 0) if (*((byte*)x1) != *((byte*)x2)) return false;
  63. return true;
  64. }
  65. }
  66. /// <summary>
  67. /// Gets a path relative to the provided folder.
  68. /// </summary>
  69. /// <param name="file">the file to relativize</param>
  70. /// <param name="folder">the source folder</param>
  71. /// <returns>a path to get from <paramref name="folder"/> to <paramref name="file"/></returns>
  72. public static string GetRelativePath(string file, string folder)
  73. {
  74. Uri pathUri = new Uri(file);
  75. // Folders must end in a slash
  76. if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
  77. {
  78. folder += Path.DirectorySeparatorChar;
  79. }
  80. Uri folderUri = new Uri(folder);
  81. return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
  82. }
  83. /// <summary>
  84. /// Copies all files from <paramref name="source"/> to <paramref name="target"/>.
  85. /// </summary>
  86. /// <param name="source">the source directory</param>
  87. /// <param name="target">the destination directory</param>
  88. /// <param name="appendFileName"></param>
  89. public static void CopyAll(DirectoryInfo source, DirectoryInfo target, string appendFileName = "")
  90. {
  91. if (source.FullName.ToLower() == target.FullName.ToLower())
  92. {
  93. return;
  94. }
  95. // Check if the target directory exists, if not, create it.
  96. if (Directory.Exists(target.FullName) == false)
  97. {
  98. Directory.CreateDirectory(target.FullName);
  99. }
  100. // Copy each file into it's new directory.
  101. foreach (FileInfo fi in source.GetFiles())
  102. {
  103. if (fi.Name == appendFileName)
  104. File.AppendAllLines(Path.Combine(target.ToString(), fi.Name), File.ReadAllLines(fi.FullName));
  105. else
  106. fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
  107. }
  108. // Copy each subdirectory using recursion.
  109. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  110. {
  111. DirectoryInfo nextTargetSubDir =
  112. target.CreateSubdirectory(diSourceSubDir.Name);
  113. CopyAll(diSourceSubDir, nextTargetSubDir, appendFileName);
  114. }
  115. }
  116. }
  117. }