using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
//using GlobalSetting;
///
/// 将/StreamingAssets/Res下所有的bundle都计算md5然后生产版本文件VersionMD5.txt 。
///
public class AssetBundle_CreateMD5List {
[MenuItem("Tools/MD5Creater")]
public static void Execute()
{
Dictionary DicFileMD5 = new Dictionary();
Dictionary DicFileSize = new Dictionary();
MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();
/**
* 这里是项目路径~~本地路径~
* 注意文件夹里的结构不要去改,后边再整理吧...
*/
// string dir = System.IO.Path.Combine( Application.dataPath + "/StreamingAssets/Res");
string dir = Application.dataPath + "/StreamingAssets/Res";
#region 把所有当前平台下的包包整理一下,生成一份儿全新的md5列表~~~
string[] filesPathArray=Directory.GetFiles(dir);
Debug.Log("把所有当前平台下的包包整理一下..\nDir=" + dir+"\n一共"+filesPathArray.Length+"个文件...");
foreach (string filePath in filesPathArray)
{
/**
* 先过滤一下吧~~都在一个文件夹里~
*/
if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".txt"))
continue;
//读取这个文件,瞅瞅里边是啥~
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] hash = md5Generator.ComputeHash(file);
string strMD5 = System.BitConverter.ToString(hash);
file.Close();
System.IO.FileInfo f = new FileInfo(filePath);
string strSIZE = f.Length.ToString();
string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);
if (DicFileMD5.ContainsKey(key) == false)
{
DicFileMD5.Add(key, strMD5);
DicFileSize.Add(key, strSIZE);
}
else
Debug.LogWarning(" name = " + filePath);
}
#endregion
string savePath = Application.dataPath + "/StreamingAssets/Res";
if (Directory.Exists(savePath) == false)
Directory.CreateDirectory(savePath);
// 删除上~上一版的old数据
if (File.Exists(savePath + "/VersionMD5-old.txt"))
{
Debug.Log("删除前两版的old数据...");
System.IO.File.Delete(savePath + "/VersionMD5-old.txt");
}
// 如果上一版的数据存在,则将其名字改为VersionMD5-old.xml
if (File.Exists(savePath + "/VersionMD5.txt"))
{
Debug.Log("将上一版本的名字改为VersionMD5-old.txt...");
System.IO.File.Move(savePath + "/VersionMD5.txt", savePath + "/VersionMD5-old.txt");
}
#region 将所有检测到的bundle包,形成路径+MD5节点...
Debug.Log("将所有检测到的bundle包,形成路径+MD5节点");
XmlDocument XmlDoc = new XmlDocument();
XmlElement XmlRoot = XmlDoc.CreateElement("Files");
XmlDoc.AppendChild(XmlRoot);
foreach (KeyValuePair pair in DicFileMD5)
{
XmlElement xmlElem = XmlDoc.CreateElement("File");
XmlRoot.AppendChild(xmlElem);
xmlElem.SetAttribute("FileName", pair.Key);
xmlElem.SetAttribute("MD5", pair.Value);
xmlElem.SetAttribute("Size", DicFileSize[pair.Key]);
}
#endregion
Debug.Log("读取旧版本的MD5,和新版本的文件比较.....");
// 读取旧版本的MD5
Dictionary dicOldMD5 = ReadMD5File(savePath + "/VersionMD5-old.txt");
// 读取旧版本的size
Dictionary dicOldSize = ReadSizeFile(savePath + "/VersionMD5-old.txt");
// VersionMD5-old中有,而VersionMD5中没有的信息,手动添加到VersionMD5
foreach (KeyValuePair pair in dicOldMD5)
{
if (DicFileMD5.ContainsKey(pair.Key) == false)
DicFileMD5.Add(pair.Key, pair.Value);
}
//根据资源生成新的xml,供客户端去检测.....
XmlDoc.Save(savePath + "/VersionMD5.txt");
XmlDoc.Save(savePath+ "/" + GlobalConfig.ClientVersion + ".txt");
XmlDoc = null;
Debug.Log("保存成功.....");
}
static Dictionary ReadMD5File(string fileName)
{
Dictionary DicMD5 = new Dictionary();
// 如果文件不存在,则直接返回
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;
string file = (node as XmlElement).GetAttribute("FileName");
string md5 = (node as XmlElement).GetAttribute("MD5");
if (DicMD5.ContainsKey(file) == false)
{
DicMD5.Add(file, md5);
}
}
XmlRoot = null;
XmlDoc = null;
return DicMD5;
}
static Dictionary ReadSizeFile(string fileName)
{
Dictionary DicSize = new Dictionary();
// 如果文件不存在,则直接返回
if (System.IO.File.Exists(fileName) == false)
return DicSize;
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(fileName);
XmlElement XmlRoot = XmlDoc.DocumentElement;
foreach (XmlNode node in XmlRoot.ChildNodes)
{
if ((node is XmlElement) == false)
continue;
string file = (node as XmlElement).GetAttribute("FileName");
string size = (node as XmlElement).GetAttribute("SIZE");
if (DicSize.ContainsKey(file) == false)
{
DicSize.Add(file, size );
}
}
XmlRoot = null;
XmlDoc = null;
return DicSize;
}
}