Files
JJBB/Assets/Plugins/Script/AssetUpdate/Actions/AssetCompareAction.cs
2024-08-23 15:49:34 +08:00

76 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
namespace AssetUpdate
{
public class AssetCompareAction : IAssetAsyncAction
{
public readonly List<AssetDependencyItem> downloadList = new List<AssetDependencyItem>();
public Dictionary<string, string> streamingPathInfo { get; private set; }
public bool isEnd { get; private set; }
public bool UpdateTimeout()
{
return isEnd;
}
public void Start(AssetDependencyInfo target)
{
var streamingText = Resources.Load<TextAsset>(AssetConst.assetPathDataPath.Open(AssetConst.pathFile));
var formatter = new BinaryFormatter();
try
{
using (var ms = new MemoryStream(streamingText.bytes))
streamingPathInfo = formatter.Deserialize(ms) as Dictionary<string, string>;
}
catch (Exception e)
{
Debug.LogError(e);
}
if (streamingPathInfo == null)
throw new NullReferenceException("Failed to load streamingPathInfo, this is not recoverable!");
Dictionary<string, string> persist = null;
var persistTextPath = AssetConst.persistentDataPath.Open(AssetConst.pathFile + AssetConst.bytesExtension);
if (File.Exists(persistTextPath))
{
try
{
using (var fs = File.OpenRead(persistTextPath))
persist = formatter.Deserialize(fs) as Dictionary<string, string>;
}
catch (Exception e)
{
Debug.LogError(e);
}
}
foreach (var asset in target.assetList)
if (asset.level >= AssetLevel.onInit)
{
string md5;
if (streamingPathInfo.TryGetValue(asset.name, out md5) && md5 == asset.md5)
continue;
if (persist != null && persist.TryGetValue(asset.name, out md5))
{
if (md5 != asset.md5)
{
#if UNITY_EDITOR
Debug.LogWarning(string.Format("Replacing asset {0} with new version!", asset.name));
#endif
}
else
continue;
}
downloadList.Add(asset);
}
isEnd = true;
}
public void Dispose()
{
}
}
}