CheckHotUpdateRes.cs 16 KB

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