using UnityEngine;
using System.IO;
using System.Text;
using System.Linq;
using UnityEditor;
using System.Security.Cryptography;
public class VersionMd5Util
{
public static string abPath = Path.Combine(Application.streamingAssetsPath, "AB");//AsserBundles资源
public static string hotFixPath = Path.Combine(Application.streamingAssetsPath, "fishhotfix");//热更新文件
[MenuItem("EngineTools/CreateVersionMd5", false, 1)]
public static void Create()
{
CreateUpdateTXT(abPath);
AppendUpdateTXT(hotFixPath);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Tips", "Create MD5 Finish.", "Current");
}
/// <summary>
/// 创建Version文本
/// </summary>
public static void CreateVersionMd5(string path)
{
CreateUpdateTXT(path);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Tips", "Create MD5 Finish.", "Current");
}
private static void CreateUpdateTXT(string path)
{
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => !s.EndsWith(".manifest") && !s.EndsWith(".meta"));
StringBuilder stringBuilder = new StringBuilder();
foreach (string filePath in files)
{
string md5 = BuildFileMd5(filePath);
byte[] data = File.ReadAllBytes(filePath);
string fpath = Path.Combine(Application.streamingAssetsPath, "AB\\");
string fileName = filePath.Substring(filePath.LastIndexOf(fpath) + (fpath).Length);
stringBuilder.AppendLine(string.Format("{0}:{1}:{2}", fileName, md5, (float)data.Length / 1024));
}
string updatePath = Path.Combine(Application.streamingAssetsPath, "Version/update.txt");
WriteTXT(updatePath, stringBuilder.ToString());
}
private static void WriteTXT(string path, string content)
{
string directory = Path.GetDirectoryName(path);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream fs = File.Create(path))
{
StreamWriter sw = new StreamWriter(fs, Encoding.ASCII);
try
{
sw.Write(content);
sw.Close();
fs.Close();
fs.Dispose();
}
catch (IOException e)
{
Debug.Log(e.Message);
}
}
}
/// <summary>
/// 更新Version文本
/// </summary>
public static void UpdateVersion(string path)
{
AppendUpdateTXT(path);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Tips", "Update MD5 Finish.", "Current");
}
private static void AppendUpdateTXT(string path)
{
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Where(s => !s.EndsWith(".manifest") && !s.EndsWith(".meta"));
StringBuilder stringBuilder = new StringBuilder();
foreach (string filePath in files)
{
string md5 = BuildFileMd5(filePath);
byte[] data = File.ReadAllBytes(filePath);
string fpath = Path.Combine(Application.streamingAssetsPath, "fishhotfix\\");
string fileName = filePath.Substring(filePath.LastIndexOf(fpath) + (fpath).Length);
stringBuilder.AppendLine(string.Format("{0}:{1}:{2}", fileName, md5, (float)data.Length / 1024));
}
string updatePath = Path.Combine(Application.streamingAssetsPath, "Version/update.txt");
AppendWriteTXT(updatePath, stringBuilder.ToString());
}
private static void AppendWriteTXT(string path, string content)
{
string directory = Path.GetDirectoryName(path);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (File.Exists(path))
{
StreamWriter sw;
FileInfo fi = new FileInfo(path);
//sw = fi.CreateText();//直接重新写入
sw = fi.AppendText();//在原文件后面追加内容
sw.WriteLine(content);
sw.Close();
sw.Dispose();
}
else
{
Debug.LogError("Version 不存在!");
}
}
private static string BuildFileMd5(string filePath)
{
string fileMd5 = string.Empty;
try
{
using (FileStream fs = File.OpenRead(filePath))
{
MD5 md5 = MD5.Create();
byte[] fileMd5Bytes = md5.ComputeHash(fs);// 计算FileStream 对象的哈希值
fileMd5 = System.BitConverter.ToString(fileMd5Bytes).Replace("-", "").ToLower();
}
}
catch (System.Exception ex)
{
Debug.LogError(ex);
}
return fileMd5;
}
}