using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
public static class AssetUtils
{
private static readonly MD5 _md5 = MD5.Create();
private static readonly char[] _seperateChars = {'/', '\\'};
///
/// Unity文件路径向上一级,可用于取得文件路径
///
public static string MoveUp(this string path)
{
path = path.TrimEnd(_seperateChars);
var index = path.LastIndexOfAny(_seperateChars);
path = index >= 0 ? path.Remove(index) : string.Empty;
return path;
}
///
/// Unity文件路径到下一级
///
public static string Open(this string path, string next)
{
return string.IsNullOrEmpty(next) ? path : path.TrimEnd(_seperateChars) + "/" + next;
}
///
/// 路径转化为超链接路径(Unity资源路径)格式
///
public static string ToUrl(this string path)
{
return path.Replace("\\", "/");
}
public static string GetFileMd5(string filePath)
{
return GetMd5FromBytes(File.ReadAllBytes(filePath));
}
public static string GetTextMd5(string text)
{
return GetMd5FromBytes(Encoding.UTF8.GetBytes(text));
}
private static string GetMd5FromBytes(byte[] bytes)
{
return string.Concat(_md5.ComputeHash(bytes).Select(x => x.ToString("x2")).ToArray()).ToLower();
}
public static string[] TextToLines(string text)
{
return Regex.Split(text, "\\r\\n|\\r|\\n");
}
}