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