UpdateTools_Android.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEditor;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. using System;
  9. using System.Linq;
  10. using System.Security.Cryptography;
  11. /// <summary>
  12. /// 更新工具(安卓)
  13. /// By WJ 2020-04-27 11:24:14
  14. /// </summary>
  15. public class UpdateTools_Android
  16. {
  17. /// <summary>
  18. /// 更新文件的路径
  19. /// </summary>
  20. private static string mFilePath = Application.dataPath + @"/StreamingAssets/" + UpdateHelper.AndroidUpdateFileName;
  21. /// <summary>
  22. /// 更新的资源所在目录
  23. /// </summary>
  24. private static string mResSavePath = Application.dataPath + @"/StreamingAssets/UpdateAndroid";
  25. /// <summary>
  26. /// 更新信息
  27. /// </summary>
  28. private static UpdateFileInfo mUpdateFileInfo = null;
  29. #if UNITY_ANDROID
  30. /// <summary>
  31. /// 创建资源更新版本
  32. /// </summary>
  33. [MenuItem("Build/构建更新版本(Android)", false, 301)]
  34. private static void CreateUpdateResources()
  35. {
  36. // 读取Json文件
  37. bool haveJson = ReadFile();
  38. // 获取 UpdateResBackups/UpdateAndroid下面所有的.unity3d文件
  39. string[] files = Directory.GetFiles("Assets/StreamingAssets/Res", "*.unity3d", SearchOption.AllDirectories);
  40. for (int i = 0; i < files.Length; i++)
  41. {
  42. string filePath = files[i];
  43. filePath = filePath.Replace("\\", "/");
  44. //编辑器进度条
  45. EditorUtility.DisplayProgressBar("处理中>>>", filePath, (float)i / (float)files.Length);
  46. // 添加数据
  47. AddData(filePath, GetFileMD5(filePath));
  48. AssetDatabase.SaveAssets();
  49. DoAssetReimport(filePath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
  50. }
  51. // 保存更新的Json文件
  52. SaveUpdateFile();
  53. EditorUtility.ClearProgressBar();
  54. EditorUtility.DisplayDialog("成功", "处理完成!", "好的");
  55. }
  56. /// <summary>
  57. /// 删除更新资源所在的文件夹
  58. /// </summary>
  59. ///
  60. [MenuItem("Build/清空更新版本(Android)", false, 302)]
  61. private static void DeleteUpdateFile()
  62. {
  63. if (Directory.Exists(mResSavePath))
  64. {
  65. Directory.Delete(mResSavePath, true);
  66. AssetDatabase.Refresh();
  67. }
  68. }
  69. #endif
  70. /// <summary>
  71. /// 保存Json更新文件
  72. /// </summary>
  73. private static void SaveUpdateFile()
  74. {
  75. // 保存文件
  76. WriteFile();
  77. AssetDatabase.Refresh();
  78. }
  79. /// <summary>
  80. /// 添加数据
  81. /// </summary>
  82. /// <param name="pathName">名称</param>
  83. /// <param name="sizeInfo">大小信息</param>
  84. private static void AddData(string pathName, FileSizeInfo sizeInfo)
  85. {
  86. // 获取文件名字
  87. string[] strs = pathName.Split('/');
  88. string fileName = "";
  89. if (strs.Length > 0)
  90. {
  91. fileName = strs[strs.Length - 1];
  92. }
  93. else
  94. {
  95. fileName = pathName;
  96. }
  97. for (int i = 0; i < mUpdateFileInfo.Infos.Count; i++)
  98. {
  99. if (mUpdateFileInfo.Infos[i].Name == fileName)
  100. {
  101. if (mUpdateFileInfo.Infos[i].MD5 == sizeInfo.md5)
  102. {
  103. // 相同,什么也不做
  104. Debug.Log(fileName + " MD5未变更,无需更新!");
  105. }
  106. else
  107. {
  108. // md5不同,设置新md5
  109. Debug.Log(fileName + " 原MD5:" + mUpdateFileInfo.Infos[i].MD5 + " 新MD5:" + sizeInfo.md5);
  110. mUpdateFileInfo.Infos[i].MD5 = sizeInfo.md5;
  111. mUpdateFileInfo.Infos[i].PackSizeB = sizeInfo.sizeB;
  112. mUpdateFileInfo.Infos[i].PackSizeM = sizeInfo.sizeM;
  113. mUpdateFileInfo.Infos[i].PackVersion = mUpdateFileInfo.Version + 1;
  114. // 拷贝需要更新的文件
  115. CopyResources(pathName);
  116. }
  117. return;
  118. }
  119. }
  120. // 添加信息
  121. FileMD5Info md5Info = new FileMD5Info();
  122. md5Info.Name = fileName;
  123. md5Info.MD5 = sizeInfo.md5;
  124. md5Info.PackSizeB = sizeInfo.sizeB;
  125. md5Info.PackSizeM = sizeInfo.sizeM;
  126. md5Info.PackVersion = mUpdateFileInfo.Version + 1;
  127. mUpdateFileInfo.Infos.Add(md5Info);
  128. // 拷贝需要更新的文件
  129. CopyResources(pathName);
  130. }
  131. /// <summary>
  132. /// 拷贝资源去更新文件夹
  133. /// </summary>
  134. /// <param name="path">路径</param>
  135. private static void CopyResources(string path)
  136. {
  137. string[] strs = path.Split('/');
  138. string resName = "";
  139. if (strs.Length > 0)
  140. {
  141. resName = strs[strs.Length - 1];
  142. }
  143. if (resName == "")
  144. {
  145. return;
  146. }
  147. // 文件不存在,创建
  148. if (Directory.Exists(mResSavePath) == false)
  149. {
  150. Directory.CreateDirectory(mResSavePath);
  151. AssetDatabase.Refresh();
  152. }
  153. // 保存的路径
  154. string savePath = mResSavePath + "/" + resName;
  155. // 复制
  156. File.Copy(path, savePath);
  157. }
  158. /// <summary>
  159. /// 写文件
  160. /// </summary>
  161. private static void WriteFile()
  162. {
  163. byte[] texts = new byte[] { };
  164. try
  165. {
  166. // 版本叠加
  167. mUpdateFileInfo.Version++;
  168. string AutoSaveJson = JsonUtility.ToJson(mUpdateFileInfo);
  169. byte[] curTexts = Encoding.UTF8.GetBytes(AutoSaveJson);
  170. texts = curTexts;
  171. }
  172. catch (Exception e)
  173. {
  174. Debug.LogError("Message:" + e.Message);
  175. }
  176. if (File.Exists(mFilePath))
  177. {
  178. File.Delete(mFilePath);
  179. }
  180. using (FileStream fs = new FileStream(mFilePath, FileMode.OpenOrCreate, FileAccess.Write))
  181. {
  182. if (fs != null)
  183. {
  184. fs.Write(texts, 0, texts.Length);
  185. fs.Flush();
  186. fs.Dispose();
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// 读文件
  192. /// </summary>
  193. private static bool ReadFile()
  194. {
  195. try
  196. {
  197. string DecrypText = string.Empty;
  198. using (FileStream fs = new FileStream(mFilePath, FileMode.Open, FileAccess.Read))
  199. {
  200. if (fs.Length > 0)
  201. {
  202. int fsLen = (int)fs.Length;
  203. byte[] heByte = new byte[fsLen];
  204. int r = fs.Read(heByte, 0, heByte.Length);
  205. DecrypText = System.Text.Encoding.UTF8.GetString(heByte);
  206. fs.Dispose();
  207. }
  208. }
  209. mUpdateFileInfo = JsonUtility.FromJson<UpdateFileInfo>(DecrypText);
  210. }
  211. catch (Exception e)
  212. {
  213. mUpdateFileInfo = new UpdateFileInfo();
  214. return false;
  215. }
  216. return true;
  217. }
  218. /// <summary>
  219. /// 获取文件的md5
  220. /// </summary>
  221. /// <param name="filePath">路径</param>
  222. /// <returns>md5</returns>
  223. public static FileSizeInfo GetFileMD5(string filePath)
  224. {
  225. FileSizeInfo sizeInfo = new FileSizeInfo();
  226. try
  227. {
  228. FileStream fs = new FileStream(filePath, FileMode.Open);
  229. int len = (int)fs.Length;
  230. byte[] data = new byte[len];
  231. fs.Read(data, 0, len);
  232. fs.Close();
  233. MD5 md5 = new MD5CryptoServiceProvider();
  234. byte[] result = md5.ComputeHash(data);
  235. string fileMD5 = "";
  236. foreach (byte b in result)
  237. {
  238. fileMD5 += Convert.ToString(b, 16);
  239. }
  240. sizeInfo.md5 = fileMD5;
  241. sizeInfo.sizeB = len;
  242. // 1048576=1024 * 1024
  243. sizeInfo.sizeM = len / 1048576;
  244. return sizeInfo;
  245. }
  246. catch (FileNotFoundException e)
  247. {
  248. Console.WriteLine(e.Message);
  249. return sizeInfo;
  250. }
  251. }
  252. /// <summary>
  253. /// 同步编辑器进度条
  254. /// </summary>
  255. /// <param name="path">路径</param>
  256. /// <param name="options">选项</param>
  257. public static void DoAssetReimport(string path, ImportAssetOptions options)
  258. {
  259. try
  260. {
  261. AssetDatabase.StartAssetEditing();
  262. AssetDatabase.ImportAsset(path, options);
  263. }
  264. finally
  265. {
  266. AssetDatabase.StopAssetEditing();
  267. }
  268. }
  269. }