JJBB/Assets/Plugins/Script/AssetUpdate/Actions/AssetVersionAction.cs

85 lines
2.8 KiB
C#

using System.IO;
using UnityEngine;
namespace AssetUpdate
{
/// <summary>
/// 获得服务器版本的AssetVersion
/// </summary>
public class AssetVersionAction : IAssetAsyncAction
{
private static string fileName
{
get
{
return AssetUtils.GetTextMd5(AssetConst.versionFile) + AssetConst.bundleVariant;
}
}
private AssetUpdateDownloader _downloader;
public AssetDependencyInfo mainInfo { get; private set; }
public bool isEnd { get; private set; }
public AssetUpdateManager.AssetUpdateError error { get; private set; }
public int serverVersion { get; private set; }
public bool UpdateTimeout()
{
return isEnd;
}
public void Dispose()
{
if (_downloader != null)
{
_downloader.Dispose();
_downloader = null;
}
}
public void Start(int version, string uri)
{
serverVersion = version;
LoadAssetInfo();
// 当前资源清单不存在或者过时
if (mainInfo == null)
{
var oldUri = uri;
uri = uri.Open(serverVersion.ToString()).Open(fileName);
var path = AssetConst.persistentDataPath.Open(fileName);
Debug.Log("Download version file from: " + uri + " url: " + oldUri);
_downloader = new AssetUpdateDownloader(uri, path) {onComplete = OnComplete};
_downloader.Start();
}
else
{
isEnd = true;
}
}
private void OnComplete(AssetUpdateDownloader downloader)
{
isEnd = true;
error = downloader.error;
// 转换错误类型
if (error != AssetUpdateManager.AssetUpdateError.FileCreateError
&& error != AssetUpdateManager.AssetUpdateError.Success)
error = AssetUpdateManager.AssetUpdateError.VersionDataError;
LoadAssetInfo();
if (mainInfo == null || mainInfo.version != serverVersion)
{
error = AssetUpdateManager.AssetUpdateError.VersionDataError;
Debug.LogError("Asset version info download complete, but still cannot get file under persistPath!");
}
}
private void LoadAssetInfo()
{
var configPath = AssetConst.persistentDataPath.Open(fileName);
if (File.Exists(configPath))
{
var persistInfo = AssetDependencyInfo.Create(configPath);
if (persistInfo != null && persistInfo.version == serverVersion)
mainInfo = persistInfo;
}
}
}
}