using System; using System.IO; using System.Text; using UnityEngine; namespace AssetDependencyGraph { public static class Utils { static readonly string dataPath; static Utils() { dataPath = Application.dataPath; } public static string Md5(string filename) { try { FileStream fs = new FileStream(filename, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(fs); fs.Close(); return BitConverter.ToString(retVal).ToLower().Replace("-", ""); } catch { throw; } } public static void TraverseDirectory(string path, Action action, int depth = 1) { if(depth == 0) { return; } foreach (string file in Directory.EnumerateFiles(path)) { action.Invoke(file); } foreach (string directory in Directory.EnumerateDirectories(path)) { action.Invoke(directory); TraverseDirectory(directory, action, --depth); } } public static string ToUniversalPath(this string path) { return path.Replace("\\", "/"); } public static string ToUnityRelatePath(this string path) { if(path.StartsWith(dataPath.ToUniversalPath().Replace("Assets", "")) && !path.StartsWith(dataPath.ToUniversalPath() + "/Assets")) { return path.Replace(dataPath.ToUniversalPath().Replace("Assets", ""), ""); } return path.Replace(dataPath.ToUniversalPath(), "Assets"); } public static string ToUnityFullPath(this string path) { if(path.StartsWith("Packages")) { var fullPath = (dataPath.ToUniversalPath().Replace("Assets", "") + path); fullPath ??= (dataPath.ToUniversalPath().Replace("Assets", "Library/PackageCache") + path); if (!File.Exists(fullPath) && Directory.Exists(fullPath)) { Debug.LogWarning($"ToUnityFullPath failure:{path}"); } return fullPath; } return dataPath.ToUniversalPath().Replace("Assets", "") + path; } public static string ByteString(this byte[] bytes) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { stringBuilder.Append(Convert.ToString(bytes[i], 2) ); } return stringBuilder.ToString(); } } }