PackageManifest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace YooAsset
  7. {
  8. /// <summary>
  9. /// 清单文件
  10. /// </summary>
  11. [Serializable]
  12. internal class PackageManifest
  13. {
  14. /// <summary>
  15. /// 文件版本
  16. /// </summary>
  17. public string FileVersion;
  18. /// <summary>
  19. /// 启用可寻址资源定位
  20. /// </summary>
  21. public bool EnableAddressable;
  22. /// <summary>
  23. /// 资源定位地址大小写不敏感
  24. /// </summary>
  25. public bool LocationToLower;
  26. /// <summary>
  27. /// 包含资源GUID数据
  28. /// </summary>
  29. public bool IncludeAssetGUID;
  30. /// <summary>
  31. /// 文件名称样式
  32. /// </summary>
  33. public int OutputNameStyle;
  34. /// <summary>
  35. /// 构建管线名称
  36. /// </summary>
  37. public string BuildPipeline;
  38. /// <summary>
  39. /// 资源包裹名称
  40. /// </summary>
  41. public string PackageName;
  42. /// <summary>
  43. /// 资源包裹的版本信息
  44. /// </summary>
  45. public string PackageVersion;
  46. /// <summary>
  47. /// 资源列表(主动收集的资源列表)
  48. /// </summary>
  49. public List<PackageAsset> AssetList = new List<PackageAsset>();
  50. /// <summary>
  51. /// 资源包列表
  52. /// </summary>
  53. public List<PackageBundle> BundleList = new List<PackageBundle>();
  54. /// <summary>
  55. /// 资源包集合(提供BundleName获取PackageBundle)
  56. /// </summary>
  57. [NonSerialized]
  58. public Dictionary<string, PackageBundle> BundleDic1;
  59. /// <summary>
  60. /// 资源包集合(提供FileName获取PackageBundle)
  61. /// </summary>
  62. [NonSerialized]
  63. public Dictionary<string, PackageBundle> BundleDic2;
  64. /// <summary>
  65. /// 资源映射集合(提供AssetPath获取PackageAsset)
  66. /// </summary>
  67. [NonSerialized]
  68. public Dictionary<string, PackageAsset> AssetDic;
  69. /// <summary>
  70. /// 资源路径映射集合(提供Location获取AssetPath)
  71. /// </summary>
  72. [NonSerialized]
  73. public Dictionary<string, string> AssetPathMapping1;
  74. /// <summary>
  75. /// 资源路径映射集合(提供AssetGUID获取AssetPath)
  76. /// </summary>
  77. [NonSerialized]
  78. public Dictionary<string, string> AssetPathMapping2;
  79. /// <summary>
  80. /// 该资源清单所有文件的缓存GUID集合
  81. /// </summary>
  82. [NonSerialized]
  83. public HashSet<string> CacheGUIDs = new HashSet<string>();
  84. /// <summary>
  85. /// 尝试映射为资源路径
  86. /// </summary>
  87. public string TryMappingToAssetPath(string location)
  88. {
  89. if (string.IsNullOrEmpty(location))
  90. return string.Empty;
  91. if (LocationToLower)
  92. location = location.ToLower();
  93. if (AssetPathMapping1.TryGetValue(location, out string assetPath))
  94. return assetPath;
  95. else
  96. return string.Empty;
  97. }
  98. /// <summary>
  99. /// 获取主资源包
  100. /// 注意:传入的资源路径一定合法有效!
  101. /// </summary>
  102. public PackageBundle GetMainPackageBundle(string assetPath)
  103. {
  104. if (AssetDic.TryGetValue(assetPath, out PackageAsset packageAsset))
  105. {
  106. int bundleID = packageAsset.BundleID;
  107. if (bundleID >= 0 && bundleID < BundleList.Count)
  108. {
  109. var packageBundle = BundleList[bundleID];
  110. return packageBundle;
  111. }
  112. else
  113. {
  114. throw new Exception($"Invalid bundle id : {bundleID} Asset path : {assetPath}");
  115. }
  116. }
  117. else
  118. {
  119. throw new Exception("Should never get here !");
  120. }
  121. }
  122. /// <summary>
  123. /// 获取资源依赖列表
  124. /// 注意:传入的资源路径一定合法有效!
  125. /// </summary>
  126. public PackageBundle[] GetAllDependencies(string assetPath)
  127. {
  128. var packageBundle = GetMainPackageBundle(assetPath);
  129. List<PackageBundle> result = new List<PackageBundle>(packageBundle.DependIDs.Length);
  130. foreach (var dependID in packageBundle.DependIDs)
  131. {
  132. if (dependID >= 0 && dependID < BundleList.Count)
  133. {
  134. var dependBundle = BundleList[dependID];
  135. result.Add(dependBundle);
  136. }
  137. else
  138. {
  139. throw new Exception($"Invalid bundle id : {dependID} Asset path : {assetPath}");
  140. }
  141. }
  142. return result.ToArray();
  143. }
  144. /// <summary>
  145. /// 尝试获取包裹的资源
  146. /// </summary>
  147. public bool TryGetPackageAsset(string assetPath, out PackageAsset result)
  148. {
  149. return AssetDic.TryGetValue(assetPath, out result);
  150. }
  151. /// <summary>
  152. /// 尝试获取包裹的资源包
  153. /// </summary>
  154. public bool TryGetPackageBundleByBundleName(string bundleName, out PackageBundle result)
  155. {
  156. return BundleDic1.TryGetValue(bundleName, out result);
  157. }
  158. /// <summary>
  159. /// 尝试获取包裹的资源包
  160. /// </summary>
  161. public bool TryGetPackageBundleByFileName(string fileName, out PackageBundle result)
  162. {
  163. return BundleDic2.TryGetValue(fileName, out result);
  164. }
  165. /// <summary>
  166. /// 是否包含资源文件
  167. /// </summary>
  168. public bool IsIncludeBundleFile(string cacheGUID)
  169. {
  170. return CacheGUIDs.Contains(cacheGUID);
  171. }
  172. /// <summary>
  173. /// 获取资源信息列表
  174. /// </summary>
  175. public AssetInfo[] GetAssetsInfoByTags(string[] tags)
  176. {
  177. List<AssetInfo> result = new List<AssetInfo>(100);
  178. foreach (var packageAsset in AssetList)
  179. {
  180. if (packageAsset.HasTag(tags))
  181. {
  182. AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, null);
  183. result.Add(assetInfo);
  184. }
  185. }
  186. return result.ToArray();
  187. }
  188. /// <summary>
  189. /// 资源定位地址转换为资源信息。
  190. /// </summary>
  191. /// <returns>如果转换失败会返回一个无效的资源信息类</returns>
  192. public AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
  193. {
  194. DebugCheckLocation(location);
  195. string assetPath = ConvertLocationToAssetInfoMapping(location);
  196. if (TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  197. {
  198. AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, assetType);
  199. return assetInfo;
  200. }
  201. else
  202. {
  203. string error;
  204. if (string.IsNullOrEmpty(location))
  205. error = $"The location is null or empty !";
  206. else
  207. error = $"The location is invalid : {location}";
  208. AssetInfo assetInfo = new AssetInfo(PackageName, error);
  209. return assetInfo;
  210. }
  211. }
  212. private string ConvertLocationToAssetInfoMapping(string location)
  213. {
  214. if (string.IsNullOrEmpty(location))
  215. {
  216. YooLogger.Error("Failed to mapping location to asset path, The location is null or empty.");
  217. return string.Empty;
  218. }
  219. if (LocationToLower)
  220. location = location.ToLower();
  221. if (AssetPathMapping1.TryGetValue(location, out string assetPath))
  222. {
  223. return assetPath;
  224. }
  225. else
  226. {
  227. YooLogger.Warning($"Failed to mapping location to asset path : {location}");
  228. return string.Empty;
  229. }
  230. }
  231. /// <summary>
  232. /// 资源GUID转换为资源信息。
  233. /// </summary>
  234. /// <returns>如果转换失败会返回一个无效的资源信息类</returns>
  235. public AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType)
  236. {
  237. if (IncludeAssetGUID == false)
  238. {
  239. YooLogger.Warning("Package manifest not include asset guid ! Please check asset bundle collector settings.");
  240. AssetInfo assetInfo = new AssetInfo(PackageName, "AssetGUID data is empty !");
  241. return assetInfo;
  242. }
  243. string assetPath = ConvertAssetGUIDToAssetInfoMapping(assetGUID);
  244. if (TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
  245. {
  246. AssetInfo assetInfo = new AssetInfo(PackageName, packageAsset, assetType);
  247. return assetInfo;
  248. }
  249. else
  250. {
  251. string error;
  252. if (string.IsNullOrEmpty(assetGUID))
  253. error = $"The assetGUID is null or empty !";
  254. else
  255. error = $"The assetGUID is invalid : {assetGUID}";
  256. AssetInfo assetInfo = new AssetInfo(PackageName, error);
  257. return assetInfo;
  258. }
  259. }
  260. private string ConvertAssetGUIDToAssetInfoMapping(string assetGUID)
  261. {
  262. if (string.IsNullOrEmpty(assetGUID))
  263. {
  264. YooLogger.Error("Failed to mapping assetGUID to asset path, The assetGUID is null or empty.");
  265. return string.Empty;
  266. }
  267. if (AssetPathMapping2.TryGetValue(assetGUID, out string assetPath))
  268. {
  269. return assetPath;
  270. }
  271. else
  272. {
  273. YooLogger.Warning($"Failed to mapping assetGUID to asset path : {assetGUID}");
  274. return string.Empty;
  275. }
  276. }
  277. /// <summary>
  278. /// 获取资源包内的主资源列表
  279. /// </summary>
  280. public string[] GetBundleIncludeAssets(string assetPath)
  281. {
  282. List<string> assetList = new List<string>();
  283. if (TryGetPackageAsset(assetPath, out PackageAsset result))
  284. {
  285. foreach (var packageAsset in AssetList)
  286. {
  287. if (packageAsset.BundleID == result.BundleID)
  288. {
  289. assetList.Add(packageAsset.AssetPath);
  290. }
  291. }
  292. }
  293. return assetList.ToArray();
  294. }
  295. #region 调试方法
  296. [Conditional("DEBUG")]
  297. private void DebugCheckLocation(string location)
  298. {
  299. if (string.IsNullOrEmpty(location) == false)
  300. {
  301. // 检查路径末尾是否有空格
  302. int index = location.LastIndexOf(" ");
  303. if (index != -1)
  304. {
  305. if (location.Length == index + 1)
  306. YooLogger.Warning($"Found blank character in location : \"{location}\"");
  307. }
  308. if (location.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
  309. YooLogger.Warning($"Found illegal character in location : \"{location}\"");
  310. }
  311. }
  312. #endregion
  313. }
  314. }