CheckHotUpdateRes.cs 11 KB

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