public void Open(string szFile) { using (BinaryReader br = new BinaryReader(File.OpenRead(szFile), Encoding.Default)) { byte nCipherIndex = 0; while (br.PeekChar() != -1) { CSDATA_INDEX index = new CSDATA_INDEX(); byte nStrLen = br.ReadByte(); Cipher(ref nStrLen, ref nCipherIndex); byte[] szHash = br.ReadBytes(nStrLen); Cipher(ref szHash, ref nCipherIndex); byte[] nVal = br.ReadBytes(8); Cipher(ref nVal, ref nCipherIndex); index.szHash = Encoding.Default.GetString(szHash); index.nOffset = BitConverter.ToUInt32(nVal, 0); index.nSize = BitConverter.ToUInt32(nVal, 4); index.nFile = GetFileID(index.szHash); index.szName = Decode(index.szHash); hash.Add(index.szName); m_lClient.Add(index); } } }
public void Patch(string szFile, string szPath) { FileInfo pInfo = new FileInfo(szFile); string szHash = Encode(Path.GetFileName(szFile)); var index = m_lClient.Find(r => r.szHash == szHash); bool bIsNew = false; if (index == null) { bIsNew = true; index = new CSDATA_INDEX(); index.szHash = szHash; index.nFile = GetFileID(index.szHash); } else { m_lClient.Remove(index); } string szDataFile = Path.Combine(szPath, "data.00" + index.nFile); FileStream tFileStream = (!bIsNew && pInfo.Length <= index.nSize) ? new FileStream(szDataFile, FileMode.Open) : new FileStream(szDataFile, FileMode.Append); using (BinaryWriter bWriter = new BinaryWriter(tFileStream)) { if (!bIsNew && pInfo.Length <= index.nSize) { // Replace bWriter.BaseStream.Position = index.nOffset; index.nSize = (uint)pInfo.Length; } else { // Append bWriter.BaseStream.Position = new FileInfo(szDataFile).Length; index.nSize = (uint)pInfo.Length; index.nOffset = (uint)bWriter.BaseStream.Position; } using (BinaryReader bReader = new BinaryReader(File.OpenRead(szFile))) { byte[] pBuffer = bReader.ReadBytes((int)bReader.BaseStream.Length); if (Encrypted(pInfo.Extension)) { byte nCipherIndex = 0; Cipher(ref pBuffer, ref nCipherIndex); } bWriter.Write(pBuffer); } m_lClient.Add(index); } }
public int Patch(string szPath, CSDATA_INDEX patchIndex) { FileInfo fiCheck = new FileInfo(patchIndex.szName); string szValue = string.Format("{0}(ascii){1}", Path.GetFileNameWithoutExtension(fiCheck.Name), fiCheck.Extension).ToLower(); if (hash.Contains(szValue) && fiCheck.Extension.Contains("rdb")) { return(-1); } string szDataFile = Path.Combine(szPath, "data.00" + patchIndex.nFile); string szDataFileNew = tempDataName[patchIndex.nFile] != null ? tempDataName[patchIndex.nFile] : (tempDataName[patchIndex.nFile] = Path.GetTempFileName()); //Path.Combine(string.Format("{0}\\new\\", szPath), "data.00" + patchIndex.nFile); if (!File.Exists(szDataFileNew)) { File.Create(szDataFileNew).Close(); } FileStream tFileStream = new FileStream(szDataFileNew, FileMode.Append); using (BinaryWriter bWriter = new BinaryWriter(tFileStream)) { var oldOffset = patchIndex.nOffset; // Append bWriter.BaseStream.Position = new FileInfo(szDataFileNew).Length; patchIndex.nOffset = (uint)bWriter.BaseStream.Position; using (BinaryReader bReader = new BinaryReader(File.OpenRead(szDataFile))) { bReader.BaseStream.Position = oldOffset; bWriter.Write(bReader.ReadBytes((int)patchIndex.nSize)); } } m_lClientNew.Add(patchIndex); return(0); }