UpdateTools_iOS.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /// 更新工具(IOS)
  13. /// By WJ 2020-04-27 11:24:45
  14. /// </summary>
  15. public class UpdateTools_iOS
  16. {
  17. /// <summary>
  18. /// 更新文件的路径
  19. /// </summary>
  20. private static string mFilePath = Application.dataPath + @"/UpdateResBackups/" + UpdateHelper.IosUpdateFileName;
  21. /// <summary>
  22. /// 更新的资源所在目录
  23. /// </summary>
  24. private static string mResSavePath = Application.dataPath + @"/UpdateResBackups/NewUpdateIOS";
  25. /// <summary>
  26. /// 更新信息
  27. /// </summary>
  28. private static UpdateFileInfo mUpdateFileInfo = null;
  29. #if UNITY_IOS
  30. /// <summary>
  31. /// 创建资源更新版本
  32. /// </summary>
  33. [MenuItem("Build/构建更新版本(iOS)", false, 301)]
  34. private static void CreateUpdateResources()
  35. {
  36. // 读取Json文件
  37. bool haveJson = ReadFile();
  38. // 获取 UpdateResBackups/UpdateIos下面所有的.unity3d文件
  39. string[] files = Directory.GetFiles("Assets/UpdateResBackups/UpdateIos", "*.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/清空更新版本(iOS)", 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="md5">md5</param>
  84. private static void AddData(string pathName, string md5)
  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 == 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:" + md5);
  110. mUpdateFileInfo.Infos[i].MD5 = md5;
  111. // 拷贝需要更新的文件
  112. CopyResources(pathName);
  113. }
  114. return;
  115. }
  116. }
  117. // 添加信息
  118. FileMD5Info md5Info = new FileMD5Info();
  119. md5Info.Name = fileName;
  120. md5Info.MD5 = md5;
  121. mUpdateFileInfo.Infos.Add(md5Info);
  122. // 拷贝需要更新的文件
  123. CopyResources(pathName);
  124. }
  125. /// <summary>
  126. /// 拷贝资源去更新文件夹
  127. /// </summary>
  128. /// <param name="path">路径</param>
  129. private static void CopyResources(string path)
  130. {
  131. string[] strs = path.Split('/');
  132. string resName = "";
  133. if (strs.Length > 0)
  134. {
  135. resName = strs[strs.Length - 1];
  136. }
  137. if (resName == "")
  138. {
  139. return;
  140. }
  141. // 文件不存在,创建
  142. if (Directory.Exists(mResSavePath) == false)
  143. {
  144. Directory.CreateDirectory(mResSavePath);
  145. AssetDatabase.Refresh();
  146. }
  147. // 保存的路径
  148. string savePath = mResSavePath + "/" + resName;
  149. // 复制
  150. File.Copy(path, savePath);
  151. }
  152. /// <summary>
  153. /// 写文件
  154. /// </summary>
  155. private static void WriteFile()
  156. {
  157. byte[] texts = new byte[] { };
  158. try
  159. {
  160. // 版本叠加
  161. mUpdateFileInfo.Version++;
  162. string AutoSaveJson = JsonUtility.ToJson(mUpdateFileInfo);
  163. byte[] curTexts = Encoding.UTF8.GetBytes(AutoSaveJson);
  164. texts = curTexts;
  165. }
  166. catch (Exception e)
  167. {
  168. Debug.LogError("Message:" + e.Message);
  169. }
  170. if (File.Exists(mFilePath))
  171. {
  172. File.Delete(mFilePath);
  173. }
  174. using (FileStream fs = new FileStream(mFilePath, FileMode.OpenOrCreate, FileAccess.Write))
  175. {
  176. if (fs != null)
  177. {
  178. fs.Write(texts, 0, texts.Length);
  179. fs.Flush();
  180. fs.Dispose();
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 读文件
  186. /// </summary>
  187. private static bool ReadFile()
  188. {
  189. try
  190. {
  191. string DecrypText = string.Empty;
  192. using (FileStream fs = new FileStream(mFilePath, FileMode.Open, FileAccess.Read))
  193. {
  194. if (fs.Length > 0)
  195. {
  196. int fsLen = (int)fs.Length;
  197. byte[] heByte = new byte[fsLen];
  198. int r = fs.Read(heByte, 0, heByte.Length);
  199. DecrypText = System.Text.Encoding.UTF8.GetString(heByte);
  200. fs.Dispose();
  201. }
  202. }
  203. mUpdateFileInfo = JsonUtility.FromJson<UpdateFileInfo>(DecrypText);
  204. }
  205. catch (Exception e)
  206. {
  207. mUpdateFileInfo = new UpdateFileInfo();
  208. return false;
  209. }
  210. return true;
  211. }
  212. /// <summary>
  213. /// 获取文件的md5
  214. /// </summary>
  215. /// <param name="filePath">路径</param>
  216. /// <returns>md5</returns>
  217. public static string GetFileMD5(string filePath)
  218. {
  219. try
  220. {
  221. FileStream fs = new FileStream(filePath, FileMode.Open);
  222. int len = (int)fs.Length;
  223. byte[] data = new byte[len];
  224. fs.Read(data, 0, len);
  225. fs.Close();
  226. MD5 md5 = new MD5CryptoServiceProvider();
  227. byte[] result = md5.ComputeHash(data);
  228. string fileMD5 = "";
  229. foreach (byte b in result)
  230. {
  231. fileMD5 += Convert.ToString(b, 16);
  232. }
  233. return fileMD5;
  234. }
  235. catch (FileNotFoundException e)
  236. {
  237. Console.WriteLine(e.Message);
  238. return "";
  239. }
  240. }
  241. /// <summary>
  242. /// 同步编辑器进度条
  243. /// </summary>
  244. /// <param name="path">路径</param>
  245. /// <param name="options">选项</param>
  246. public static void DoAssetReimport(string path, ImportAssetOptions options)
  247. {
  248. try
  249. {
  250. AssetDatabase.StartAssetEditing();
  251. AssetDatabase.ImportAsset(path, options);
  252. }
  253. finally
  254. {
  255. AssetDatabase.StopAssetEditing();
  256. }
  257. }
  258. }