private void DownloadGameFile(object _value) { object[] _object = _value as object[]; String[] pathArray = _object[0] as String[]; Step.State nextState = (Step.State)_object[1]; FtpProtocol protocol = _object[2] as FtpProtocol; string downloadLocalDirectory = _object[3] as String; //本地的保存地址 string sourceDirectory = _object[4] as String; //服务端的下载地址 List <string> failFile = new List <string>(); //更新过程中出现更新失败的文件 FTPUpdateValue.CurrentFileDownloadBytes = 0; FTPUpdateValue.CurrentFileByte = 0; //FTPUpdateValue.maxByte = FTPUpdateValue.maxByte - FTPUpdateValue.CurrentFileByte; //FTPUpdateValue.CurrentAccumlatereadByte =Math.Max(0, FTPUpdateValue.CurrentAccumlatereadByte- FTPUpdateValue.CurrentFileDownloadBytes); FTPUpdateValue.maxfileCount += pathArray.Length; //TODO 此处开始获取新资源大小 for (int i = 0; i < pathArray.Length; i++) { string strTarget = protocol.mainform.m_clientServerDirectory + pathArray[i]; string text = string.Format("当前更新{0}/{1}个文件", i, FTPUpdateValue.maxfileCount); long sizeI = protocol.GetFileSize(strTarget); if (sizeI >= 0) { FTPUpdateValue.maxByte += sizeI; } else { //todo 获取文件大小的文件暂时不处理 continue; } } Step.CURRENT_STEP = Step.State.__StartNeedDownLoadListing; //todo 此处开始下载资源。更新下载进度 for (int i = 0; i < pathArray.Length; i++) { var filepath = pathArray[i]; string strLocal = downloadLocalDirectory + filepath; strLocal = strLocal.Replace('\\', '/'); string strSource = sourceDirectory + filepath; FTPUpdateValue.downloadIndex++; if (!protocol.Download(strSource, strLocal, true)) { //todo下载失败 failFile.Add(filepath); continue; } else { //每次下载完校验md5后移动到本地的最终位置 bool isLocalEx = File.Exists(strLocal); bool isServer = UpdateHelper.Instance()._ServerUpdateStructs.ContainsKey(filepath); if (isLocalEx && isServer) { var computeMd5Hash = FileMatchUp.ComputeMD5Hash(strLocal); var serverUpdateStruct = UpdateHelper.Instance()._ServerUpdateStructs[filepath].Md5; if (computeMd5Hash == serverUpdateStruct) { if (UpdateHelper.Instance()._ClientUpdateStructs.ContainsKey(filepath)) { UpdateHelper.Instance()._ClientUpdateStructs[filepath].Md5 = computeMd5Hash; } else { var myUpdateStruct = new MyUpdateStruct(); myUpdateStruct.PathKey = filepath; myUpdateStruct.Md5 = computeMd5Hash; UpdateHelper.Instance()._ClientUpdateStructs.Add(myUpdateStruct.PathKey, myUpdateStruct); } string tarPath = protocol.mainform.m_curDirectory + filepath; if (File.Exists(tarPath)) { File.Delete(tarPath); } if (filepath.Contains('/')) { var strings = filepath.Split('/'); string curpath = ""; for (var i1 = 0; i1 < strings.Length; i1++) { if (i1 != strings.Length - 1) { curpath = curpath + "/" + strings[i1]; if (!Directory.Exists(protocol.mainform.m_curDirectory + curpath)) { Directory.CreateDirectory(protocol.mainform.m_curDirectory + curpath); } } } } // 将下载好的文件挪出去 File.Move(strLocal, tarPath); } } } } if (failFile.Count > 0) { string text = "更新失败的文件有:"; foreach (string var in failFile) { text = text + var + " , "; } } Step.CURRENT_STEP = nextState; }
private void ThreadGetClientMD5(object _value) { //先从服务器拉下整个客户端的md5list //临时文件路径,获取md5列表 try { var tempPath = m_MainForm.m_downloadPath + "/" + m_MainForm.m_clientmd5Txt; _ServerUpdateStructs = new Dictionary <string, MyUpdateStruct>(); StreamReader sR = File.OpenText(tempPath); string nextLine; while ((nextLine = sR.ReadLine()) != null) { var strings = nextLine.Split('='); var key = strings[0]; var value = strings[1]; var myUpdateStruct = new MyUpdateStruct(); myUpdateStruct.PathKey = key; myUpdateStruct.Md5 = value; //启动器之前校验过,此处不作校验。 _ServerUpdateStructs.Add(key, myUpdateStruct); } sR.Close(); //本地MD5 if (FileMatchUp.MakeEncodingList(_ServerUpdateStructs, m_MainForm.m_curDirectory, m_MainForm.m_clientmd5Txt) == true) { _ClientUpdateStructs.Clear(); var cilentFloder = m_MainForm.m_curDirectory + "/" + m_MainForm.m_clientmd5Txt; StreamReader sRclient = File.OpenText(cilentFloder); string nextLineclient; while ((nextLineclient = sRclient.ReadLine()) != null) { var strings = nextLineclient.Split('='); var key = strings[0]; var value = strings[1]; var myUpdateStruct = new MyUpdateStruct(); myUpdateStruct.PathKey = key; myUpdateStruct.Md5 = value; _ClientUpdateStructs.Add(key, myUpdateStruct); } sRclient.Close(); //md5列表创建完成 比对需要下载的MD文件 _DownLoadList.Clear(); foreach (var serverUpdateStruct in _ServerUpdateStructs) { var key = serverUpdateStruct.Key; var serverMd5 = serverUpdateStruct.Value.Md5; if (_ClientUpdateStructs.ContainsKey(key)) { var clientmd5 = _ClientUpdateStructs[key].Md5; if (serverMd5 != clientmd5) { var myUpdateStruct = new MyUpdateStruct(); myUpdateStruct.PathKey = key; myUpdateStruct.Md5 = serverMd5; _DownLoadList.Add(myUpdateStruct.PathKey, myUpdateStruct); } } else { var myUpdateStruct = new MyUpdateStruct(); myUpdateStruct.PathKey = key; myUpdateStruct.Md5 = serverMd5; _DownLoadList.Add(myUpdateStruct.PathKey, myUpdateStruct); } } //下载列表中不一致的文件 Step.CURRENT_STEP = Step.State.__StartNeedDownLoadFile; } } catch (Exception ex) { MessageBox.Show("文件对比失败:" + ex.Message); } }