CheckHotUpdateRes.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #if UNITY_IOS
  47. info.VersionFileName = "afivs";
  48. info.mainfestFileName = "afimft";
  49. info.NewPath = Application.dataPath + "/StreamingAssets/unityRes";
  50. //#else
  51. // versionFileName = "Version";
  52. // assetsFileName = "mainfest";
  53. #endif
  54. needResList = new List<VersionInfoData>();
  55. }
  56. private void OnGUI()
  57. {
  58. DrowSelectFolder("旧版本文件", "选择文件路径", "选择旧版本资源MD5文件路径", ref info.OldPath);
  59. DrowSelectFolder("新版本文件", "选择文件路径", "选择新版本资源MD5文件路径", ref info.NewPath);
  60. DrowSelectFolder("需跟新文件输出路径", "选择文件路径", "选择需跟新文件输出路径", ref info.OutputPath);
  61. DrawInfo();
  62. DrawCheckBtn();
  63. }
  64. private void DrowSelectFolder(string name, string btnName, string savepath, ref string path)
  65. {
  66. GUILayout.Box("", GUILayout.Width(790));
  67. GUILayout.BeginHorizontal();
  68. GUILayout.Space(10);
  69. GUILayout.Label(name, GUILayout.Width(120));
  70. GUILayout.Space(10);
  71. path = GUILayout.TextField(path, GUILayout.Width(360));
  72. if (GUILayout.Button(btnName, GUILayout.Width(100)))
  73. {
  74. string getstr = EditorUtility.SaveFolderPanel(savepath, path, "");
  75. if(!string.IsNullOrEmpty(getstr))
  76. {
  77. path = getstr;
  78. }
  79. }
  80. GUILayout.Space(10);
  81. GUILayout.EndHorizontal();
  82. }
  83. private void DrawInfo()
  84. {
  85. GUILayout.Box("", GUILayout.Width(790));
  86. GUILayout.BeginHorizontal();
  87. GUILayout.Space(10);
  88. GUILayout.Label("版本文件名:", GUILayout.Width(65));
  89. GUILayout.Space(10);
  90. info.VersionFileName = GUILayout.TextField(info.VersionFileName, GUILayout.Width(160));
  91. GUILayout.Space(60);
  92. GUILayout.Label("版本文件信息文件名:", GUILayout.Width(120));
  93. GUILayout.Space(10);
  94. info.mainfestFileName = GUILayout.TextField(info.mainfestFileName, GUILayout.Width(160));
  95. GUILayout.EndHorizontal();
  96. GUILayout.Box("", GUILayout.Width(790));
  97. GUILayout.BeginHorizontal();
  98. GUILayout.Space(10);
  99. GUILayout.Label("资源版本:", GUILayout.Width(65));
  100. GUILayout.Space(10);
  101. info.ResVersion = GUILayout.TextField(info.ResVersion, GUILayout.Width(160));
  102. GUILayout.Space(10);
  103. if (GUILayout.Button("读取版本", GUILayout.Width(100)))
  104. {
  105. info.ResVersion = File.ReadAllText($"{info.OldPath}/{info.VersionFileName}");
  106. }
  107. GUILayout.Space(10);
  108. if (GUILayout.Button("版本+1", GUILayout.Width(100)))
  109. {
  110. VersionCode lVersionCode = info.ResVersion;
  111. lVersionCode.patch += 1;
  112. info.ResVersion = lVersionCode.ToString();
  113. }
  114. GUILayout.Space(10);
  115. GUILayout.EndHorizontal();
  116. }
  117. private void DrawCheckBtn()
  118. {
  119. GUILayout.Box("", GUILayout.Width(790));
  120. GUILayout.BeginHorizontal();
  121. GUILayout.Space(10);
  122. if (GUILayout.Button("生成MD5资源版本文件"))
  123. {
  124. BundleBuilderZ.CreateMD5File(info.ResVersion);
  125. }
  126. GUILayout.Space(60);
  127. if (GUILayout.Button("对比新旧资源"))
  128. {
  129. CheckRes();
  130. }
  131. GUILayout.EndHorizontal();
  132. GUILayout.Box("", GUILayout.Width(790));
  133. if (GUILayout.Button("复制所以资源到输出路径"))
  134. {
  135. CopyFiles();
  136. }
  137. if (GUILayout.Button("复制图片资源到输出路径"))
  138. {
  139. CopyTexture();
  140. }
  141. if (GUILayout.Button("复制mainfest资源到输出路径"))
  142. {
  143. CopyMainfestFile();
  144. }
  145. if (GUILayout.Button("删除mainfest没有的资源"))
  146. {
  147. DelateNoFile();
  148. }
  149. if (GUILayout.Button("Tolua"))
  150. {
  151. AddLuaWrapBenginClassType("");
  152. }
  153. GUILayout.Box("", GUILayout.Width(790));
  154. GUILayout.BeginHorizontal();
  155. GUILayout.Space(60);
  156. info.IsIgnore =GUILayout.Toggle(info.IsIgnore, "是否开启忽略某些文件") ;
  157. GUILayout.EndHorizontal();
  158. }
  159. private List<VersionInfoData> SerizlizeResList(byte[] data)
  160. {
  161. CsvReader csvReader = new CsvReader(info.mainfestFileName, data);
  162. if (downloadFormatInfo == null)
  163. {
  164. downloadFormatInfo = new GameDataFormatInfo(csvReader.Fields(), csvReader.Types());
  165. }
  166. Type type = typeof(VersionInfoData);
  167. MethodInfo methodInfo = type.GetMethod("OnCsvLoad", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
  168. methodInfo?.Invoke(null, new object[] { csvReader });
  169. List<VersionInfoData> resList = VersionInfoData.AllData();
  170. VersionInfoData.Clear();
  171. return resList;
  172. }
  173. private void CheckRes()
  174. {
  175. needResList.Clear();
  176. oldPath = $"{info.OldPath}/{ info.mainfestFileName}";
  177. List<VersionInfoData> Oldlist = GetInfoDatas(oldPath);
  178. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  179. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  180. ChangeResMD5(newlist);
  181. if (Oldlist != null && newlist != null)
  182. {
  183. int size = newlist.Count;
  184. for (int i = 0; i < size; i++)
  185. {
  186. if (newlist[i].FullName == info.mainfestFileName || newlist[i].FullName == info.VersionFileName)
  187. {
  188. continue;
  189. }
  190. bool isignore = false;
  191. if (info.IsIgnore)
  192. {
  193. string igstr = info.IgnoreFiles.FindFirst(it => it == newlist[i].FullName);
  194. if (!string.IsNullOrEmpty(igstr))
  195. {
  196. isignore = true;
  197. Debug.Log("======================" + newlist[i].FullName);
  198. }
  199. }
  200. VersionInfoData ores = Oldlist.FindFirst(it=> it.FullName == newlist[i].FullName);
  201. if (ores != null )
  202. {
  203. if (isignore)
  204. {
  205. newlist[i].MD5 = ores.MD5;
  206. newlist[i].Size = ores.Size;
  207. }
  208. else if (newlist[i].MD5 != ores.MD5)
  209. {
  210. needResList.Add(newlist[i]);
  211. }
  212. }
  213. else
  214. {
  215. needResList.Add(newlist[i]);
  216. }
  217. }
  218. }
  219. CsvWriter<VersionInfoData> csvWriter = new CsvWriter<VersionInfoData>(newPath, "", newlist, downloadFormatInfo);
  220. csvWriter.Write();
  221. CopyFile();
  222. needResList.Clear();
  223. Debug.Log($"资源检查完成 输出路径 : 【{info.OutputPath}】");
  224. }
  225. private void CopyFile()
  226. {
  227. if (needResList == null)
  228. {
  229. return;
  230. }
  231. int size = needResList.Count;
  232. string datapath = $"{info.NewPath}/";
  233. for (int i = 0; i < size; i++)
  234. {
  235. if (needResList[i].FullName == "Version")
  236. {
  237. continue;
  238. }
  239. byte[] fdatas = File.ReadAllBytes(datapath + needResList[i].FullName);
  240. FileHelper.WirteToFile( $"{info.OutputPath}/{needResList[i].FullName}" ,fdatas);
  241. }
  242. FileHelper.WirteStringToFile($"{info.OutputPath}/{info.VersionFileName}",info.ResVersion);
  243. byte[] mdatas = File.ReadAllBytes(datapath + info.mainfestFileName);
  244. FileHelper.WirteToFile($"{info.OutputPath}/{info.mainfestFileName}", mdatas);
  245. }
  246. private void CopyMainfestFile()
  247. {
  248. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  249. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  250. if (newlist == null)
  251. {
  252. return;
  253. }
  254. int size = newlist.Count;
  255. string datapath = $"{info.NewPath}/";
  256. for (int i = 0; i < size; i++)
  257. {
  258. if (newlist[i].FullName == "Version")
  259. {
  260. continue;
  261. }
  262. byte[] fdatas = File.ReadAllBytes(datapath + newlist[i].FullName);
  263. FileHelper.WirteToFile($"{info.OutputPath}/{newlist[i].FullName}", fdatas);
  264. }
  265. }
  266. private void DelateNoFile()
  267. {
  268. newPath = $"{info.NewPath}/{ info.mainfestFileName}";
  269. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  270. List<VersionInfoData> newlist = GetInfoDatas(newPath);
  271. int size = files1.Length;
  272. string datapath = $"{info.NewPath}/";
  273. for (int i = 0; i < size; i++)
  274. {
  275. if (files1[i] == "afivs" || files1[i] == "afimft")
  276. {
  277. continue;
  278. }
  279. if (newlist.FindFirst(it => it.FullName == files1[i]) == null)
  280. {
  281. FileHelper.DeleteFile(datapath + files1[i]);
  282. }
  283. }
  284. }
  285. private List<VersionInfoData> GetInfoDatas(string path)
  286. {
  287. List<VersionInfoData> list = null;
  288. byte[] datas = null;
  289. if (FileSystem.Exists(path))
  290. {
  291. datas = File.ReadAllBytes(path);
  292. }
  293. if (datas != null)
  294. {
  295. list = SerizlizeResList(datas);
  296. }
  297. else
  298. {
  299. Debug.LogError($"请选择正确路径;[{path}]文件不存在!!!!");
  300. }
  301. return list;
  302. }
  303. private void ChangeResMD5(List<VersionInfoData> datas)
  304. {
  305. int[] ids = GenerateRandomArray(info.ChangeNum, 1, datas.Count);
  306. for (int i = 0; i < ids.Length; i++)
  307. {
  308. Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====");
  309. datas[ids[i]].MD5 = datas[ids[i]].MD5.Substring(0,6);
  310. //Debug.Log($"{ datas[ids[i]].FullName} 改===={ datas[ids[i]].MD5}=====eeee");
  311. }
  312. }
  313. int[] GenerateRandomArray(int size, int min, int max)
  314. {
  315. List<int> array = new List<int>(size);
  316. if (size >= max - min)
  317. {
  318. for (int i = min; i < max; i++)
  319. {
  320. array.Add(i);
  321. }
  322. }
  323. else
  324. {
  325. while (size > 0)
  326. {
  327. int a = UnityEngine.Random.Range(min, max);
  328. if (!array.Contains(a))
  329. {
  330. array.Add(a);
  331. size--;
  332. }
  333. }
  334. }
  335. return array.ToArray();
  336. }
  337. private void CopyFiles()
  338. {
  339. string[] files1 = FileHelper.GetAllFileNmae(info.NewPath, "meta");
  340. int size = files1.Length;
  341. string datapath = $"{info.NewPath}/";
  342. for (int i = 0; i < size; i++)
  343. {
  344. byte[] fdatas = File.ReadAllBytes(datapath + files1[i]);
  345. FileHelper.WirteToFile($"{info.OutputPath}/{files1[i]}", fdatas);
  346. }
  347. }
  348. private void CopyTexture()
  349. {
  350. string datapath = $"{Application.dataPath}/Content/Icons/";
  351. FileHelper.CopyDir(datapath, info.OutputPath, "meta");
  352. }
  353. //L.BeginClass(typeof(SingletonMono<LuaMgr>), typeof(MonoBase), \"SingletonMono_LuaMgr\");
  354. private void AddLuaWrapBenginClassType(string path)
  355. {
  356. if (String.IsNullOrEmpty(path)) return;
  357. string[] fdatas = File.ReadAllLines(path); //new string[] { "L.BeginClass(typeof(SingletonMono<LuaMgr>), typeof(MonoBase), \"SingletonMono_LuaMgr\");" };//
  358. int length = fdatas.Length;
  359. string newstr = "";
  360. for (int i = 0; i < length; i++)
  361. {
  362. if (fdatas[i].Contains("BeginClass"))
  363. {
  364. string[] strarr = fdatas[i].Split("typeof(",StringSplitOptions.RemoveEmptyEntries);
  365. string classname = strarr[1].Substring(0, strarr[1].IndexOf(")"));
  366. if (classname.Contains("<"))
  367. {
  368. string[] names = classname.Split(new char[]{ '<','>'},StringSplitOptions.RemoveEmptyEntries);
  369. classname = $"{names[0]}_{names[1]}";
  370. }
  371. Debug.Log($"Class Name = {classname}");
  372. string nameStr = $"\"{classname}\"";
  373. if (!fdatas[i].Contains(nameStr))
  374. {
  375. int endid = fdatas[i].LastIndexOf(")");
  376. newstr = fdatas[i].Substring(0, endid);
  377. newstr += $", {nameStr});";
  378. fdatas[i] = newstr;
  379. Debug.Log($"New String = {newstr}");
  380. }
  381. break;
  382. }
  383. }
  384. if (newstr != "")
  385. {
  386. //Debug.Log("写");
  387. File.WriteAllLines(path,fdatas);
  388. }
  389. }
  390. }