예제 #1
0
        public void dailog_data_ignores_other_tags()
        {
            const string compassTag = "<compass><dir value=\"e\"/><dir value=\"nw\"/></compass>";

            var reader = new ChunkReader<Tag>("<dialogData", "</dialogData");

            var result = reader.Read(Chunk.For(compassTag));

            Assert.AreEqual(compassTag, result.Chunk.Text);
        }
        public void reads_the_tag()
        {
            const string tag = "<style id=\"roomName\" />[The Western Road, Stream Bank]\n<style id=\"\"/>";

            var reader = new ChunkReader<RoomNameTag>("<style id=\"roomName\"", "<style id=\"\"");
            var result = reader.Read(Chunk.For(tag));

            Assert.AreEqual(tag, result.Tags.Single().Text);
            Assert.AreEqual("[The Western Road, Stream Bank]\n", result.Tags.OfType<RoomNameTag>().Single().Name);
        }
예제 #3
0
        static void Main(string[] args)
        {
            var   reader = new ChunkReader();
            Chunk chunk  = null;

            foreach (Chunk c in reader.Read(@"D:\test.txt"))
            {
                Console.WriteLine(c.Header);
            }

            Console.ReadKey();
        }
예제 #4
0
        public bool MoveNext()
        {
            var row = _rowPool.Allocate();

            var state = State.start;

            while (state != State.end2)
            {
                if (_currentChunk == null)
                {
                    _currentChunk = _reader.Read();
                    if (_currentChunk == null)
                    {
                        break;
                    }
                }

                state = Parse(state, row);
            }

            return(CompleteRead(row));
        }
예제 #5
0
        protected void WriteElement(BinaryWriter writer, Dna.StructDecl fileStruct, Dna.ElementDecl memoryElement, long structOffset)
        {
            bool brokenDna = (Flags & FileFlags.BrokenDna) != 0;

            int elementOffset;

            Dna.ElementDecl fileElement = fileStruct.FindElement(_fileDna, brokenDna, memoryElement.NameInfo, out elementOffset);

            ChunkReader.BaseStream.Position = structOffset + elementOffset;

            if (fileElement == null)
            {
                int elementLength = _memoryDna.GetElementSize(memoryElement);
                writer.BaseStream.Position += elementLength;
            }
            else if (fileElement.NameInfo.Name[0] == '*')
            {
                SafeSwapPtr(writer, ChunkReader);
            }
            else if (fileElement.Type.Name.Equals(memoryElement.Type.Name))
            {
                int elementLength = _fileDna.GetElementSize(fileElement);
                if (elementLength != _memoryDna.GetElementSize(memoryElement))
                {
                    throw new InvalidDataException();
                }
                byte[] mem = new byte[elementLength];
                ChunkReader.Read(mem, 0, elementLength);
                writer.Write(mem);
            }
            else
            {
                throw new InvalidDataException();
                //GetElement(arrayLen, lookupType, type, data, strcData);
            }
        }
