PackageBundle.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Linq;
  3. namespace YooAsset
  4. {
  5. [Serializable]
  6. internal class PackageBundle
  7. {
  8. /// <summary>
  9. /// 资源包名称
  10. /// </summary>
  11. public string BundleName;
  12. /// <summary>
  13. /// 文件哈希值
  14. /// </summary>
  15. public string FileHash;
  16. /// <summary>
  17. /// 文件校验码
  18. /// </summary>
  19. public string FileCRC;
  20. /// <summary>
  21. /// 文件大小(字节数)
  22. /// </summary>
  23. public long FileSize;
  24. /// <summary>
  25. /// 是否为原生文件
  26. /// </summary>
  27. public bool IsRawFile;
  28. /// <summary>
  29. /// 加载方法
  30. /// </summary>
  31. public byte LoadMethod;
  32. /// <summary>
  33. /// 资源包的分类标签
  34. /// </summary>
  35. public string[] Tags;
  36. /// <summary>
  37. /// 引用该资源包的ID列表
  38. /// </summary>
  39. public int[] ReferenceIDs;
  40. /// <summary>
  41. /// 所属的包裹名称
  42. /// </summary>
  43. public string PackageName { private set; get; }
  44. /// <summary>
  45. /// 缓存GUID
  46. /// </summary>
  47. public string CacheGUID
  48. {
  49. get { return FileHash; }
  50. }
  51. /// <summary>
  52. /// 缓存的数据文件路径
  53. /// </summary>
  54. private string _cachedDataFilePath;
  55. public string CachedDataFilePath
  56. {
  57. get
  58. {
  59. if (string.IsNullOrEmpty(_cachedDataFilePath) == false)
  60. return _cachedDataFilePath;
  61. string folderName = FileHash.Substring(0, 2);
  62. if (IsRawFile)
  63. {
  64. string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheRawFilesRoot;
  65. _cachedDataFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleDataFileName);
  66. _cachedDataFilePath += _fileExtension;
  67. }
  68. else
  69. {
  70. string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheBundleFilesRoot;
  71. _cachedDataFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleDataFileName);
  72. }
  73. return _cachedDataFilePath;
  74. }
  75. }
  76. /// <summary>
  77. /// 缓存的信息文件路径
  78. /// </summary>
  79. private string _cachedInfoFilePath;
  80. public string CachedInfoFilePath
  81. {
  82. get
  83. {
  84. if (string.IsNullOrEmpty(_cachedInfoFilePath) == false)
  85. return _cachedInfoFilePath;
  86. string folderName = FileHash.Substring(0, 2);
  87. if (IsRawFile)
  88. {
  89. string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheRawFilesRoot;
  90. _cachedInfoFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleInfoFileName);
  91. }
  92. else
  93. {
  94. string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheBundleFilesRoot;
  95. _cachedInfoFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleInfoFileName);
  96. }
  97. return _cachedInfoFilePath;
  98. }
  99. }
  100. /// <summary>
  101. /// 临时的数据文件路径
  102. /// </summary>
  103. private string _tempDataFilePath;
  104. public string TempDataFilePath
  105. {
  106. get
  107. {
  108. if (string.IsNullOrEmpty(_tempDataFilePath) == false)
  109. return _tempDataFilePath;
  110. _tempDataFilePath = $"{CachedDataFilePath}.temp";
  111. return _tempDataFilePath;
  112. }
  113. }
  114. /// <summary>
  115. /// 内置文件路径
  116. /// </summary>
  117. private string _streamingFilePath;
  118. public string StreamingFilePath
  119. {
  120. get
  121. {
  122. if (string.IsNullOrEmpty(_streamingFilePath) == false)
  123. return _streamingFilePath;
  124. string root = PersistentTools.GetPersistent(PackageName).BuildinPackageRoot;
  125. _streamingFilePath = PathUtility.Combine(root, FileName);
  126. return _streamingFilePath;
  127. }
  128. }
  129. /// <summary>
  130. /// 文件名称
  131. /// </summary>
  132. private string _fileName;
  133. public string FileName
  134. {
  135. get
  136. {
  137. if (string.IsNullOrEmpty(_fileName))
  138. throw new Exception("Should never get here !");
  139. return _fileName;
  140. }
  141. }
  142. /// <summary>
  143. /// 文件后缀名
  144. /// </summary>
  145. private string _fileExtension;
  146. public string FileExtension
  147. {
  148. get
  149. {
  150. if (string.IsNullOrEmpty(_fileExtension))
  151. throw new Exception("Should never get here !");
  152. return _fileExtension;
  153. }
  154. }
  155. public PackageBundle()
  156. {
  157. }
  158. /// <summary>
  159. /// 解析资源包
  160. /// </summary>
  161. public void ParseBundle(string packageName, int nameStype)
  162. {
  163. PackageName = packageName;
  164. _fileExtension = ManifestTools.GetRemoteBundleFileExtension(BundleName);
  165. _fileName = ManifestTools.GetRemoteBundleFileName(nameStype, BundleName, _fileExtension, FileHash);
  166. }
  167. /// <summary>
  168. /// 是否包含Tag
  169. /// </summary>
  170. public bool HasTag(string[] tags)
  171. {
  172. if (tags == null || tags.Length == 0)
  173. return false;
  174. if (Tags == null || Tags.Length == 0)
  175. return false;
  176. foreach (var tag in tags)
  177. {
  178. if (Tags.Contains(tag))
  179. return true;
  180. }
  181. return false;
  182. }
  183. /// <summary>
  184. /// 是否包含任意Tags
  185. /// </summary>
  186. public bool HasAnyTags()
  187. {
  188. if (Tags != null && Tags.Length > 0)
  189. return true;
  190. else
  191. return false;
  192. }
  193. /// <summary>
  194. /// 检测资源包文件内容是否相同
  195. /// </summary>
  196. public bool Equals(PackageBundle otherBundle)
  197. {
  198. if (FileHash == otherBundle.FileHash)
  199. return true;
  200. return false;
  201. }
  202. }
  203. }