123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Text.RegularExpressions;
- using UnityEngine;
- namespace GFGEditor
- {
- public class FileUtil
- {
- public static bool CheckPathInParent(string path, string parent)
- {
- path = path.Replace('\\', '/');
- path = path + "/";
- parent = parent.Replace('\\', '/');
- parent = parent + "/";
- parent = Regex.Replace(parent, "//", "/");
- return path.Contains(parent);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dirPath"></param>
- /// <param name="excludeDirs"></param>,路径比须从Asset起,例如"Assets/Res/Effect"
- /// <param name="action"></param>
- public static void ForeachFileInDir(string dirPath, List<string> excludeDirs, Action<string> action)
- {
- List<string> excludeDirPaths = new List<string>();
- if (excludeDirs != null)
- {
- foreach (var excDir in excludeDirs)
- {
- var t = Environment.CurrentDirectory + "/" + excDir;
- t = t.Replace('\\', '/');
- excludeDirPaths.Add(t);
- }
- }
- var t1 = dirPath.Replace('\\', '/');
- if (Directory.Exists(dirPath) && !excludeDirPaths.Contains(t1))
- {
- var files = Directory.GetFiles(dirPath);
- var dirs = Directory.GetDirectories(dirPath);
- foreach (var file in files)
- {
- action?.Invoke(file);
- }
- foreach (var dir in dirs)
- {
- ForeachFileInDir(dir, excludeDirs, action);
- }
- }
- }
- public static void ForeachDirInDir(string dirPath, Action<string> action)
- {
- if (Directory.Exists(dirPath))
- {
- var dirs = Directory.GetDirectories(dirPath);
- foreach (var dir in dirs)
- {
- action?.Invoke(dir);
- }
- }
- }
- public static List<string> CopyFilesTo(string sourcePath, string targetPath, string saveName, string[] includeExtensionNames = null, string regular = null)
- {
- List<string> result = new List<string>();
- if (Directory.Exists(sourcePath))
- {
- var files = Directory.GetFiles(sourcePath);
- var dirs = Directory.GetDirectories(sourcePath);
- Dictionary<string, string> sourceImageMD5 = new Dictionary<string, string>();
- sourceImageMD5 = ImagesClip.ReadSourceImagesMD5(saveName);
- foreach (var file in files)
- {
- var fileName = Path.GetFileName(file);
- if (fileName.Trim() == fileName && CheckStringByRegular(fileName, regular))
- {
- string extensionName = Path.GetExtension(file);
- if (includeExtensionNames == null || Array.IndexOf(includeExtensionNames, extensionName) >= 0)
- {
- string targetFilePath = targetPath + fileName;
- if (!File.Exists(targetFilePath) || !Equals(file, targetFilePath))
- {
- if (!sourceImageMD5.ContainsKey(file) || sourceImageMD5[file] != FileUtil.md5file(file))
- {
- sourceImageMD5[file] = FileUtil.md5file(file);
- }
- File.Copy(file, targetFilePath, true);
- result.Add(file);
- }
- }
- }
- else
- {
- Debug.LogError($"请检查文件名 {file}");
- }
- }
- foreach (var dir in dirs)
- {
- List<string> tempResult = CopyFilesTo(dir, targetPath, saveName, includeExtensionNames, regular);
- result.AddRange(tempResult);
- }
- ImagesClip.WriteSourceImagesMD5(sourceImageMD5, saveName);
- }
- return result;
- }
- public static bool CheckStringByRegular(string str, string regular)
- {
- str = Path.GetFileNameWithoutExtension(str);
- if (regular == null)//不进行正则检测
- {
- return true;
- }
- if (System.Text.RegularExpressions.Regex.IsMatch(str, regular))
- {
- return true;
- }
- return false;
- }
- public static List<string> CopyDirsAndFilesInSubDirTo(string sourcePath, string targetPath, string[] excludeExtensionNames = null, string regular = null)
- {
- List<string> result = new List<string>();
- if (Directory.Exists(sourcePath))
- {
- var dirs = Directory.GetDirectories(sourcePath);
- foreach (var dir in dirs)
- {
- var dirName = Path.GetFileName(dir);
- var currTargetPath = targetPath + dirName + "/";
- if (!CheckStringByRegular(dirName, regular)) continue;
- if (!Directory.Exists(currTargetPath))
- {
- Directory.CreateDirectory(currTargetPath);
- }
- List<string> tempResult = CopyFilesAndDirsTo(dir, currTargetPath, excludeExtensionNames);
- result.AddRange(tempResult);
- }
- }
- return result;
- }
- public static List<string> CopyFilesAndDirsTo(string sourcePath, string targetPath, string[] excludeExtensionNames = null)
- {
- List<string> result = new List<string>();
- if (Directory.Exists(sourcePath))
- {
- var files = Directory.GetFiles(sourcePath);
- var dirs = Directory.GetDirectories(sourcePath);
- foreach (var file in files)
- {
- var fileName = Path.GetFileName(file);
- if (fileName.Trim() == fileName)
- {
- string extensionName = Path.GetExtension(file);
- if (!FileNameContainsArrayElement(fileName, excludeExtensionNames))
- {
- string targetFilePath = targetPath + fileName;
- if (!File.Exists(targetFilePath) || !Equals(file, targetFilePath))
- {
- File.Copy(file, targetFilePath, true);
- result.Add(file);
- }
- }
- }
- else
- {
- Debug.LogError($"请检查文件名 {file}");
- }
- }
- foreach (var dir in dirs)
- {
- var dirName = Path.GetFileName(dir);
- var currTargetPath = targetPath + dirName + "/";
- if (!Directory.Exists(currTargetPath))
- {
- Directory.CreateDirectory(currTargetPath);
- }
- List<string> tempResult = CopyFilesAndDirsTo(dir, currTargetPath, excludeExtensionNames);
- result.AddRange(tempResult);
- }
- }
- return result;
- }
- public static string md5file(string file)
- {
- try
- {
- FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
- System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(fs);
- fs.Close();
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- return sb.ToString();
- }
- catch (Exception ex)
- {
- throw new Exception("md5file() fail, error:" + ex.Message);
- }
- }
- public static bool Equals(string fileA, string fileB)
- {
- return md5file(fileA) == md5file(fileB);
- }
- public static bool FileNameContainsArrayElement(string fileName, string[] excludeExtensionNames = null)
- {
- if (excludeExtensionNames != null)
- {
- foreach (var item in excludeExtensionNames)
- {
- if (fileName.Contains(item))
- {
- return true;
- }
- }
- }
- return false;
- }
- public static void DeleteUnnecessaryImage(string targetPath, string[] saveNames)
- {
- if (saveNames.Length > 0)
- {
- DeleteUnnecessaryImageMD5(saveNames);
- DeleteUnnecessaryImageRes(targetPath, saveNames);
- }
- if (targetPath == ImportArtResTool.DressUpTargetPath)
- {
- DeleteUnnecessaryImagePos(ImportArtResTool.DressUpTargetPath);
- // DeleteUnnecessaryImageMD5(ImportArtResTool.DressUpMd5FileSaveName);
- }
- Debug.Log(targetPath + "清除完成");
- }
- //清除无资源的Md5数据
- public static void DeleteUnnecessaryImageMD5(string[] saveNames)
- {
- for (int j = 0; j < saveNames.Length; j++)
- {
- string saveName = saveNames[j];
- List<string> rKeys = new List<string>();
- Dictionary<string, string> sourceResMD5 = new Dictionary<string, string>();
- sourceResMD5 = ImagesClip.ReadSourceImagesMD5(saveName);
- ICollection keys = sourceResMD5.Keys;
- foreach (string key in keys)
- {
- if (!File.Exists(key))
- {
- rKeys.Add(key);
- }
- }
- for (int i = 0; i < rKeys.Count; i++)
- {
- sourceResMD5.Remove(rKeys[i]);
- }
- ImagesClip.WriteSourceImagesMD5(sourceResMD5, saveName);
- }
- }
- public static void DeleteUnnecessaryImageRes(string targetPath, string[] saveNames)
- {
- string[] includeExtensionNames = new string[] { ".png", ".jpg" };
- if (!Directory.Exists(targetPath))
- {
- Debug.Log(targetPath + "不存在!!!!!");
- return;
- }
- var files = Directory.GetFiles(targetPath);
- foreach (var file in files)//遍历项目中的所有资源
- {
- if (file.Trim() != file)
- {
- Debug.LogError($"请检查文件名 {file}");
- continue;
- }
- string fileName = Path.GetFileName(file);
- string extensionName = Path.GetExtension(file);
- if (extensionName == ".meta" || extensionName == ".bytes") continue;
- bool has = false;
- for (int i = 0; i < saveNames.Length; i++)
- {
- string saveName = saveNames[i];
- Dictionary<string, string> sourceResMD5 = new Dictionary<string, string>();
- sourceResMD5 = ImagesClip.ReadSourceImagesMD5(saveName);
- ICollection keys = sourceResMD5.Keys;
- foreach (string key in keys)//遍历MD5中记录的所有正式资源路径
- {
- // if (!File.Exists(key))//记录的路径在正式资源里不存在
- // {
- string sourceName = Path.GetFileName(key);
- if (sourceName == fileName)
- {
- has = true;
- break;
- }
- }
- }
- if (!has) File.Delete(targetPath + fileName);
- }
- Debug.Log(targetPath + "清除完成!");
- }
- //清除冗余位置文件
- public static void DeleteUnnecessaryImagePos(string targetPath)
- {
- string[] includeExtensionNames = new string[] { ".png", ".jpg" };
- var files = Directory.GetFiles(targetPath);
- foreach (var file in files)
- {
- if (file.Trim() != file)
- {
- Debug.LogError($"请检查文件名 {file}");
- continue;
- }
- string extensionName = Path.GetExtension(file);
- string fileName = Path.GetFileName(file).Split('.')[0]; ;
- string targetFilePath = targetPath + fileName;
- if (extensionName == ".bytes" && !File.Exists(targetFilePath + includeExtensionNames[0]) && !File.Exists(targetFilePath + includeExtensionNames[1]))
- {
- File.Delete(targetPath + Path.GetFileName(file));
- }
- }
- Debug.Log("位置文件清除完成!");
- }
- public static void DeleteUnnecessaryAnimation(string localDir, string sourceDirs)
- {
- if (Directory.Exists(localDir))
- {
- var localFiles = Directory.GetFiles(localDir);
- var localDirs = Directory.GetDirectories(localDir);
- var souFiles = Directory.GetFiles(sourceDirs);
- var souDirs = Directory.GetDirectories(sourceDirs);
- foreach (var lfile in localFiles)
- {
- var lfileName = Path.GetFileName(lfile);
- bool existFile = false;
- foreach (var sfile in souFiles)
- {
- var sfileName = Path.GetFileName(sfile);
- if (lfileName == sfileName)
- {
- existFile = true;
- break;
- }
- }
- if (!existFile)
- {
- File.Delete(lfile);
- }
- }
- foreach (var lDir in localDirs)
- {
- string lDirName = Path.GetFileName(lDir);
- // if (lDirName == "Card") continue;
- bool needDelete = false;
- foreach (var sDir in souDirs)
- {
- string sDirName = Path.GetFileName(sDir);
- if (lDirName == sDirName)
- {
- needDelete = true;
- DeleteUnnecessaryAnimation(lDir, sDir);
- }
- }
- if (!needDelete)
- {
- Directory.Delete(lDir, true);
- }
- }
- }
- }
- }
- }
|