Unity5.X AssetBundle创建MD5文件

来源:互联网 发布:seo中h1是什么意思 编辑:程序博客网 时间:2024/06/06 03:56
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;


public class CreatMD5 : MonoBehaviour {




    public static void  CreatMD5XML()
    {
        Dictionary<string, List<string>> DicFileMD5 = new Dictionary<string, List<string>>();
        MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();


        //需要创建MD5文件的文件夹路径
        string dir = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");
        //MD5保存路径
        string savePath = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");






        foreach (string filePath in Directory.GetFiles(dir))
        {
            if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".xml"))
                continue;


            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] hash = md5Generator.ComputeHash(file);
            List<string> tempList = new List<string>();
            string strMD5 = System.BitConverter.ToString(hash);
            string size = file.Length.ToString();
            tempList.Add(strMD5);
            tempList.Add(size);


            file.Close();


            string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);


            if (DicFileMD5.ContainsKey(key) == false)
                DicFileMD5.Add(key, tempList);
            else
                Debug.LogWarning("<Two File has the same name> name = " + filePath);
        }




        if (Directory.Exists(savePath) == false)
            Directory.CreateDirectory(savePath);




        XmlDocument XmlDoc = new XmlDocument();
        XmlElement XmlRoot = XmlDoc.CreateElement("Files");
        XmlDoc.AppendChild(XmlRoot);
        foreach (KeyValuePair<string, List<string>> pair in DicFileMD5)
        {
            XmlElement xmlElem = XmlDoc.CreateElement("File");
            XmlRoot.AppendChild(xmlElem);


            xmlElem.SetAttribute("FileName", pair.Key);
            xmlElem.SetAttribute("MD5", pair.Value[0]);
            xmlElem.SetAttribute("Size", pair.Value[1]);
        }


        XmlDoc.Save(savePath + "/VersionMD5.xml");
        XmlDoc = null;




        AssetDatabase.Refresh();
    }


    static Dictionary<string, List<string>> ReadMD5File(string fileName)
    {
        Dictionary<string, List<string>> DicMD5 = new Dictionary<string, List<string>>();


        // 如果文件不存在,则直接返回
        if (System.IO.File.Exists(fileName) == false)
            return DicMD5;


        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(fileName);
        XmlElement XmlRoot = XmlDoc.DocumentElement;


        foreach (XmlNode node in XmlRoot.ChildNodes)
        {
            if ((node is XmlElement) == false)
                continue;


            List<string> tempList = new List<string>();
            string file = (node as XmlElement).GetAttribute("FileName");
            string md5 = (node as XmlElement).GetAttribute("MD5");
            string size = (node as XmlElement).GetAttribute("Size");
            tempList.Add(md5);
            tempList.Add(size);


            if (DicMD5.ContainsKey(file) == false)
            {
                DicMD5.Add(file, tempList);
            }
        }


        XmlRoot = null;
        XmlDoc = null;


        return DicMD5;
    }
}