CheckHotUpdateRes.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. if (GUILayout.Button("复制mainfest资源到输出路径"))
  174. {
  175. CopyMainfestFile();
  176. }
  177. if (GUILayout.Button("删除mainfest没有的资源"))
  178. {
  179. DelateNoFile();
  180. }
  181. if (GUILayout.Button("Tolua"))
  182. {
  183. ChangeLuaWrapCs();
  184. }
  185. GUILayout.Box("", GUILayout.Width(790));
  186. GUILayout.BeginHorizontal();
  187. GUILayout.Space(60);
  188. curResInfo.IsIgnore =GUILayout.Toggle(curResInfo.IsIgnore, "是否开启忽略某些文件") ;
  189. GUILayout.Space(60);
  190. if (GUILayout.Button("制作混淆信息"))
  191. {
  192. MakeAbHxNameAssets();
  193. }
  194. GUILayout.EndHorizontal();
  195. }
  196. private List<VersionInfoData> SerizlizeResList(byte[] data)
  197. {
  198. CsvReader csvReader = new CsvReader(curResInfo.mainfestFileName, data);
  199. if (downloadFormatInfo == null)
  200. {
  201. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  202. }
  203. Type type = typeof(VersionInfoData);
  204. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  205. methodInfo?.Invoke(null, new object[] { csvReader });
  206. List<VersionInfoData> resList = VersionInfoData.AllData();
  207. VersionInfoData.Clear();
  208. return resList;
  209. }
  210. private void CheckRes()
  211. {
  212. needResList.Clear();
  213. oldPath = $"{curResInfo.OldPath}/{ curResInfo.mainfestFileName}";
  214. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  215. newPath = $"{curResInfo.NewPath}/{ curResInfo.mainfestFileName}";
  216. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  217. ChangeResMD5(newlist);
  218. Dictionary<string, string> abNamedic = null;
  219. if (curResInfo.IsHx)
  220. {
  221. abNamedic= GetCurInfoAbNameInfos();
  222. }
  223. if (Oldlist != null && newlist != null)
  224. {
  225. int size = newlist.Count;
  226. for (int i = 0; i < size; i++)
  227. {
  228. if (newlist[i].FullName == curResInfo.mainfestFileName || newlist[i].FullName == curResInfo.VersionFileName)
  229. {
  230. continue;
  231. }
  232. bool isignore = false;
  233. string baseName = newlist[i].FullName; ;
  234. RG_Ignore_Info rG_Ignore_Info = null;
  235. if (curResInfo.IsHx && abNamedic.ContainsKey(newlist[i].FullName))
  236. {
  237. baseName = abNamedic[newlist[i].FullName];
  238. }
  239. rG_Ignore_Info = curResInfo.Rg_Essential_Infos.FindFirst(it => !it.IsIgnore && baseName.Contains(it.Field));
  240. bool isEssential = rG_Ignore_Info != null;
  241. if (curResInfo.IsIgnore && !isEssential)
  242. {
  243. string igstr = curResInfo.IgnoreFiles.FindFirst(it => it == baseName);
  244. rG_Ignore_Info = curResInfo.rG_Ignore_Infos.FindFirst(it => it.IsIgnore && baseName.Contains( it.Field));
  245. if (!string.IsNullOrEmpty(igstr) || rG_Ignore_Info != null)
  246. {
  247. isignore = true;
  248. Debug.Log("=========忽略=============" + baseName);
  249. }
  250. }
  251. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  252. if (ores != null )
  253. {
  254. if (isignore)
  255. {
  256. newlist[i].MD5 = ores.MD5;
  257. newlist[i].Size = ores.Size;
  258. }
  259. else if (newlist[i].MD5 != ores.MD5)
  260. {
  261. Debug.Log($"新 {newlist[i].FullName} MD5=[{newlist[i].MD5}] Base = [{baseName}]");
  262. Debug.Log($"旧 {ores.FullName} MD5=[{ores.MD5}]");
  263. needResList.Add(newlist[i]);
  264. }
  265. }
  266. else
  267. {
  268. Debug.Log($"新 {newlist[i].FullName} MD5=[{newlist[i].MD5}] Base = [{baseName}]");
  269. needResList.Add(newlist[i]);
  270. }
  271. }
  272. }
  273. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  274. csvWriter.Write();
  275. CopyFile();
  276. string FiletempPath = $"{curResInfo.OutputPath}/../ZipTempPath";
  277. if(Directory.Exists(FiletempPath))
  278. Directory.Delete(FiletempPath,true);
  279. Directory.CreateDirectory(FiletempPath);
  280. CopyFile(FiletempPath);
  281. string zipName = $"{curResInfo.CN_Name}-热更资源---{curResInfo.ResVersion}---{DateTime.Now.Month}.{DateTime.Now.Day}--{DateTime.Now.Hour}.{DateTime.Now.Minute}.zip";
  282. ZipFile.CreateFromDirectory(FiletempPath,$"{curResInfo.OutputPath}/{zipName}");
  283. Directory.Delete(FiletempPath,true);
  284. needResList.Clear();
  285. Debug.Log($"资源检查完成 输出路径 : 【{curResInfo.OutputPath}】【{zipName}】");
  286. }
  287. private void CopyFile(string outP = "")
  288. {
  289. if (needResList == null)
  290. {
  291. return;
  292. }
  293. if (string.IsNullOrEmpty(outP))
  294. {
  295. outP = curResInfo.OutputPath;
  296. }
  297. int size = needResList.Count;
  298. string datapath = $"{curResInfo.NewPath}/";
  299. for (int i = 0; i < size; i++)
  300. {
  301. if (needResList[i].FullName == "Version")
  302. {
  303. continue;
  304. }
  305. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  306. FileHelper.WirteToFile( $"{outP}/{needResList[i].FullName}" ,fdatas);
  307. }
  308. FileHelper.WirteStringToFile($"{outP}/{curResInfo.VersionFileName}",curResInfo.ResVersion);
  309. byte[] mdatas = File.ReadAllBytes(datapath + curResInfo.mainfestFileName);
  310. FileHelper.WirteToFile($"{outP}/{curResInfo.mainfestFileName}", mdatas);
  311. }
  312. private void CopyMainfestFile()
  313. {
  314. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  315. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  316. if (newlist == null)
  317. {
  318. return;
  319. }
  320. int size = newlist.Count;
  321. string datapath = $"{info.NewPath}/";
  322. for (int i = 0; i < size; i++)
  323. {
  324. if (newlist[i].FullName == "Version")
  325. {
  326. continue;
  327. }
  328. byte[] fdatas = File.ReadAllBytes(datapath + newlist[i].FullName);
  329. FileHelper.WirteToFile($"{info.OutputPath}/{newlist[i].FullName}", fdatas);
  330. }
  331. }
  332. private void DelateNoFile()
  333. {
  334. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  335. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  336. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  337. int size = files1.Length;
  338. string datapath = $"{info.NewPath}/";
  339. for (int i = 0; i < size; i++)
  340. {
  341. if (files1[i] == "afivs" || files1[i] == "afimft")
  342. {
  343. continue;
  344. }
  345. if (newlist.FindFirst(it => it.FullName == files1[i]) == null)
  346. {
  347. FileHelper.DeleteFile(datapath + files1[i]);
  348. }
  349. }
  350. }
  351. private List<VersionInfoData> GetInfoDatas(string path)
  352. {
  353. List<VersionInfoData> list = null;
  354. byte[] datas = null;
  355. if (FileSystem.Exists(path))
  356. {
  357. datas = File.ReadAllBytes(path);
  358. }
  359. if (datas != null)
  360. {
  361. list = SerizlizeResList(datas);
  362. }
  363. else
  364. {
  365. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  366. }
  367. return list;
  368. }
  369. private void ChangeResMD5(List<VersionInfoData> datas)
  370. {
  371. int[] ids = GenerateRandomArray(curResInfo.ChangeNum, 1, datas.Count);
  372. for (int i = 0; i < ids.Length; i++)
  373. {
  374. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  375. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  376. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  377. }
  378. }
  379. int[] GenerateRandomArray(int size, int min, int max)
  380. {
  381. List<int> array = new List<int>(size);
  382. if (size >= max - min)
  383. {
  384. for (int i = min; i < max; i++)
  385. {
  386. array.Add(i);
  387. }
  388. }
  389. else
  390. {
  391. while (size > 0)
  392. {
  393. int a = UnityEngine.Random.Range(min, max);
  394. if (!array.Contains(a))
  395. {
  396. array.Add(a);
  397. size--;
  398. }
  399. }
  400. }
  401. return array.ToArray();
  402. }
  403. private void CopyFiles()
  404. {
  405. string[] files1 = FileHelper.GetAllFileNmae(curResInfo.NewPath, "meta");
  406. int size = files1.Length;
  407. string datapath = $"{curResInfo.NewPath}/";
  408. for (int i = 0; i < size; i++)
  409. {
  410. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  411. FileHelper.WirteToFile($"{curResInfo.OutputPath}/{files1[i]}", fdatas);
  412. }
  413. }
  414. private void CopyTexture()
  415. {
  416. string datapath = $"{Application.dataPath}/Content/Icons/";
  417. FileHelper.CopyDir(datapath, curResInfo.OutputPath, "meta");
  418. }
  419. private void ChangeLuaWrapCs()
  420. {
  421. //ChangeLuaWrapCs("ToLua/BaseType");
  422. ChangeLuaWrapCs("ToLua/Source/Generate");
  423. }
  424. private void ChangeLuaWrapCs(string path)
  425. {
  426. string luapath = $"{Application.dataPath}/{path}";//ToLua/BaseType//ToLua/Source/Generate
  427. string[] files1 = FileHelper.GetAllFileNmae(luapath, "meta");
  428. int size = files1.Length;
  429. string datapath = $"{luapath}/";
  430. for (int i = 0; i < size; i++)
  431. {
  432. //AddLuaWrapBenginClassType(datapath + files1[i]);
  433. AddLuaWrapBenginClassType(datapath + files1[i], "BeginEnum");
  434. }
  435. }
  436. //L.BeginClass(typeof(SingletonMono<LuaMgr>), typeof(MonoBase), \"SingletonMono_LuaMgr\");
  437. private void AddLuaWrapBenginClassType(string path,string ckStr = "BeginClass")
  438. {
  439. if (String.IsNullOrEmpty(path)) return;
  440. string[] fdatas = File.ReadAllLines(path); //new string[] { "L.BeginClass(typeof(SingletonMono<LuaMgr>), typeof(MonoBase), \"SingletonMono_LuaMgr\");" };//
  441. int length = fdatas.Length;
  442. string newstr = "";
  443. for (int i = 0; i < length; i++)
  444. {
  445. if (fdatas[i].Contains(ckStr))
  446. {
  447. string[] strarr = fdatas[i].Split("typeof(",StringSplitOptions.RemoveEmptyEntries);
  448. string classname = strarr[1].Substring(0, strarr[1].IndexOf(")"));
  449. if (classname.Contains("."))
  450. {
  451. int stid = classname.LastIndexOf('.');
  452. classname = classname.Substring(stid+1);
  453. }
  454. if (classname.Contains("<"))
  455. {
  456. string[] names = classname.Split(new char[]{ '<','>'},StringSplitOptions.RemoveEmptyEntries);
  457. if (names.Length == 1)
  458. {
  459. classname = names[0];
  460. }
  461. else if (names[1].Contains(','))
  462. {
  463. string[] subNames = names[1].Split(',',StringSplitOptions.RemoveEmptyEntries);
  464. classname = names[0];
  465. foreach (var item in subNames)
  466. {
  467. classname = $"{classname}_{item}";
  468. }
  469. }
  470. else
  471. {
  472. classname = $"{names[0]}_{names[1]}";
  473. }
  474. }
  475. Debug.Log($"Class Name = {classname}");
  476. string nameStr = $"\"{classname}\"";
  477. if (!fdatas[i].Contains(nameStr))
  478. {
  479. int endid = fdatas[i].LastIndexOf(")");
  480. newstr = fdatas[i].Substring(0, endid);
  481. newstr += $", {nameStr});";
  482. fdatas[i] = newstr;
  483. Debug.Log($"New String = {newstr}");
  484. }
  485. break;
  486. }
  487. }
  488. if (newstr != "")
  489. {
  490. //Debug.Log("写");
  491. File.WriteAllLines(path,fdatas);
  492. }
  493. }
  494. public static string GetABFileName(string abName,string s_ObscureKey)
  495. {
  496. try
  497. {
  498. using (var md5 = new MD5CryptoServiceProvider())
  499. {
  500. UTF8Encoding encoding = new UTF8Encoding(false);
  501. byte[] bytes = encoding.GetBytes((abName + s_ObscureKey).ToLower());
  502. bytes = md5.ComputeHash(bytes);
  503. StringBuilder sb = new StringBuilder();
  504. for (int i = 0; i < bytes.Length; i++)
  505. {
  506. sb.Append(bytes[i].ToString("x2"));
  507. }
  508. return sb.ToString();
  509. }
  510. }
  511. catch (Exception e)
  512. {
  513. Debug.LogException(e);
  514. }
  515. return abName;
  516. }
  517. private void MakeAbHxNameAssets()
  518. {
  519. List<VersionInfoData> baseData = GetInfoDatas(curResInfo.BaseNameFileInfoPath);
  520. AbNameHxInfo hxInfo = curResInfo.abNameHxInfo == null? new AbNameHxInfo():curResInfo.abNameHxInfo;
  521. if (hxInfo.Infos == null)
  522. hxInfo.Infos = new List<abNameInfo>();
  523. foreach (var item in baseData)
  524. {
  525. string hxName = GetABFileName(item.FullName,curResInfo.hxKey);
  526. abNameInfo nameInfo = new abNameInfo()
  527. {
  528. BaseName = item.FullName,
  529. HxName = hxName,
  530. };
  531. hxInfo.Infos.Add(nameInfo);
  532. }
  533. curResInfo.abNameHxInfo = hxInfo;
  534. }
  535. public Dictionary<string,string> GetCurInfoAbNameInfos()
  536. {
  537. Dictionary<string, string> abinfos = new Dictionary<string, string>();
  538. foreach (var item in curResInfo.abNameHxInfo.Infos)
  539. {
  540. abinfos.Add(item.HxName,item.BaseName);
  541. }
  542. return abinfos;
  543. }
  544. }