Skip to content

Commit

Permalink
[fix][editor]:将资源meta文件的MD5也写入缓存记录中
Browse files Browse the repository at this point in the history
  • Loading branch information
CatImmortal committed Mar 30, 2023
1 parent f68b8d3 commit dd0d705
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
28 changes: 13 additions & 15 deletions Assets/CatAsset/Editor/BuildPipeline/Task/FillBuildInfoParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ public ReturnCode Run()
}

//计算补丁包
//读取上次完整构建时的资源文件MD5信息
//读取上次完整构建时的资源文件AssetCache信息
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> cacheMD5Dict = assetCacheManifest.GetCacheDict();
Dictionary<string, AssetCacheManifest.AssetCacheInfo> assetCacheDict = assetCacheManifest.GetCacheDict();

//当前资源文件的md5字典
Dictionary<string, string> curMD5Dict = new Dictionary<string, string>();
//当前资源文件的AssetCache字典
Dictionary<string, AssetCacheManifest.AssetCacheInfo> curAssetCacheDict = new Dictionary<string, AssetCacheManifest.AssetCacheInfo>();

//资源名 -> 是否已变化
Dictionary<string, bool> assetChangeStateDict = new Dictionary<string, bool>();
Expand All @@ -118,7 +118,7 @@ public ReturnCode Run()
var asset = bundle.Assets[j];

//1.自身是否变化
bool isChanged = IsChangedAsset(asset.Name, assetChangeStateDict, cacheMD5Dict, curMD5Dict,
bool isChanged = IsChangedAsset(asset.Name, assetChangeStateDict, assetCacheDict, curAssetCacheDict,
assetToBundle, cacheAssetToBundle);

if (!isChanged)
Expand All @@ -128,7 +128,7 @@ public ReturnCode Run()
{
foreach (string upStream in upStreamList)
{
isChanged = IsChangedAsset(upStream, assetChangeStateDict, cacheMD5Dict, curMD5Dict,
isChanged = IsChangedAsset(upStream, assetChangeStateDict, assetCacheDict, curAssetCacheDict,
assetToBundle, cacheAssetToBundle);
if (isChanged)
{
Expand All @@ -145,7 +145,7 @@ public ReturnCode Run()
{
foreach (string downStream in downStreamList)
{
isChanged = IsChangedAsset(downStream, assetChangeStateDict, cacheMD5Dict, curMD5Dict,
isChanged = IsChangedAsset(downStream, assetChangeStateDict, assetCacheDict, curAssetCacheDict,
assetToBundle, cacheAssetToBundle);
if (isChanged)
{
Expand Down Expand Up @@ -196,7 +196,7 @@ public ReturnCode Run()
/// 是否为已变化的资源
/// </summary>
private bool IsChangedAsset(string assetName, Dictionary<string, bool> assetChangeStateDict,
Dictionary<string, string> cacheMD5Dict, Dictionary<string, string> curMD5Dict,
Dictionary<string, AssetCacheManifest.AssetCacheInfo> assetCacheDict, Dictionary<string, AssetCacheManifest.AssetCacheInfo> curAssetCacheDict,
Dictionary<string, string> assetToBundle, Dictionary<string, string> cacheAssetToBundle)

{
Expand All @@ -207,19 +207,19 @@ private bool IsChangedAsset(string assetName, Dictionary<string, bool> assetChan
}

//1.新资源
if (!cacheMD5Dict.TryGetValue(assetName, out string cachedMD5))
if (!assetCacheDict.TryGetValue(assetName, out var assetCache))
{
assetChangeStateDict.Add(assetName, true);
return true;
}

//2.变化的旧资源
if (!curMD5Dict.TryGetValue(assetName, out string curMD5))
if (!curAssetCacheDict.TryGetValue(assetName, out var curAssetCache))
{
curMD5 = RuntimeUtil.GetFileMD5(assetName);
curMD5Dict.Add(assetName, curMD5);
curAssetCache = AssetCacheManifest.AssetCacheInfo.Create(assetName);
curAssetCacheDict.Add(assetName, curAssetCache);
}
if (curMD5 != cachedMD5)
if (curAssetCache != assetCache)
{
assetChangeStateDict.Add(assetName, true);
return true;
Expand All @@ -231,8 +231,6 @@ private bool IsChangedAsset(string assetName, Dictionary<string, bool> assetChan
assetChangeStateDict.Add(assetName, true);
return true;
}



//未变化
assetChangeStateDict.Add(assetName, false);
Expand Down
8 changes: 2 additions & 6 deletions Assets/CatAsset/Editor/BuildPipeline/Task/WriteCacheFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,8 @@ public ReturnCode Run()
{
foreach (AssetManifestInfo assetManifestInfo in bundleManifestInfo.Assets)
{
string md5 = RuntimeUtil.GetFileMD5(assetManifestInfo.Name);
assetCacheManifest.Caches.Add(new AssetCacheManifest.AssetCache()
{
Name = assetManifestInfo.Name,
MD5 = md5,
});
AssetCacheManifest.AssetCacheInfo cacheInfo = AssetCacheManifest.AssetCacheInfo.Create(assetManifestInfo.Name);
assetCacheManifest.Caches.Add(cacheInfo);
}
}
string json = EditorJsonUtility.ToJson(assetCacheManifest,true);
Expand Down
56 changes: 50 additions & 6 deletions Assets/CatAsset/Editor/Misc/AssetCacheManifest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using CatAsset.Runtime;

namespace CatAsset.Editor
{
Expand All @@ -9,11 +10,52 @@ namespace CatAsset.Editor
[Serializable]
public class AssetCacheManifest
{
/// <summary>
/// 资源缓存信息
/// </summary>
[Serializable]
public class AssetCache
public struct AssetCacheInfo : IEquatable<AssetCacheInfo>
{
public string Name;
public string MD5;
public string MetaMD5;

public bool Equals(AssetCacheInfo other)
{
return Name == other.Name && MD5 == other.MD5 && MetaMD5 == other.MetaMD5;
}

public override bool Equals(object obj)
{
return obj is AssetCacheInfo other && Equals(other);
}

public override int GetHashCode()
{
return HashCode.Combine(Name, MD5, MetaMD5);
}

public static bool operator ==(AssetCacheInfo a,AssetCacheInfo b)
{
return Equals(a, b);
}

public static bool operator !=(AssetCacheInfo a,AssetCacheInfo b)
{
return !(a == b);
}

public static AssetCacheInfo Create(string assetName)
{
AssetCacheInfo assetCacheInfo = new AssetCacheInfo
{
Name = assetName,
MD5 = RuntimeUtil.GetFileMD5(assetName),
MetaMD5 = RuntimeUtil.GetFileMD5($"{assetName}.meta")
};
return assetCacheInfo;
}

}

/// <summary>
Expand All @@ -22,17 +64,19 @@ public class AssetCache
public const string ManifestJsonFileName = "AssetCacheManifest.json";


public List<AssetCache> Caches = new List<AssetCache>();
public List<AssetCacheInfo> Caches = new List<AssetCacheInfo>();

public Dictionary<string, string> GetCacheDict()
public Dictionary<string, AssetCacheInfo> GetCacheDict()
{
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (AssetCache assetCache in Caches)
Dictionary<string, AssetCacheInfo> result = new Dictionary<string, AssetCacheInfo>();
foreach (AssetCacheInfo assetCache in Caches)
{
result.Add(assetCache.Name,assetCache.MD5);
result.Add(assetCache.Name,assetCache);
}

return result;
}


}
}

0 comments on commit dd0d705

Please sign in to comment.