/// <summary> /// 通知事件 /// </summary> /// <param name="state"></param> private void OnSyncEvent(VFStruct data) { if (this.func != null) { func(data); //回调逻辑层 } }
/// <summary> /// 添加到事件队列 /// </summary> public void AddEvent(VFStruct target, Action <VFStruct> func) { lock (m_lockObject) { this.func = func; events.Enqueue(target); } }
/// <summary> /// 一个文件下载完成,更新通知当前文件下载完毕,包括更新下载进度等 /// </summary> /// <param name="vf"></param> public void DownFileFinish(VFStruct vf) { string tts = NumUtil.GetFormatFileSize(totalSize); currentSize += vf.size; string crs = NumUtil.GetFormatFileSize(currentSize); Debug.Log("下载中:" + crs + "/" + tts); }
/// <summary> /// 下载文件 /// </summary> void OnDownloadFile(VFStruct vf) { string url = ApkInfoVo.remoteResPath + remoteVersionVo.CurrentVersion + "/" + vf.resId + "/" + vf.file; currDownFile = gameDataPath + vf.file; currentDownVf = vf; using (WebClient client = new WebClient()) { client.DownloadFileAsync(new System.Uri(url), currDownFile); client.DownloadFileCompleted += FileDownComplete; } }
/// <summary> /// 文件下载完成,网络下载事件的回调,将文件添加到缓存列表,下载完成再删除缓存列表 /// </summary> /// <param name="vf"></param> public void OnDownOneFileComplete(VFStruct vf) { if (vf.size >= 0) { downLs.Add(vf.file); } else { Debug.LogError("文件下载错误:" + vf.file + "\n====错误信息:" + vf.md5); //让下载流程继续,但是记录下载错误的信息 downLs.Add(vf.file); } }
/// <summary> /// 下载文件 /// </summary> void OnDownloadFile(VFStruct vf) { string url = AppConst.RemoteUpdatePath + vf.resId + "/" + vf.file; currDownFile = currentDataPath + "/" + vf.file; currentDownVf = vf; using (WebClient client = new WebClient()) { // client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); client.DownloadFileAsync(new System.Uri(url), currDownFile); client.DownloadFileCompleted += FileDownComplete; } }
private void FileDownComplete(object sender, AsyncCompletedEventArgs e) { if (e.Error == null) { m_SyncEvent(currentDownVf); } else { //如果下载文件错误,添加一个列表,忽略并抛出 VFStruct errV = new VFStruct(); errV.file = currentDownVf.file; errV.size = -1; errV.md5 = e.Error.ToString(); m_SyncEvent(errV); } }
/// <summary> /// 初始化差异文件字典,保存了所有版本中的差异资源 /// </summary> public void InitDifferDict() { vStructDict.Clear(); XmlNode rootNode = xdoc.SelectSingleNode("version"); XmlNodeList resList = rootNode.SelectNodes("vList"); foreach (XmlNode node in resList) { int resId = int.Parse(node.Attributes["resId"].Value); XmlNodeList resNodeDetail = node.SelectNodes("res"); List <VFStruct> vfList = new List <VFStruct>(); foreach (XmlNode resNode in resNodeDetail) { VFStruct vs = new VFStruct(resNode, resId); vfList.Add(vs); } if (!vStructDict.ContainsKey(resId)) { vStructDict.Add(resId, vfList); } } }
void OnUpdate() { while (true) { lock (m_lockObject) { if (events.Count > 0) { VFStruct e = events.Dequeue(); try { OnDownloadFile(e); } catch (System.Exception ex) { UnityEngine.Debug.LogError(ex.Message); } } } Thread.Sleep(1); } }
/// <summary> /// 输入当前版本的版本号,自动获取到最新版本的资源差异列表 /// </summary> /// <param name="resId"></param> /// <returns></returns> public Dictionary <string, VFStruct> GetDiffFromBeginVer(int resId) { Dictionary <string, VFStruct> diffDict = new Dictionary <string, VFStruct>(); //key是文件名,如果字典中已经有了,就检查最新的 int tmpResId = resId; //差异资源列表中可能存在重复资源,需要进行去重,只保留最新的差异文件,直接使用字典保存的方式去重 while (true) { tmpResId++; if (!vStructDict.ContainsKey(tmpResId)) { break; } else { List <VFStruct> vfLs = vStructDict[tmpResId]; //拿到资源列表 for (int i = 0; i < vfLs.Count; i++) //遍历添加 { VFStruct vs = vfLs[i]; if (diffDict.ContainsKey(vs.file))//如果字典中已经存在这个差异文件,则比较版本号,保存版本号大的 { VFStruct ovs = diffDict[vs.file]; if (vs.IsNewerThan(ovs)) //新的资源版本更新,直接替换 { diffDict.Remove(ovs.file); diffDict.Add(vs.file, vs); } } else { diffDict.Add(vs.file, vs); } } } } return(diffDict); }
/// <summary> /// 判断当前是否资源号更新 /// </summary> /// <param name="vf"></param> /// <returns></returns> public bool IsNewerThan(VFStruct vf) { return(this.resId > vf.resId); }
/// <summary> /// 一个文件下载完成,更新通知当前文件下载完毕,包括更新下载进度等 /// </summary> /// <param name="vf"></param> public void DownFileFinish(VFStruct vf) { currentSize += vf.size; SetProgress(currentSize); }
/// <summary> /// 下载文件 /// </summary> /// <param name="vf"></param> public void StartDownFile(VFStruct vf) { AddEvent(vf, OnDownOneFileComplete); }
/// <summary> /// 协程判断下载完成的回调,主线程不能判断事件,所以从子线程进行判断 /// </summary> /// <param name="vf"></param> public void OnDownFinisCallByWWW(VFStruct vf) { downLs.Add(vf.file); SaveToTemp(vf.file); DownFileFinish(vf); }
/// <summary> /// 文件下载完成,网络下载事件的回调,将文件添加到缓存列表,下载完成再删除缓存列表 /// </summary> /// <param name="vf"></param> public void OnDownOneFileComplete(VFStruct vf) { downLs.Add(vf.file); }