CheckHotUpdateRes.cs 11 KB

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