Files
YooAsset/Assets/YooAsset/Editor/AssetBundleBuilder/BuildTasks/TaskCreateReport.cs

221 lines
8.2 KiB
C#
Raw Normal View History

2022-03-16 22:25:55 +08:00
using System;
using System.Collections.Generic;
2022-03-17 21:52:08 +08:00
using System.IO;
using UnityEditor;
2022-03-16 22:25:55 +08:00
namespace YooAsset.Editor
{
[TaskAttribute("创建构建报告文件")]
2022-03-16 22:25:55 +08:00
public class TaskCreateReport : IBuildTask
{
void IBuildTask.Run(BuildContext context)
{
2022-06-30 14:29:27 +08:00
var buildParameters = context.GetContextObject<BuildParametersContext>();
2022-04-02 15:12:08 +08:00
var buildMapContext = context.GetContextObject<BuildMapContext>();
2023-03-10 23:44:15 +08:00
var manifestContext = context.GetContextObject<ManifestContext>();
2022-05-06 20:09:36 +08:00
var buildMode = buildParameters.Parameters.BuildMode;
if (buildMode != EBuildMode.SimulateBuild)
{
2023-03-10 23:44:15 +08:00
CreateReportFile(buildParameters, buildMapContext, manifestContext);
2022-05-06 20:09:36 +08:00
}
2022-03-16 22:25:55 +08:00
}
2023-03-10 23:44:15 +08:00
private void CreateReportFile(BuildParametersContext buildParametersContext, BuildMapContext buildMapContext, ManifestContext manifestContext)
2022-03-16 22:25:55 +08:00
{
2022-09-27 21:04:08 +08:00
var buildParameters = buildParametersContext.Parameters;
string packageOutputDirectory = buildParametersContext.GetPackageOutputDirectory();
2023-03-10 23:44:15 +08:00
PackageManifest manifest = manifestContext.Manifest;
BuildReport buildReport = new BuildReport();
2022-03-16 22:25:55 +08:00
2022-03-17 21:52:08 +08:00
// 概述信息
2022-03-19 00:41:50 +08:00
{
#if UNITY_2019_4_OR_NEWER
UnityEditor.PackageManager.PackageInfo packageInfo = UnityEditor.PackageManager.PackageInfo.FindForAssembly(typeof(BuildReport).Assembly);
if (packageInfo != null)
buildReport.Summary.YooVersion = packageInfo.version;
#endif
2022-03-19 00:41:50 +08:00
buildReport.Summary.UnityVersion = UnityEngine.Application.unityVersion;
buildReport.Summary.BuildDate = DateTime.Now.ToString();
buildReport.Summary.BuildSeconds = BuildRunner.TotalSeconds;
2022-09-27 21:04:08 +08:00
buildReport.Summary.BuildTarget = buildParameters.BuildTarget;
buildReport.Summary.BuildPipeline = buildParameters.BuildPipeline;
buildReport.Summary.BuildMode = buildParameters.BuildMode;
buildReport.Summary.BuildPackageName = buildParameters.PackageName;
buildReport.Summary.BuildPackageVersion = buildParameters.PackageVersion;
2022-10-19 15:54:59 +08:00
buildReport.Summary.EnableAddressable = buildMapContext.EnableAddressable;
buildReport.Summary.UniqueBundleName = buildMapContext.UniqueBundleName;
2022-09-27 21:04:08 +08:00
buildReport.Summary.EncryptionServicesClassName = buildParameters.EncryptionServices == null ?
"null" : buildParameters.EncryptionServices.GetType().FullName;
2022-03-19 00:41:50 +08:00
// 构建参数
2022-09-27 21:04:08 +08:00
buildReport.Summary.OutputNameStyle = buildParameters.OutputNameStyle;
buildReport.Summary.CompressOption = buildParameters.CompressOption;
buildReport.Summary.DisableWriteTypeTree = buildParameters.DisableWriteTypeTree;
buildReport.Summary.IgnoreTypeTreeChanges = buildParameters.IgnoreTypeTreeChanges;
2022-03-19 00:41:50 +08:00
// 构建结果
buildReport.Summary.AssetFileTotalCount = buildMapContext.AssetFileCount;
2023-03-10 23:44:15 +08:00
buildReport.Summary.MainAssetTotalCount = GetMainAssetCount(manifest);
buildReport.Summary.AllBundleTotalCount = GetAllBundleCount(manifest);
buildReport.Summary.AllBundleTotalSize = GetAllBundleSize(manifest);
buildReport.Summary.EncryptedBundleTotalCount = GetEncryptedBundleCount(manifest);
buildReport.Summary.EncryptedBundleTotalSize = GetEncryptedBundleSize(manifest);
buildReport.Summary.RawBundleTotalCount = GetRawBundleCount(manifest);
buildReport.Summary.RawBundleTotalSize = GetRawBundleSize(manifest);
2022-03-19 00:41:50 +08:00
}
2022-03-16 22:25:55 +08:00
2022-03-17 21:52:08 +08:00
// 资源对象列表
2023-03-10 23:44:15 +08:00
buildReport.AssetInfos = new List<ReportAssetInfo>(manifest.AssetList.Count);
foreach (var packageAsset in manifest.AssetList)
2022-03-17 21:52:08 +08:00
{
2023-03-10 23:44:15 +08:00
var mainBundle = manifest.BundleList[packageAsset.BundleID];
2022-03-17 21:52:08 +08:00
ReportAssetInfo reportAssetInfo = new ReportAssetInfo();
2023-03-10 23:44:15 +08:00
reportAssetInfo.Address = packageAsset.Address;
reportAssetInfo.AssetPath = packageAsset.AssetPath;
reportAssetInfo.AssetTags = packageAsset.AssetTags;
reportAssetInfo.AssetGUID = AssetDatabase.AssetPathToGUID(packageAsset.AssetPath);
reportAssetInfo.MainBundleName = mainBundle.BundleName;
2022-08-03 20:47:43 +08:00
reportAssetInfo.MainBundleSize = mainBundle.FileSize;
2023-03-10 23:44:15 +08:00
reportAssetInfo.DependBundles = GetDependBundles(manifest, packageAsset);
reportAssetInfo.DependAssets = GetDependAssets(buildMapContext, mainBundle.BundleName, packageAsset.AssetPath);
2022-03-17 21:52:08 +08:00
buildReport.AssetInfos.Add(reportAssetInfo);
}
// 资源包列表
2023-03-10 23:44:15 +08:00
buildReport.BundleInfos = new List<ReportBundleInfo>(manifest.BundleList.Count);
foreach (var packageBundle in manifest.BundleList)
2022-03-17 21:52:08 +08:00
{
ReportBundleInfo reportBundleInfo = new ReportBundleInfo();
2023-03-10 23:44:15 +08:00
reportBundleInfo.BundleName = packageBundle.BundleName;
reportBundleInfo.FileName = packageBundle.FileName;
reportBundleInfo.FileHash = packageBundle.FileHash;
reportBundleInfo.FileCRC = packageBundle.FileCRC;
reportBundleInfo.FileSize = packageBundle.FileSize;
reportBundleInfo.IsRawFile = packageBundle.IsRawFile;
reportBundleInfo.LoadMethod = (EBundleLoadMethod)packageBundle.LoadMethod;
reportBundleInfo.Tags = packageBundle.Tags;
reportBundleInfo.ReferenceIDs = packageBundle.ReferenceIDs;
reportBundleInfo.AllBuiltinAssets = GetAllBuiltinAssets(buildMapContext, packageBundle.BundleName);
2022-03-17 21:52:08 +08:00
buildReport.BundleInfos.Add(reportBundleInfo);
}
2022-03-16 22:25:55 +08:00
2022-03-17 21:52:08 +08:00
// 序列化文件
string fileName = YooAssetSettingsData.GetReportFileName(buildParameters.PackageName, buildParameters.PackageVersion);
string filePath = $"{packageOutputDirectory}/{fileName}";
2022-03-16 22:25:55 +08:00
BuildReport.Serialize(filePath, buildReport);
BuildLogger.Log($"资源构建报告文件创建完成:{filePath}");
2022-03-16 22:25:55 +08:00
}
2022-03-17 21:52:08 +08:00
/// <summary>
/// 获取资源对象依赖的所有资源包
/// </summary>
2023-03-10 23:44:15 +08:00
private List<string> GetDependBundles(PackageManifest manifest, PackageAsset packageAsset)
2022-03-17 21:52:08 +08:00
{
2023-03-10 23:44:15 +08:00
List<string> dependBundles = new List<string>(packageAsset.DependIDs.Length);
foreach (int index in packageAsset.DependIDs)
2022-03-17 21:52:08 +08:00
{
2023-03-10 23:44:15 +08:00
string dependBundleName = manifest.BundleList[index].BundleName;
2022-03-17 21:52:08 +08:00
dependBundles.Add(dependBundleName);
}
return dependBundles;
}
/// <summary>
/// 获取资源对象依赖的其它所有资源
/// </summary>
2022-04-02 15:12:08 +08:00
private List<string> GetDependAssets(BuildMapContext buildMapContext, string bundleName, string assetPath)
2022-03-17 21:52:08 +08:00
{
List<string> result = new List<string>();
var bundleInfo = buildMapContext.GetBundleInfo(bundleName);
2022-03-17 21:52:08 +08:00
{
BuildAssetInfo findAssetInfo = null;
foreach (var assetInfo in bundleInfo.AllMainAssets)
2022-03-17 21:52:08 +08:00
{
if (assetInfo.AssetPath == assetPath)
2022-03-17 21:52:08 +08:00
{
findAssetInfo = assetInfo;
2022-03-17 21:52:08 +08:00
break;
}
}
if (findAssetInfo == null)
{
throw new Exception($"Not found asset {assetPath} in bunlde {bundleName}");
}
2022-03-19 00:41:50 +08:00
foreach (var dependAssetInfo in findAssetInfo.AllDependAssetInfos)
2022-03-17 21:52:08 +08:00
{
result.Add(dependAssetInfo.AssetPath);
}
}
return result;
}
2022-03-19 00:41:50 +08:00
/// <summary>
/// 获取该资源包内的所有资源(包括零依赖资源)
/// </summary>
private List<string> GetAllBuiltinAssets(BuildMapContext buildMapContext, string bundleName)
{
var bundleInfo = buildMapContext.GetBundleInfo(bundleName);
return bundleInfo.GetAllBuiltinAssetPaths();
}
2023-03-10 23:44:15 +08:00
private int GetMainAssetCount(PackageManifest manifest)
{
2023-03-10 23:44:15 +08:00
return manifest.AssetList.Count;
}
2023-03-10 23:44:15 +08:00
private int GetAllBundleCount(PackageManifest manifest)
2022-03-19 00:41:50 +08:00
{
2023-03-10 23:44:15 +08:00
return manifest.BundleList.Count;
2022-03-19 00:41:50 +08:00
}
2023-03-10 23:44:15 +08:00
private long GetAllBundleSize(PackageManifest manifest)
2022-03-19 00:41:50 +08:00
{
long fileBytes = 0;
2023-03-10 23:44:15 +08:00
foreach (var packageBundle in manifest.BundleList)
2022-03-19 00:41:50 +08:00
{
2023-03-10 23:44:15 +08:00
fileBytes += packageBundle.FileSize;
2022-03-19 00:41:50 +08:00
}
return fileBytes;
}
2023-03-10 23:44:15 +08:00
private int GetEncryptedBundleCount(PackageManifest manifest)
2022-03-19 00:41:50 +08:00
{
int fileCount = 0;
2023-03-10 23:44:15 +08:00
foreach (var packageBundle in manifest.BundleList)
2022-03-19 00:41:50 +08:00
{
2023-03-10 23:44:15 +08:00
if (packageBundle.LoadMethod != (byte)EBundleLoadMethod.Normal)
2022-03-19 00:41:50 +08:00
fileCount++;
}
return fileCount;
}
2023-03-10 23:44:15 +08:00
private long GetEncryptedBundleSize(PackageManifest manifest)
2022-03-19 00:41:50 +08:00
{
long fileBytes = 0;
2023-03-10 23:44:15 +08:00
foreach (var packageBundle in manifest.BundleList)
2022-03-19 00:41:50 +08:00
{
2023-03-10 23:44:15 +08:00
if (packageBundle.LoadMethod != (byte)EBundleLoadMethod.Normal)
fileBytes += packageBundle.FileSize;
2022-03-19 00:41:50 +08:00
}
return fileBytes;
}
2023-03-10 23:44:15 +08:00
private int GetRawBundleCount(PackageManifest manifest)
2022-03-19 00:41:50 +08:00
{
int fileCount = 0;
2023-03-10 23:44:15 +08:00
foreach (var packageBundle in manifest.BundleList)
2022-03-19 00:41:50 +08:00
{
2023-03-10 23:44:15 +08:00
if (packageBundle.IsRawFile)
2022-03-19 00:41:50 +08:00
fileCount++;
}
return fileCount;
}
2023-03-10 23:44:15 +08:00
private long GetRawBundleSize(PackageManifest manifest)
2022-03-19 00:41:50 +08:00
{
long fileBytes = 0;
2023-03-10 23:44:15 +08:00
foreach (var packageBundle in manifest.BundleList)
2022-03-19 00:41:50 +08:00
{
2023-03-10 23:44:15 +08:00
if (packageBundle.IsRawFile)
fileBytes += packageBundle.FileSize;
2022-03-19 00:41:50 +08:00
}
return fileBytes;
}
2022-03-16 22:25:55 +08:00
}
}