调用的地方
只有下载完毕之后才保存可写区资源版本号
#region BeginCheckVersionChange 开始检查更新
/// <summary>
/// 开始检查更新
/// </summary>
private void BeginCheckVersionChange()
{
m_DownloadingParams = GameEntry.Pool.DequeueClassObject<BaseParams>();
m_DownloadingParams.Reset();
//需要删除的文件
LinkedList<string> delList = new LinkedList<string>();
//可写区资源MD5和CDN资源CD5不一致的文件
LinkedList<string> inconformityList = new LinkedList<string>();
LinkedList<string> needDownloadList = new LinkedList<string>();
#region 找出需要删除的可写区文件链表 和 需要下载的可写区文件链表
//拿到可写区的资源版本信息
var enumerator = m_LocalAssetsVersionDic.GetEnumerator();
while (enumerator.MoveNext())
{
//资源包名字
string assetBundleName = enumerator.Current.Key;
AssetBundleInfoEntity cdnAssetBundleInfo = null;
//拿到资源包名字和CDN进行对比
if (m_CDNVersionDic.TryGetValue(assetBundleName, out cdnAssetBundleInfo))
{
//可写区有 CDN也有
if (!cdnAssetBundleInfo.MD5.Equals(enumerator.Current.Value.MD5, StringComparison.CurrentCultureIgnoreCase))
{
//如果MD5不一致 加入不一致链表
inconformityList.AddLast(assetBundleName);
}
}
else
{
//可写区有 CDN上没有,文件过期了, 加入删除链表
delList.AddLast(assetBundleName);
}
}
//循环可写区和CDN都有的版本文件,但是版本不一致
//判断这个文件在只读区的MD5和CDN是否一致 一致的要删除 不一致的则重新下载
LinkedListNode<string> currInconformity = inconformityList.First;
while (currInconformity != null)
{
//CDN的资源包信息
AssetBundleInfoEntity cdnAssetBundleInfo = null;
m_CDNVersionDic.TryGetValue(currInconformity.Value, out cdnAssetBundleInfo);
//只读区的资源包信息
AssetBundleInfoEntity streamingAssetsAssetBundleInfo = null;
if (m_StreamingAssetsVersionDic != null)
{
m_StreamingAssetsVersionDic.TryGetValue(currInconformity.Value, out streamingAssetsAssetBundleInfo);
}
if (streamingAssetsAssetBundleInfo == null)
{
//如果只读区没有,则重新下载
needDownloadList.AddLast(currInconformity.Value);
}
else
{
//只读区有,再判断只读区的和CDN是否一致
if (cdnAssetBundleInfo.MD5.Equals(streamingAssetsAssetBundleInfo.MD5, StringComparison.CurrentCultureIgnoreCase))
{
//一致,则删除可写区文件
delList.AddLast(currInconformity.Value);
}
else
{
//不一致,则重新下载,加入到下载列表
needDownloadList.AddLast(currInconformity.Value);
}
}
currInconformity = currInconformity.Next;
}
#endregion
#region 处理需要删除的可写区文件链表
LinkedListNode<string> currDel = delList.First;
while (currDel != null)
{
string filePath = string.Format("{0}/{1}", GameEntry.Resource.LocalFilePath, currDel.Value);
if (File.Exists(filePath)) File.Delete(filePath);
LinkedListNode<string> next = currDel.Next;
delList.Remove(currDel);
currDel = next;
}
#endregion
#region 处理需要下载的可写区文件链表,新增了一部分资源或者上次下载没下载完的
//循环CDN的资源字典
enumerator = m_CDNVersionDic.GetEnumerator();
while (enumerator.MoveNext())
{
AssetBundleInfoEntity cdnAssetBundleInfo = enumerator.Current.Value;
//拿到当前资源判断
if (cdnAssetBundleInfo.IsFirstData)//是否初始资源
{
//判断可写区文件有没有
if (!m_LocalAssetsVersionDic.ContainsKey(cdnAssetBundleInfo.AssetBundleName))
{
//如果可写区没有 则去只读区查找
AssetBundleInfoEntity streamingAssetsAssetBundleInfo = null;
if (m_StreamingAssetsVersionDic != null)
{
m_StreamingAssetsVersionDic.TryGetValue(cdnAssetBundleInfo.AssetBundleName, out streamingAssetsAssetBundleInfo);
}
if (streamingAssetsAssetBundleInfo == null)
{
//如果只读区也没有,则去CDN上下载
needDownloadList.AddLast(cdnAssetBundleInfo.AssetBundleName);
}
else
{
//如果只读区有 验证MD5
if (!cdnAssetBundleInfo.MD5.Equals(streamingAssetsAssetBundleInfo.MD5, StringComparison.CurrentCultureIgnoreCase))
{
//MD5不一致,则去CDN上下载
needDownloadList.AddLast(cdnAssetBundleInfo.AssetBundleName);
}
}
}
}
}
#endregion
//发出开始下载的事件,uiloading条的显示
GameEntry.Event.CommonEvent.Dispatch(SysEventId.CheckVersionBeginDownload);
//进行下载,需要下载的文件,下载中,下载完成
GameEntry.Download.BeginDownloadMulit(needDownloadList, OnDownloadMulitUpdate, OnDownloadMulitComplete);
}
#endregion
/// <summary>
/// 下载进行中
/// </summary>
private void OnDownloadMulitUpdate(int t1, int t2, ulong t3, ulong t4)
{
m_DownloadingParams.IntParam1 = t1;
m_DownloadingParams.IntParam2 = t2;
m_DownloadingParams.ULongParam1 = t3;
m_DownloadingParams.ULongParam2 = t4;
GameEntry.Event.CommonEvent.Dispatch(SysEventId.CheckVersionDownloadUpdate, m_DownloadingParams);
}
/// <summary>
/// 下载完毕
/// </summary>
private void OnDownloadMulitComplete()
{
//保存资源版本号,可写区的
SetResourceVersion();
GameEntry.Event.CommonEvent.Dispatch(SysEventId.CheckVersionDownloadComplete);
GameEntry.Pool.EnqueueClassObject(m_DownloadingParams);
//进入预加载流程
GameEntry.Procedure.ChangeState(ProcedureState.Preload);
}