PackageBundle.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace YooAsset
  5. {
  6. [Serializable]
  7. internal class PackageBundle
  8. {
  9. /// <summary>
  10. /// 资源包名称
  11. /// </summary>
  12. public string BundleName;
  13. /// <summary>
  14. /// Unity引擎生成的CRC
  15. /// </summary>
  16. public uint UnityCRC;
  17. /// <summary>
  18. /// 文件哈希值
  19. /// </summary>
  20. public string FileHash;
  21. /// <summary>
  22. /// 文件校验码
  23. /// </summary>
  24. public string FileCRC;
  25. /// <summary>
  26. /// 文件大小(字节数)
  27. /// </summary>
  28. public long FileSize;
  29. /// <summary>
  30. /// 文件是否加密
  31. /// </summary>
  32. public bool Encrypted;
  33. /// <summary>
  34. /// 资源包的分类标签
  35. /// </summary>
  36. public string[] Tags;
  37. /// <summary>
  38. /// 依赖的资源包ID集合
  39. /// 注意:引擎层构建查询结果
  40. /// </summary>
  41. public int[] DependBundleIDs;
  42. /// <summary>
  43. /// 资源包GUID
  44. /// </summary>
  45. public string BundleGUID
  46. {
  47. get { return FileHash; }
  48. }
  49. /// <summary>
  50. /// 资源包类型
  51. /// </summary>
  52. public int BundleType
  53. {
  54. get
  55. {
  56. return _bundleType;
  57. }
  58. }
  59. private int _bundleType;
  60. /// <summary>
  61. /// 文件名称
  62. /// </summary>
  63. public string FileName
  64. {
  65. get
  66. {
  67. if (string.IsNullOrEmpty(_fileName))
  68. throw new Exception("Should never get here !");
  69. return _fileName;
  70. }
  71. }
  72. private string _fileName;
  73. /// <summary>
  74. /// 文件后缀名
  75. /// </summary>
  76. public string FileExtension
  77. {
  78. get
  79. {
  80. if (string.IsNullOrEmpty(_fileExtension))
  81. throw new Exception("Should never get here !");
  82. return _fileExtension;
  83. }
  84. }
  85. private string _fileExtension;
  86. /// <summary>
  87. /// 包含的主资源集合
  88. /// </summary>
  89. [NonSerialized]
  90. public readonly List<PackageAsset> IncludeMainAssets = new List<PackageAsset>(10);
  91. /// <summary>
  92. /// 引用该资源包的资源包列表
  93. /// 说明:谁引用了该资源包
  94. /// </summary>
  95. [NonSerialized]
  96. public readonly List<int> ReferenceBundleIDs = new List<int>(10);
  97. private readonly HashSet<int> _referenceBundleIDs = new HashSet<int>();
  98. public PackageBundle()
  99. {
  100. }
  101. /// <summary>
  102. /// 初始化资源包
  103. /// </summary>
  104. public void InitBundle(PackageManifest manifest)
  105. {
  106. _mainfest = manifest;
  107. _bundleType = manifest.BuildBundleType;
  108. _fileExtension = ManifestTools.GetRemoteBundleFileExtension(BundleName);
  109. _fileName = ManifestTools.GetRemoteBundleFileName(manifest.OutputNameStyle, BundleName, _fileExtension, FileHash);
  110. }
  111. /// <summary>
  112. /// 添加引用该资源包的资源包ID
  113. /// 说明:谁引用了该资源包
  114. /// </summary>
  115. public void AddReferenceBundleID(int bundleID)
  116. {
  117. if (_referenceBundleIDs.Contains(bundleID) == false)
  118. {
  119. _referenceBundleIDs.Add(bundleID);
  120. ReferenceBundleIDs.Add(bundleID);
  121. }
  122. }
  123. /// <summary>
  124. /// 是否包含Tag
  125. /// </summary>
  126. public bool HasTag(string[] tags)
  127. {
  128. if (tags == null || tags.Length == 0)
  129. return false;
  130. if (Tags == null || Tags.Length == 0)
  131. return false;
  132. foreach (var tag in tags)
  133. {
  134. if (Tags.Contains(tag))
  135. return true;
  136. }
  137. return false;
  138. }
  139. /// <summary>
  140. /// 是否包含任意Tags
  141. /// </summary>
  142. public bool HasAnyTags()
  143. {
  144. if (Tags != null && Tags.Length > 0)
  145. return true;
  146. else
  147. return false;
  148. }
  149. /// <summary>
  150. /// 检测资源包文件内容是否相同
  151. /// </summary>
  152. public bool Equals(PackageBundle otherBundle)
  153. {
  154. if (FileHash == otherBundle.FileHash)
  155. return true;
  156. return false;
  157. }
  158. #region 调试信息
  159. private PackageManifest _mainfest;
  160. private List<string> _debugReferenceBundles;
  161. public List<string> GetDebugReferenceBundles()
  162. {
  163. if (_debugReferenceBundles == null)
  164. {
  165. _debugReferenceBundles = new List<string>(ReferenceBundleIDs.Count);
  166. foreach (int bundleID in ReferenceBundleIDs)
  167. {
  168. var packageBundle = _mainfest.BundleList[bundleID];
  169. _debugReferenceBundles.Add(packageBundle.BundleName);
  170. }
  171. }
  172. return _debugReferenceBundles;
  173. }
  174. #endregion
  175. }
  176. }