using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
namespace YooAsset.Editor
{
public class BuildBundleInfo
{
#region 补丁文件的关键信息
///
/// Unity引擎生成的哈希值(构建内容的哈希值)
///
public string PackageUnityHash { set; get; }
///
/// Unity引擎生成的CRC
///
public uint PackageUnityCRC { set; get; }
///
/// 文件哈希值
///
public string PackageFileHash { set; get; }
///
/// 文件哈希值
///
public string PackageFileCRC { set; get; }
///
/// 文件哈希值
///
public long PackageFileSize { set; get; }
///
/// 构建输出的文件路径
///
public string BuildOutputFilePath { set; get; }
///
/// 补丁包的源文件路径
///
public string PackageSourceFilePath { set; get; }
///
/// 补丁包的目标文件路径
///
public string PackageDestFilePath { set; get; }
///
/// 加密生成文件的路径
/// 注意:如果未加密该路径为空
///
public string EncryptedFilePath { set; get; }
#endregion
private readonly Dictionary _packAssetDic = new Dictionary(100);
///
/// 参与构建的资源列表
/// 注意:不包含零依赖资源和冗余资源
///
public readonly List AllPackAssets = new List(100);
///
/// 资源包名称
///
public string BundleName { private set; get; }
///
/// 加密文件
///
public bool Encrypted { set; get; }
public BuildBundleInfo(string bundleName)
{
BundleName = bundleName;
}
///
/// 添加一个打包资源
///
public void PackAsset(BuildAssetInfo buildAsset)
{
string assetPath = buildAsset.AssetInfo.AssetPath;
if (_packAssetDic.ContainsKey(assetPath))
throw new System.Exception($"Should never get here ! Asset is existed : {assetPath}");
_packAssetDic.Add(assetPath, buildAsset);
AllPackAssets.Add(buildAsset);
}
///
/// 是否包含指定资源
///
public bool IsContainsPackAsset(string assetPath)
{
return _packAssetDic.ContainsKey(assetPath);
}
///
/// 获取构建的资源路径列表
///
public string[] GetAllPackAssetPaths()
{
return AllPackAssets.Select(t => t.AssetInfo.AssetPath).ToArray();
}
///
/// 获取构建的主资源信息
///
public BuildAssetInfo GetPackAssetInfo(string assetPath)
{
if (_packAssetDic.TryGetValue(assetPath, out BuildAssetInfo value))
{
return value;
}
else
{
throw new Exception($"Can not found pack asset info {assetPath} in bundle : {BundleName}");
}
}
///
/// 获取资源包内部所有资产
///
public List GetBundleContents()
{
Dictionary result = new Dictionary(AllPackAssets.Count);
foreach (var packAsset in AllPackAssets)
{
result.Add(packAsset.AssetInfo.AssetPath, packAsset.AssetInfo);
if (packAsset.AllDependAssetInfos != null)
{
foreach (var dependAssetInfo in packAsset.AllDependAssetInfos)
{
// 注意:依赖资源里只添加零依赖资源和冗余资源
if (dependAssetInfo.HasBundleName() == false)
{
string dependAssetPath = dependAssetInfo.AssetInfo.AssetPath;
if (result.ContainsKey(dependAssetPath) == false)
result.Add(dependAssetPath, dependAssetInfo.AssetInfo);
}
}
}
}
return result.Values.ToList();
}
///
/// 创建AssetBundleBuild类
///
public UnityEditor.AssetBundleBuild CreatePipelineBuild()
{
// 注意:我们不再支持AssetBundle的变种机制
AssetBundleBuild build = new AssetBundleBuild();
build.assetBundleName = BundleName;
build.assetBundleVariant = string.Empty;
build.assetNames = GetAllPackAssetPaths();
return build;
}
///
/// 获取所有写入补丁清单的资源
///
public BuildAssetInfo[] GetAllManifestAssetInfos()
{
return AllPackAssets.Where(t => t.CollectorType == ECollectorType.MainAssetCollector).ToArray();
}
///
/// 创建PackageBundle类
///
internal PackageBundle CreatePackageBundle()
{
PackageBundle packageBundle = new PackageBundle();
packageBundle.BundleName = BundleName;
packageBundle.UnityCRC = PackageUnityCRC;
packageBundle.FileHash = PackageFileHash;
packageBundle.FileCRC = PackageFileCRC;
packageBundle.FileSize = PackageFileSize;
packageBundle.Encrypted = Encrypted;
return packageBundle;
}
}
}