CheckHotUpdateRes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. }
  110. private void DrawCheckBtn()
  111. {
  112. GUILayout.Box("", GUILayout.Width(790));
  113. GUILayout.BeginHorizontal();
  114. GUILayout.Space(10);
  115. if (GUILayout.Button("生成MD5资源版本文件"))
  116. {
  117. BundleBuilderZ.CreateMD5File(info.ResVersion);
  118. }
  119. GUILayout.Space(60);
  120. if (GUILayout.Button("对比新旧资源"))
  121. {
  122. CheckRes();
  123. }
  124. GUILayout.EndHorizontal();
  125. GUILayout.Box("", GUILayout.Width(790));
  126. if (GUILayout.Button("复制所以资源到输出路径"))
  127. {
  128. CopyFiles();
  129. }
  130. if (GUILayout.Button("复制图片资源到输出路径"))
  131. {
  132. CopyTexture();
  133. }
  134. if (GUILayout.Button("复制PSD资源到输出路径"))
  135. {
  136. CopyPsd();
  137. }
  138. GUILayout.Box("", GUILayout.Width(790));
  139. GUILayout.BeginHorizontal();
  140. GUILayout.Space(60);
  141. info.IsIgnore =GUILayout.Toggle(info.IsIgnore, "是否开启忽略某些文件") ;
  142. GUILayout.EndHorizontal();
  143. }
  144. private List<VersionInfoData> SerizlizeResList(byte[] data)
  145. {
  146. CsvReader csvReader = new CsvReader(info.mainfestFileName, data);
  147. if (downloadFormatInfo == null)
  148. {
  149. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  150. }
  151. Type type = typeof(VersionInfoData);
  152. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  153. methodInfo?.Invoke(null, new object[] { csvReader });
  154. List<VersionInfoData> resList = VersionInfoData.AllData();
  155. VersionInfoData.Clear();
  156. return resList;
  157. }
  158. private void CheckRes()
  159. {
  160. needResList.Clear();
  161. oldPath = $"{info.OldPath}/{ info.mainfestFileName}";
  162. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  163. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  164. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  165. ChangeResMD5(newlist);
  166. if (Oldlist != null && newlist != null)
  167. {
  168. int size = newlist.Count;
  169. for (int i = 0; i < size; i++)
  170. {
  171. if (newlist[i].FullName == info.mainfestFileName || newlist[i].FullName == info.VersionFileName)
  172. {
  173. continue;
  174. }
  175. bool isignore = false;
  176. if (info.IsIgnore)
  177. {
  178. string igstr = info.IgnoreFiles.FindFirst(it => it == newlist[i].FullName);
  179. if (!string.IsNullOrEmpty(igstr))
  180. {
  181. isignore = true;
  182. Debug.Log("======================" + newlist[i].FullName);
  183. }
  184. }
  185. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  186. if (ores != null )
  187. {
  188. if (isignore)
  189. {
  190. newlist[i].MD5 = ores.MD5;
  191. newlist[i].Size = ores.Size;
  192. }
  193. else if (newlist[i].MD5 != ores.MD5)
  194. {
  195. needResList.Add(newlist[i]);
  196. }
  197. }
  198. else
  199. {
  200. needResList.Add(newlist[i]);
  201. }
  202. }
  203. }
  204. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  205. csvWriter.Write();
  206. CopyFile();
  207. needResList.Clear();
  208. Debug.Log($"资源检查完成 输出路径 : 【{info.OutputPath}】");
  209. }
  210. private void CopyFile()
  211. {
  212. if (needResList == null)
  213. {
  214. return;
  215. }
  216. int size = needResList.Count;
  217. string datapath = $"{info.NewPath}/";
  218. StringBuilder ChangedataUrls = new StringBuilder();
  219. for (int i = 0; i < size; i++)
  220. {
  221. ChangedataUrls.Append($"http://weix.vvfyj.cn/res/WDAndroidRes/{ needResList[i].FullName}\n\r");
  222. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  223. FileHelper.WirteToFile( $"{info.OutputPath}/{needResList[i].FullName}" ,fdatas);
  224. }
  225. FileHelper.WirteStringToFile($"{info.OutputPath}/{info.VersionFileName}",info.ResVersion);
  226. byte[] mdatas = File.ReadAllBytes(datapath + info.mainfestFileName);
  227. ChangedataUrls.Append($"http://weix.vvfyj.cn/res/WDAndroidRes/{ info.mainfestFileName}\n\r");
  228. ChangedataUrls.Append($"http://weix.vvfyj.cn/res/WDAndroidRes/{ info.VersionFileName}\n\r");
  229. FileHelper.WirteToFile($"{info.OutputPath}/{info.mainfestFileName}", mdatas);
  230. FileHelper.WirteToFile($"{info.OutputPath}/ChangeFileUrls.txt", Encoding.UTF8.GetBytes(ChangedataUrls.ToString()));
  231. }
  232. private List<VersionInfoData> GetInfoDatas(string path)
  233. {
  234. List<VersionInfoData> list = null;
  235. byte[] datas = null;
  236. if (FileSystem.Exists(path))
  237. {
  238. datas = File.ReadAllBytes(path);
  239. }
  240. if (datas != null)
  241. {
  242. list = SerizlizeResList(datas);
  243. }
  244. else
  245. {
  246. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  247. }
  248. return list;
  249. }
  250. private void ChangeResMD5(List<VersionInfoData> datas)
  251. {
  252. int[] ids = GenerateRandomArray(info.ChangeNum, 1, datas.Count);
  253. for (int i = 0; i < ids.Length; i++)
  254. {
  255. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  256. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  257. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  258. }
  259. }
  260. int[] GenerateRandomArray(int size, int min, int max)
  261. {
  262. List<int> array = new List<int>(size);
  263. if (size >= max - min)
  264. {
  265. for (int i = min; i < max; i++)
  266. {
  267. array.Add(i);
  268. }
  269. }
  270. else
  271. {
  272. while (size > 0)
  273. {
  274. int a = UnityEngine.Random.Range(min, max);
  275. if (!array.Contains(a))
  276. {
  277. array.Add(a);
  278. size--;
  279. }
  280. }
  281. }
  282. return array.ToArray();
  283. }
  284. private void CopyFiles()
  285. {
  286. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  287. int size = files1.Length;
  288. string datapath = $"{info.NewPath}/";
  289. for (int i = 0; i < size; i++)
  290. {
  291. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  292. FileHelper.WirteToFile($"{info.OutputPath}/{files1[i]}", fdatas);
  293. }
  294. }
  295. private void CopyTexture()
  296. {
  297. string datapath = $"{Application.dataPath}/Content/Icons/";
  298. FileHelper.CopyDir(datapath, info.OutputPath, "meta");
  299. }
  300. private void CopyPsd()
  301. {
  302. string datapath = $"{Application.dataPath}/";
  303. FileHelper.CopyDir_Only(datapath, info.OutputPath, ".psd");
  304. }
  305. }