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

236 lines
8.6 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>();
2022-05-06 20:09:36 +08:00
buildParameters.StopWatch();
var buildMode = buildParameters.Parameters.BuildMode;
if (buildMode != EBuildMode.SimulateBuild)
{
CreateReportFile(buildParameters, buildMapContext);
}
else
{
float buildSeconds = buildParameters.GetBuildingSeconds();
BuildRunner.Info($"Build time consuming {buildSeconds} seconds.");
}
2022-03-16 22:25:55 +08:00
}
2022-06-30 14:29:27 +08:00
private void CreateReportFile(BuildParametersContext buildParameters, BuildMapContext buildMapContext)
2022-03-16 22:25:55 +08:00
{
PatchManifest patchManifest = AssetBundleBuilderHelper.LoadPatchManifestFile(buildParameters.PipelineOutputDirectory, buildParameters.Parameters.BuildVersion);
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();
2022-05-06 20:09:36 +08:00
buildReport.Summary.BuildSeconds = (int)buildParameters.GetBuildingSeconds();
2022-03-19 00:41:50 +08:00
buildReport.Summary.BuildTarget = buildParameters.Parameters.BuildTarget;
buildReport.Summary.BuildPipeline = buildParameters.Parameters.BuildPipeline;
buildReport.Summary.BuildMode = buildParameters.Parameters.BuildMode;
2022-03-19 00:41:50 +08:00
buildReport.Summary.BuildVersion = buildParameters.Parameters.BuildVersion;
buildReport.Summary.BuildinTags = buildParameters.Parameters.BuildinTags;
buildReport.Summary.EnableAddressable = buildParameters.Parameters.EnableAddressable;
2022-03-19 00:41:50 +08:00
buildReport.Summary.AppendFileExtension = buildParameters.Parameters.AppendFileExtension;
buildReport.Summary.CopyBuildinTagFiles = buildParameters.Parameters.CopyBuildinTagFiles;
buildReport.Summary.AutoCollectShaders = AssetBundleCollectorSettingData.Setting.AutoCollectShaders;
buildReport.Summary.ShadersBundleName = AssetBundleCollectorSettingData.Setting.ShadersBundleName;
2022-03-23 17:18:43 +08:00
buildReport.Summary.EncryptionServicesClassName = buildParameters.Parameters.EncryptionServices == null ?
"null" : buildParameters.Parameters.EncryptionServices.GetType().FullName;
2022-03-19 00:41:50 +08:00
// 构建参数
buildReport.Summary.CompressOption = buildParameters.Parameters.CompressOption;
buildReport.Summary.DisableWriteTypeTree = buildParameters.Parameters.DisableWriteTypeTree;
buildReport.Summary.IgnoreTypeTreeChanges = buildParameters.Parameters.IgnoreTypeTreeChanges;
// 构建结果
buildReport.Summary.AssetFileTotalCount = buildMapContext.AssetFileCount;
buildReport.Summary.AllBundleTotalCount = GetAllBundleCount(patchManifest);
buildReport.Summary.AllBundleTotalSize = GetAllBundleSize(patchManifest);
buildReport.Summary.BuildinBundleTotalCount = GetBuildinBundleCount(patchManifest);
buildReport.Summary.BuildinBundleTotalSize = GetBuildinBundleSize(patchManifest);
buildReport.Summary.EncryptedBundleTotalCount = GetEncryptedBundleCount(patchManifest);
buildReport.Summary.EncryptedBundleTotalSize = GetEncryptedBundleSize(patchManifest);
buildReport.Summary.RawBundleTotalCount = GetRawBundleCount(patchManifest);
buildReport.Summary.RawBundleTotalSize = GetRawBundleSize(patchManifest);
}
2022-03-16 22:25:55 +08:00
2022-03-17 21:52:08 +08:00
// 资源对象列表
buildReport.AssetInfos = new List<ReportAssetInfo>(patchManifest.AssetList.Count);
foreach (var patchAsset in patchManifest.AssetList)
{
var mainBundle = patchManifest.BundleList[patchAsset.BundleID];
ReportAssetInfo reportAssetInfo = new ReportAssetInfo();
reportAssetInfo.Address = patchAsset.Address;
2022-03-17 21:52:08 +08:00
reportAssetInfo.AssetPath = patchAsset.AssetPath;
reportAssetInfo.AssetTags = patchAsset.AssetTags;
reportAssetInfo.AssetGUID = AssetDatabase.AssetPathToGUID(patchAsset.AssetPath);
reportAssetInfo.MainBundleName = mainBundle.BundleName;
reportAssetInfo.MainBundleSize = mainBundle.SizeBytes;
2022-03-17 21:52:08 +08:00
reportAssetInfo.DependBundles = GetDependBundles(patchManifest, patchAsset);
reportAssetInfo.DependAssets = GetDependAssets(buildMapContext, mainBundle.BundleName, patchAsset.AssetPath);
buildReport.AssetInfos.Add(reportAssetInfo);
}
// 资源包列表
buildReport.BundleInfos = new List<ReportBundleInfo>(patchManifest.BundleList.Count);
foreach (var patchBundle in patchManifest.BundleList)
{
ReportBundleInfo reportBundleInfo = new ReportBundleInfo();
reportBundleInfo.BundleName = patchBundle.BundleName;
reportBundleInfo.Hash = patchBundle.Hash;
reportBundleInfo.CRC = patchBundle.CRC;
reportBundleInfo.SizeBytes = patchBundle.SizeBytes;
reportBundleInfo.Tags = patchBundle.Tags;
reportBundleInfo.Flags = patchBundle.Flags;
buildReport.BundleInfos.Add(reportBundleInfo);
}
2022-03-16 22:25:55 +08:00
// 删除旧文件
string filePath = $"{buildParameters.PipelineOutputDirectory}/{YooAssetSettingsData.GetReportFileName(buildParameters.Parameters.BuildVersion)}";
2022-03-16 22:25:55 +08:00
if (File.Exists(filePath))
File.Delete(filePath);
2022-03-17 21:52:08 +08:00
// 序列化文件
2022-03-16 22:25:55 +08:00
BuildReport.Serialize(filePath, buildReport);
BuildRunner.Log($"资源构建报告文件创建完成:{filePath}");
2022-03-16 22:25:55 +08:00
}
2022-03-17 21:52:08 +08:00
/// <summary>
/// 获取资源对象依赖的所有资源包
/// </summary>
private List<string> GetDependBundles(PatchManifest patchManifest, PatchAsset patchAsset)
{
List<string> dependBundles = new List<string>(patchAsset.DependIDs.Length);
2022-03-19 00:41:50 +08:00
foreach (int index in patchAsset.DependIDs)
2022-03-17 21:52:08 +08:00
{
string dependBundleName = patchManifest.BundleList[index].BundleName;
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>();
2022-03-19 00:41:50 +08:00
if (buildMapContext.TryGetBundleInfo(bundleName, out BuildBundleInfo bundleInfo))
2022-03-17 21:52:08 +08:00
{
BuildAssetInfo findAssetInfo = null;
2022-03-19 00:41:50 +08:00
foreach (var buildinAsset in bundleInfo.BuildinAssets)
2022-03-17 21:52:08 +08:00
{
2022-03-19 00:41:50 +08:00
if (buildinAsset.AssetPath == assetPath)
2022-03-17 21:52:08 +08:00
{
findAssetInfo = buildinAsset;
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);
}
}
else
{
throw new Exception($"Not found bundle : {bundleName}");
}
return result;
}
2022-03-19 00:41:50 +08:00
private int GetAllBundleCount(PatchManifest patchManifest)
{
return patchManifest.BundleList.Count;
}
private long GetAllBundleSize(PatchManifest patchManifest)
{
long fileBytes = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
fileBytes += patchBundle.SizeBytes;
}
return fileBytes;
}
private int GetBuildinBundleCount(PatchManifest patchManifest)
{
int fileCount = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
if (patchBundle.IsBuildin)
fileCount++;
}
return fileCount;
}
private long GetBuildinBundleSize(PatchManifest patchManifest)
{
long fileBytes = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
if (patchBundle.IsBuildin)
fileBytes += patchBundle.SizeBytes;
}
return fileBytes;
}
private int GetEncryptedBundleCount(PatchManifest patchManifest)
{
int fileCount = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
if (patchBundle.IsEncrypted)
fileCount++;
}
return fileCount;
}
private long GetEncryptedBundleSize(PatchManifest patchManifest)
{
long fileBytes = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
if (patchBundle.IsEncrypted)
fileBytes += patchBundle.SizeBytes;
}
return fileBytes;
}
private int GetRawBundleCount(PatchManifest patchManifest)
{
int fileCount = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
if (patchBundle.IsRawFile)
fileCount++;
}
return fileCount;
}
private long GetRawBundleSize(PatchManifest patchManifest)
{
long fileBytes = 0;
foreach (var patchBundle in patchManifest.BundleList)
{
if (patchBundle.IsRawFile)
fileBytes += patchBundle.SizeBytes;
}
return fileBytes;
}
2022-03-16 22:25:55 +08:00
}
}