123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using UnityEngine;
- using UnityEditor;
- using System.IO;
- using System.Xml;
- using System.Collections;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- //using GlobalSetting;
- /// <summary>
- /// 将/StreamingAssets/Res下所有的bundle都计算md5然后生产版本文件VersionMD5.txt 。
- /// </summary>
- public class AssetBundle_CreateMD5List {
- [MenuItem("Tools/MD5Creater")]
- public static void Execute()
- {
- Dictionary<string, string> DicFileMD5 = new Dictionary<string, string>();
- Dictionary<string, string> DicFileSize = new Dictionary<string, string>();
- 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("<Two File has the same name> 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<string, string> 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<string, string> dicOldMD5 = ReadMD5File(savePath + "/VersionMD5-old.txt");
- // 读取旧版本的size
- Dictionary<string, string> dicOldSize = ReadSizeFile(savePath + "/VersionMD5-old.txt");
- // VersionMD5-old中有,而VersionMD5中没有的信息,手动添加到VersionMD5
- foreach (KeyValuePair<string, string> 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<string, string> ReadMD5File(string fileName)
- {
- Dictionary<string, string> DicMD5 = new Dictionary<string, 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;
- 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<string, string> ReadSizeFile(string fileName)
- {
- Dictionary<string, string> DicSize = new Dictionary<string, string>();
- // 如果文件不存在,则直接返回
- 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;
- }
- }
|