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.

122 lines
4.8 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
6 years ago
6 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 LoneFunctions
  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. ///
  26. /// </summary>
  27. /// <param name="ba"></param>
  28. /// <returns></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"></param>
  86. public static void CopyAll(DirectoryInfo source, DirectoryInfo target, string appendFileName = "")
  87. {
  88. if (source.FullName.ToLower() == target.FullName.ToLower())
  89. {
  90. return;
  91. }
  92. // Check if the target directory exists, if not, create it.
  93. if (Directory.Exists(target.FullName) == false)
  94. {
  95. Directory.CreateDirectory(target.FullName);
  96. }
  97. // Copy each file into it's new directory.
  98. foreach (FileInfo fi in source.GetFiles())
  99. {
  100. if (fi.Name == appendFileName)
  101. File.AppendAllLines(Path.Combine(target.ToString(), fi.Name), File.ReadAllLines(fi.FullName));
  102. else
  103. fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
  104. }
  105. // Copy each subdirectory using recursion.
  106. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  107. {
  108. DirectoryInfo nextTargetSubDir =
  109. target.CreateSubdirectory(diSourceSubDir.Name);
  110. CopyAll(diSourceSubDir, nextTargetSubDir, appendFileName);
  111. }
  112. }
  113. }
  114. }