private void LoadFileCallback(WWW download, string fileDir, string serverMD5) { //对比MD5 string md5 = UtilTools.GetFileMD5(download.bytes); if (serverMD5 != md5) { LoadError(VersionData.ErrorCode.DownLoadFileMD5Error); return; } //创建临时文件 string filePath = Application.persistentDataPath + "/" + ClientDefine.LOCAL_PROGRAM_VERSION + "/" + fileDir; string tempFilePath = Application.persistentDataPath + "/" + ClientDefine.LOCAL_PROGRAM_VERSION + "/" + fileDir + "temp"; string dirPath = Path.GetDirectoryName(filePath); FileStream stream; try { System.IO.Directory.CreateDirectory(dirPath); System.IO.File.SetAttributes(dirPath, FileAttributes.Normal); stream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write); //stream = File.Create(tempFilePath); } catch (System.Exception ex) { LoadError(VersionData.ErrorCode.CreateFileFailed); Debug.LogException(ex); return; } //写临时文件 try { stream.Write(download.bytes, 0, download.bytes.Length); stream.Flush(); stream.Close(); } catch (System.Exception e) { LoadError(VersionData.ErrorCode.WriteFileFailed); Debug.LogException(e); return; } //名字改为正式 try { if (System.IO.File.Exists(filePath)) { System.IO.File.SetAttributes(filePath, FileAttributes.Normal); System.IO.File.Delete(filePath); } System.IO.File.Move(tempFilePath, filePath); } catch (System.Exception e) { LoadError(VersionData.ErrorCode.RenameFileFailed); Debug.LogException(e); return; } if (!_loadedFilesList.Contains(fileDir)) { _loadedFilesList.Add(fileDir); } if (_loadedFilesList.Count == _waittingFilesList.Count)//完成 { if (EventFinished != null) { EventFinished(_sVersion, _loadedFilesList.Count); } } }
private void BakeToDir(string sCurPath, string sSaveToPath) { string[] files = Directory.GetFiles(sCurPath, "*.*", SearchOption.AllDirectories); //遍历资源文件 foreach (string file in files) { if (file.Contains(".meta")) { continue; } if (file.Contains(".svn")) { continue; } if (file.Contains(".manifest")) { continue; } //if (file.Contains("Assets/Resources/Levels")) continue; //if (file.Contains("Assets/Resources/Models")) continue; string normalFile = file.Replace("\\", "/"); normalFile = normalFile.Substring(sCurPath.Length); if (File.Exists(sSaveToPath + normalFile)) { try { FileStream _stream_new = File.Open(sCurPath + normalFile, FileMode.Open, FileAccess.Read); byte[] byte_new = new byte[_stream_new.Length]; _stream_new.Read(byte_new, 0, byte_new.Length); _stream_new.Close(); string md5_new = UtilTools.GetFileMD5(byte_new); FileStream _stream_old = File.Open(sSaveToPath + normalFile, FileMode.Open, FileAccess.Read); byte[] byte_old = new byte[_stream_old.Length]; _stream_old.Read(byte_old, 0, byte_old.Length); _stream_old.Close(); string md5_old = UtilTools.GetFileMD5(byte_old); if (md5_new != md5_old) { SaveFileToDir(sSaveToPath, normalFile, byte_new, null); } } catch (System.Exception ex) { _error = "Bake2FilesFailed:" + normalFile; _bSaving = false; Debug.LogException(ex); return; } } else { try { FileStream _stream_new = File.Open(sCurPath + normalFile, FileMode.Open, FileAccess.Read); byte[] byte_new = new byte[_stream_new.Length]; _stream_new.Read(byte_new, 0, byte_new.Length); _stream_new.Close(); //string md5_new = UtilTools.GetFileMD5(byte_new); SaveFileToDir(sSaveToPath, normalFile, byte_new, null); } catch (System.Exception ex) { _error = "BakeFileFailed:" + normalFile; _bSaving = false; Debug.LogException(ex); return; } } } }