Tools.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 ResBuilder
  8. {
  9. static class Tools
  10. {
  11. public static readonly Encoding UTF8 = new UTF8Encoding(false, false);
  12. public static readonly Encoding UTF8_BOM = new UTF8Encoding(true, false);
  13. public static string DecodeUTF8(byte[] data)
  14. {
  15. if (data.Length > 3)
  16. {
  17. if ((data[0] == 0xEF) && (data[1] == 0xBB) && (data[2] == 0xBF))
  18. {
  19. return UTF8_BOM.GetString(data);
  20. }
  21. }
  22. return UTF8.GetString(data);
  23. }
  24. public static bool ExistCharInLineBefore(string str, int endIdex, char c)
  25. {
  26. for (int i = endIdex - 1; i >= 0; i--)
  27. {
  28. if (str[i] == '\n')
  29. {
  30. return false;
  31. }
  32. if (str[i] == c)
  33. {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. public static string FindUpDirectory(string dirName, int rlev = 3)
  40. {
  41. var curPath = Environment.CurrentDirectory;
  42. var curNode = new DirectoryInfo(curPath);
  43. var uplev = 0;
  44. while (curNode.Parent != null && uplev < rlev)
  45. {
  46. var ret = curNode.GetDirectories(dirName, SearchOption.TopDirectoryOnly);
  47. if (ret.Length > 0)
  48. {
  49. return ret[0].FullName;
  50. }
  51. else
  52. {
  53. curNode = curNode.Parent;
  54. }
  55. uplev++;
  56. }
  57. return null;
  58. }
  59. internal static uint HashFileNameWithoutExtension(string relativeFilePath)
  60. {
  61. uint hash = 0;
  62. for (int i = 0; i < relativeFilePath.Length; i++)
  63. {
  64. uint cc = relativeFilePath[i];
  65. if (cc == '.')
  66. {
  67. break;
  68. }
  69. if (cc >= '0' && cc <= '9')
  70. {
  71. cc = cc - '0';
  72. }
  73. else if (cc >= 'A' && cc <= 'Z')
  74. {
  75. cc = cc - 'A' + 10;
  76. }
  77. else if (cc >= 'a' && cc <= 'z')
  78. {
  79. cc = cc - 'a' + 34;
  80. }
  81. else
  82. {
  83. cc = 59;
  84. }
  85. hash += (hash << 5) + cc;
  86. }
  87. return hash;
  88. }
  89. }
  90. }