Skip to content

Commit

Permalink
[feat][editor]:增加构建补丁包功能(部分打包)
Browse files Browse the repository at this point in the history
  • Loading branch information
CatImmortal committed Mar 30, 2023
1 parent 872eae5 commit 6d92b3b
Show file tree
Hide file tree
Showing 16 changed files with 358 additions and 153 deletions.
8 changes: 7 additions & 1 deletion Assets/CatAsset/Editor/BuildInfo/BundleBuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public class BundleBuildInfo : IComparable<BundleBuildInfo>,IEquatable<BundleBui
/// </summary>
public List<AssetBuildInfo> Assets = new List<AssetBuildInfo>();

public static string GetBundleIdentifyName(string dirName, string bundleName)
{
return RuntimeUtil.GetRegularPath(Path.Combine(dirName, bundleName));
}

public BundleBuildInfo(string directoryName, string bundleName,string group,bool isRaw,BundleCompressOptions compressOption,BundleEncryptOptions encryptOption)
{
DirectoryName = directoryName;
Expand All @@ -70,7 +75,8 @@ public BundleBuildInfo(string directoryName, string bundleName,string group,bool
{
EncryptOption = BundleEncryptOptions.XOr;
}
BundleIdentifyName = RuntimeUtil.GetRegularPath(Path.Combine(DirectoryName, BundleName));

BundleIdentifyName = GetBundleIdentifyName(DirectoryName, BundleName);
}

/// <summary>
Expand Down
17 changes: 14 additions & 3 deletions Assets/CatAsset/Editor/BuildPipeline/BuildPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,21 @@ public static ReturnCode BuildBundles(BuildTarget targetPlatform,bool isBuildPat
taskList.Add(new BuildManifest());
taskList.Add(new EncryptBundles());
taskList.Add(new CalculateVerifyInfo());
taskList.Add(new AppendMD5());
taskList.Add(new WriteManifestFile());
taskList.Add(new WriteManifestFileToCache());
if (HasOption(bundleBuildConfig.Options,BundleBuildOptions.AppendMD5))
{
taskList.Add(new AppendMD5());
}
if (isBuildPatch)
{
//补丁包需要合并资源清单
taskList.Add(new MergePatchManifest());
}
taskList.Add(new WriteManifestFile());
if (!isBuildPatch)
{
//非补丁包 写入缓存
taskList.Add(new WriteCacheFile());
}
if (bundleBuildConfig.IsCopyToReadOnlyDirectory && bundleBuildConfig.TargetPlatforms.Count == 1)
{
//需要复制资源包到只读目录下
Expand Down
5 changes: 0 additions & 5 deletions Assets/CatAsset/Editor/BuildPipeline/Task/AppendMD5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public class AppendMD5 : IBuildTask

public ReturnCode Run()
{
if ((configParam.Config.Options & BundleBuildOptions.AppendMD5) == 0)
{
return ReturnCode.SuccessNotRun;
}

CatAssetManifest manifest = manifestParam.Manifest;
string outputFolder = ((BundleBuildParameters) buildParam).OutputFolder;

Expand Down
10 changes: 4 additions & 6 deletions Assets/CatAsset/Editor/BuildPipeline/Task/BuildManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public class BuildManifest : IBuildTask
public ReturnCode Run()
{
string outputFolder = ((BundleBuildParameters) buildParam).OutputFolder;



HashSet<string> atlasPackableSet = new HashSet<string>();
if (infoParam.NormalBundleBuilds.Count > 0)
{
Expand All @@ -57,8 +56,7 @@ public ReturnCode Run()
}
}
}



//创建资源清单
CatAssetManifest manifest = new CatAssetManifest
{
Expand All @@ -67,8 +65,8 @@ public ReturnCode Run()
Bundles = new List<BundleManifestInfo>(),
};

//增加内置Shader资源包的构建信息
if (results.BundleInfos.ContainsKey(RuntimeUtil.BuiltInShaderBundleName))
//非构建补丁包时 增加内置Shader资源包的构建信息
if (!configParam.IsBuildPatch && results.BundleInfos.ContainsKey(RuntimeUtil.BuiltInShaderBundleName))
{
BundleBuildInfo bundleBuildInfo =
new BundleBuildInfo(string.Empty, RuntimeUtil.BuiltInShaderBundleName, GroupInfo.DefaultGroup, false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ public class CopyToReadOnlyDirectory : IBuildTask
/// <inheritdoc />
public int Version => 1;

[InjectContext(ContextUsage.In)] private IBundleBuildParameters buildParam;
[InjectContext(ContextUsage.In)]
private IBundleBuildParameters buildParam;

[InjectContext(ContextUsage.In)] private IBundleBuildConfigParam configParam;
[InjectContext(ContextUsage.In)]
private IBundleBuildConfigParam configParam;

[InjectContext(ContextUsage.InOut)] private IManifestParam manifestParam;
[InjectContext(ContextUsage.InOut)]
private IManifestParam manifestParam;


/// <inheritdoc />
Expand Down
104 changes: 100 additions & 4 deletions Assets/CatAsset/Editor/BuildPipeline/Task/FillBuildInfoParam.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using CatAsset.Runtime;
using UnityEditor;
using UnityEditor.Build.Pipeline;
using UnityEditor.Build.Pipeline.Injector;
using UnityEditor.Build.Pipeline.Interfaces;
using UnityEditor.Build.Pipeline.Utilities;
using UnityEngine;

namespace CatAsset.Editor
{
Expand All @@ -13,6 +17,7 @@ public class FillBuildInfoParam : IBuildTask
{
public int Version { get; }


[InjectContext(ContextUsage.In)]
private IBundleBuildParameters buildParam;

Expand All @@ -21,6 +26,9 @@ public class FillBuildInfoParam : IBuildTask

[InjectContext(ContextUsage.In)]
private IBundleBuildConfigParam configParam;

[InjectContext(ContextUsage.In)]
private IBuildCache buildCache;

[InjectContext(ContextUsage.InOut)]
private IBundleBuildContent content;
Expand All @@ -39,10 +47,98 @@ public ReturnCode Run()
}
else
{
string folder = EditorUtil.GetBundleCacheFolder(config.OutputRootDirectory, configParam.TargetPlatform);
string path = RuntimeUtil.GetRegularPath(Path.Combine(folder, CatAssetManifest.ManifestJsonFileName));
CatAssetManifest cachedManifest = CatAssetManifest.DeserializeFromJson(File.ReadAllText(path));

HashSet<string> cachedBundles = new HashSet<string>();
foreach (var bundleManifestInfo in cachedManifest.Bundles)
{
cachedBundles.Add(bundleManifestInfo.BundleIdentifyName);
}

//构建补丁资源包
var folder = EditorUtil.GetManifestCacheFolder(config.OutputRootDirectory, configParam.TargetPlatform);
string path = RuntimeUtil.GetRegularPath(Path.Combine(folder, CatAssetManifest.ManifestBinaryFileName));
CatAssetManifest cacheManifest = CatAssetManifest.DeserializeFromBinary(File.ReadAllBytes(path));
folder = EditorUtil.GetAssetCacheManifestFolder(config.OutputRootDirectory);
path = RuntimeUtil.GetRegularPath(Path.Combine(folder, AssetCacheManifest.ManifestJsonFileName));
string json = File.ReadAllText(path);
AssetCacheManifest assetCacheManifest = JsonUtility.FromJson<AssetCacheManifest>(json);
Dictionary<string, string> cacheDict = assetCacheManifest.GetCacheDict();

var clonedConfig = Object.Instantiate(config); //深拷贝一份进行操作
HashSet<BundleBuildInfo> patchBundles = new HashSet<BundleBuildInfo>();

for (int i = clonedConfig.Bundles.Count - 1; i >= 0; i--)
{
BundleBuildInfo bundleBuildInfo = clonedConfig.Bundles[i];

//是新资源包 直接跳过处理
if (!cachedBundles.Contains(bundleBuildInfo.BundleIdentifyName))
{
continue;
}

//是旧资源包
//有新资源 或者 旧资源文件发生了变化 就认为是补丁包
for (int j = bundleBuildInfo.Assets.Count - 1; j >= 0; j--)
{
AssetBuildInfo assetBuildInfo = bundleBuildInfo.Assets[j];

bool isNewOrChangedAsset = false;
if (!cacheDict.TryGetValue(assetBuildInfo.Name,out string cachedMD5))
{
//新资源
isNewOrChangedAsset = true;
}
else
{
string md5 = RuntimeUtil.GetFileMD5(assetBuildInfo.Name);
if (md5 != cachedMD5)
{
//旧资源文件发生了变化
Debug.Log($"{assetBuildInfo.Name}发生了变化:{cachedMD5} -> {md5}");
isNewOrChangedAsset = true;
}
}

if (isNewOrChangedAsset)
{
//是补丁包
patchBundles.Add(bundleBuildInfo);
}
else
{
//无变化的资源文件 从 bundleBuildInfo 中删除
bundleBuildInfo.Assets.RemoveAt(j);
}
}

if (!patchBundles.Contains(bundleBuildInfo))
{
//是旧资源包 但不是补丁包 需要移除
clonedConfig.Bundles.RemoveAt(i);
}
}

foreach (BundleBuildInfo patchBundle in patchBundles)
{
if (patchBundle.IsRaw)
{
//原生资源包就不当补丁包处理了 直接构建为新资源包
continue;
}

//将资源包名设置为对应补丁包的名字
var part = patchBundle.BundleName.Split('.');
patchBundle.BundleName = $"{part[0]}_patch.{part[1]}";
patchBundle.BundleIdentifyName =
BundleBuildInfo.GetBundleIdentifyName(patchBundle.DirectoryName, patchBundle.BundleName);
}

//TODO:处理冗余资源

buildInfoParam = new BundleBuildInfoParam(clonedConfig.GetAssetBundleBuilds(),clonedConfig.GetNormalBundleBuilds(),
clonedConfig.GetRawBundleBuilds());

}

((BundleBuildParameters)buildParam).SetBundleBuilds(buildInfoParam.NormalBundleBuilds);
Expand Down

This file was deleted.

Loading

0 comments on commit 6d92b3b

Please sign in to comment.