void StartDownload(UpdateVersion zipInfo) { UpdateClient = HttpManager.Instance.Alloc(); StartCoroutine(UpdateProgress()); UpdateClient.AddRequest(string.Format(strFile, strHost, Main.port, strProjectUrl, zipInfo.File.strFile), zipInfo.File.strLocalPath, (ref HttpRequest req) => { zipInfo.File.Loadbytes = req.loadBytes; zipInfo.File.Totalbytes = req.totalBytes; if (req.loadBytes == req.totalBytes && CheckUpdateCompleted(zipInfo.File)) { if (req.fs != null) { req.fs.Close(); req.fs = null; } req.bDone = true; req.Status = TaskStatus.Done; Complete = zipInfo; HttpManager.Instance.Quit(); } else if (req.error != null) { //中断连接、断网,服务器关闭,或者http 404 /或者 http1.1发 thunk 或者其他,直接进入游戏。 HttpManager.Instance.Quit(); LocalMsg msg = new LocalMsg(); msg.Message = (int)LocalMsgType.GameStart; ProtoHandler.PostMessage(msg); } } , zipInfo.File.Loadbytes, zipInfo.File); UpdateClient.StartDownload(); }
IEnumerator DownloadNecessaryData() { //check data is ok //if (strServerVer == null || GameData.Version().CompareTo(strServerVer) != -1) // yield break; //check last download is interrupt if (MainLoader.config != null && MainLoader.config.mlst.Count != 0) { string strTargetV = MainLoader.config.strTargetVer; //target ver == server ver restore from last state if (strTargetV == strServerVer) { foreach (var item in MainLoader.config.mlst) { if (!string.IsNullOrEmpty(item.strLoadbytes) && item.strTotalbytes == item.strLoadbytes && item.strTotalbytes != "0") { if (item.bHashChecked) { continue; } } if (!string.IsNullOrEmpty(item.strFile) && item.bForceUpdate) { UpdateClient.AddRequest(item.strFile, strUpdatePath + "/" + item.strFile, new HttpClient.cb(UpdateTableProgress), System.Convert.ToInt64(item.strLoadbytes), item); } } AllCount = HttpClient.RequestMap.Count; if (AllCount != 0) { MainLoader.ChangeStep(LoadStep.NeedUpdate); errorCount = 0; DownloadCount = 0; HttpClient.StartDownload(); } else { MainLoader.ChangeStep(LoadStep.NotNeedUpdate); } yield break; } } //download 0.0.0.0-0.0.0.1.xml string strUrl = string.Format(strVFile, strHost, strPort, strProjectUrl, strPlatform, strUpdateFile); string strLocalFile = strUpdatePath + "/" + strUpdateFile; using (WWW updateXml = new WWW(strUrl)) { yield return(updateXml); File.WriteAllBytes(strLocalFile, updateXml.bytes); DeCompressFile(strLocalFile, strUpdatePath + "/" + "update.xml"); XmlDocument xmlVer = new XmlDocument(); xmlVer.Load(strUpdatePath + "/" + "update.xml"); XmlElement updateFilelst = xmlVer.DocumentElement; if (updateFilelst != null) { string strFileNum = updateFilelst.GetAttribute("Filenum"); List <UpdateFile> data = new List <UpdateFile>(); foreach (XmlElement each in updateFilelst) { string strFileName = each.GetAttribute("name"); string strMD5 = each.GetAttribute("MD5"); bool bFind = false; UpdateFile res = null; foreach (var item in MainLoader.config.mlst) { if (item.strFile == strFileName) { bFind = true; break; } } if (bFind && res != null) { res.strMd5 = strMD5; res.bHashChecked = false; res.strLoadbytes = "0"; res.strTotalbytes = "0"; //下载的必要数据包括在Resources目录内的文件,以及以.zip结束的文件(表格或者数据或脚本) string[] strResourceDirectory = strFileName.Split('/'); bool bForce = false; foreach (var eachDir in strResourceDirectory) { if (eachDir == "Resources") { bForce = true; break; } } if (strFileName.EndsWith(".zip")) { bForce = true; } res.bForceUpdate = bForce; data.Add(res); } else { string[] strResourceDirectory = strFileName.Split('/'); bool bForce = false; foreach (var eachDir in strResourceDirectory) { if (eachDir == "Resources") { bForce = true; break; } } if (strFileName.EndsWith(".zip")) { bForce = true; } UpdateFile file = new UpdateFile(strFileName, strMD5, "0", "0", bForce); data.Add(file); } } MainLoader.config.mlst = data; MainLoader.config.strTargetVer = strServerVer; foreach (var item in MainLoader.config.mlst) { if (!UpdateConfig.keySearch.ContainsKey(item.strFile)) { UpdateConfig.keySearch.Add(item.strFile, item); } if (item.strLoadbytes != "0" && item.strLoadbytes == item.strTotalbytes) { continue; } if (item.bForceUpdate) { UpdateClient.AddRequest(item.strFile, strUpdatePath + "/" + item.strFile, new HttpClient.cb(UpdateTableProgress), Convert.ToInt64(item.strLoadbytes), item); } } AllCount = HttpClient.RequestMap.Count; if (AllCount != 0) { int nUnNecessCount = Convert.ToInt32(strFileNum) - AllCount; MainLoader.ChangeStep(LoadStep.NeedUpdate); errorCount = 0; DownloadCount = 0; HttpClient.StartDownload(); } else { MainLoader.ChangeStep(LoadStep.NotNeedUpdate); } } } }