FileUtil.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using UnityEngine;
  8. namespace GFGEditor
  9. {
  10. public class FileUtil
  11. {
  12. public static bool CheckPathInParent(string path, string parent)
  13. {
  14. path = path.Replace('\\', '/');
  15. path = path + "/";
  16. parent = parent.Replace('\\', '/');
  17. parent = parent + "/";
  18. parent = Regex.Replace(parent, "//", "/");
  19. return path.Contains(parent);
  20. }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="dirPath"></param>
  25. /// <param name="excludeDirs"></param>,路径比须从Asset起,例如"Assets/Res/Effect"
  26. /// <param name="action"></param>
  27. public static void ForeachFileInDir(string dirPath, List<string> excludeDirs, Action<string> action)
  28. {
  29. List<string> excludeDirPaths = new List<string>();
  30. if (excludeDirs != null)
  31. {
  32. foreach (var excDir in excludeDirs)
  33. {
  34. var t = Environment.CurrentDirectory + "/" + excDir;
  35. t = t.Replace('\\', '/');
  36. excludeDirPaths.Add(t);
  37. }
  38. }
  39. var t1 = dirPath.Replace('\\', '/');
  40. if (Directory.Exists(dirPath) && !excludeDirPaths.Contains(t1))
  41. {
  42. var files = Directory.GetFiles(dirPath);
  43. var dirs = Directory.GetDirectories(dirPath);
  44. foreach (var file in files)
  45. {
  46. action?.Invoke(file);
  47. }
  48. foreach (var dir in dirs)
  49. {
  50. ForeachFileInDir(dir, excludeDirs, action);
  51. }
  52. }
  53. }
  54. public static void ForeachDirInDir(string dirPath, Action<string> action)
  55. {
  56. if (Directory.Exists(dirPath))
  57. {
  58. var dirs = Directory.GetDirectories(dirPath);
  59. foreach (var dir in dirs)
  60. {
  61. action?.Invoke(dir);
  62. }
  63. }
  64. }
  65. public static List<string> CopyFilesTo(string sourcePath, string targetPath, string saveName, string[] includeExtensionNames = null, string regular = null)
  66. {
  67. List<string> result = new List<string>();
  68. if (Directory.Exists(sourcePath))
  69. {
  70. var files = Directory.GetFiles(sourcePath);
  71. var dirs = Directory.GetDirectories(sourcePath);
  72. Dictionary<string, string> sourceImageMD5 = new Dictionary<string, string>();
  73. sourceImageMD5 = ImagesClip.ReadSourceImagesMD5(saveName);
  74. foreach (var file in files)
  75. {
  76. var fileName = Path.GetFileName(file);
  77. if (fileName.Trim() == fileName && CheckStringByRegular(fileName, regular))
  78. {
  79. string extensionName = Path.GetExtension(file);
  80. if (includeExtensionNames == null || Array.IndexOf(includeExtensionNames, extensionName) >= 0)
  81. {
  82. string targetFilePath = targetPath + fileName;
  83. if (!File.Exists(targetFilePath) || !Equals(file, targetFilePath))
  84. {
  85. if (!sourceImageMD5.ContainsKey(file) || sourceImageMD5[file] != FileUtil.md5file(file))
  86. {
  87. sourceImageMD5[file] = FileUtil.md5file(file);
  88. }
  89. File.Copy(file, targetFilePath, true);
  90. result.Add(file);
  91. }
  92. }
  93. }
  94. else
  95. {
  96. Debug.LogError($"请检查文件名 {file}");
  97. }
  98. }
  99. foreach (var dir in dirs)
  100. {
  101. List<string> tempResult = CopyFilesTo(dir, targetPath, saveName, includeExtensionNames, regular);
  102. result.AddRange(tempResult);
  103. }
  104. ImagesClip.WriteSourceImagesMD5(sourceImageMD5, saveName);
  105. }
  106. return result;
  107. }
  108. public static bool CheckStringByRegular(string str, string regular)
  109. {
  110. str = Path.GetFileNameWithoutExtension(str);
  111. if (regular == null)//不进行正则检测
  112. {
  113. return true;
  114. }
  115. if (System.Text.RegularExpressions.Regex.IsMatch(str, regular))
  116. {
  117. return true;
  118. }
  119. return false;
  120. }
  121. public static List<string> CopyDirsAndFilesInSubDirTo(string sourcePath, string targetPath, string[] excludeExtensionNames = null, string regular = null)
  122. {
  123. List<string> result = new List<string>();
  124. if (Directory.Exists(sourcePath))
  125. {
  126. var dirs = Directory.GetDirectories(sourcePath);
  127. foreach (var dir in dirs)
  128. {
  129. var dirName = Path.GetFileName(dir);
  130. var currTargetPath = targetPath + dirName + "/";
  131. if (!CheckStringByRegular(dirName, regular)) continue;
  132. if (!Directory.Exists(currTargetPath))
  133. {
  134. Directory.CreateDirectory(currTargetPath);
  135. }
  136. List<string> tempResult = CopyFilesAndDirsTo(dir, currTargetPath, excludeExtensionNames);
  137. result.AddRange(tempResult);
  138. }
  139. }
  140. return result;
  141. }
  142. public static List<string> CopyFilesAndDirsTo(string sourcePath, string targetPath, string[] excludeExtensionNames = null)
  143. {
  144. List<string> result = new List<string>();
  145. if (Directory.Exists(sourcePath))
  146. {
  147. var files = Directory.GetFiles(sourcePath);
  148. var dirs = Directory.GetDirectories(sourcePath);
  149. foreach (var file in files)
  150. {
  151. var fileName = Path.GetFileName(file);
  152. if (fileName.Trim() == fileName)
  153. {
  154. string extensionName = Path.GetExtension(file);
  155. if (!FileNameContainsArrayElement(fileName, excludeExtensionNames))
  156. {
  157. string targetFilePath = targetPath + fileName;
  158. if (!File.Exists(targetFilePath) || !Equals(file, targetFilePath))
  159. {
  160. File.Copy(file, targetFilePath, true);
  161. result.Add(file);
  162. }
  163. }
  164. }
  165. else
  166. {
  167. Debug.LogError($"请检查文件名 {file}");
  168. }
  169. }
  170. foreach (var dir in dirs)
  171. {
  172. var dirName = Path.GetFileName(dir);
  173. var currTargetPath = targetPath + dirName + "/";
  174. if (!Directory.Exists(currTargetPath))
  175. {
  176. Directory.CreateDirectory(currTargetPath);
  177. }
  178. List<string> tempResult = CopyFilesAndDirsTo(dir, currTargetPath, excludeExtensionNames);
  179. result.AddRange(tempResult);
  180. }
  181. }
  182. return result;
  183. }
  184. public static string md5file(string file)
  185. {
  186. try
  187. {
  188. FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
  189. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  190. byte[] retVal = md5.ComputeHash(fs);
  191. fs.Close();
  192. StringBuilder sb = new StringBuilder();
  193. for (int i = 0; i < retVal.Length; i++)
  194. {
  195. sb.Append(retVal[i].ToString("x2"));
  196. }
  197. return sb.ToString();
  198. }
  199. catch (Exception ex)
  200. {
  201. throw new Exception("md5file() fail, error:" + ex.Message);
  202. }
  203. }
  204. public static bool Equals(string fileA, string fileB)
  205. {
  206. return md5file(fileA) == md5file(fileB);
  207. }
  208. public static bool FileNameContainsArrayElement(string fileName, string[] excludeExtensionNames = null)
  209. {
  210. if (excludeExtensionNames != null)
  211. {
  212. foreach (var item in excludeExtensionNames)
  213. {
  214. if (fileName.Contains(item))
  215. {
  216. return true;
  217. }
  218. }
  219. }
  220. return false;
  221. }
  222. public static void DeleteUnnecessaryImage(string targetPath, string saveName = "")
  223. {
  224. if (saveName != "")
  225. {
  226. DeleteUnnecessaryImageRes(targetPath, saveName);
  227. DeleteUnnecessaryImageMD5(saveName);
  228. }
  229. if (targetPath == ImportArtResTool.DressUpTargetPath)
  230. {
  231. DeleteUnnecessaryImagePos(ImportArtResTool.DressUpTargetPath);
  232. // DeleteUnnecessaryImageMD5(ImportArtResTool.DressUpMd5FileSaveName);
  233. }
  234. Debug.Log(targetPath + "清除完成");
  235. }
  236. //清除无资源的Md5数据
  237. public static void DeleteUnnecessaryImageMD5(string saveName)
  238. {
  239. List<string> rKeys = new List<string>();
  240. Dictionary<string, string> sourceResMD5 = new Dictionary<string, string>();
  241. sourceResMD5 = ImagesClip.ReadSourceImagesMD5(saveName);
  242. ICollection keys = sourceResMD5.Keys;
  243. foreach (string key in keys)
  244. {
  245. if (!File.Exists(key))
  246. {
  247. rKeys.Add(key);
  248. }
  249. }
  250. for (int i = 0; i < rKeys.Count; i++)
  251. {
  252. sourceResMD5.Remove(rKeys[i]);
  253. }
  254. ImagesClip.WriteSourceImagesMD5(sourceResMD5, saveName);
  255. }
  256. public static void DeleteUnnecessaryImageRes(string targetPath, string saveName)
  257. {
  258. string[] includeExtensionNames = new string[] { ".png", ".jpg" };
  259. if (!Directory.Exists(targetPath))
  260. {
  261. Debug.Log(targetPath + "不存在!!!!!");
  262. return;
  263. }
  264. var files = Directory.GetFiles(targetPath);
  265. foreach (var file in files)
  266. {
  267. if (file.Trim() != file)
  268. {
  269. Debug.LogError($"请检查文件名 {file}");
  270. continue;
  271. }
  272. string fileName = Path.GetFileName(file);
  273. Dictionary<string, string> sourceResMD5 = new Dictionary<string, string>();
  274. sourceResMD5 = ImagesClip.ReadSourceImagesMD5(saveName);
  275. ICollection keys = sourceResMD5.Keys;
  276. foreach (string key in keys)
  277. {
  278. if (!File.Exists(key))
  279. {
  280. string sourceName = Path.GetFileName(key);
  281. string targetFilePath = targetPath + fileName;
  282. if (sourceName == fileName)
  283. {
  284. File.Delete(targetFilePath);
  285. }
  286. }
  287. }
  288. }
  289. Debug.Log(targetPath + "清除完成!");
  290. }
  291. //清除冗余位置文件
  292. public static void DeleteUnnecessaryImagePos(string targetPath)
  293. {
  294. string[] includeExtensionNames = new string[] { ".png", ".jpg" };
  295. var files = Directory.GetFiles(targetPath);
  296. foreach (var file in files)
  297. {
  298. if (file.Trim() != file)
  299. {
  300. Debug.LogError($"请检查文件名 {file}");
  301. continue;
  302. }
  303. string extensionName = Path.GetExtension(file);
  304. string fileName = Path.GetFileName(file).Split('.')[0]; ;
  305. string targetFilePath = targetPath + fileName;
  306. if (extensionName == ".bytes" && !File.Exists(targetFilePath + includeExtensionNames[0]) && !File.Exists(targetFilePath + includeExtensionNames[1]))
  307. {
  308. File.Delete(targetPath + Path.GetFileName(file));
  309. }
  310. }
  311. Debug.Log("位置文件清除完成!");
  312. }
  313. public static void DeleteUnnecessaryAnimation(string localDir, string sourceDirs)
  314. {
  315. if (Directory.Exists(localDir))
  316. {
  317. var localFiles = Directory.GetFiles(localDir);
  318. var localDirs = Directory.GetDirectories(localDir);
  319. var souFiles = Directory.GetFiles(sourceDirs);
  320. var souDirs = Directory.GetDirectories(sourceDirs);
  321. foreach (var lfile in localFiles)
  322. {
  323. var lfileName = Path.GetFileName(lfile);
  324. bool existFile = false;
  325. foreach (var sfile in souFiles)
  326. {
  327. var sfileName = Path.GetFileName(sfile);
  328. if (lfileName == sfileName)
  329. {
  330. existFile = true;
  331. break;
  332. }
  333. }
  334. if (!existFile)
  335. {
  336. File.Delete(lfile);
  337. }
  338. }
  339. foreach (var lDir in localDirs)
  340. {
  341. string lDirName = Path.GetFileName(lDir);
  342. if (lDirName == "Card") continue;
  343. bool needDelete = false;
  344. foreach (var sDir in souDirs)
  345. {
  346. string sDirName = Path.GetFileName(sDir);
  347. if (lDirName == sDirName)
  348. {
  349. needDelete = true;
  350. DeleteUnnecessaryAnimation(lDir, sDir);
  351. }
  352. }
  353. if (!needDelete)
  354. {
  355. Directory.Delete(lDir, true);
  356. }
  357. }
  358. }
  359. }
  360. }
  361. }