AssetBundle_CreateMD5List.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Security.Cryptography;
  8. //using GlobalSetting;
  9. /// <summary>
  10. /// 将/StreamingAssets/Res下所有的bundle都计算md5然后生产版本文件VersionMD5.txt 。
  11. /// </summary>
  12. public class AssetBundle_CreateMD5List {
  13. [MenuItem("Tools/MD5Creater")]
  14. public static void Execute()
  15. {
  16. Dictionary<string, string> DicFileMD5 = new Dictionary<string, string>();
  17. Dictionary<string, string> DicFileSize = new Dictionary<string, string>();
  18. MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();
  19. /**
  20. * 这里是项目路径~~本地路径~
  21. * 注意文件夹里的结构不要去改,后边再整理吧...
  22. */
  23. // string dir = System.IO.Path.Combine( Application.dataPath + "/StreamingAssets/Res");
  24. string dir = Application.dataPath + "/StreamingAssets/Res";
  25. #region 把所有当前平台下的包包整理一下,生成一份儿全新的md5列表~~~
  26. string[] filesPathArray=Directory.GetFiles(dir);
  27. Debug.Log("把所有当前平台下的包包整理一下..\nDir=" + dir+"\n一共"+filesPathArray.Length+"个文件...");
  28. foreach (string filePath in filesPathArray)
  29. {
  30. /**
  31. * 先过滤一下吧~~都在一个文件夹里~
  32. */
  33. if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".txt"))
  34. continue;
  35. //读取这个文件,瞅瞅里边是啥~
  36. FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  37. byte[] hash = md5Generator.ComputeHash(file);
  38. string strMD5 = System.BitConverter.ToString(hash);
  39. file.Close();
  40. System.IO.FileInfo f = new FileInfo(filePath);
  41. string strSIZE = f.Length.ToString();
  42. string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);
  43. if (DicFileMD5.ContainsKey(key) == false)
  44. {
  45. DicFileMD5.Add(key, strMD5);
  46. DicFileSize.Add(key, strSIZE);
  47. }
  48. else
  49. Debug.LogWarning("<Two File has the same name> name = " + filePath);
  50. }
  51. #endregion
  52. string savePath = Application.dataPath + "/StreamingAssets/Res";
  53. if (Directory.Exists(savePath) == false)
  54. Directory.CreateDirectory(savePath);
  55. // 删除上~上一版的old数据
  56. if (File.Exists(savePath + "/VersionMD5-old.txt"))
  57. {
  58. Debug.Log("删除前两版的old数据...");
  59. System.IO.File.Delete(savePath + "/VersionMD5-old.txt");
  60. }
  61. // 如果上一版的数据存在,则将其名字改为VersionMD5-old.xml
  62. if (File.Exists(savePath + "/VersionMD5.txt"))
  63. {
  64. Debug.Log("将上一版本的名字改为VersionMD5-old.txt...");
  65. System.IO.File.Move(savePath + "/VersionMD5.txt", savePath + "/VersionMD5-old.txt");
  66. }
  67. #region 将所有检测到的bundle包,形成路径+MD5节点...
  68. Debug.Log("将所有检测到的bundle包,形成路径+MD5节点");
  69. XmlDocument XmlDoc = new XmlDocument();
  70. XmlElement XmlRoot = XmlDoc.CreateElement("Files");
  71. XmlDoc.AppendChild(XmlRoot);
  72. foreach (KeyValuePair<string, string> pair in DicFileMD5)
  73. {
  74. XmlElement xmlElem = XmlDoc.CreateElement("File");
  75. XmlRoot.AppendChild(xmlElem);
  76. xmlElem.SetAttribute("FileName", pair.Key);
  77. xmlElem.SetAttribute("MD5", pair.Value);
  78. xmlElem.SetAttribute("Size", DicFileSize[pair.Key]);
  79. }
  80. #endregion
  81. Debug.Log("读取旧版本的MD5,和新版本的文件比较.....");
  82. // 读取旧版本的MD5
  83. Dictionary<string, string> dicOldMD5 = ReadMD5File(savePath + "/VersionMD5-old.txt");
  84. // 读取旧版本的size
  85. Dictionary<string, string> dicOldSize = ReadSizeFile(savePath + "/VersionMD5-old.txt");
  86. // VersionMD5-old中有,而VersionMD5中没有的信息,手动添加到VersionMD5
  87. foreach (KeyValuePair<string, string> pair in dicOldMD5)
  88. {
  89. if (DicFileMD5.ContainsKey(pair.Key) == false)
  90. DicFileMD5.Add(pair.Key, pair.Value);
  91. }
  92. //根据资源生成新的xml,供客户端去检测.....
  93. XmlDoc.Save(savePath + "/VersionMD5.txt");
  94. XmlDoc.Save(savePath+ "/" + GlobalConfig.ClientVersion + ".txt");
  95. XmlDoc = null;
  96. Debug.Log("保存成功.....");
  97. }
  98. static Dictionary<string, string> ReadMD5File(string fileName)
  99. {
  100. Dictionary<string, string> DicMD5 = new Dictionary<string, string>();
  101. // 如果文件不存在,则直接返回
  102. if (System.IO.File.Exists(fileName) == false)
  103. return DicMD5;
  104. XmlDocument XmlDoc = new XmlDocument();
  105. XmlDoc.Load(fileName);
  106. XmlElement XmlRoot = XmlDoc.DocumentElement;
  107. foreach (XmlNode node in XmlRoot.ChildNodes)
  108. {
  109. if ((node is XmlElement) == false)
  110. continue;
  111. string file = (node as XmlElement).GetAttribute("FileName");
  112. string md5 = (node as XmlElement).GetAttribute("MD5");
  113. if (DicMD5.ContainsKey(file) == false)
  114. {
  115. DicMD5.Add(file, md5);
  116. }
  117. }
  118. XmlRoot = null;
  119. XmlDoc = null;
  120. return DicMD5;
  121. }
  122. static Dictionary<string, string> ReadSizeFile(string fileName)
  123. {
  124. Dictionary<string, string> DicSize = new Dictionary<string, string>();
  125. // 如果文件不存在,则直接返回
  126. if (System.IO.File.Exists(fileName) == false)
  127. return DicSize;
  128. XmlDocument XmlDoc = new XmlDocument();
  129. XmlDoc.Load(fileName);
  130. XmlElement XmlRoot = XmlDoc.DocumentElement;
  131. foreach (XmlNode node in XmlRoot.ChildNodes)
  132. {
  133. if ((node is XmlElement) == false)
  134. continue;
  135. string file = (node as XmlElement).GetAttribute("FileName");
  136. string size = (node as XmlElement).GetAttribute("SIZE");
  137. if (DicSize.ContainsKey(file) == false)
  138. {
  139. DicSize.Add(file, size );
  140. }
  141. }
  142. XmlRoot = null;
  143. XmlDoc = null;
  144. return DicSize;
  145. }
  146. }