CheckHotUpdateRes.cs 16 KB

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