PackageManifest.cs 8.7 KB

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