Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
[refactor]:使用打包Hash判断是否需要更新,使用文件MD5校验文件
[feat][editor]:构建选项增加DisableTypeTree
  • Loading branch information
CatImmortal committed May 24, 2023
1 parent 7d924ae commit 6b1e31c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 30 deletions.
13 changes: 10 additions & 3 deletions Assets/CatAsset/Editor/BuildPipeline/BuildPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using CatAsset.Runtime;
using UnityEditor;
using UnityEditor.Build.Content;
using UnityEditor.Build.Pipeline;
using UnityEditor.Build.Pipeline.Interfaces;
using UnityEditor.Build.Pipeline.Tasks;
Expand Down Expand Up @@ -59,10 +60,10 @@ public static ReturnCode BuildBundles(BuildTarget targetPlatform,bool isBuildPat
taskList.Add(new BuildManifest());
taskList.Add(new EncryptBundles());
taskList.Add(new CalculateVerifyInfo());
if (HasOption(config.Options,BundleBuildOptions.AppendMD5))
if (HasOption(config.Options,BundleBuildOptions.AppendHash))
{
//附加MD5到包名中
taskList.Add(new AppendMD5());
//附加Hash到包名中
taskList.Add(new AppendHash());
}
if (isBuildPatch)
{
Expand Down Expand Up @@ -173,6 +174,12 @@ private static BundleBuildParameters GetSBPParameters(BundleBuildConfigSO bundle
//是否增量构建
parameters.UseCache = !HasOption(bundleBuildConfig.Options,BundleBuildOptions.ForceRebuild);

//关闭TypeTree
if (HasOption(bundleBuildConfig.Options,BundleBuildOptions.DisableTypeTree))
{
parameters.ContentBuildFlags |= ContentBuildFlags.DisableWriteTypeTree;
}

//全局压缩格式
switch (bundleBuildConfig.GlobalCompress)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
namespace CatAsset.Editor
{
/// <summary>
/// 附加MD5到资源包名中
/// 附加Hash到资源包名中
/// </summary>
public class AppendMD5 : IBuildTask
public class AppendHash : IBuildTask
{
[InjectContext(ContextUsage.In)]
private IManifestParam manifestParam;
Expand All @@ -29,7 +29,7 @@ public ReturnCode Run()

foreach (BundleManifestInfo bundleManifestInfo in manifest.Bundles)
{
bundleManifestInfo.IsAppendMD5 = true;
bundleManifestInfo.IsAppendHash = true;

string oldPath = Path.Combine(outputFolder, bundleManifestInfo.BundleIdentifyName);
string newPath = Path.Combine(outputFolder, bundleManifestInfo.RelativePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ public ReturnCode Run()
bundleManifestInfo.Length = (ulong)fi.Length;
bundleManifestInfo.MD5 = RuntimeUtil.GetFileMD5(path);

if (configParam.TargetPlatform == BuildTarget.WebGL && !bundleManifestInfo.IsRaw)
if (!bundleManifestInfo.IsRaw)
{
//WebGL平台 且不是原生资源包 记录Hash128用于缓存系统
//不是原生资源 记录Hash
BundleDetails details = results.BundleInfos[bundleManifestInfo.BundleIdentifyName];
bundleManifestInfo.Hash = details.Hash.ToString();
}
else
{
//原生资源就将MD5视为Hash处理
bundleManifestInfo.Hash = bundleManifestInfo.MD5;
}
}

return ReturnCode.Success;
Expand Down
9 changes: 7 additions & 2 deletions Assets/CatAsset/Editor/Config/BundleBuildOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public enum BundleBuildOptions
ForceRebuild = 1 << 1,

/// <summary>
/// 附加MD5到资源包名中
/// 附加Hash到资源包名中
/// </summary>
AppendMD5 = 1 << 2,
AppendHash = 1 << 2,

/// <summary>
/// 关闭TypeTree
/// </summary>
DisableTypeTree = 1 << 3,
}
}
34 changes: 14 additions & 20 deletions Assets/CatAsset/Runtime/Database/ManifestInfo/BundleManifestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public string RelativePath{
{
if (relativePath == null)
{
if (IsAppendMD5)
if (IsAppendHash)
{
//附加了MD5值到资源包文件名中
//附加了Hash值到资源包文件名中
string[] nameArray = BundleName.Split('.');
string md5BundleName = $"{nameArray[0]}_{MD5}.{nameArray[1]}";
relativePath = RuntimeUtil.GetRegularPath(Path.Combine(Directory,md5BundleName));
string hashBundleName = $"{nameArray[0]}_{Hash}.{nameArray[1]}";
relativePath = RuntimeUtil.GetRegularPath(Path.Combine(Directory,hashBundleName));
}
else
{
Expand Down Expand Up @@ -87,19 +87,19 @@ public string BundleIdentifyName
public ulong Length;

/// <summary>
/// 文件MD5
/// 文件MD5(用于文件校验)
/// </summary>
public string MD5;

/// <summary>
/// 是否附加MD5值到资源包名中
/// 文件Hash值(用于判断是否需要更新)
/// </summary>
public bool IsAppendMD5;

public string Hash;
/// <summary>
/// 文件Hash值
/// 是否附加Hash值到资源包名中
/// </summary>
public string Hash = string.Empty;
public bool IsAppendHash;

/// <summary>
/// 是否依赖内置Shader资源包
Expand Down Expand Up @@ -130,7 +130,7 @@ public bool Equals(BundleManifestInfo other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return BundleIdentifyName == other.BundleIdentifyName && Length == other.Length && MD5 == other.MD5;
return BundleIdentifyName == other.BundleIdentifyName && Length == other.Length && Hash == other.Hash;
}

public override bool Equals(object obj)
Expand All @@ -143,13 +143,7 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
unchecked
{
var hashCode = (BundleIdentifyName != null ? BundleIdentifyName.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Length.GetHashCode();
hashCode = (hashCode * 397) ^ (MD5 != null ? MD5.GetHashCode() : 0);
return hashCode;
}
return (Hash != null ? Hash.GetHashCode() : 0);
}

/// <summary>
Expand All @@ -164,8 +158,8 @@ public void Serialize(BinaryWriter writer)
writer.Write(IsScene);
writer.Write(Length);
writer.Write(MD5);
writer.Write(IsAppendMD5);
writer.Write(Hash);
writer.Write(IsAppendHash);
writer.Write(IsDependencyBuiltInShaderBundle);
writer.Write((byte)EncryptOption);
writer.Write(Assets.Count);
Expand All @@ -188,8 +182,8 @@ public static BundleManifestInfo Deserialize(BinaryReader reader,int serializeVe
info.IsScene = reader.ReadBoolean();
info.Length = reader.ReadUInt64();
info.MD5 = reader.ReadString();
info.IsAppendMD5 = reader.ReadBoolean();
info.Hash = reader.ReadString();
info.IsAppendHash = reader.ReadBoolean();
info.IsDependencyBuiltInShaderBundle = reader.ReadBoolean();
info.EncryptOption = (BundleEncryptOptions)reader.ReadByte();

Expand Down

0 comments on commit 6b1e31c

Please sign in to comment.