DepBundleCollector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using XGame.Editor.Asset;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using UnityEditor;
  7. using UnityEngine;
  8. namespace XGame.Editor.Build.AssetBundles
  9. {
  10. public class DepBundleCollector
  11. {
  12. private BundleContext _context;
  13. public DepBundleCollector(BundleContext context)
  14. {
  15. _context = context;
  16. }
  17. /// <summary>
  18. /// 收集ResStatic目录下被引用的资源
  19. /// </summary>
  20. /// <param name="assetBundleBuilds"></param>
  21. /// <param name="filePaths"></param>
  22. public void Collect()
  23. {
  24. //<assetPath, List<bundleName>>
  25. var refBundlesMap = new Dictionary<string, HashSet<long>>();
  26. foreach (var item in _context.bundleDataMap)
  27. {
  28. if (item.Value.isTextureBundle)
  29. {
  30. continue;
  31. }
  32. var assetDependencies = _context.GetDependencies(item.Value.bundleId, item.Value.assetNames);
  33. foreach (var dependency in assetDependencies)
  34. {
  35. if (_context.bundleAssetPaths.Contains(dependency) || Asset.FileUtil.IsFileIgnore(dependency))
  36. continue;
  37. if (dependency.Contains(_context.ResStaticDir))
  38. {
  39. if (dependency.Contains(_context.UiPrefabDir) && dependency.EndsWith(_context.PrefabExt, StringComparison.OrdinalIgnoreCase))
  40. {
  41. //忽略ResStatic下的UI预制
  42. continue;
  43. }
  44. }
  45. else if (dependency.StartsWith(PathDefine.PackageRelative, StringComparison.OrdinalIgnoreCase))
  46. {
  47. if (dependency.EndsWith(_context.PrefabExt, StringComparison.OrdinalIgnoreCase))
  48. {
  49. //忽略预制体文件
  50. //Debug.LogWarning($"Bundle:{build.Value.bundleId} 依赖了Editor或者Resources资源. dependency:{dependency}");
  51. continue;
  52. }
  53. }
  54. else
  55. {
  56. //if (dependency.StartsWith(PathDefine.ResourcesRelative, StringComparison.OrdinalIgnoreCase))
  57. //{
  58. // Debug.LogWarning($"Bundle:{build.Value.bundleId} 依赖了Resources资源. dependency:{dependency}");
  59. //}
  60. continue;
  61. }
  62. if (!refBundlesMap.TryGetValue(dependency, out var bundles))
  63. {
  64. bundles = new HashSet<long>();
  65. refBundlesMap.Add(dependency, bundles);
  66. }
  67. if (!bundles.Contains(item.Value.bundleId))
  68. {
  69. bundles.Add(item.Value.bundleId);
  70. }
  71. //Debug.Log($"ResStatic dependency :{dependency} dir:{dir}");
  72. }
  73. }
  74. //<assetPath, bundleNames>
  75. var bundleNamesMap = new Dictionary<string, string>();
  76. foreach (var item in refBundlesMap)
  77. {
  78. //只被单个bundle引用的,不需要单独打包bundle
  79. if (item.Value.Count < 2) continue;
  80. var str = string.Join('_', item.Value);
  81. bundleNamesMap.Add(item.Key, str);
  82. }
  83. var groups = bundleNamesMap.GroupBy(p => p.Value);
  84. //由于AssetBundle的资源重名只认资源文件名,因此这里做好重名检测
  85. //用于排序,减少二次分包的次数
  86. var assetPaths = new List<string>();
  87. var fileNames = new HashSet<string>();
  88. foreach (var group in groups)
  89. {
  90. //先取出所有assetPath
  91. foreach (var item in group)
  92. {
  93. assetPaths.Add(item.Key);
  94. }
  95. //排序
  96. assetPaths.Sort((a, b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase));
  97. //是否重名
  98. var isNameSake = false;
  99. foreach (var assetPath in assetPaths)
  100. {
  101. var fileName = Path.GetFileName(assetPath);
  102. if (!fileNames.Add(fileName))
  103. {
  104. //有重名
  105. isNameSake = true;
  106. break;
  107. }
  108. }
  109. if (isNameSake)
  110. {
  111. var depAssetsMap = AssetsGroupByDependencies(assetPaths);
  112. //生成依赖包,bundleName由引用的BundleId拼接而成
  113. var firstKey = "0";
  114. var assets = depAssetsMap[firstKey];
  115. var bundleBuild = _context.AddRawAssetBundle(assets, group.Key, true);
  116. //var bundleBuild = new AssetBundleData(group.Key, KCDefine.BUNDLE_VARIANT, assets, assets);
  117. //_context.bundleBuildMap.TryAdd(bundleBuild);
  118. depAssetsMap.Remove(firstKey);
  119. foreach (var item in depAssetsMap)
  120. {
  121. var bundleName = $"{bundleBuild.bundleId}_{item.Key}";
  122. assets = item.Value;
  123. _context.AddRawAssetBundle(assets, bundleName, true);
  124. //var depBundle = new AssetBundleData(bundleName, KCDefine.BUNDLE_VARIANT, assets, assets);
  125. //_context.bundleBuildMap.TryAdd(depBundle);
  126. }
  127. }
  128. else
  129. {
  130. //生成依赖包,bundleName由引用的BundleId拼接而成
  131. var assets = assetPaths.ToArray();
  132. _context.AddRawAssetBundle(assets, group.Key, true);
  133. //var bundleBuild = new AssetBundleData(group.Key, KCDefine.BUNDLE_VARIANT, assets, assets);
  134. //_context.bundleBuildMap.TryAdd(bundleBuild);
  135. }
  136. assetPaths.Clear();
  137. fileNames.Clear();
  138. }
  139. }
  140. /// <summary>
  141. /// Assets根据依赖关系二次分组
  142. /// 返回<groupId, assetPath[]>
  143. /// </summary>
  144. /// <param name="assetPaths"></param>
  145. /// <returns></returns>
  146. private Dictionary<string, string[]> AssetsGroupByDependencies(List<string> assetPaths)
  147. {
  148. var result = new Dictionary<string, string[]>();
  149. var assetDependencies = new Dictionary<string, string[]>();
  150. var dependencies = new List<string>();
  151. //先收集assetPaths之间的依赖信息
  152. foreach (var assetPath in assetPaths)
  153. {
  154. var deps = AssetDatabase.GetDependencies(assetPath, true);
  155. foreach (var dep in deps)
  156. {
  157. if (!assetPath.Equals(dep) && assetPaths.Contains(dep))
  158. {
  159. dependencies.Add(dep);
  160. }
  161. }
  162. assetDependencies.Add(assetPath, dependencies.ToArray());
  163. dependencies.Clear();
  164. }
  165. var assetGroupMap = new Dictionary<string, int>();
  166. var pendingPaths = new List<string>();
  167. foreach (var item in assetDependencies)
  168. {
  169. if (item.Value.Length > 0)
  170. {
  171. pendingPaths.Add(item.Key);
  172. }
  173. else
  174. {
  175. //没有依赖的资源,直接保存到assetGroupMap
  176. assetGroupMap.Add(item.Key, 0);
  177. }
  178. }
  179. var groupId = 1;
  180. var tempPaths = new List<string>();
  181. var fileNames = new HashSet<string>();
  182. //递归查询
  183. while (pendingPaths.Count > 0)
  184. {
  185. foreach (var assetPath in pendingPaths)
  186. {
  187. var isTrue = true;
  188. foreach (var dep in assetDependencies[assetPath])
  189. {
  190. if (!assetGroupMap.ContainsKey(dep))
  191. {
  192. isTrue = false;
  193. break;
  194. }
  195. }
  196. if (isTrue)
  197. {
  198. //assetPath依赖的资源都有分组记录
  199. tempPaths.Add(assetPath);
  200. }
  201. }
  202. if (tempPaths.Count > 0)
  203. {
  204. //新增分组
  205. foreach (var assetPath in tempPaths)
  206. {
  207. pendingPaths.Remove(assetPath);
  208. assetGroupMap.Add(assetPath, groupId);
  209. }
  210. groupId += 1;
  211. tempPaths.Clear();
  212. }
  213. else
  214. {
  215. //有互相依赖的资源,直接归为同一个分组,这里面可能有重名的资源
  216. //由CheckAssetsNameRepeat方法统一报错处理
  217. var isNameSake = false;
  218. foreach (var assetPath in pendingPaths)
  219. {
  220. var fileName = Path.GetFileName(assetPath);
  221. if (!fileNames.Add(fileName))
  222. {
  223. //有重名资源
  224. isNameSake = true;
  225. break;
  226. }
  227. }
  228. if (isNameSake)
  229. {
  230. BuildLog.Error($"DepAssetbundle Group Error: 存在同名文件且Asset之间互相依赖。");
  231. foreach (var assetPath in pendingPaths)
  232. {
  233. BuildLog.Error($"Error File:{assetPath}");
  234. }
  235. }
  236. result.Add(groupId.ToString(), pendingPaths.ToArray());
  237. pendingPaths.Clear();
  238. fileNames.Clear();
  239. }
  240. }
  241. var groups = assetGroupMap.GroupBy(item => item.Value);
  242. foreach (var group in groups)
  243. {
  244. foreach (var item in group)
  245. {
  246. var assetPath = item.Key;
  247. var fileName = Path.GetFileName(assetPath);
  248. if (fileNames.Add(fileName))
  249. {
  250. tempPaths.Add(assetPath);
  251. }
  252. else
  253. {
  254. //有同名文件,该文件直接单独打包
  255. var guid = AssetDatabase.AssetPathToGUID(assetPath);
  256. result.Add($"{item.Value}_{guid}", new string[] { assetPath });
  257. }
  258. }
  259. if (tempPaths.Count > 0)
  260. {
  261. result.Add(group.Key.ToString(), tempPaths.ToArray());
  262. }
  263. else
  264. {
  265. BuildLog.Error($"DepAssetbundle Group Error: GroupId:{group.Key} Count:{group.Count()} 分组数据为空。");
  266. }
  267. tempPaths.Clear();
  268. fileNames.Clear();
  269. }
  270. return result;
  271. }
  272. }
  273. }