CheckHotUpdateRes.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.Text;
  10. using System.IO.Compression;
  11. public class CheckHotUpdateRes : EditorWindow
  12. {
  13. public class VersionInfoData:GameData<VersionInfoData>
  14. {
  15. public string FullName;
  16. public string MD5;
  17. public ulong Size;
  18. public VersionInfoData()
  19. {
  20. FullName = "";
  21. MD5 = "";
  22. Size = 0;
  23. }
  24. private static void OnCsvLoad(CsvReader csvReader)
  25. {
  26. VersionInfoData.Onload(csvReader);
  27. }
  28. }
  29. [MenuItem("AssetBundle/热更相关")]
  30. public static void OpenCheckHotUpdateWindow()
  31. {
  32. CheckHotUpdateRes window = GetWindow<CheckHotUpdateRes>("热更相关");
  33. window.maxSize = new Vector2(800, 600);
  34. window.minSize = new Vector2(600, 400);
  35. window.Init();
  36. window.Show();
  37. }
  38. private string oldPath = "";
  39. private string newPath = "";
  40. private CheckResWindowInfo info;
  41. private ResInfo curResInfo;
  42. private List<VersionInfoData> needResList;
  43. private GameDataFormatInfo downloadFormatInfo;
  44. public void Init()
  45. {
  46. string path = "Assets/Editor/AssetBundle/CheckHotResWindownInfo.asset";
  47. //Debug.Log();
  48. info = AssetDatabase.LoadAssetAtPath<CheckResWindowInfo>(path);
  49. curResInfo = info.GetResInfo();
  50. if (curResInfo == null)
  51. {
  52. curResInfo = new ResInfo();
  53. }
  54. #if UNITY_IOS
  55. info.VersionFileName = "afivs";
  56. info.mainfestFileName = "afimft";
  57. info.NewPath = Application.dataPath + "/StreamingAssets/ios";
  58. //#else
  59. // versionFileName = "Version";
  60. // assetsFileName = "mainfest";
  61. #endif
  62. needResList = new List<VersionInfoData>();
  63. }
  64. private void OnGUI()
  65. {
  66. DrowSelectFolder("旧版本文件", "选择文件路径", "选择旧版本资源MD5文件路径", ref curResInfo.OldPath);
  67. DrowSelectFolder("新版本文件", "选择文件路径", "选择新版本资源MD5文件路径", ref curResInfo.NewPath);
  68. DrowSelectFolder("需跟新文件输出路径", "选择文件路径", "选择需跟新文件输出路径", ref curResInfo.OutputPath);
  69. DrawInfo();
  70. DrawCheckBtn();
  71. }
  72. private void DrowSelectFolder(string name, string btnName, string savepath, ref string path)
  73. {
  74. GUILayout.Box("", GUILayout.Width(790));
  75. GUILayout.BeginHorizontal();
  76. GUILayout.Space(10);
  77. GUILayout.Label(name, GUILayout.Width(120));
  78. GUILayout.Space(10);
  79. path = GUILayout.TextField(path, GUILayout.Width(360));
  80. if (GUILayout.Button(btnName, GUILayout.Width(100)))
  81. {
  82. string getstr = EditorUtility.SaveFolderPanel(savepath, path, "");
  83. if(!string.IsNullOrEmpty(getstr))
  84. {
  85. path = getstr;
  86. }
  87. }
  88. GUILayout.Space(10);
  89. GUILayout.EndHorizontal();
  90. }
  91. private void DrawInfo()
  92. {
  93. GUILayout.Box("", GUILayout.Width(790));
  94. GUILayout.BeginHorizontal();
  95. GUILayout.Label("平台", GUILayout.Width(65));
  96. BuildTag tag = (BuildTag)EditorGUILayout.EnumPopup(info.buildTag, GUILayout.Width(100));
  97. if (tag != info.buildTag)
  98. {
  99. info.buildTag = tag;
  100. ResInfo ri = info.GetResInfo();
  101. if (ri != null)
  102. {
  103. curResInfo = ri;
  104. }
  105. }
  106. GUILayout.EndHorizontal();
  107. GUILayout.Box("", GUILayout.Width(790));
  108. GUILayout.BeginHorizontal();
  109. GUILayout.Space(10);
  110. GUILayout.Label("版本文件名:", GUILayout.Width(65));
  111. GUILayout.Space(10);
  112. curResInfo.VersionFileName = GUILayout.TextField(curResInfo.VersionFileName, GUILayout.Width(160));
  113. GUILayout.Space(60);
  114. GUILayout.Label("版本文件信息文件名:", GUILayout.Width(120));
  115. GUILayout.Space(10);
  116. curResInfo.mainfestFileName = GUILayout.TextField(curResInfo.mainfestFileName, GUILayout.Width(160));
  117. GUILayout.EndHorizontal();
  118. GUILayout.Box("", GUILayout.Width(790));
  119. GUILayout.BeginHorizontal();
  120. GUILayout.Space(10);
  121. GUILayout.Label("资源版本:", GUILayout.Width(65));
  122. GUILayout.Space(10);
  123. curResInfo.ResVersion = GUILayout.TextField(curResInfo.ResVersion, GUILayout.Width(160));
  124. GUILayout.Space(10);
  125. if (GUILayout.Button("读取版本", GUILayout.Width(100)))
  126. {
  127. curResInfo.ResVersion = File.ReadAllText($"{curResInfo.OldPath}/{curResInfo.VersionFileName}");
  128. }
  129. GUILayout.Space(10);
  130. if (GUILayout.Button("版本+1", GUILayout.Width(100)))
  131. {
  132. VersionCode lVersionCode = curResInfo.ResVersion;
  133. lVersionCode.patch += 1;
  134. curResInfo.ResVersion = lVersionCode.ToString();
  135. }
  136. GUILayout.Space(10);
  137. GUILayout.EndHorizontal();
  138. GUILayout.BeginHorizontal();
  139. GUILayout.Label("语言:", GUILayout.Width(65));
  140. GUILayout.Space(10);
  141. curResInfo.Language = (BuildLanguage)EditorGUILayout.EnumPopup(curResInfo.Language, GUILayout.Width(100));
  142. GUILayout.Space(10);
  143. GUILayout.EndHorizontal();
  144. }
  145. private void DrawCheckBtn()
  146. {
  147. GUILayout.Box("", GUILayout.Width(790));
  148. GUILayout.BeginHorizontal();
  149. GUILayout.Space(10);
  150. if (GUILayout.Button("生成MD5资源版本文件"))
  151. {
  152. BundleBuilderZ.CreateMD5File(curResInfo.ResVersion);
  153. }
  154. GUILayout.Space(60);
  155. if (GUILayout.Button("对比新旧资源"))
  156. {
  157. CheckRes();
  158. }
  159. GUILayout.EndHorizontal();
  160. GUILayout.Box("", GUILayout.Width(790));
  161. if (GUILayout.Button("复制所以资源到输出路径"))
  162. {
  163. CopyFiles();
  164. }
  165. if (GUILayout.Button("复制图片资源到输出路径"))
  166. {
  167. CopyTexture();
  168. }
  169. if (GUILayout.Button("复制PSD资源到输出路径"))
  170. {
  171. CopyPsd();
  172. }
  173. GUILayout.Box("", GUILayout.Width(790));
  174. GUILayout.BeginHorizontal();
  175. GUILayout.Space(60);
  176. curResInfo.IsIgnore =GUILayout.Toggle(curResInfo.IsIgnore, "是否开启忽略某些文件") ;
  177. GUILayout.EndHorizontal();
  178. }
  179. private List<VersionInfoData> SerizlizeResList(byte[] data)
  180. {
  181. CsvReader csvReader = new CsvReader(curResInfo.mainfestFileName, data);
  182. if (downloadFormatInfo == null)
  183. {
  184. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  185. }
  186. Type type = typeof(VersionInfoData);
  187. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  188. methodInfo?.Invoke(null, new object[] { csvReader });
  189. List<VersionInfoData> resList = VersionInfoData.AllData();
  190. VersionInfoData.Clear();
  191. return resList;
  192. }
  193. private void CheckRes()
  194. {
  195. needResList.Clear();
  196. oldPath = $"{curResInfo.OldPath}/{ curResInfo.mainfestFileName}";
  197. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  198. newPath = $"{curResInfo.NewPath}/{ curResInfo.mainfestFileName}";
  199. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  200. ChangeResMD5(newlist);
  201. if (Oldlist != null && newlist != null)
  202. {
  203. int size = newlist.Count;
  204. for (int i = 0; i < size; i++)
  205. {
  206. if (newlist[i].FullName == curResInfo.mainfestFileName || newlist[i].FullName == curResInfo.VersionFileName)
  207. {
  208. continue;
  209. }
  210. bool isignore = false;
  211. if (curResInfo.IsIgnore)
  212. {
  213. string igstr = curResInfo.IgnoreFiles.FindFirst(it => it == newlist[i].FullName);
  214. if (!string.IsNullOrEmpty(igstr))
  215. {
  216. isignore = true;
  217. Debug.Log("======================" + newlist[i].FullName);
  218. }
  219. }
  220. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  221. if (ores != null )
  222. {
  223. if (isignore)
  224. {
  225. newlist[i].MD5 = ores.MD5;
  226. newlist[i].Size = ores.Size;
  227. }
  228. else if (newlist[i].MD5 != ores.MD5)
  229. {
  230. needResList.Add(newlist[i]);
  231. }
  232. }
  233. else
  234. {
  235. needResList.Add(newlist[i]);
  236. }
  237. }
  238. }
  239. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  240. csvWriter.Write();
  241. CopyFile();
  242. string FiletempPath = $"{curResInfo.OutputPath}/../ZipTempPath";
  243. if(Directory.Exists(FiletempPath))
  244. Directory.Delete(FiletempPath,true);
  245. Directory.CreateDirectory(FiletempPath);
  246. CopyFile(FiletempPath);
  247. string zipName = $"{curResInfo.CN_Name}-热更资源---{curResInfo.ResVersion}---{DateTime.Now.Month}.{DateTime.Now.Day}--{DateTime.Now.Hour}.{DateTime.Now.Minute}.zip";
  248. ZipFile.CreateFromDirectory(FiletempPath,$"{curResInfo.OutputPath}/{zipName}");
  249. Directory.Delete(FiletempPath,true);
  250. needResList.Clear();
  251. Debug.Log($"资源检查完成 输出路径 : 【{curResInfo.OutputPath}】【{zipName}】");
  252. }
  253. private void CopyFile(string outP = "")
  254. {
  255. if (needResList == null)
  256. {
  257. return;
  258. }
  259. if (string.IsNullOrEmpty(outP))
  260. {
  261. outP = curResInfo.OutputPath;
  262. }
  263. int size = needResList.Count;
  264. string datapath = $"{curResInfo.NewPath}/";
  265. StringBuilder ChangedataUrls = new StringBuilder();
  266. for (int i = 0; i < size; i++)
  267. {
  268. ChangedataUrls.Append($"http://weix.vvfyj.cn/res/WDAndroidRes/{ needResList[i].FullName}\n\r");
  269. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  270. FileHelper.WirteToFile( $"{outP}/{needResList[i].FullName}" ,fdatas);
  271. }
  272. FileHelper.WirteStringToFile($"{outP}/{curResInfo.VersionFileName}",curResInfo.ResVersion);
  273. byte[] mdatas = File.ReadAllBytes(datapath + curResInfo.mainfestFileName);
  274. ChangedataUrls.Append($"http://weix.vvfyj.cn/res/WDAndroidRes/{ info.mainfestFileName}\n\r");
  275. ChangedataUrls.Append($"http://weix.vvfyj.cn/res/WDAndroidRes/{ info.VersionFileName}\n\r");
  276. //FileHelper.WirteToFile($"{info.OutputPath}/{info.mainfestFileName}", mdatas);
  277. FileHelper.WirteToFile($"{outP}/{curResInfo.mainfestFileName}", mdatas);
  278. FileHelper.WirteToFile($"{info.OutputPath}/ChangeFileUrls.txt", Encoding.UTF8.GetBytes(ChangedataUrls.ToString()));
  279. }
  280. private List<VersionInfoData> GetInfoDatas(string path)
  281. {
  282. List<VersionInfoData> list = null;
  283. byte[] datas = null;
  284. if (FileSystem.Exists(path))
  285. {
  286. datas = File.ReadAllBytes(path);
  287. }
  288. if (datas != null)
  289. {
  290. list = SerizlizeResList(datas);
  291. }
  292. else
  293. {
  294. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  295. }
  296. return list;
  297. }
  298. private void ChangeResMD5(List<VersionInfoData> datas)
  299. {
  300. int[] ids = GenerateRandomArray(curResInfo.ChangeNum, 1, datas.Count);
  301. for (int i = 0; i < ids.Length; i++)
  302. {
  303. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  304. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  305. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  306. }
  307. }
  308. int[] GenerateRandomArray(int size, int min, int max)
  309. {
  310. List<int> array = new List<int>(size);
  311. if (size >= max - min)
  312. {
  313. for (int i = min; i < max; i++)
  314. {
  315. array.Add(i);
  316. }
  317. }
  318. else
  319. {
  320. while (size > 0)
  321. {
  322. int a = UnityEngine.Random.Range(min, max);
  323. if (!array.Contains(a))
  324. {
  325. array.Add(a);
  326. size--;
  327. }
  328. }
  329. }
  330. return array.ToArray();
  331. }
  332. private void CopyFiles()
  333. {
  334. string[] files1 = FileHelper.GetAllFileNmae(curResInfo.NewPath, "meta");
  335. int size = files1.Length;
  336. string datapath = $"{curResInfo.NewPath}/";
  337. for (int i = 0; i < size; i++)
  338. {
  339. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  340. FileHelper.WirteToFile($"{curResInfo.OutputPath}/{files1[i]}", fdatas);
  341. }
  342. }
  343. private void CopyTexture()
  344. {
  345. string datapath = $"{Application.dataPath}/Content/Icons/";
  346. FileHelper.CopyDir(datapath, curResInfo.OutputPath, "meta");
  347. }
  348. private void CopyPsd()
  349. {
  350. string datapath = $"{Application.dataPath}/";
  351. FileHelper.CopyDir_Only(datapath, info.OutputPath, ".psd");
  352. }
  353. }