YooUtility.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. namespace YooAsset
  8. {
  9. /// <summary>
  10. /// 字符串工具类
  11. /// </summary>
  12. internal static class StringUtility
  13. {
  14. [ThreadStatic]
  15. private static StringBuilder _cacheBuilder = new StringBuilder(1024);
  16. public static string Format(string format, object arg0)
  17. {
  18. if (string.IsNullOrEmpty(format))
  19. throw new ArgumentNullException();
  20. _cacheBuilder.Length = 0;
  21. _cacheBuilder.AppendFormat(format, arg0);
  22. return _cacheBuilder.ToString();
  23. }
  24. public static string Format(string format, object arg0, object arg1)
  25. {
  26. if (string.IsNullOrEmpty(format))
  27. throw new ArgumentNullException();
  28. _cacheBuilder.Length = 0;
  29. _cacheBuilder.AppendFormat(format, arg0, arg1);
  30. return _cacheBuilder.ToString();
  31. }
  32. public static string Format(string format, object arg0, object arg1, object arg2)
  33. {
  34. if (string.IsNullOrEmpty(format))
  35. throw new ArgumentNullException();
  36. _cacheBuilder.Length = 0;
  37. _cacheBuilder.AppendFormat(format, arg0, arg1, arg2);
  38. return _cacheBuilder.ToString();
  39. }
  40. public static string Format(string format, params object[] args)
  41. {
  42. if (string.IsNullOrEmpty(format))
  43. throw new ArgumentNullException();
  44. if (args == null)
  45. throw new ArgumentNullException();
  46. _cacheBuilder.Length = 0;
  47. _cacheBuilder.AppendFormat(format, args);
  48. return _cacheBuilder.ToString();
  49. }
  50. public static List<string> StringToStringList(string str, char separator)
  51. {
  52. List<string> result = new List<string>();
  53. if (!String.IsNullOrEmpty(str))
  54. {
  55. string[] splits = str.Split(separator);
  56. foreach (string split in splits)
  57. {
  58. string value = split.Trim(); //移除首尾空格
  59. if (!String.IsNullOrEmpty(value))
  60. {
  61. result.Add(value);
  62. }
  63. }
  64. }
  65. return result;
  66. }
  67. public static bool StringToBool(string str)
  68. {
  69. int value = (int)Convert.ChangeType(str, typeof(int));
  70. return value > 0;
  71. }
  72. public static T NameToEnum<T>(string name)
  73. {
  74. if (Enum.IsDefined(typeof(T), name) == false)
  75. {
  76. throw new ArgumentException($"Enum {typeof(T)} is not defined name {name}");
  77. }
  78. return (T)Enum.Parse(typeof(T), name);
  79. }
  80. public static string RemoveFirstChar(string str)
  81. {
  82. if (string.IsNullOrEmpty(str))
  83. return str;
  84. return str.Substring(1);
  85. }
  86. public static string RemoveLastChar(string str)
  87. {
  88. if (string.IsNullOrEmpty(str))
  89. return str;
  90. return str.Substring(0, str.Length - 1);
  91. }
  92. public static string RemoveExtension(string str)
  93. {
  94. if (string.IsNullOrEmpty(str))
  95. return str;
  96. int index = str.LastIndexOf(".");
  97. if (index == -1)
  98. return str;
  99. else
  100. return str.Remove(index); //"assets/config/test.unity3d" --> "assets/config/test"
  101. }
  102. }
  103. /// <summary>
  104. /// 文件工具类
  105. /// </summary>
  106. internal static class FileUtility
  107. {
  108. /// <summary>
  109. /// 读取文件
  110. /// </summary>
  111. public static string ReadFile(string filePath)
  112. {
  113. if (File.Exists(filePath) == false)
  114. return string.Empty;
  115. return File.ReadAllText(filePath, Encoding.UTF8);
  116. }
  117. /// <summary>
  118. /// 创建文件(如果已经存在则删除旧文件)
  119. /// </summary>
  120. public static void CreateFile(string filePath, string content)
  121. {
  122. // 删除旧文件
  123. if (File.Exists(filePath))
  124. File.Delete(filePath);
  125. // 创建文件夹路径
  126. CreateFileDirectory(filePath);
  127. // 创建新文件
  128. byte[] bytes = Encoding.UTF8.GetBytes(content);
  129. using (FileStream fs = File.Create(filePath))
  130. {
  131. fs.Write(bytes, 0, bytes.Length);
  132. fs.Flush();
  133. fs.Close();
  134. }
  135. }
  136. /// <summary>
  137. /// 创建文件的文件夹路径
  138. /// </summary>
  139. public static void CreateFileDirectory(string filePath)
  140. {
  141. // 获取文件的文件夹路径
  142. string directory = Path.GetDirectoryName(filePath);
  143. CreateDirectory(directory);
  144. }
  145. /// <summary>
  146. /// 创建文件夹路径
  147. /// </summary>
  148. public static void CreateDirectory(string directory)
  149. {
  150. // If the directory doesn't exist, create it.
  151. if (Directory.Exists(directory) == false)
  152. Directory.CreateDirectory(directory);
  153. }
  154. /// <summary>
  155. /// 获取文件大小(字节数)
  156. /// </summary>
  157. public static long GetFileSize(string filePath)
  158. {
  159. FileInfo fileInfo = new FileInfo(filePath);
  160. return fileInfo.Length;
  161. }
  162. }
  163. /// <summary>
  164. /// 哈希工具类
  165. /// </summary>
  166. internal static class HashUtility
  167. {
  168. private static string ToString(byte[] hashBytes)
  169. {
  170. string result = BitConverter.ToString(hashBytes);
  171. result = result.Replace("-", "");
  172. return result.ToLower();
  173. }
  174. #region SHA1
  175. /// <summary>
  176. /// 获取字符串的Hash值
  177. /// </summary>
  178. public static string StringSHA1(string str)
  179. {
  180. byte[] buffer = Encoding.UTF8.GetBytes(str);
  181. return BytesSHA1(buffer);
  182. }
  183. /// <summary>
  184. /// 获取文件的Hash值
  185. /// </summary>
  186. public static string FileSHA1(string filePath)
  187. {
  188. try
  189. {
  190. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  191. {
  192. return StreamSHA1(fs);
  193. }
  194. }
  195. catch (Exception e)
  196. {
  197. YooLogger.Exception(e);
  198. return string.Empty;
  199. }
  200. }
  201. /// <summary>
  202. /// 获取数据流的Hash值
  203. /// </summary>
  204. public static string StreamSHA1(Stream stream)
  205. {
  206. // 说明:创建的是SHA1类的实例,生成的是160位的散列码
  207. HashAlgorithm hash = HashAlgorithm.Create();
  208. byte[] hashBytes = hash.ComputeHash(stream);
  209. return ToString(hashBytes);
  210. }
  211. /// <summary>
  212. /// 获取字节数组的Hash值
  213. /// </summary>
  214. public static string BytesSHA1(byte[] buffer)
  215. {
  216. // 说明:创建的是SHA1类的实例,生成的是160位的散列码
  217. HashAlgorithm hash = HashAlgorithm.Create();
  218. byte[] hashBytes = hash.ComputeHash(buffer);
  219. return ToString(hashBytes);
  220. }
  221. #endregion
  222. #region MD5
  223. /// <summary>
  224. /// 获取字符串的MD5
  225. /// </summary>
  226. public static string StringMD5(string str)
  227. {
  228. byte[] buffer = Encoding.UTF8.GetBytes(str);
  229. return BytesMD5(buffer);
  230. }
  231. /// <summary>
  232. /// 获取文件的MD5
  233. /// </summary>
  234. public static string FileMD5(string filePath)
  235. {
  236. try
  237. {
  238. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  239. {
  240. return StreamMD5(fs);
  241. }
  242. }
  243. catch (Exception e)
  244. {
  245. YooLogger.Exception(e);
  246. return string.Empty;
  247. }
  248. }
  249. /// <summary>
  250. /// 获取数据流的MD5
  251. /// </summary>
  252. public static string StreamMD5(Stream stream)
  253. {
  254. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  255. byte[] hashBytes = provider.ComputeHash(stream);
  256. return ToString(hashBytes);
  257. }
  258. /// <summary>
  259. /// 获取字节数组的MD5
  260. /// </summary>
  261. public static string BytesMD5(byte[] buffer)
  262. {
  263. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  264. byte[] hashBytes = provider.ComputeHash(buffer);
  265. return ToString(hashBytes);
  266. }
  267. #endregion
  268. #region CRC32
  269. /// <summary>
  270. /// 获取字符串的CRC32
  271. /// </summary>
  272. public static string StringCRC32(string str)
  273. {
  274. byte[] buffer = Encoding.UTF8.GetBytes(str);
  275. return BytesCRC32(buffer);
  276. }
  277. /// <summary>
  278. /// 获取文件的CRC32
  279. /// </summary>
  280. public static string FileCRC32(string filePath)
  281. {
  282. try
  283. {
  284. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  285. {
  286. return StreamCRC32(fs);
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. YooLogger.Exception(e);
  292. return string.Empty;
  293. }
  294. }
  295. /// <summary>
  296. /// 获取数据流的CRC32
  297. /// </summary>
  298. public static string StreamCRC32(Stream stream)
  299. {
  300. CRC32Algorithm hash = new CRC32Algorithm();
  301. byte[] hashBytes = hash.ComputeHash(stream);
  302. return ToString(hashBytes);
  303. }
  304. /// <summary>
  305. /// 获取字节数组的CRC32
  306. /// </summary>
  307. public static string BytesCRC32(byte[] buffer)
  308. {
  309. CRC32Algorithm hash = new CRC32Algorithm();
  310. byte[] hashBytes = hash.ComputeHash(buffer);
  311. return ToString(hashBytes);
  312. }
  313. #endregion
  314. }
  315. }