CheckHotUpdateRes.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using Game.Config;
  6. using System;
  7. using System.Reflection;
  8. using System.IO;
  9. public class CheckHotUpdateRes : EditorWindow
  10. {
  11. public class VersionInfoData:GameData<VersionInfoData>
  12. {
  13. public string FullName;
  14. public string MD5;
  15. public ulong Size;
  16. public VersionInfoData()
  17. {
  18. FullName = "";
  19. MD5 = "";
  20. Size = 0;
  21. }
  22. private static void OnCsvLoad(CsvReader csvReader)
  23. {
  24. VersionInfoData.Onload(csvReader);
  25. }
  26. }
  27. [MenuItem("AssetBundle/热更相关")]
  28. public static void OpenCheckHotUpdateWindow()
  29. {
  30. CheckHotUpdateRes window = GetWindow<CheckHotUpdateRes>("热更相关");
  31. window.maxSize = new Vector2(800, 600);
  32. window.minSize = new Vector2(600, 400);
  33. window.Show();
  34. window.Init();
  35. }
  36. private string oldPath = "";
  37. private string newPath = "";
  38. private CheckResWindowInfo info;
  39. private List<VersionInfoData> needResList;
  40. private GameDataFormatInfo downloadFormatInfo;
  41. public void Init()
  42. {
  43. string path = "Assets/Editor/AssetBundle/CheckHotResWindownInfo.asset";
  44. //Debug.Log();
  45. info = AssetDatabase.LoadAssetAtPath<CheckResWindowInfo>(path);
  46. needResList = new List<VersionInfoData>();
  47. }
  48. private void OnGUI()
  49. {
  50. DrowSelectFolder("旧版本文件", "选择文件路径", "选择旧版本资源MD5文件路径", ref info.OldPath);
  51. DrowSelectFolder("新版本文件", "选择文件路径", "选择新版本资源MD5文件路径", ref info.NewPath);
  52. DrowSelectFolder("需跟新文件输出路径", "选择文件路径", "选择需跟新文件输出路径", ref info.OutputPath);
  53. DrawInfo();
  54. DrawCheckBtn();
  55. }
  56. private void DrowSelectFolder(string name, string btnName, string savepath, ref string path)
  57. {
  58. GUILayout.Box("", GUILayout.Width(790));
  59. GUILayout.BeginHorizontal();
  60. GUILayout.Space(10);
  61. GUILayout.Label(name, GUILayout.Width(120));
  62. GUILayout.Space(10);
  63. path = GUILayout.TextField(path, GUILayout.Width(360));
  64. if (GUILayout.Button(btnName, GUILayout.Width(100)))
  65. {
  66. string getstr = EditorUtility.SaveFolderPanel(savepath, path, "");
  67. if(!string.IsNullOrEmpty(getstr))
  68. {
  69. path = getstr;
  70. }
  71. }
  72. GUILayout.Space(10);
  73. GUILayout.EndHorizontal();
  74. }
  75. private void DrawInfo()
  76. {
  77. GUILayout.Box("", GUILayout.Width(790));
  78. GUILayout.BeginHorizontal();
  79. GUILayout.Space(10);
  80. GUILayout.Label("版本文件名:", GUILayout.Width(65));
  81. GUILayout.Space(10);
  82. info.VersionFileName = GUILayout.TextField(info.VersionFileName, GUILayout.Width(160));
  83. GUILayout.Space(60);
  84. GUILayout.Label("版本文件信息文件名:", GUILayout.Width(120));
  85. GUILayout.Space(10);
  86. info.mainfestFileName = GUILayout.TextField(info.mainfestFileName, GUILayout.Width(160));
  87. GUILayout.EndHorizontal();
  88. GUILayout.Box("", GUILayout.Width(790));
  89. GUILayout.BeginHorizontal();
  90. GUILayout.Space(10);
  91. GUILayout.Label("资源版本:", GUILayout.Width(65));
  92. GUILayout.Space(10);
  93. info.ResVersion = GUILayout.TextField(info.ResVersion, GUILayout.Width(160));
  94. GUILayout.EndHorizontal();
  95. }
  96. private void DrawCheckBtn()
  97. {
  98. GUILayout.Box("", GUILayout.Width(790));
  99. GUILayout.BeginHorizontal();
  100. GUILayout.Space(10);
  101. if (GUILayout.Button("生成MD5资源版本文件"))
  102. {
  103. BundleBuilderZ.CreateMD5File(info.ResVersion);
  104. }
  105. GUILayout.Space(60);
  106. if (GUILayout.Button("对比新旧资源"))
  107. {
  108. CheckRes();
  109. }
  110. GUILayout.EndHorizontal();
  111. GUILayout.Box("", GUILayout.Width(790));
  112. if (GUILayout.Button("复制所以资源到输出路径"))
  113. {
  114. CopyFiles();
  115. }
  116. GUILayout.Box("", GUILayout.Width(790));
  117. GUILayout.BeginHorizontal();
  118. GUILayout.Space(60);
  119. info.IsIgnore =GUILayout.Toggle(info.IsIgnore, "是否开启忽略某些文件") ;
  120. GUILayout.EndHorizontal();
  121. }
  122. private List<VersionInfoData> SerizlizeResList(byte[] data)
  123. {
  124. CsvReader csvReader = new CsvReader(info.mainfestFileName, data);
  125. if (downloadFormatInfo == null)
  126. {
  127. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  128. }
  129. Type type = typeof(VersionInfoData);
  130. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  131. methodInfo?.Invoke(null, new object[] { csvReader });
  132. List<VersionInfoData> resList = VersionInfoData.AllData();
  133. VersionInfoData.Clear();
  134. return resList;
  135. }
  136. private void CheckRes()
  137. {
  138. needResList.Clear();
  139. oldPath = $"{info.OldPath}/{ info.mainfestFileName}";
  140. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  141. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  142. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  143. ChangeResMD5(newlist);
  144. if (Oldlist != null && newlist != null)
  145. {
  146. int size = newlist.Count;
  147. for (int i = 0; i < size; i++)
  148. {
  149. if (newlist[i].FullName == info.mainfestFileName || newlist[i].FullName == info.VersionFileName)
  150. {
  151. continue;
  152. }
  153. bool isignore = false;
  154. if (info.IsIgnore)
  155. {
  156. string igstr = info.IgnoreFiles.FindFirst(it => it == newlist[i].FullName);
  157. if (!string.IsNullOrEmpty(igstr))
  158. {
  159. isignore = true;
  160. Debug.Log("======================" + newlist[i].FullName);
  161. }
  162. }
  163. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  164. if (ores != null )
  165. {
  166. if (isignore)
  167. {
  168. newlist[i].MD5 = ores.MD5;
  169. newlist[i].Size = ores.Size;
  170. }
  171. else if (newlist[i].MD5 != ores.MD5)
  172. {
  173. needResList.Add(newlist[i]);
  174. }
  175. }
  176. else
  177. {
  178. needResList.Add(newlist[i]);
  179. }
  180. }
  181. }
  182. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  183. csvWriter.Write();
  184. CopyFile();
  185. needResList.Clear();
  186. Debug.Log($"资源检查完成 输出路径 : 【{info.OutputPath}】");
  187. }
  188. private void CopyFile()
  189. {
  190. if (needResList == null)
  191. {
  192. return;
  193. }
  194. int size = needResList.Count;
  195. string datapath = $"{Application.dataPath}/StreamingAssets/AssetsAndroid/";
  196. for (int i = 0; i < size; i++)
  197. {
  198. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  199. FileHelper.WirteToFile( $"{info.OutputPath}/{needResList[i].FullName}" ,fdatas);
  200. }
  201. FileHelper.WirteStringToFile($"{info.OutputPath}/{info.VersionFileName}",info.ResVersion);
  202. byte[] mdatas = File.ReadAllBytes(datapath + info.mainfestFileName);
  203. FileHelper.WirteToFile($"{info.OutputPath}/{info.mainfestFileName}", mdatas);
  204. }
  205. private List<VersionInfoData> GetInfoDatas(string path)
  206. {
  207. List<VersionInfoData> list = null;
  208. byte[] datas = null;
  209. if (FileSystem.Exists(path))
  210. {
  211. datas = File.ReadAllBytes(path);
  212. }
  213. if (datas != null)
  214. {
  215. list = SerizlizeResList(datas);
  216. }
  217. else
  218. {
  219. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  220. }
  221. return list;
  222. }
  223. private void ChangeResMD5(List<VersionInfoData> datas)
  224. {
  225. int[] ids = GenerateRandomArray(info.ChangeNum, 1, datas.Count);
  226. for (int i = 0; i < ids.Length; i++)
  227. {
  228. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  229. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  230. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  231. }
  232. }
  233. int[] GenerateRandomArray(int size, int min, int max)
  234. {
  235. List<int> array = new List<int>(size);
  236. if (size >= max - min)
  237. {
  238. for (int i = min; i < max; i++)
  239. {
  240. array.Add(i);
  241. }
  242. }
  243. else
  244. {
  245. while (size > 0)
  246. {
  247. int a = UnityEngine.Random.Range(min, max);
  248. if (!array.Contains(a))
  249. {
  250. array.Add(a);
  251. size--;
  252. }
  253. }
  254. }
  255. return array.ToArray();
  256. }
  257. private void CopyFiles()
  258. {
  259. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  260. int size = files1.Length;
  261. string datapath = $"{Application.dataPath}/StreamingAssets/AssetsAndroid/";
  262. for (int i = 0; i < size; i++)
  263. {
  264. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  265. FileHelper.WirteToFile($"{info.OutputPath}/{files1[i]}", fdatas);
  266. }
  267. }
  268. }