生成Version更新对比文件MD5

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;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值