/// <summary> /// Parses the TPI from specified <see cref="MemoryStream"/>. /// </summary> /// <param name="Stream">Instance of <see cref="MemoryStream"/> containing the TPI file data.</param> /// <returns>Parsed data of TPI as <see cref="TPIPackageClass"/>.</returns> public static TPIPackageClass FromStream(MemoryStream Stream) { using (Stream Bytes = new MemoryStream(Stream.ToArray())) { try { TPIPackageClass Pack = Expressions.CreateTPIPackage(); while (ConditionsOK(Bytes)) { uint Header = Expressions.GetUNumber(Expressions.GetBytes(Bytes, 0, 4)); if (Header == (uint)ChunkType.Head) { //Processing the HEAD. Reading first 4 bytes to find out length. int Next = Expressions.GetNumber(Expressions.GetBytes(Bytes, 0, 4)); //Buaa getting all bytes now. byte[] Data = Expressions.GetBytes(Bytes, 0, Next); Pack.PackageID = Expressions.GetCRCID(Data.SubArray(0, 4)); //Next 4 bytes are indicating total files used by this TPI. But it is read-only because it uses the ones //actually parsed by the parser. //Now, we should have DATA. Ending this if else. } else if (Header == (uint)ChunkType.Data) { //Processing the DATA. int Next = Expressions.GetNumber(Expressions.GetBytes(Bytes, 0, 4)); //Fun part ;) int NameLen = Expressions.GetUNumber16(Expressions.GetBytes(Bytes, 0, 2)); Pack.PackageName = Expressions.GetText(Bytes, 0, NameLen); int VerLen = Expressions.GetUNumber16(Expressions.GetBytes(Bytes, 0, 2)); Pack.PackageVer = Expressions.GetText(Bytes, 0, VerLen); int OwnerLen = Expressions.GetUNumber16(Expressions.GetBytes(Bytes, 0, 2)); Pack.PackageOwner = Expressions.GetText(Bytes, 0, OwnerLen); Pack.Type = (PackageType)Expressions.GetNumber(Expressions.GetBytes(Bytes, 0, 4)); //Now, we should have FILEs. Ending this if else and letting while continue it's work. } else if (Header == (uint)ChunkType.File) { TTFileClass File = Expressions.CreateTTFile(); int Next = Expressions.GetNumber(Expressions.GetBytes(Bytes, 0, 4)); File.CRC = Expressions.GetCRCID(Expressions.GetBytes(Bytes, 0, 4)); File.FileSize = Expressions.GetUNumber(Expressions.GetBytes(Bytes, 0, 4)); int namelen = Expressions.GetUNumber16(Expressions.GetBytes(Bytes, 0, 2)); File.FileName = Expressions.GetText(Bytes, 0, namelen); //No NULs after file name, so let while loop read next header if Stream not ended. //Let's add file to collection... Pack.Files.Add(File); } else { //Hmm... } } //Return when everything is good to go. return(Pack); } catch (Exception ex) { throw new TPIParserException("Failed to parse TPI.", ex); //Wrapping the exception to our TPIParserException class. } } }