76 lines
2.4 KiB
C#
Raw Normal View History

2025-04-10 20:39:04 +08:00
using System;
using System.IO;
using UnityEngine;
namespace AssetDependencyGraph
{
public static class Utils
{
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<string> 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(Application.dataPath.ToUniversalPath().Replace("Assets", "")) && !path.StartsWith(Application.dataPath.ToUniversalPath() + "/Assets"))
{
return path.Replace(Application.dataPath.ToUniversalPath().Replace("Assets", ""), "");
}
return path.Replace(Application.dataPath.ToUniversalPath(), "Assets");
}
public static string ToUnityFullPath(this string path)
{
if(path.StartsWith("Packages"))
{
var fullPath = (Application.dataPath.ToUniversalPath().Replace("Assets", "") + path);
fullPath ??= (Application.dataPath.ToUniversalPath().Replace("Assets", "Library/PackageCache") + path);
if (!File.Exists(fullPath) && Directory.Exists(fullPath))
{
Debug.LogWarning($"ToUnityFullPath failure:{path}");
}
return fullPath;
}
return Application.dataPath.ToUniversalPath().Replace("Assets", "") + path;
}
}
}