예제 #6
0
        protected override void ReadChunks()
        {
            bool brokenDna = (Flags & FileFlags.BrokenDna) != 0;

            _dataStart = SizeOfBlenderHeader;
            long   chunkPtr = _dataStart;
            Stream stream   = ChunkReader.BaseStream;

            stream.Position = chunkPtr;

            ChunkInd chunk = GetNextBlock(ChunkReader);

            while (chunk.Code != DnaID.Dna)
            {
                long chunkDataOffset = chunkPtr + ChunkUtils.GetChunkSize(Flags);

                if (!brokenDna || chunk.Code != DnaID.QuantizedBvh)
                {
                    // One behind
                    if (chunk.Code == DnaID.Sdna)
                    {
                        break;
                    }

                    byte[] chunkData;
                    if (chunk.StructIndex >= 0)
                    {
                        _chunks.Add(chunk);

                        chunkData = ReadChunkData(chunk, chunkDataOffset);
                        switch (chunk.Code)
                        {
                        case DnaID.CollisionObject:
                            CollisionObjects.Add(chunkData);
                            break;

                        case DnaID.Constraint:
                            Constraints.Add(chunkData);
                            break;

                        case DnaID.DynamicsWorld:
                            DynamicsWorldInfo.Add(chunkData);
                            break;

                        case DnaID.MultiBody:
                            MultiBodies.Add(chunkData);
                            break;

                        case DnaID.SoftBody:
                        case DnaID.TriangleInfoMap:
                            throw new NotImplementedException();

                        case DnaID.QuantizedBvh:
                            Bvhs.Add(chunkData);
                            break;

                        case DnaID.RigidBody:
                            RigidBodies.Add(chunkData);
                            break;

                        case DnaID.Shape:
                            CollisionShapes.Add(chunkData);
                            break;
                        }
                    }
                    else
                    {
#if DEBUG
                        Console.WriteLine("unknown chunk " + chunk.Code);
#endif
                        chunkData = new byte[chunk.Length];
                        ChunkReader.Read(chunkData, 0, chunk.Length);
                    }
                    LibPointers.Add(chunk.OldPtr, chunkData);
                }
                else
                {
#if DEBUG
                    Console.WriteLine("skipping B3_QUANTIZED_BVH_CODE due to broken DNA");
#endif
                }

                chunkPtr        = chunkDataOffset + chunk.Length;
                stream.Position = chunkPtr;

                chunk = GetNextBlock(ChunkReader);
                if (chunk.Length < 0)
                {
                    break;
                }
            }
        }
예제 #7
0
 public static void ReadChunks(ChunkReader reader)
 {
     reader.Read(nameof(AppReady), ref AppReady);
 }
예제 #8
0
        protected byte[] ReadChunkData(ChunkInd dataChunk, long chunkDataOffset)
        {
            //bool ignoreEndianFlag = false;

            if ((Flags & FileFlags.EndianSwap) != 0)
            {
                //swap(head, dataChunk, ignoreEndianFlag);
            }

            if (StructChanged(dataChunk.StructIndex))
            {
                // Ouch! need to rebuild the struct
                Dna.StructDecl oldStruct = _fileDna.GetStruct(dataChunk.StructIndex);

                if ((Flags & FileFlags.BrokenDna) != 0)
                {
                    if (oldStruct.Type.Name.Equals("btQuantizedBvhNodeData") && oldStruct.Type.Length == 28)
                    {
                        throw new NotImplementedException();
                    }

                    if (oldStruct.Type.Name.Equals("btShortIntIndexData"))
                    {
                        throw new NotImplementedException();
                    }
                }

                // Don't try to convert Link block data, just memcpy it. Other data can be converted.
                if (oldStruct.Type.Name.Equals("Link"))
                {
                    //Console.WriteLine("Link found");
                }
                else
                {
                    Dna.StructDecl curStruct = _memoryDna.GetStruct(oldStruct.Type.Name);
                    if (curStruct != null)
                    {
                        byte[] structAlloc = new byte[dataChunk.NumBlocks * curStruct.Type.Length];
                        AddDataBlock(structAlloc);
                        using (var stream = new MemoryStream(structAlloc))
                        {
                            using (var writer = new BinaryWriter(stream))
                            {
                                long structOffset = chunkDataOffset;
                                for (int block = 0; block < dataChunk.NumBlocks; block++)
                                {
                                    ParseStruct(writer, curStruct, oldStruct, structOffset);
                                    structOffset += oldStruct.Type.Length;
                                }
                            }
                        }
                        return(structAlloc);
                    }
                }
            }
            else
            {
#if DEBUG_EQUAL_STRUCTS
#endif
            }

            byte[] dataAlloc = new byte[dataChunk.Length];
            ChunkReader.Read(dataAlloc, 0, dataChunk.Length);
            return(dataAlloc);
        }
