CheckHotUpdateRes.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. if (GUILayout.Button("复制图片资源到输出路径"))
  117. {
  118. CopyTexture();
  119. }
  120. GUILayout.Box("", GUILayout.Width(790));
  121. GUILayout.BeginHorizontal();
  122. GUILayout.Space(60);
  123. info.IsIgnore =GUILayout.Toggle(info.IsIgnore, "是否开启忽略某些文件") ;
  124. GUILayout.EndHorizontal();
  125. }
  126. private List<VersionInfoData> SerizlizeResList(byte[] data)
  127. {
  128. CsvReader csvReader = new CsvReader(info.mainfestFileName, data);
  129. if (downloadFormatInfo == null)
  130. {
  131. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  132. }
  133. Type type = typeof(VersionInfoData);
  134. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  135. methodInfo?.Invoke(null, new object[] { csvReader });
  136. List<VersionInfoData> resList = VersionInfoData.AllData();
  137. VersionInfoData.Clear();
  138. return resList;
  139. }
  140. private void CheckRes()
  141. {
  142. needResList.Clear();
  143. oldPath = $"{info.OldPath}/{ info.mainfestFileName}";
  144. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  145. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  146. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  147. ChangeResMD5(newlist);
  148. if (Oldlist != null && newlist != null)
  149. {
  150. int size = newlist.Count;
  151. for (int i = 0; i < size; i++)
  152. {
  153. if (newlist[i].FullName == info.mainfestFileName || newlist[i].FullName == info.VersionFileName)
  154. {
  155. continue;
  156. }
  157. bool isignore = false;
  158. if (info.IsIgnore)
  159. {
  160. string igstr = info.IgnoreFiles.FindFirst(it => it == newlist[i].FullName);
  161. if (!string.IsNullOrEmpty(igstr))
  162. {
  163. isignore = true;
  164. Debug.Log("======================" + newlist[i].FullName);
  165. }
  166. }
  167. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  168. if (ores != null )
  169. {
  170. if (isignore)
  171. {
  172. newlist[i].MD5 = ores.MD5;
  173. newlist[i].Size = ores.Size;
  174. }
  175. else if (newlist[i].MD5 != ores.MD5)
  176. {
  177. needResList.Add(newlist[i]);
  178. }
  179. }
  180. else
  181. {
  182. needResList.Add(newlist[i]);
  183. }
  184. }
  185. }
  186. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  187. csvWriter.Write();
  188. CopyFile();
  189. needResList.Clear();
  190. Debug.Log($"资源检查完成 输出路径 : 【{info.OutputPath}】");
  191. }
  192. private void CopyFile()
  193. {
  194. if (needResList == null)
  195. {
  196. return;
  197. }
  198. int size = needResList.Count;
  199. string datapath = $"{info.NewPath}/";
  200. for (int i = 0; i < size; i++)
  201. {
  202. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  203. FileHelper.WirteToFile( $"{info.OutputPath}/{needResList[i].FullName}" ,fdatas);
  204. }
  205. FileHelper.WirteStringToFile($"{info.OutputPath}/{info.VersionFileName}",info.ResVersion);
  206. byte[] mdatas = File.ReadAllBytes(datapath + info.mainfestFileName);
  207. FileHelper.WirteToFile($"{info.OutputPath}/{info.mainfestFileName}", mdatas);
  208. }
  209. private List<VersionInfoData> GetInfoDatas(string path)
  210. {
  211. List<VersionInfoData> list = null;
  212. byte[] datas = null;
  213. if (FileSystem.Exists(path))
  214. {
  215. datas = File.ReadAllBytes(path);
  216. }
  217. if (datas != null)
  218. {
  219. list = SerizlizeResList(datas);
  220. }
  221. else
  222. {
  223. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  224. }
  225. return list;
  226. }
  227. private void ChangeResMD5(List<VersionInfoData> datas)
  228. {
  229. int[] ids = GenerateRandomArray(info.ChangeNum, 1, datas.Count);
  230. for (int i = 0; i < ids.Length; i++)
  231. {
  232. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  233. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  234. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  235. }
  236. }
  237. int[] GenerateRandomArray(int size, int min, int max)
  238. {
  239. List<int> array = new List<int>(size);
  240. if (size >= max - min)
  241. {
  242. for (int i = min; i < max; i++)
  243. {
  244. array.Add(i);
  245. }
  246. }
  247. else
  248. {
  249. while (size > 0)
  250. {
  251. int a = UnityEngine.Random.Range(min, max);
  252. if (!array.Contains(a))
  253. {
  254. array.Add(a);
  255. size--;
  256. }
  257. }
  258. }
  259. return array.ToArray();
  260. }
  261. private void CopyFiles()
  262. {
  263. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  264. int size = files1.Length;
  265. string datapath = $"{Application.dataPath}/{info.NewPath}/";
  266. for (int i = 0; i < size; i++)
  267. {
  268. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  269. FileHelper.WirteToFile($"{info.OutputPath}/{files1[i]}", fdatas);
  270. }
  271. }
  272. private void CopyTexture()
  273. {
  274. string datapath = $"{Application.dataPath}/Content/Icons/";
  275. FileHelper.CopyDir(datapath, info.OutputPath, "meta");
  276. }
  277. }