CheckHotUpdateRes.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. using System.IO.Compression;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Linq;
  13. public class CheckHotUpdateRes : EditorWindow
  14. {
  15. public class VersionInfoData:GameData<VersionInfoData>
  16. {
  17. public string FullName;
  18. public string MD5;
  19. public ulong Size;
  20. public VersionInfoData()
  21. {
  22. FullName = "";
  23. MD5 = "";
  24. Size = 0;
  25. }
  26. private static void OnCsvLoad(CsvReader csvReader)
  27. {
  28. VersionInfoData.Onload(csvReader);
  29. }
  30. }
  31. [MenuItem("AssetBundle/热更相关")]
  32. public static void OpenCheckHotUpdateWindow()
  33. {
  34. CheckHotUpdateRes window = GetWindow<CheckHotUpdateRes>("热更相关");
  35. window.maxSize = new Vector2(800, 600);
  36. window.minSize = new Vector2(600, 400);
  37. window.Init();
  38. window.Show();
  39. }
  40. private string oldPath = "";
  41. private string newPath = "";
  42. private CheckResWindowInfo info;
  43. private ResInfo curResInfo;
  44. private List<VersionInfoData> needResList;
  45. private GameDataFormatInfo downloadFormatInfo;
  46. public void Init()
  47. {
  48. string path = "Assets/Editor/AssetBundle/CheckHotResWindownInfo.asset";
  49. //Debug.Log();
  50. info = AssetDatabase.LoadAssetAtPath<CheckResWindowInfo>(path);
  51. info.InitTags();
  52. curResInfo = info.GetResInfo();
  53. if (curResInfo == null)
  54. {
  55. curResInfo = new ResInfo();
  56. }
  57. #if UNITY_IOS
  58. info.VersionFileName = "afivs";
  59. info.mainfestFileName = "afimft";
  60. info.NewPath = Application.dataPath + "/StreamingAssets/unityRes";
  61. //#else
  62. // versionFileName = "Version";
  63. // assetsFileName = "mainfest";
  64. #endif
  65. needResList = new List<VersionInfoData>();
  66. }
  67. private void OnGUI()
  68. {
  69. DrowSelectFolder("旧版本文件", "选择文件路径", "选择旧版本资源MD5文件路径", ref curResInfo.OldPath);
  70. DrowSelectFolder("新版本文件", "选择文件路径", "选择新版本资源MD5文件路径", ref curResInfo.NewPath);
  71. DrowSelectFolder("需跟新文件输出路径", "选择文件路径", "选择需跟新文件输出路径", ref curResInfo.OutputPath);
  72. DrowSelectFolder("ab包名基础名字文件路径", "选择文件路径", "选择ab包名基础名字文件路径", ref curResInfo.BaseNameFileInfoPath);
  73. DrawInfo();
  74. DrawCheckBtn();
  75. }
  76. private void DrowSelectFolder(string name, string btnName, string savepath, ref string path)
  77. {
  78. GUILayout.Box("", GUILayout.Width(790));
  79. GUILayout.BeginHorizontal();
  80. GUILayout.Space(10);
  81. GUILayout.Label(name, GUILayout.Width(120));
  82. GUILayout.Space(10);
  83. path = GUILayout.TextField(path, GUILayout.Width(360));
  84. if (GUILayout.Button(btnName, GUILayout.Width(100)))
  85. {
  86. string getstr = EditorUtility.SaveFolderPanel(savepath, path, "");
  87. if(!string.IsNullOrEmpty(getstr))
  88. {
  89. path = getstr;
  90. }
  91. }
  92. GUILayout.Space(10);
  93. GUILayout.EndHorizontal();
  94. }
  95. private void DrawInfo()
  96. {
  97. GUILayout.Box("", GUILayout.Width(790));
  98. GUILayout.BeginHorizontal();
  99. GUILayout.Label("平台", GUILayout.Width(65));
  100. int TagIndex = EditorGUILayout.Popup(info.buildTag, info.Tags, GUILayout.Width(100));
  101. if (TagIndex != info.buildTag)
  102. {
  103. info.buildTag = TagIndex;
  104. ResInfo ri = info.GetResInfo();
  105. if (ri != null)
  106. {
  107. curResInfo = ri;
  108. }
  109. }
  110. GUILayout.EndHorizontal();
  111. GUILayout.Box("", GUILayout.Width(790));
  112. GUILayout.BeginHorizontal();
  113. GUILayout.Space(10);
  114. GUILayout.Label("版本文件名:", GUILayout.Width(65));
  115. GUILayout.Space(10);
  116. curResInfo.VersionFileName = GUILayout.TextField(curResInfo.VersionFileName, GUILayout.Width(160));
  117. GUILayout.Space(60);
  118. GUILayout.Label("版本文件信息文件名:", GUILayout.Width(120));
  119. GUILayout.Space(10);
  120. curResInfo.mainfestFileName = GUILayout.TextField(curResInfo.mainfestFileName, GUILayout.Width(160));
  121. GUILayout.EndHorizontal();
  122. GUILayout.Box("", GUILayout.Width(790));
  123. GUILayout.BeginHorizontal();
  124. GUILayout.Space(10);
  125. GUILayout.Label("资源版本:", GUILayout.Width(65));
  126. GUILayout.Space(10);
  127. curResInfo.ResVersion = GUILayout.TextField(curResInfo.ResVersion, GUILayout.Width(160));
  128. GUILayout.Space(10);
  129. if (GUILayout.Button("读取版本", GUILayout.Width(100)))
  130. {
  131. curResInfo.ResVersion = File.ReadAllText($"{curResInfo.OldPath}/{curResInfo.VersionFileName}");
  132. }
  133. GUILayout.Space(10);
  134. if (GUILayout.Button("版本+1", GUILayout.Width(100)))
  135. {
  136. VersionCode lVersionCode = curResInfo.ResVersion;
  137. lVersionCode.patch += 1;
  138. curResInfo.ResVersion = lVersionCode.ToString();
  139. }
  140. GUILayout.Space(10);
  141. GUILayout.EndHorizontal();
  142. GUILayout.BeginHorizontal();
  143. GUILayout.Label("语言:", GUILayout.Width(65));
  144. GUILayout.Space(10);
  145. curResInfo.Language = (BuildLanguage)EditorGUILayout.EnumPopup(curResInfo.Language, GUILayout.Width(100));
  146. GUILayout.Space(10);
  147. GUILayout.EndHorizontal();
  148. }
  149. private void DrawCheckBtn()
  150. {
  151. GUILayout.Box("", GUILayout.Width(790));
  152. GUILayout.BeginHorizontal();
  153. GUILayout.Space(10);
  154. if (GUILayout.Button("生成MD5资源版本文件"))
  155. {
  156. BundleBuilderZ.CreateMD5File(curResInfo.ResVersion);
  157. }
  158. GUILayout.Space(60);
  159. if (GUILayout.Button("对比新旧资源"))
  160. {
  161. CheckRes();
  162. }
  163. GUILayout.EndHorizontal();
  164. GUILayout.Box("", GUILayout.Width(790));
  165. if (GUILayout.Button("复制所以资源到输出路径"))
  166. {
  167. CopyFiles();
  168. }
  169. if (GUILayout.Button("复制图片资源到输出路径"))
  170. {
  171. CopyTexture();
  172. }
  173. GUILayout.Box("", GUILayout.Width(790));
  174. GUILayout.BeginHorizontal();
  175. GUILayout.Space(60);
  176. curResInfo.IsIgnore =GUILayout.Toggle(curResInfo.IsIgnore, "是否开启忽略某些文件") ;
  177. GUILayout.Space(60);
  178. if (GUILayout.Button("制作混淆信息"))
  179. {
  180. MakeAbHxNameAssets();
  181. }
  182. GUILayout.EndHorizontal();
  183. }
  184. private List<VersionInfoData> SerizlizeResList(byte[] data)
  185. {
  186. CsvReader csvReader = new CsvReader(curResInfo.mainfestFileName, data);
  187. if (downloadFormatInfo == null)
  188. {
  189. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  190. }
  191. Type type = typeof(VersionInfoData);
  192. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  193. methodInfo?.Invoke(null, new object[] { csvReader });
  194. List<VersionInfoData> resList = VersionInfoData.AllData();
  195. VersionInfoData.Clear();
  196. return resList;
  197. }
  198. private void CheckRes()
  199. {
  200. needResList.Clear();
  201. oldPath = $"{curResInfo.OldPath}/{ curResInfo.mainfestFileName}";
  202. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  203. newPath = $"{curResInfo.NewPath}/{ curResInfo.mainfestFileName}";
  204. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  205. ChangeResMD5(newlist);
  206. Dictionary<string, string> abNamedic = null;
  207. if (curResInfo.IsHx)
  208. {
  209. abNamedic= GetCurInfoAbNameInfos();
  210. }
  211. if (Oldlist != null && newlist != null)
  212. {
  213. int size = newlist.Count;
  214. for (int i = 0; i < size; i++)
  215. {
  216. if (newlist[i].FullName == curResInfo.mainfestFileName || newlist[i].FullName == curResInfo.VersionFileName)
  217. {
  218. continue;
  219. }
  220. bool isignore = false;
  221. string baseName = newlist[i].FullName; ;
  222. RG_Ignore_Info rG_Ignore_Info = null;
  223. if (curResInfo.IsHx && abNamedic.ContainsKey(newlist[i].FullName))
  224. {
  225. baseName = abNamedic[newlist[i].FullName];
  226. }
  227. rG_Ignore_Info = curResInfo.Rg_Essential_Infos.FindFirst(it => !it.IsIgnore && baseName.Contains(it.Field));
  228. bool isEssential = rG_Ignore_Info != null;
  229. if (curResInfo.IsIgnore && !isEssential)
  230. {
  231. string igstr = curResInfo.IgnoreFiles.FindFirst(it => it == baseName);
  232. rG_Ignore_Info = curResInfo.rG_Ignore_Infos.FindFirst(it => it.IsIgnore && baseName.Contains( it.Field));
  233. if (!string.IsNullOrEmpty(igstr) || rG_Ignore_Info != null)
  234. {
  235. isignore = true;
  236. Debug.Log("=========忽略=============" + baseName);
  237. }
  238. }
  239. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  240. if (ores != null )
  241. {
  242. if (isignore)
  243. {
  244. newlist[i].MD5 = ores.MD5;
  245. newlist[i].Size = ores.Size;
  246. }
  247. else if (newlist[i].MD5 != ores.MD5)
  248. {
  249. Debug.Log($"新 {newlist[i].FullName} MD5=[{newlist[i].MD5}]");
  250. Debug.Log($"旧 {ores.FullName} MD5=[{ores.MD5}]");
  251. needResList.Add(newlist[i]);
  252. }
  253. }
  254. else
  255. {
  256. needResList.Add(newlist[i]);
  257. }
  258. }
  259. }
  260. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  261. csvWriter.Write();
  262. CopyFile();
  263. string FiletempPath = $"{curResInfo.OutputPath}/../ZipTempPath";
  264. if(Directory.Exists(FiletempPath))
  265. Directory.Delete(FiletempPath,true);
  266. Directory.CreateDirectory(FiletempPath);
  267. CopyFile(FiletempPath);
  268. string zipName = $"{curResInfo.CN_Name}-热更资源---{curResInfo.ResVersion}---{DateTime.Now.Month}.{DateTime.Now.Day}--{DateTime.Now.Hour}.{DateTime.Now.Minute}.zip";
  269. ZipFile.CreateFromDirectory(FiletempPath,$"{curResInfo.OutputPath}/{zipName}");
  270. Directory.Delete(FiletempPath,true);
  271. needResList.Clear();
  272. Debug.Log($"资源检查完成 输出路径 : 【{curResInfo.OutputPath}】【{zipName}】");
  273. }
  274. private void CopyFile(string outP = "")
  275. {
  276. if (needResList == null)
  277. {
  278. return;
  279. }
  280. if (string.IsNullOrEmpty(outP))
  281. {
  282. outP = curResInfo.OutputPath;
  283. }
  284. int size = needResList.Count;
  285. string datapath = $"{curResInfo.NewPath}/";
  286. for (int i = 0; i < size; i++)
  287. {
  288. if (needResList[i].FullName == "Version")
  289. {
  290. continue;
  291. }
  292. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  293. FileHelper.WirteToFile( $"{outP}/{needResList[i].FullName}" ,fdatas);
  294. }
  295. FileHelper.WirteStringToFile($"{outP}/{curResInfo.VersionFileName}",curResInfo.ResVersion);
  296. byte[] mdatas = File.ReadAllBytes(datapath + curResInfo.mainfestFileName);
  297. FileHelper.WirteToFile($"{outP}/{curResInfo.mainfestFileName}", mdatas);
  298. }
  299. private List<VersionInfoData> GetInfoDatas(string path)
  300. {
  301. List<VersionInfoData> list = null;
  302. byte[] datas = null;
  303. if (FileSystem.Exists(path))
  304. {
  305. datas = File.ReadAllBytes(path);
  306. }
  307. if (datas != null)
  308. {
  309. list = SerizlizeResList(datas);
  310. }
  311. else
  312. {
  313. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  314. }
  315. return list;
  316. }
  317. private void ChangeResMD5(List<VersionInfoData> datas)
  318. {
  319. int[] ids = GenerateRandomArray(curResInfo.ChangeNum, 1, datas.Count);
  320. for (int i = 0; i < ids.Length; i++)
  321. {
  322. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  323. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  324. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  325. }
  326. }
  327. int[] GenerateRandomArray(int size, int min, int max)
  328. {
  329. List<int> array = new List<int>(size);
  330. if (size >= max - min)
  331. {
  332. for (int i = min; i < max; i++)
  333. {
  334. array.Add(i);
  335. }
  336. }
  337. else
  338. {
  339. while (size > 0)
  340. {
  341. int a = UnityEngine.Random.Range(min, max);
  342. if (!array.Contains(a))
  343. {
  344. array.Add(a);
  345. size--;
  346. }
  347. }
  348. }
  349. return array.ToArray();
  350. }
  351. private void CopyFiles()
  352. {
  353. string[] files1 = FileHelper.GetAllFileNmae(curResInfo.NewPath, "meta");
  354. int size = files1.Length;
  355. string datapath = $"{curResInfo.NewPath}/";
  356. for (int i = 0; i < size; i++)
  357. {
  358. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  359. FileHelper.WirteToFile($"{curResInfo.OutputPath}/{files1[i]}", fdatas);
  360. }
  361. }
  362. private void CopyTexture()
  363. {
  364. string datapath = $"{Application.dataPath}/Content/Icons/";
  365. FileHelper.CopyDir(datapath, curResInfo.OutputPath, "meta");
  366. }
  367. public static string GetABFileName(string abName,string s_ObscureKey)
  368. {
  369. try
  370. {
  371. using (var md5 = new MD5CryptoServiceProvider())
  372. {
  373. UTF8Encoding encoding = new UTF8Encoding(false);
  374. byte[] bytes = encoding.GetBytes((abName + s_ObscureKey).ToLower());
  375. bytes = md5.ComputeHash(bytes);
  376. StringBuilder sb = new StringBuilder();
  377. for (int i = 0; i < bytes.Length; i++)
  378. {
  379. sb.Append(bytes[i].ToString("x2"));
  380. }
  381. return sb.ToString();
  382. }
  383. }
  384. catch (Exception e)
  385. {
  386. Debug.LogException(e);
  387. }
  388. return abName;
  389. }
  390. private void MakeAbHxNameAssets()
  391. {
  392. List<VersionInfoData> baseData = GetInfoDatas(curResInfo.BaseNameFileInfoPath);
  393. AbNameHxInfo hxInfo = curResInfo.abNameHxInfo == null? new AbNameHxInfo():curResInfo.abNameHxInfo;
  394. if (hxInfo.Infos == null)
  395. hxInfo.Infos = new List<abNameInfo>();
  396. foreach (var item in baseData)
  397. {
  398. string hxName = GetABFileName(item.FullName,curResInfo.hxKey);
  399. abNameInfo nameInfo = new abNameInfo()
  400. {
  401. BaseName = item.FullName,
  402. HxName = hxName,
  403. };
  404. hxInfo.Infos.Add(nameInfo);
  405. }
  406. curResInfo.abNameHxInfo = hxInfo;
  407. }
  408. public Dictionary<string,string> GetCurInfoAbNameInfos()
  409. {
  410. Dictionary<string, string> abinfos = new Dictionary<string, string>();
  411. foreach (var item in curResInfo.abNameHxInfo.Infos)
  412. {
  413. abinfos.Add(item.HxName,item.BaseName);
  414. }
  415. return abinfos;
  416. }
  417. }