/// <summary> /// Gets the byte[] data of the file in the grf. (Uncompressed) /// </summary> /// <param name="file"></param> /// <returns></returns> public byte[] GetCompressedDataFromFile(GRFFile file) { byte[] compressedBody = new byte[file.CompressedLengthAligned]; _grfStream.Seek(46 + file.Offset, SeekOrigin.Begin); _grfStream.Read(compressedBody, 0, file.CompressedLength); return(compressedBody); }
/// <summary> /// Extracts a file from the grf to the specified path. /// </summary> /// <param name="file">The file inside the grf</param> /// <param name="path">The path where to extract the file</param> public void ExtractFileToPath(GRFFile file, string path) { if (file.Flags == 1) { file.WriteToDisk(path); OnExtractComplete(new GRFFileExtractEventArg(file)); } }
/// <summary> /// Gets the data of the file in the grf. /// </summary> /// <returns> /// byte[] the data in bytes /// </returns> /// <param name='file'> /// (GRFFile) The file to get /// </param> public byte[] GetDataFromFile(GRFFile file) { byte[] compressedBody = new byte[file.CompressedLengthAligned]; _grfStream.Seek(46 + file.Offset, SeekOrigin.Begin); _grfStream.Read(compressedBody, 0, file.CompressedLengthAligned); DES.GrfDecode(compressedBody, file.Flags, file.CompressedLength); return(ZlibStream.UncompressBuffer(compressedBody)); }
/// <summary> /// Add a file inside the grf. /// </summary> /// <param name="filename">The name of the file to be added.</param> /// <param name="data">The data of the file to be added.</param> public void AddFile(string inputFilePath, string outputFilePath) { byte[] data = File.ReadAllBytes(inputFilePath); GRFFile f; f = (GRFFile)_GRFFiles[outputFilePath]; if (f != null) { f.UncompressedBody = data; return; } f = new GRFFile(outputFilePath, 0, 0, 0, 1, 0, 0, this); f.UncompressedBody = data; _GRFFiles.Add(f.Name, f); _fileCount++; OnFileAddComplete(new GRFEventArg(f)); }
/// <summary> /// Add a file inside the grf. /// </summary> /// <param name="filename">The name of the file to be added.</param> /// <param name="data">The data of the file to be added.</param> public void AddFile(string inputFilePath, string outputFilePath) { int i = 0; byte[] data = File.ReadAllBytes(inputFilePath); foreach (GRFFile file in _GRFFiles) { if (file.Name.ToLower() == outputFilePath.ToLower()) { _GRFFiles[i].UncompressedBody = data; return; } i++; } GRFFile f = new GRFFile(outputFilePath, 0, 0, 0, 1, 0, 0, this); f.UncompressedBody = data; _GRFFiles.Add(f); _fileCount++; OnFileAddComplete(new GRFEventArg(f)); }
public GRFEventArg(GRFFile file) { _file = file; }
/// <summary> /// Open the GRF File to start reading. /// </summary> public void Open() { string signature; int tableOffset, version, m1, m2; _GRFFiles.Clear(); _grfStream = new FileStream(_filePathToGRF, FileMode.Open); BinaryReader br = new BinaryReader(_grfStream); //Read GRF File Header -> Signature byte[] signatureByte = new byte[15]; _grfStream.Read(signatureByte, 0, 15); signature = System.Text.Encoding.ASCII.GetString(signatureByte); br.ReadByte(); // Read GRF File Header -> Encryption Key byte[] encryptionKey = new byte[14]; _grfStream.Read(encryptionKey, 0, 14); tableOffset = br.ReadInt32(); m1 = br.ReadInt32(); m2 = br.ReadInt32(); version = br.ReadInt32(); this._signature = signature; this._encryptionKey = encryptionKey; this._fileTableOffset = tableOffset; this._m1 = m1; this._m2 = m2; this._version = version; _grfStream.Seek(_fileTableOffset, SeekOrigin.Current); _compressedLength = br.ReadInt32(); _uncompressedLength = br.ReadInt32(); byte[] compressedBodyBytes = new byte[_compressedLength]; _grfStream.Read(compressedBodyBytes, 0, _compressedLength); _bodyBytes = ZlibStream.UncompressBuffer(compressedBodyBytes); _fileCount = m2 - m1 - 7; OnFileCountReadComplete(); MemoryStream bodyStream = new MemoryStream(_bodyBytes); BinaryReader bodyReader = new BinaryReader(bodyStream); for (int x = 0; x < _fileCount; x++) { string fileName = string.Empty; char currentChar; while ((currentChar = (char)bodyReader.ReadByte()) != 0) { fileName += currentChar; } int fileCompressedLength = 0, fileCompressedLengthAligned = 0, fileUncompressedLength = 0, fileOffset = 0, fileCycle = 0; byte fileFlags = 0; fileCompressedLength = bodyReader.ReadInt32(); fileCompressedLengthAligned = bodyReader.ReadInt32(); fileUncompressedLength = bodyReader.ReadInt32(); fileFlags = bodyReader.ReadByte(); fileOffset = bodyReader.ReadInt32(); if (fileFlags == 1 || fileFlags == 5 || fileFlags == 3) { int lop, srccount, srclen = fileCompressedLength; if (fileFlags == 3) { for (lop = 10, srccount = 1; srclen >= lop; lop *= 10, srccount++) { fileCycle = srccount; } } else if (fileFlags == 5) { fileCycle = 0; } else { fileCycle = -1; } } if (fileFlags == 2) // Do not add folders { OnFileReadComplete(new GRFEventArg(new GRFFile(System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(fileName)), 0, 0, 0, 0, 0, 0, this))); continue; } GRFFile newGRFFile = new GRFFile( System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(fileName)), fileCompressedLength, fileCompressedLengthAligned, fileUncompressedLength, fileFlags, fileOffset, fileCycle, this); _GRFFiles.Add(newGRFFile); OnFileReadComplete(new GRFEventArg(newGRFFile)); } _isOpen = true; OnGRFOpenComplete(); }
/// <summary> /// Open the GRF File to start reading. /// </summary> public void Open() { string signature; int tableOffset, version, m1, m2; _GRFFiles.Clear(); _grfStream = new FileStream(_filePathToGRF, FileMode.Open); BinaryReader br = new BinaryReader(_grfStream); //Read GRF File Header -> Signature byte[] signatureByte = new byte[15]; _grfStream.Read(signatureByte, 0, 15); signature = System.Text.Encoding.ASCII.GetString(signatureByte); br.ReadByte(); // Read GRF File Header -> Encryption Key byte[] encryptionKey = new byte[14]; _grfStream.Read(encryptionKey, 0, 14); tableOffset = br.ReadInt32(); m1 = br.ReadInt32(); m2 = br.ReadInt32(); version = br.ReadInt32(); this._signature = signature; this._encryptionKey = encryptionKey; this._fileTableOffset = tableOffset; this._m1 = m1; this._m2 = m2; this._version = version; _grfStream.Seek(_fileTableOffset, SeekOrigin.Current); _compressedLength = br.ReadInt32(); _uncompressedLength = br.ReadInt32(); byte[] compressedBodyBytes = new byte[_compressedLength]; _grfStream.Read(compressedBodyBytes, 0, _compressedLength); _bodyBytes = ZlibStream.UncompressBuffer(compressedBodyBytes); _fileCount = m2 - m1 - 7; OnFileCountReadComplete(); MemoryStream bodyStream = new MemoryStream(_bodyBytes); BinaryReader bodyReader = new BinaryReader(bodyStream); for (int x = 0; x < _fileCount; x++) { string fileName = string.Empty; char currentChar; while ((currentChar = (char)bodyReader.ReadByte()) != 0) fileName += currentChar; int fileCompressedLength = 0, fileCompressedLengthAligned = 0, fileUncompressedLength = 0, fileOffset = 0, fileCycle = 0; byte fileFlags = 0; fileCompressedLength = bodyReader.ReadInt32(); fileCompressedLengthAligned = bodyReader.ReadInt32(); fileUncompressedLength = bodyReader.ReadInt32(); fileFlags = bodyReader.ReadByte(); fileOffset = bodyReader.ReadInt32(); if (fileFlags == 1 || fileFlags == 5 || fileFlags == 3) { int lop, srccount, srclen = fileCompressedLength; if (fileFlags == 3) { for (lop = 10, srccount = 1; srclen >= lop; lop *= 10, srccount++) fileCycle = srccount; } else if (fileFlags == 5) fileCycle = 0; else fileCycle = -1; } if (fileFlags == 2) // Do not add folders { OnFileReadComplete(new GRFEventArg(new GRFFile(System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(fileName)),0,0,0,0,0,0,this))); continue; } GRFFile newGRFFile = new GRFFile( System.Text.Encoding.Default.GetString(System.Text.Encoding.Default.GetBytes(fileName)), fileCompressedLength, fileCompressedLengthAligned, fileUncompressedLength, fileFlags, fileOffset, fileCycle, this); _GRFFiles.Add(newGRFFile); OnFileReadComplete(new GRFEventArg(newGRFFile)); } _isOpen = true; OnGRFOpenComplete(); }
public GRFFileExtractEventArg(GRFFile file) : base(file) { }
/// <summary> /// Gets the data of the file in the grf. /// </summary> /// <returns> /// byte[] the data in bytes /// </returns> /// <param name='file'> /// (GRFFile) The file to get /// </param> public byte[] GetDataFromFile(GRFFile file) { byte[] compressedBody = new byte[file.CompressedLengthAligned]; _grfStream.Seek(46 + file.Offset, SeekOrigin.Begin); _grfStream.Read(compressedBody, 0, file.CompressedLengthAligned); DES.GrfDecode(compressedBody, file.Flags, file.CompressedLength); return ZlibStream.UncompressBuffer(compressedBody); }
/// <summary> /// Gets the byte[] data of the file in the grf. (Uncompressed) /// </summary> /// <param name="file"></param> /// <returns></returns> public byte[] GetCompressedDataFromFile(GRFFile file) { byte[] compressedBody = new byte[file.CompressedLength]; _grfStream.Seek(46 + file.Offset, SeekOrigin.Begin); _grfStream.Read(compressedBody, 0, file.CompressedLengthAligned); return compressedBody; }
/// <summary> /// Extracts a file from the grf to the specified path. /// </summary> /// <param name="file">The file inside the grf</param> /// <param name="path">The path where to extract the file</param> public void ExtractFileToPath(GRFFile file,string path) { if (file.Flags == 1) { file.WriteToDisk(path); OnExtractComplete(new GRFFileExtractEventArg(file)); } }
/// <summary> /// Add a file inside the grf. /// </summary> /// <param name="filename">The name of the file to be added.</param> /// <param name="data">The data of the file to be added.</param> public void AddFile(string inputFilePath,string outputFilePath) { int i = 0; byte[] data = File.ReadAllBytes(inputFilePath); foreach (GRFFile file in _GRFFiles) { if (file.Name.ToLower() == outputFilePath.ToLower()) { _GRFFiles[i].UncompressedBody = data; return; } i++; } GRFFile f = new GRFFile(outputFilePath, 0, 0, 0, 1, 0, 0, this); f.UncompressedBody = data; _GRFFiles.Add(f); _fileCount++; OnFileAddComplete(new GRFEventArg(f)); }