///---------------------------------------------------------------------------------------------- /// <summary> /// 判断是否需要更新 /// </summary> ///---------------------------------------------------------------------------------------------- private bool needUpdate(VersionResFile local, VersionResFile server, List <string> update, List <int> updateSize) { //后期采用先判定版本号 foreach (KeyValuePair <string, string> file in server.mPathMD5) { if (local.mPathMD5.ContainsKey(file.Key)) { if (local.mPathMD5[file.Key] != file.Value) { update.Add(file.Key); mAllNeedDownLoadBytes += server.mSize[file.Key]; updateSize.Add(server.mSize[file.Key]); } } else { update.Add(file.Key); mAllNeedDownLoadBytes += server.mSize[file.Key]; updateSize.Add(server.mSize[file.Key]); } } //测试 foreach (string str in update) { Debug.LogError("need update" + str); } return(update.Count > 0 ? true : false); }
///---------------------------------------------------------------------------------------------- /// <summary> /// 热更新,更新资源 /// </summary> ///---------------------------------------------------------------------------------------------- public void updateResource() { mLocalPathMD5 = new VersionResFile(); mServerPathMD5 = new VersionResFile(); mUpdateAssets = new List <string>(); mUpdateAssetSize = new List <int>(); mAllNeedDownLoadSize = 0; mCurNeedDownLoadSize = 0; mAllNeedDownLoadBytes = 0; mCurNeedDownLoadBytes = 0; mComplete = false; mCurDownLoadAsset = ""; mCurDownLoadAssetFailedTimes = 0; mMaxRepeatDownLoadTimes = 5; mForceTermination = false; mNetworkConnections = true; mNeedCheckNetWorkConnections = false; mCheckNetWorkConnectionsMaxTimes = 10; //1.加载本地资源version.xml信息 StreamReader localReader = new StreamReader(Common.Open("Version"), System.Text.Encoding.Default); parseResourceVersionFile(localReader.ReadToEnd(), ref mLocalPathMD5); localReader.Close(); //2.加载服务器version.xml信息 StartCoroutine(DownLoadAsset(Common.getServerPath("Version.bytes"), delegate(WWW resourceVersion) { parseResourceVersionFile(resourceVersion.text, ref mServerPathMD5); if (needUpdate(mLocalPathMD5, mServerPathMD5, mUpdateAssets, mUpdateAssetSize)) { mAllNeedDownLoadSize = mUpdateAssets.Count; //4.下载最新资源 DownLoadNeedUpdateAsset(); } else { mComplete = true; OnUpdateComplete(); Debug.LogError("Complete update"); } })); }
///---------------------------------------------------------------------------------------------- /// <summary> /// 解析版本资源信息 /// </summary> ///---------------------------------------------------------------------------------------------- private void parseResourceVersionFile(string text, ref VersionResFile file) { XmlDocument doc = new XmlDocument(); doc.LoadXml(text); XmlElement root = doc.DocumentElement; file.mNumber = root.GetAttribute("Number"); file.mBig = Convert.ToBoolean(root.GetAttribute("Big")); IEnumerator iter = root.GetEnumerator(); while (iter.MoveNext()) { XmlElement child = iter.Current as XmlElement; file.mPathMD5.Add(child.GetAttribute("fpath"), child.GetAttribute("md5")); file.mSize.Add(child.GetAttribute("fpath"), Convert.ToInt32(child.GetAttribute("size"))); } Debug.LogError("paser version fil okk" + text); }