PatchBundle.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Linq;
  3. using System.IO;
  4. namespace YooAsset
  5. {
  6. [Serializable]
  7. internal class PatchBundle
  8. {
  9. /// <summary>
  10. /// 资源包名称
  11. /// </summary>
  12. public string BundleName;
  13. /// <summary>
  14. /// 文件哈希值
  15. /// </summary>
  16. public string FileHash;
  17. /// <summary>
  18. /// 文件校验码
  19. /// </summary>
  20. public string FileCRC;
  21. /// <summary>
  22. /// 文件大小(字节数)
  23. /// </summary>
  24. public long FileSize;
  25. /// <summary>
  26. /// 资源包的分类标签
  27. /// </summary>
  28. public string[] Tags;
  29. /// <summary>
  30. /// Flags
  31. /// </summary>
  32. public int Flags;
  33. /// <summary>
  34. /// 是否为加密文件
  35. /// </summary>
  36. public bool IsEncrypted { private set; get; }
  37. /// <summary>
  38. /// 是否为内置文件
  39. /// </summary>
  40. public bool IsBuildin { private set; get; }
  41. /// <summary>
  42. /// 是否为原生文件
  43. /// </summary>
  44. public bool IsRawFile { private set; get; }
  45. /// <summary>
  46. /// 文件名称
  47. /// </summary>
  48. public string FileName { private set; get; }
  49. /// <summary>
  50. /// 缓存文件路径
  51. /// </summary>
  52. private string _cachedFilePath;
  53. public string CachedFilePath
  54. {
  55. get
  56. {
  57. if (string.IsNullOrEmpty(_cachedFilePath) == false)
  58. return _cachedFilePath;
  59. string cacheRoot = SandboxHelper.GetCacheFolderPath();
  60. _cachedFilePath = $"{cacheRoot}/{FileName}";
  61. return _cachedFilePath;
  62. }
  63. }
  64. /// <summary>
  65. /// 内置文件路径
  66. /// </summary>
  67. private string _streamingFilePath;
  68. public string StreamingFilePath
  69. {
  70. get
  71. {
  72. if (string.IsNullOrEmpty(_streamingFilePath) == false)
  73. return _streamingFilePath;
  74. _streamingFilePath = PathHelper.MakeStreamingLoadPath(FileName);
  75. return _streamingFilePath;
  76. }
  77. }
  78. public PatchBundle(string bundleName, string fileHash, string fileCRC, long fileSize, string[] tags)
  79. {
  80. BundleName = bundleName;
  81. FileHash = fileHash;
  82. FileCRC = fileCRC;
  83. FileSize = fileSize;
  84. Tags = tags;
  85. }
  86. /// <summary>
  87. /// 设置Flags
  88. /// </summary>
  89. public void SetFlagsValue(bool isEncrypted, bool isBuildin, bool isRawFile)
  90. {
  91. IsEncrypted = isEncrypted;
  92. IsBuildin = isBuildin;
  93. IsRawFile = isRawFile;
  94. BitMask32 mask = new BitMask32(0);
  95. if (isEncrypted) mask.Open(0);
  96. if (isBuildin) mask.Open(1);
  97. if (isRawFile) mask.Open(2);
  98. Flags = mask;
  99. }
  100. /// <summary>
  101. /// 解析Flags
  102. /// </summary>
  103. public void ParseFlagsValue()
  104. {
  105. BitMask32 value = Flags;
  106. IsEncrypted = value.Test(0);
  107. IsBuildin = value.Test(1);
  108. IsRawFile = value.Test(2);
  109. }
  110. /// <summary>
  111. /// 解析文件名称
  112. /// </summary>
  113. public void ParseFileName(int nameStype)
  114. {
  115. if (nameStype == 1)
  116. {
  117. FileName = FileHash;
  118. }
  119. else if (nameStype == 2)
  120. {
  121. string tempFileExtension = System.IO.Path.GetExtension(BundleName);
  122. FileName = $"{FileHash}{tempFileExtension}";
  123. }
  124. else if (nameStype == 3)
  125. {
  126. string tempFileExtension = System.IO.Path.GetExtension(BundleName);
  127. string tempBundleName = BundleName.Replace('/', '_').Replace(tempFileExtension, "");
  128. FileName = $"{tempBundleName}_{FileHash}";
  129. }
  130. else if (nameStype == 4)
  131. {
  132. string tempFileExtension = System.IO.Path.GetExtension(BundleName);
  133. string tempBundleName = BundleName.Replace('/', '_').Replace(tempFileExtension, "");
  134. FileName = $"{tempBundleName}_{FileHash}{tempFileExtension}";
  135. }
  136. else
  137. {
  138. throw new NotImplementedException();
  139. }
  140. }
  141. /// <summary>
  142. /// 是否包含Tag
  143. /// </summary>
  144. public bool HasTag(string[] tags)
  145. {
  146. if (tags == null || tags.Length == 0)
  147. return false;
  148. if (Tags == null || Tags.Length == 0)
  149. return false;
  150. foreach (var tag in tags)
  151. {
  152. if (Tags.Contains(tag))
  153. return true;
  154. }
  155. return false;
  156. }
  157. /// <summary>
  158. /// 是否为纯内置资源(不带任何Tag的资源)
  159. /// </summary>
  160. public bool IsPureBuildin()
  161. {
  162. if (Tags == null || Tags.Length == 0)
  163. return true;
  164. else
  165. return false;
  166. }
  167. /// <summary>
  168. /// 检测资源包文件内容是否相同
  169. /// </summary>
  170. public bool Equals(PatchBundle otherBundle)
  171. {
  172. if (FileHash == otherBundle.FileHash)
  173. return true;
  174. return false;
  175. }
  176. }
  177. }