FileUtil.cs 14 KB

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