CheckHotUpdateRes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #if UNITY_IOS
  47. info.VersionFileName = "afivs";
  48. info.mainfestFileName = "afimft";
  49. //#else
  50. // versionFileName = "Version";
  51. // assetsFileName = "mainfest";
  52. #endif
  53. needResList = new List<VersionInfoData>();
  54. }
  55. private void OnGUI()
  56. {
  57. DrowSelectFolder("旧版本文件", "选择文件路径", "选择旧版本资源MD5文件路径", ref info.OldPath);
  58. DrowSelectFolder("新版本文件", "选择文件路径", "选择新版本资源MD5文件路径", ref info.NewPath);
  59. DrowSelectFolder("需跟新文件输出路径", "选择文件路径", "选择需跟新文件输出路径", ref info.OutputPath);
  60. DrawInfo();
  61. DrawCheckBtn();
  62. }
  63. private void DrowSelectFolder(string name, string btnName, string savepath, ref string path)
  64. {
  65. GUILayout.Box("", GUILayout.Width(790));
  66. GUILayout.BeginHorizontal();
  67. GUILayout.Space(10);
  68. GUILayout.Label(name, GUILayout.Width(120));
  69. GUILayout.Space(10);
  70. path = GUILayout.TextField(path, GUILayout.Width(360));
  71. if (GUILayout.Button(btnName, GUILayout.Width(100)))
  72. {
  73. string getstr = EditorUtility.SaveFolderPanel(savepath, path, "");
  74. if(!string.IsNullOrEmpty(getstr))
  75. {
  76. path = getstr;
  77. }
  78. }
  79. GUILayout.Space(10);
  80. GUILayout.EndHorizontal();
  81. }
  82. private void DrawInfo()
  83. {
  84. GUILayout.Box("", GUILayout.Width(790));
  85. GUILayout.BeginHorizontal();
  86. GUILayout.Space(10);
  87. GUILayout.Label("版本文件名:", GUILayout.Width(65));
  88. GUILayout.Space(10);
  89. info.VersionFileName = GUILayout.TextField(info.VersionFileName, GUILayout.Width(160));
  90. GUILayout.Space(60);
  91. GUILayout.Label("版本文件信息文件名:", GUILayout.Width(120));
  92. GUILayout.Space(10);
  93. info.mainfestFileName = GUILayout.TextField(info.mainfestFileName, GUILayout.Width(160));
  94. GUILayout.EndHorizontal();
  95. GUILayout.Box("", GUILayout.Width(790));
  96. GUILayout.BeginHorizontal();
  97. GUILayout.Space(10);
  98. GUILayout.Label("资源版本:", GUILayout.Width(65));
  99. GUILayout.Space(10);
  100. info.ResVersion = GUILayout.TextField(info.ResVersion, GUILayout.Width(160));
  101. GUILayout.Space(10);
  102. if (GUILayout.Button("读取版本", GUILayout.Width(100)))
  103. {
  104. info.ResVersion = File.ReadAllText($"{info.OldPath}/{info.VersionFileName}");
  105. }
  106. GUILayout.Space(10);
  107. if (GUILayout.Button("版本+1", GUILayout.Width(100)))
  108. {
  109. VersionCode lVersionCode = info.ResVersion;
  110. lVersionCode.patch += 1;
  111. info.ResVersion = lVersionCode.ToString();
  112. }
  113. GUILayout.Space(10);
  114. GUILayout.EndHorizontal();
  115. }
  116. private void DrawCheckBtn()
  117. {
  118. GUILayout.Box("", GUILayout.Width(790));
  119. GUILayout.BeginHorizontal();
  120. GUILayout.Space(10);
  121. if (GUILayout.Button("生成MD5资源版本文件"))
  122. {
  123. BundleBuilderZ.CreateMD5File(info.ResVersion);
  124. }
  125. GUILayout.Space(60);
  126. if (GUILayout.Button("对比新旧资源"))
  127. {
  128. CheckRes();
  129. }
  130. GUILayout.EndHorizontal();
  131. GUILayout.Box("", GUILayout.Width(790));
  132. if (GUILayout.Button("复制所以资源到输出路径"))
  133. {
  134. CopyFiles();
  135. }
  136. if (GUILayout.Button("复制图片资源到输出路径"))
  137. {
  138. CopyTexture();
  139. }
  140. GUILayout.Box("", GUILayout.Width(790));
  141. GUILayout.BeginHorizontal();
  142. GUILayout.Space(60);
  143. info.IsIgnore =GUILayout.Toggle(info.IsIgnore, "是否开启忽略某些文件") ;
  144. GUILayout.EndHorizontal();
  145. }
  146. private List<VersionInfoData> SerizlizeResList(byte[] data)
  147. {
  148. CsvReader csvReader = new CsvReader(info.mainfestFileName, data);
  149. if (downloadFormatInfo == null)
  150. {
  151. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  152. }
  153. Type type = typeof(VersionInfoData);
  154. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  155. methodInfo?.Invoke(null, new object[] { csvReader });
  156. List<VersionInfoData> resList = VersionInfoData.AllData();
  157. VersionInfoData.Clear();
  158. return resList;
  159. }
  160. private void CheckRes()
  161. {
  162. needResList.Clear();
  163. oldPath = $"{info.OldPath}/{ info.mainfestFileName}";
  164. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  165. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  166. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  167. ChangeResMD5(newlist);
  168. if (Oldlist != null && newlist != null)
  169. {
  170. int size = newlist.Count;
  171. for (int i = 0; i < size; i++)
  172. {
  173. if (newlist[i].FullName == info.mainfestFileName || newlist[i].FullName == info.VersionFileName)
  174. {
  175. continue;
  176. }
  177. bool isignore = false;
  178. if (info.IsIgnore)
  179. {
  180. string igstr = info.IgnoreFiles.FindFirst(it => it == newlist[i].FullName);
  181. if (!string.IsNullOrEmpty(igstr))
  182. {
  183. isignore = true;
  184. Debug.Log("======================" + newlist[i].FullName);
  185. }
  186. }
  187. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  188. if (ores != null )
  189. {
  190. if (isignore)
  191. {
  192. newlist[i].MD5 = ores.MD5;
  193. newlist[i].Size = ores.Size;
  194. }
  195. else if (newlist[i].MD5 != ores.MD5)
  196. {
  197. needResList.Add(newlist[i]);
  198. }
  199. }
  200. else
  201. {
  202. needResList.Add(newlist[i]);
  203. }
  204. }
  205. }
  206. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  207. csvWriter.Write();
  208. CopyFile();
  209. needResList.Clear();
  210. Debug.Log($"资源检查完成 输出路径 : 【{info.OutputPath}】");
  211. }
  212. private void CopyFile()
  213. {
  214. if (needResList == null)
  215. {
  216. return;
  217. }
  218. int size = needResList.Count;
  219. string datapath = $"{info.NewPath}/";
  220. for (int i = 0; i < size; i++)
  221. {
  222. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  223. FileHelper.WirteToFile( $"{info.OutputPath}/{needResList[i].FullName}" ,fdatas);
  224. }
  225. FileHelper.WirteStringToFile($"{info.OutputPath}/{info.VersionFileName}",info.ResVersion);
  226. byte[] mdatas = File.ReadAllBytes(datapath + info.mainfestFileName);
  227. FileHelper.WirteToFile($"{info.OutputPath}/{info.mainfestFileName}", mdatas);
  228. }
  229. private List<VersionInfoData> GetInfoDatas(string path)
  230. {
  231. List<VersionInfoData> list = null;
  232. byte[] datas = null;
  233. if (FileSystem.Exists(path))
  234. {
  235. datas = File.ReadAllBytes(path);
  236. }
  237. if (datas != null)
  238. {
  239. list = SerizlizeResList(datas);
  240. }
  241. else
  242. {
  243. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  244. }
  245. return list;
  246. }
  247. private void ChangeResMD5(List<VersionInfoData> datas)
  248. {
  249. int[] ids = GenerateRandomArray(info.ChangeNum, 1, datas.Count);
  250. for (int i = 0; i < ids.Length; i++)
  251. {
  252. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  253. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  254. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  255. }
  256. }
  257. int[] GenerateRandomArray(int size, int min, int max)
  258. {
  259. List<int> array = new List<int>(size);
  260. if (size >= max - min)
  261. {
  262. for (int i = min; i < max; i++)
  263. {
  264. array.Add(i);
  265. }
  266. }
  267. else
  268. {
  269. while (size > 0)
  270. {
  271. int a = UnityEngine.Random.Range(min, max);
  272. if (!array.Contains(a))
  273. {
  274. array.Add(a);
  275. size--;
  276. }
  277. }
  278. }
  279. return array.ToArray();
  280. }
  281. private void CopyFiles()
  282. {
  283. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  284. int size = files1.Length;
  285. string datapath = $"{Application.dataPath}/{info.NewPath}/";
  286. for (int i = 0; i < size; i++)
  287. {
  288. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  289. FileHelper.WirteToFile($"{info.OutputPath}/{files1[i]}", fdatas);
  290. }
  291. }
  292. private void CopyTexture()
  293. {
  294. string datapath = $"{Application.dataPath}/Content/Icons/";
  295. FileHelper.CopyDir(datapath, info.OutputPath, "meta");
  296. }
  297. }