/// <summary> /// 无论啥错误都会返回一个默认的 /// </summary> /// <param name="cfgBytes"></param> /// <returns></returns> public static FileMapGroupInfo ReadFromByteBuf(byte[] cfgBytes) { var group = new FileMapGroupInfo(); var byteBuf = ByteBuf.CreateFromBytes(cfgBytes); group.MD51 = byteBuf.ReadLong(); group.MD52 = byteBuf.ReadLong(); group.Ver = new Version() { Version_Major = byteBuf.ReadInt(), Version_Minor = byteBuf.ReadInt(), Version_Build = byteBuf.ReadInt(), }; var readedMd5 = MD5Creater.MD5Struct.CreateFromLong(group.MD51, group.MD52); var fileMD5 = MD5Creater.GenerateMd5Code(cfgBytes, 16); if (!readedMd5.Equals(fileMD5)) { CommonLog.Error("MD5校验不通过!"); return(null); } int FileNum = byteBuf.ReadInt(); group.AllFileMapInfo = new FileMapInfo[FileNum]; for (int i = 0; i < group.AllFileMapInfo.Length; i++) { group.AllFileMapInfo[i] = FileMapInfo.ReadFromByteBuf(byteBuf); } return(group); }
private (WebRequestError, string) CheckWebRequestError(SingleFileDownloadRequest sr, UnityWebRequest request) { var error = request.error; if (!string.IsNullOrEmpty(error)) { return(WebRequestError.Raw, error); } if (!File.Exists(sr.targetPath)) { error = $"download finish but {sr.targetPath} not exist"; return(WebRequestError.NoExist, error); } if (sr.md5 != null) { var bs = File.ReadAllBytes(sr.targetPath); var md5Struct = MD5Creater.GenerateMd5Code(bs); var bm = sr.md5.Value; if (md5Struct.MD51 != bm.MD51 || md5Struct.MD52 != bm.MD52) { error = $"download file {sr.targetPath} md5 error {md5Struct.GetMD5Str(false)}->{bm.GetMD5Str(false)}"; return(WebRequestError.Md5, error); } } return(WebRequestError.None, null); }
public MD5Creater.MD5Struct WriteToByteBuf(ByteBuf buf) { //标记MD5 int firstIndex = buf.WriterIndex; buf.WriteLong(0); buf.WriteLong(0); //写入Ver buf.WriteInt(Ver.Version_Major); buf.WriteInt(Ver.Version_Minor); buf.WriteInt(Ver.Version_Build); //data buf.WriteInt(AllFileMapInfo.Length); for (int i = 0; i < AllFileMapInfo.Length; i++) { var info = AllFileMapInfo[i]; info.WriteToByteBuf(buf); } int finalIndex = buf.WriterIndex; var bytelen = finalIndex - firstIndex; //计算MD5 var fileMD5 = MD5Creater.GenerateMd5Code(buf.GetRaw(), firstIndex + 16, bytelen - 16); buf.SetWriterIndex(firstIndex); buf.WriteLong(fileMD5.MD51); buf.WriteLong(fileMD5.MD52); buf.SetWriterIndex(finalIndex); return(fileMD5); }
private void CheckMd5AndReplaceTempFile(RequestState myRequestState) { var ms = new MemoryStream(); myRequestState.m_file_stream.Position = 0; myRequestState.m_file_stream.CopyTo(ms); var md5Struct = MD5Creater.GenerateMd5Code(ms.ToArray()); myRequestState.m_file_stream.Close(); ms.Dispose(); var pathRoot = UnityPersistFileHelper.GetPersistAssetFilePath(SavedDir, ""); if (md5Struct.MD51 == myRequestState.fileInfo.MapedFileName_MD51 && md5Struct.MD52 == myRequestState.fileInfo.MapedFileName_MD52) { CommonLog.Log($"complete download file {myRequestState.fileInfo.FileName}"); myRequestState.progress.RealCompletedSize -= myRequestState.downloadedSize; myRequestState.progress.RealCompletedSize += myRequestState.fileInfo.FileSize; //myRequestState.fileSize; myRequestState.progress.UpdateProgress(); FileUtils.RenameFile(pathRoot, myRequestState.fileInfo.FileName + TempExtension, myRequestState.fileInfo.FileName); } else { var tempFilePath = pathRoot + myRequestState.fileInfo.FileName + TempExtension; CommonLog.Error(MAuthor.WY, $"file {myRequestState.fileInfo.FileName} md5 error, delete file {tempFilePath}"); FileUtils.DeleteFile(tempFilePath); myRequestState.progress.SetException("md5 check failed", WWWFileDownloader.eWWWErrorType.WriteFileError); } }