예제 #9
0
	// Load a PNG image from the specified stream.  The first 4 bytes
	// have already been read and discarded.
	public static void Load(Stream stream, Image image)
			{
				byte[] buffer = new byte [1024];
				int width = 0;
				int height = 0;
				int bitDepth = 0;
				int colorType = 0;
				int compressionMethod = 0;
				int filterMethod = 0;
				int interlaceMethod = 0;
				Frame frame = null;
				PixelFormat format = 0;
				int index;
				int significant = 0;
				ZlibDecompressor decompress = null;
				ScanlineReader scanlineReader;
				int pass, passWidth, passHeight;
				PassFunc passFunc;

				// Read the rest of the magic number and check it.
				if(stream.Read(buffer, 0, 4) != 4)
				{
					throw new FormatException("could not read magic number");
				}
				if(buffer[0] != (byte)13 ||
				   buffer[1] != (byte)10 ||
				   buffer[2] != (byte)26 ||
				   buffer[3] != (byte)10)
				{
					throw new FormatException("invalid magic number");
				}

				// Create a chunk reader for the stream.
				ChunkReader reader = new ChunkReader(stream, buffer);

				// Read all of the chunks from the stream.
				while(reader.Type != IEND)
				{
					// Process the critical chunk types.
					if(reader.Type == IHDR)
					{
						// We can only have one header per PNG image.
						if(image.NumFrames > 0)
						{
							throw new FormatException("multiple headers");
						}

						// Read the contents of the image header.
						if(reader.Read(buffer, 0, 13) != 13)
						{
							throw new FormatException("truncated header");
						}
						width = Utils.ReadInt32B(buffer, 0);
						height = Utils.ReadInt32B(buffer, 4);
						bitDepth = buffer[8];
						colorType = buffer[9];
						compressionMethod = buffer[10];
						filterMethod = buffer[11];
						interlaceMethod = buffer[12];

						// Sanity-check the values.
						if(width < 1 || height < 1)
						{
							throw new FormatException("invalid size");
						}
						if(colorType == 0)
						{
							if(bitDepth != 1 && bitDepth != 2 &&
							   bitDepth != 4 && bitDepth != 8 &&
							   bitDepth != 16)
							{
								throw new FormatException
									("invalid depth for color type 0");
							}
						}
						else if(colorType == 2 || colorType == 4 ||
								colorType == 6)
						{
							if(bitDepth != 8 && bitDepth != 16)
							{
								throw new FormatException
									("invalid depth for color type " +
									 colorType.ToString());
							}
						}
						else if(colorType == 3)
						{
							if(bitDepth != 1 && bitDepth != 2 &&
							   bitDepth != 4 && bitDepth != 8)
							{
								throw new FormatException
									("invalid depth for color type 3");
							}
						}
						else
						{
							throw new FormatException("invalid color type");
						}
						if(compressionMethod != 0)
						{
							throw new FormatException
								("invalid compression method");
						}
						if(filterMethod != 0)
						{
							throw new FormatException
								("invalid filter method");
						}
						if(interlaceMethod != 0 && interlaceMethod != 1)
						{
							throw new FormatException
								("invalid interlace method");
						}

						// Create the image frame with the requested values.
						if(colorType == 3)
						{
							format = PixelFormat.Format8bppIndexed;
						}
						else if((colorType & 4) != 0)
						{
							if(significant == 0x01050505 && bitDepth == 8)
							{
								format = PixelFormat.Format16bppArgb1555;
							}
							else if(bitDepth == 8)
							{
								format = PixelFormat.Format32bppArgb;
							}
							else
							{
								format = PixelFormat.Format64bppArgb;
							}
						}
						else if(colorType == 0 && bitDepth == 16)
						{
							format = PixelFormat.Format16bppGrayScale;
						}
						else
						{
							if(significant == 0x00050505 && bitDepth == 8)
							{
								format = PixelFormat.Format16bppRgb555;
							}
							else if(significant == 0x00050605 && bitDepth == 8)
							{
								format = PixelFormat.Format16bppRgb565;
							}
							else if(bitDepth == 8)
							{
								format = PixelFormat.Format24bppRgb;
							}
							else
							{
								format = PixelFormat.Format48bppRgb;
							}
						}
						image.Width = width;
						image.Height = height;
						image.PixelFormat = format;
						image.LoadFormat = Image.Png;
						frame = image.AddFrame(width, height, format);
					}
					else if(reader.Type == PLTE)
					{
						// We must have a frame at this point.
						if(frame == null)
						{
							throw new FormatException
								("palette specified before image header");
						}

						// The palette is only required for color type 3.
						// Other color types use it as a hint only.
						if(colorType == 3)
						{
							int[] palette = new int [256];
							frame.Palette = palette;
							Array.Clear(buffer, 0, buffer.Length);
							if(reader.Length > 768)
							{
								reader.Read(buffer, 0, 768);
							}
							else
							{
								reader.Read(buffer, 0, buffer.Length);
							}
							for(index = 0; index < 256; ++index)
							{
								palette[index] =
									Utils.ReadRGB(buffer, index * 3);
							}
						}
					}
					else if(reader.Type == tRNS)
					{
						// We must have a frame at this point.
						if(frame == null)
						{
							throw new FormatException
								("transparency specified before image header");
						}

						// We only support simple transparencies for
						// color type 3 at present.  The transparency
						// information is ignored for other color types.
						if(colorType == 3)
						{
							index = 0;
							while(index < 256 && reader.Length > 0)
							{
								if(reader.Read(buffer, 0, 1) != 1)
								{
									break;
								}
								if(buffer[0] < 0x80)
								{
									frame.TransparentPixel = index;
									break;
								}
								++index;
							}
						}
					}
					else if(reader.Type == sBIT)
					{
						// Read the number of significant bits so that
						// we can detect images that started off life
						// as 15-bit or 16-bit RGB.
						if(reader.Length == 3)
						{
							reader.Read(buffer, 0, 3);
							significant = Utils.ReadRGB(buffer, 0);
						}
						else if(reader.Length == 4)
						{
							reader.Read(buffer, 0, 4);
							significant = Utils.ReadRGB(buffer, 0) |
										  (buffer[3] << 24);
						}
					}
					else if(reader.Type == IDAT)
					{
						// We must have a frame at this point.
						if(frame == null)
						{
							throw new FormatException
								("image data specified before image header");
						}

						// There can be only one set of data chunks.
						if(decompress != null)
						{
							throw new FormatException
								("multiple image data blocks encountered");
						}

						// Create a zlib decompressor.
						decompress = new ZlibDecompressor(reader);

						// Get the pass processing function.
						passFunc = GetPassFunc(colorType, bitDepth, format);

						// Process the data in the image.
						if(interlaceMethod == 0)
						{
							// No interlacing.
							scanlineReader = new ScanlineReader
								(decompress, width, height,
								colorType, bitDepth);
							passFunc(frame, scanlineReader, width, height,
								     0, 0, 1, 1);
						}
						else
						{
							// Use Adam7 interlacing.
							for(pass = 0; pass < 7; ++pass)
							{
								// Calculate the width and height of the pass.
								// Please refer "PNG - The Definitive Guide"
								// for a totally misleading and incompatible
								// description of the following code - Gopal

								passWidth = width + 
											adam7Rules[(pass+1) * 4 + 2] - 1;
								passWidth /= adam7Rules[pass * 4 + 2];
								if(passWidth <= 0)
								{
									continue;
								}
								
								passHeight = height + 
											adam7Rules[(pass+1) * 4 + 3 ] - 1;
								passHeight /= adam7Rules[pass * 4 + 3];
								if(passHeight <= 0)
								{
									continue;
								}

								// Create a scanline reader for the pass.
								scanlineReader = new ScanlineReader
									(decompress, passWidth, passHeight,
									 colorType, bitDepth);

								// Process the Adam7 pass.
								passFunc(frame, scanlineReader,
										 passWidth, passHeight,
										 adam7Rules[pass * 4],
										 adam7Rules[pass * 4 + 1],
										 adam7Rules[pass * 4 + 2],
										 adam7Rules[pass * 4 + 3]);
							}
						}

						// Eat any remaining IDAT data blocks.
						decompress.EatRemaining();

						// Skip the "Reset", because we've already done it.
						continue;
					}

					// Reset the chunk reader and move on to the next chunk.
					reader.Reset(buffer);
				}

				// Skip the contents of the IEND chunk and check its CRC.
				reader.Skip(buffer);

				// If we don't have a frame or decompressor,
				// then the PNG stream was empty.
				if(frame == null || decompress == null)
				{
					throw new FormatException("PNG did not contain an image");
				}
			}
예제 #10
0
 public static void ReadChunks(ChunkReader reader)
 {
     reader.Read(nameof(JoinedProfile), ref JoinedProfile);
     reader.Read(nameof(SendTransferNotifications), ref SendTransferNotifications);
 }