CheckHotUpdateRes.cs 11 KB

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