CheckHotUpdateRes.cs 17 KB

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