CheckHotUpdateRes.cs 11 KB

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