using System;
using System.Linq;
namespace YooAsset
{
[Serializable]
internal class PackageBundle
{
///
/// 资源包名称
///
public string BundleName;
///
/// Unity引擎生成的CRC
///
public uint UnityCRC;
///
/// 文件哈希值
///
public string FileHash;
///
/// 文件校验码
///
public string FileCRC;
///
/// 文件大小(字节数)
///
public long FileSize;
///
/// 是否为原生文件
///
public bool IsRawFile;
///
/// 加载方法
///
public byte LoadMethod;
///
/// 资源包的分类标签
///
public string[] Tags;
///
/// 引用该资源包的ID列表
///
public int[] ReferenceIDs;
///
/// 所属的包裹名称
///
public string PackageName { private set; get; }
///
/// 缓存GUID
///
public string CacheGUID
{
get { return FileHash; }
}
///
/// 缓存的数据文件路径
///
private string _cachedDataFilePath;
public string CachedDataFilePath
{
get
{
if (string.IsNullOrEmpty(_cachedDataFilePath) == false)
return _cachedDataFilePath;
string folderName = FileHash.Substring(0, 2);
if (IsRawFile)
{
string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheRawFilesRoot;
_cachedDataFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleDataFileName);
_cachedDataFilePath += _fileExtension;
}
else
{
string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheBundleFilesRoot;
_cachedDataFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleDataFileName);
}
return _cachedDataFilePath;
}
}
///
/// 缓存的信息文件路径
///
private string _cachedInfoFilePath;
public string CachedInfoFilePath
{
get
{
if (string.IsNullOrEmpty(_cachedInfoFilePath) == false)
return _cachedInfoFilePath;
string folderName = FileHash.Substring(0, 2);
if (IsRawFile)
{
string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheRawFilesRoot;
_cachedInfoFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleInfoFileName);
}
else
{
string cacheRoot = PersistentTools.GetPersistent(PackageName).SandboxCacheBundleFilesRoot;
_cachedInfoFilePath = PathUtility.Combine(cacheRoot, folderName, CacheGUID, YooAssetSettings.CacheBundleInfoFileName);
}
return _cachedInfoFilePath;
}
}
///
/// 临时的数据文件路径
///
private string _tempDataFilePath;
public string TempDataFilePath
{
get
{
if (string.IsNullOrEmpty(_tempDataFilePath) == false)
return _tempDataFilePath;
_tempDataFilePath = $"{CachedDataFilePath}.temp";
return _tempDataFilePath;
}
}
///
/// 内置文件路径
///
private string _streamingFilePath;
public string StreamingFilePath
{
get
{
if (string.IsNullOrEmpty(_streamingFilePath) == false)
return _streamingFilePath;
string root = PersistentTools.GetPersistent(PackageName).BuildinPackageRoot;
_streamingFilePath = PathUtility.Combine(root, FileName);
return _streamingFilePath;
}
}
///
/// 文件名称
///
private string _fileName;
public string FileName
{
get
{
if (string.IsNullOrEmpty(_fileName))
throw new Exception("Should never get here !");
return _fileName;
}
}
///
/// 文件后缀名
///
private string _fileExtension;
public string FileExtension
{
get
{
if (string.IsNullOrEmpty(_fileExtension))
throw new Exception("Should never get here !");
return _fileExtension;
}
}
public PackageBundle()
{
}
///
/// 解析资源包
///
public void ParseBundle(string packageName, int nameStype)
{
PackageName = packageName;
_fileExtension = ManifestTools.GetRemoteBundleFileExtension(BundleName);
_fileName = ManifestTools.GetRemoteBundleFileName(nameStype, BundleName, _fileExtension, FileHash);
}
///
/// 是否包含Tag
///
public bool HasTag(string[] tags)
{
if (tags == null || tags.Length == 0)
return false;
if (Tags == null || Tags.Length == 0)
return false;
foreach (var tag in tags)
{
if (Tags.Contains(tag))
return true;
}
return false;
}
///
/// 是否包含任意Tags
///
public bool HasAnyTags()
{
if (Tags != null && Tags.Length > 0)
return true;
else
return false;
}
///
/// 检测资源包文件内容是否相同
///
public bool Equals(PackageBundle otherBundle)
{
if (FileHash == otherBundle.FileHash)
return true;
return false;
}
}
}