Files
YooAsset/Assets/YooAsset/Editor/AssetBundleBuilder/BuildBundleInfo.cs

176 lines
4.2 KiB
C#
Raw Normal View History

2022-03-16 11:33:45 +08:00
using System;
using System.Linq;
2022-03-01 10:44:12 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
namespace YooAsset.Editor
{
public class BuildBundleInfo
{
2023-07-17 19:38:11 +08:00
#region
/// <summary>
/// Unity引擎生成的哈希值构建内容的哈希值
/// </summary>
public string PackageUnityHash { set; get; }
2022-03-01 10:44:12 +08:00
/// <summary>
2023-07-17 19:38:11 +08:00
/// Unity引擎生成的CRC
2022-03-01 10:44:12 +08:00
/// </summary>
2023-07-17 19:38:11 +08:00
public uint PackageUnityCRC { set; get; }
2022-03-01 10:44:12 +08:00
/// <summary>
2023-07-17 19:38:11 +08:00
/// 文件哈希值
2022-03-01 10:44:12 +08:00
/// </summary>
2023-07-17 19:38:11 +08:00
public string PackageFileHash { set; get; }
2022-03-01 10:44:12 +08:00
/// <summary>
2023-07-17 19:38:11 +08:00
/// 文件哈希值
/// </summary>
2023-07-17 19:38:11 +08:00
public string PackageFileCRC { set; get; }
2022-11-02 21:57:08 +08:00
/// <summary>
2023-07-17 19:38:11 +08:00
/// 文件哈希值
2022-11-02 21:57:08 +08:00
/// </summary>
2023-07-17 19:38:11 +08:00
public long PackageFileSize { set; get; }
/// <summary>
/// 构建输出的文件路径
/// </summary>
public string BuildOutputFilePath { set; get; }
/// <summary>
/// 补丁包的源文件路径
/// </summary>
public string PackageSourceFilePath { set; get; }
/// <summary>
/// 补丁包的目标文件路径
/// </summary>
public string PackageDestFilePath { set; get; }
2022-11-02 21:57:08 +08:00
/// <summary>
/// 加密生成文件的路径
/// 注意:如果未加密该路径为空
/// </summary>
2022-11-02 21:57:08 +08:00
public string EncryptedFilePath { set; get; }
2023-07-17 19:38:11 +08:00
#endregion
/// <summary>
/// 参与构建的资源列表
/// 注意:不包含零依赖资源和冗余资源
/// </summary>
2023-09-20 16:09:52 +08:00
public readonly List<BuildAssetInfo> MainAssets = new List<BuildAssetInfo>();
2022-03-01 10:44:12 +08:00
/// <summary>
2023-09-20 16:09:52 +08:00
/// 资源包名称
2022-03-01 10:44:12 +08:00
/// </summary>
2023-09-20 16:09:52 +08:00
public string BundleName { private set; get; }
2022-03-01 10:44:12 +08:00
2022-08-03 20:47:43 +08:00
/// <summary>
2023-09-20 16:09:52 +08:00
/// 加密文件
2022-08-03 20:47:43 +08:00
/// </summary>
2023-09-20 16:09:52 +08:00
public bool Encrypted { set; get; }
2022-08-03 20:47:43 +08:00
2022-03-01 10:44:12 +08:00
2022-03-16 17:35:21 +08:00
public BuildBundleInfo(string bundleName)
2022-03-01 10:44:12 +08:00
{
2022-03-16 17:35:21 +08:00
BundleName = bundleName;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 添加一个打包资源
/// </summary>
public void PackAsset(BuildAssetInfo assetInfo)
{
if (IsContainsAsset(assetInfo.AssetPath))
2023-10-17 19:03:11 +08:00
throw new System.Exception($"Should never get here ! Asset is existed : {assetInfo.AssetPath}");
2022-03-01 10:44:12 +08:00
2023-09-20 16:09:52 +08:00
MainAssets.Add(assetInfo);
2022-03-01 10:44:12 +08:00
}
/// <summary>
2022-04-02 15:12:08 +08:00
/// 是否包含指定资源
2022-03-01 10:44:12 +08:00
/// </summary>
2022-04-02 15:12:08 +08:00
public bool IsContainsAsset(string assetPath)
2022-03-01 10:44:12 +08:00
{
2023-09-20 16:09:52 +08:00
foreach (var assetInfo in MainAssets)
2022-04-02 15:12:08 +08:00
{
if (assetInfo.AssetPath == assetPath)
{
return true;
}
}
return false;
2022-03-01 10:44:12 +08:00
}
/// <summary>
/// 获取构建的资源路径列表
/// </summary>
public string[] GetAllMainAssetPaths()
{
2023-09-20 16:09:52 +08:00
return MainAssets.Select(t => t.AssetPath).ToArray();
}
/// <summary>
/// 获取该资源包内的所有资源(包括零依赖资源和冗余资源)
/// </summary>
public List<string> GetAllBuiltinAssetPaths()
{
var packAssets = GetAllMainAssetPaths();
List<string> result = new List<string>(packAssets);
2023-09-20 16:09:52 +08:00
foreach (var assetInfo in MainAssets)
{
if (assetInfo.AllDependAssetInfos == null)
continue;
2023-03-14 16:12:48 +08:00
foreach (var dependAssetInfo in assetInfo.AllDependAssetInfos)
{
// 注意:依赖资源里只添加零依赖资源和冗余资源
2023-03-14 16:12:48 +08:00
if (dependAssetInfo.HasBundleName() == false)
{
2023-03-14 16:12:48 +08:00
if (result.Contains(dependAssetInfo.AssetPath) == false)
result.Add(dependAssetInfo.AssetPath);
}
}
}
return result;
}
2022-03-01 10:44:12 +08:00
/// <summary>
/// 创建AssetBundleBuild类
/// </summary>
public UnityEditor.AssetBundleBuild CreatePipelineBuild()
{
// 注意我们不在支持AssetBundle的变种机制
AssetBundleBuild build = new AssetBundleBuild();
build.assetBundleName = BundleName;
build.assetBundleVariant = string.Empty;
build.assetNames = GetAllMainAssetPaths();
2022-03-01 10:44:12 +08:00
return build;
}
/// <summary>
/// 获取所有写入补丁清单的资源
/// </summary>
public BuildAssetInfo[] GetAllManifestAssetInfos()
{
2023-09-20 16:09:52 +08:00
return MainAssets.Where(t => t.CollectorType == ECollectorType.MainAssetCollector).ToArray();
}
/// <summary>
2023-03-10 23:44:15 +08:00
/// 创建PackageBundle类
/// </summary>
2023-03-10 23:44:15 +08:00
internal PackageBundle CreatePackageBundle()
{
2023-03-10 23:44:15 +08:00
PackageBundle packageBundle = new PackageBundle();
packageBundle.BundleName = BundleName;
2023-10-18 12:14:24 +08:00
packageBundle.UnityCRC = PackageUnityCRC;
2023-07-17 19:38:11 +08:00
packageBundle.FileHash = PackageFileHash;
packageBundle.FileCRC = PackageFileCRC;
packageBundle.FileSize = PackageFileSize;
2023-09-20 16:09:52 +08:00
packageBundle.Encrypted = Encrypted;
2023-03-10 23:44:15 +08:00
return packageBundle;
}
2022-03-01 10:44:12 +08:00
}
}