CheckHotUpdateRes.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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}] Base = [{baseName}]");
  250. Debug.Log($"旧 {ores.FullName} MD5=[{ores.MD5}]");
  251. needResList.Add(newlist[i]);
  252. }
  253. }
  254. else
  255. {
  256. Debug.Log($"新 {newlist[i].FullName} MD5=[{newlist[i].MD5}] Base = [{baseName}]");
  257. needResList.Add(newlist[i]);
  258. }
  259. }
  260. }
  261. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  262. csvWriter.Write();
  263. CopyFile();
  264. string FiletempPath = $"{curResInfo.OutputPath}/../ZipTempPath";
  265. if(Directory.Exists(FiletempPath))
  266. Directory.Delete(FiletempPath,true);
  267. Directory.CreateDirectory(FiletempPath);
  268. CopyFile(FiletempPath);
  269. string zipName = $"{curResInfo.CN_Name}-热更资源---{curResInfo.ResVersion}---{DateTime.Now.Month}.{DateTime.Now.Day}--{DateTime.Now.Hour}.{DateTime.Now.Minute}.zip";
  270. ZipFile.CreateFromDirectory(FiletempPath,$"{curResInfo.OutputPath}/{zipName}");
  271. Directory.Delete(FiletempPath,true);
  272. needResList.Clear();
  273. Debug.Log($"资源检查完成 输出路径 : 【{curResInfo.OutputPath}】【{zipName}】");
  274. }
  275. private void CopyFile(string outP = "")
  276. {
  277. if (needResList == null)
  278. {
  279. return;
  280. }
  281. if (string.IsNullOrEmpty(outP))
  282. {
  283. outP = curResInfo.OutputPath;
  284. }
  285. int size = needResList.Count;
  286. string datapath = $"{curResInfo.NewPath}/";
  287. for (int i = 0; i < size; i++)
  288. {
  289. if (needResList[i].FullName == "Version")
  290. {
  291. continue;
  292. }
  293. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  294. FileHelper.WirteToFile( $"{outP}/{needResList[i].FullName}" ,fdatas);
  295. }
  296. FileHelper.WirteStringToFile($"{outP}/{curResInfo.VersionFileName}",curResInfo.ResVersion);
  297. byte[] mdatas = File.ReadAllBytes(datapath + curResInfo.mainfestFileName);
  298. FileHelper.WirteToFile($"{outP}/{curResInfo.mainfestFileName}", mdatas);
  299. }
  300. private List<VersionInfoData> GetInfoDatas(string path)
  301. {
  302. List<VersionInfoData> list = null;
  303. byte[] datas = null;
  304. if (FileSystem.Exists(path))
  305. {
  306. datas = File.ReadAllBytes(path);
  307. }
  308. if (datas != null)
  309. {
  310. list = SerizlizeResList(datas);
  311. }
  312. else
  313. {
  314. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  315. }
  316. return list;
  317. }
  318. private void ChangeResMD5(List<VersionInfoData> datas)
  319. {
  320. int[] ids = GenerateRandomArray(curResInfo.ChangeNum, 1, datas.Count);
  321. for (int i = 0; i < ids.Length; i++)
  322. {
  323. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  324. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  325. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  326. }
  327. }
  328. int[] GenerateRandomArray(int size, int min, int max)
  329. {
  330. List<int> array = new List<int>(size);
  331. if (size >= max - min)
  332. {
  333. for (int i = min; i < max; i++)
  334. {
  335. array.Add(i);
  336. }
  337. }
  338. else
  339. {
  340. while (size > 0)
  341. {
  342. int a = UnityEngine.Random.Range(min, max);
  343. if (!array.Contains(a))
  344. {
  345. array.Add(a);
  346. size--;
  347. }
  348. }
  349. }
  350. return array.ToArray();
  351. }
  352. private void CopyFiles()
  353. {
  354. string[] files1 = FileHelper.GetAllFileNmae(curResInfo.NewPath, "meta");
  355. int size = files1.Length;
  356. string datapath = $"{curResInfo.NewPath}/";
  357. for (int i = 0; i < size; i++)
  358. {
  359. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  360. FileHelper.WirteToFile($"{curResInfo.OutputPath}/{files1[i]}", fdatas);
  361. }
  362. }
  363. private void CopyTexture()
  364. {
  365. string datapath = $"{Application.dataPath}/Content/Icons/";
  366. FileHelper.CopyDir(datapath, curResInfo.OutputPath, "meta");
  367. }
  368. public static string GetABFileName(string abName,string s_ObscureKey)
  369. {
  370. try
  371. {
  372. using (var md5 = new MD5CryptoServiceProvider())
  373. {
  374. UTF8Encoding encoding = new UTF8Encoding(false);
  375. byte[] bytes = encoding.GetBytes((abName + s_ObscureKey).ToLower());
  376. bytes = md5.ComputeHash(bytes);
  377. StringBuilder sb = new StringBuilder();
  378. for (int i = 0; i < bytes.Length; i++)
  379. {
  380. sb.Append(bytes[i].ToString("x2"));
  381. }
  382. return sb.ToString();
  383. }
  384. }
  385. catch (Exception e)
  386. {
  387. Debug.LogException(e);
  388. }
  389. return abName;
  390. }
  391. private void MakeAbHxNameAssets()
  392. {
  393. List<VersionInfoData> baseData = GetInfoDatas(curResInfo.BaseNameFileInfoPath);
  394. AbNameHxInfo hxInfo = curResInfo.abNameHxInfo == null? new AbNameHxInfo():curResInfo.abNameHxInfo;
  395. if (hxInfo.Infos == null)
  396. hxInfo.Infos = new List<abNameInfo>();
  397. foreach (var item in baseData)
  398. {
  399. string hxName = GetABFileName(item.FullName,curResInfo.hxKey);
  400. abNameInfo nameInfo = new abNameInfo()
  401. {
  402. BaseName = item.FullName,
  403. HxName = hxName,
  404. };
  405. hxInfo.Infos.Add(nameInfo);
  406. }
  407. curResInfo.abNameHxInfo = hxInfo;
  408. }
  409. public Dictionary<string,string> GetCurInfoAbNameInfos()
  410. {
  411. Dictionary<string, string> abinfos = new Dictionary<string, string>();
  412. foreach (var item in curResInfo.abNameHxInfo.Infos)
  413. {
  414. abinfos.Add(item.HxName,item.BaseName);
  415. }
  416. return abinfos;
  417. }
  418. }