Seek() public method

Seeks within the stream.
public Seek ( int offset, SeekOrigin origin ) : void
offset int Offset to seek to.
origin SeekOrigin Origin of seek operation.
return void
Example #1
0
        public PaletteFile(string filename)
        {
            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Big, File.Open(filename, FileMode.Open));

            while (true)
            {
                int blockLength = 0;
                PaletteBlockType blockType = (PaletteBlockType)reader.ReadInt32();
                blockLength = reader.ReadInt32();

                switch (blockType)
                {
                    case PaletteBlockType.Attributes:

                        //contains name of palette and some attributes
                        //we dont care about this
                        reader.Seek(blockLength, SeekOrigin.Current);
                        break;

                    case PaletteBlockType.PixelData:
                        int entryCount = reader.ReadInt32();
                        int bytesPerEntry = reader.ReadInt32();
                        _paletteData = reader.ReadBytes(entryCount * bytesPerEntry);

                        break;

                    case PaletteBlockType.Null:
                        break;

                    default:
                        reader.Seek(blockLength, SeekOrigin.Current);
                        break;
                }
                if (reader.BaseStream.Position == reader.BaseStream.Length)
                    break;
            }

            reader.Close();
        }
        public static bool needsConversion(String inputFile)
        {
            var platform = getPlatform(inputFile);
            EndianBitConverter bitConverter = platform.GetBitConverter();

            using (var inputFileStream = File.Open(inputFile, FileMode.Open))
            using (var reader = new EndianBinaryReader(bitConverter, inputFileStream))
            {
                reader.Seek(16, SeekOrigin.Begin);
                if (reader.ReadUInt32() == 24)
                    return true;
            }
            return false;
        }
        public static void VerifyHeaders(string inputFile)
        {
            var platform = getPlatform(inputFile);
            EndianBitConverter bitConverter = platform.GetBitConverter();

            using (var inputFileStream = File.Open(inputFile, FileMode.Open))
            using (var reader = new EndianBinaryReader(bitConverter, inputFileStream))
            {
                reader.Seek(4, SeekOrigin.Begin);
                if (reader.ReadUInt32() != reader.BaseStream.Length - 8)
                    throw new InvalidDataException("The input OGG file appears to be truncated.");

                if (System.Text.Encoding.ASCII.GetString(reader.ReadBytes(4)) != "WAVE")
                    throw new InvalidDataException("Erorr reading input file - expected WAVE");

                if (System.Text.Encoding.ASCII.GetString(reader.ReadBytes(4)) != "fmt ")
                    throw new InvalidDataException("Error reading input file - expected fmt");

                var fmtLength = reader.ReadUInt32();
                if (fmtLength != 24 && fmtLength != 66)
                    throw new InvalidDataException("Error reading input file - expected fmt length of 24 or 66");

                if (fmtLength == 24)
                {
                    if (reader.ReadUInt16() != 0xFFFF)
                        throw new InvalidDataException("Error reading input file - expected fmt tag of 0xFFFF");

                    reader.BaseStream.Seek(14, SeekOrigin.Current);

                    if (reader.ReadUInt16() != 6)
                        throw new InvalidDataException("Error reading input file - expected cbSize of 6");

                    reader.BaseStream.Seek(6, SeekOrigin.Current);

                    if (System.Text.Encoding.ASCII.GetString(reader.ReadBytes(4)) != "vorb")
                        throw new InvalidDataException("Error reading input file - expected vorb");

                    if (reader.ReadUInt32() != 42)
                        throw new InvalidDataException("Error reading input file - expected vorb length of 42");
                }
            }
        }
Example #4
0
        public ActFile(string filename)
        {
            Stream file = OpenDataFile(filename);
            if (!Exists)
                return;

            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Big, file);

            CActor currentActor = null;
            Stack<CActor> actorStack = new Stack<CActor>();
            List<CActor> flatActorList = new List<CActor>();

            while (true)
            {
                int blockLength = 0;
                ActorBlockType blockType = (ActorBlockType)reader.ReadInt32();
                blockLength = reader.ReadInt32();

                switch (blockType)
                {
                    case ActorBlockType.Name:

                        currentActor = new CActor();

                        if (actorStack.Count == 0)
                        {
                            _actors.Add(currentActor);
                        }
                        else
                        {
                            CActor parent = actorStack.Peek();
                            currentActor.Parent = parent;
                            parent.Children.Add(currentActor);
                        }

                        flatActorList.Add(currentActor);
                        actorStack.Push(currentActor);

                        currentActor.Flags = reader.ReadBytes(2);
                        currentActor.SetName(ReadNullTerminatedString(reader));
                        break;

                    case ActorBlockType.TransformMatrix:

                        Matrix matrix = new Matrix();
                        matrix.M11 = reader.ReadSingle();
                        matrix.M12 = reader.ReadSingle();
                        matrix.M13 = reader.ReadSingle();
                        matrix.M21 = reader.ReadSingle();
                        matrix.M22 = reader.ReadSingle();
                        matrix.M23 = reader.ReadSingle();
                        matrix.M31 = reader.ReadSingle();
                        matrix.M32 = reader.ReadSingle();
                        matrix.M33 = reader.ReadSingle();
                        matrix.M41 = reader.ReadSingle();
                        matrix.M42 = reader.ReadSingle();
                        matrix.M43 = reader.ReadSingle();
                        matrix.M44 = 1;

                        currentActor.Matrix = matrix;

                        break;

                    case ActorBlockType.HierarchyStart:
                        //Debug.WriteLine("Hierarchy start");
                        break;

                    case ActorBlockType.ActorEnd:
                        actorStack.Pop();
                        //Debug.WriteLine("Hierarchy end");
                        break;

                    case ActorBlockType.MaterialNames:
                        currentActor.MaterialName = ReadNullTerminatedString(reader);
                        break;

                    case ActorBlockType.ModelName:
                        string modelName = ReadNullTerminatedString(reader);
                        currentActor.ModelName = modelName;
                        break;

                    case ActorBlockType.BoundingBox:
                        currentActor.BoundingBox = new BoundingBox(
                            new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()) * GameVars.Scale,
                            new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()) * GameVars.Scale
                            );
                        break;

                    case ActorBlockType.Null:
                        break;

                    default:
                        reader.Seek(blockLength, SeekOrigin.Current);
                        break;
                }
                if (reader.BaseStream.Position == reader.BaseStream.Length)
                    break;
            }
            reader.Close();

            _actors.ResolveMaterials();
        }
        public static bool NeedsConversion(this Stream input)
        {
            var platform = input.GetAudioPlatform();
            EndianBitConverter bitConverter = platform.GetBitConverter();

            using (var MS = new MemoryStream())
            using (var reader = new EndianBinaryReader(bitConverter, MS)) {
                input.Position = 0; input.CopyTo(MS);
                MS.Position = 0; input.Position = 0;
                reader.Seek(16, SeekOrigin.Begin);
                if (reader.ReadUInt32() == 24)
                    return true;
            }

            return false;
        }
Example #6
0
        public MatFile(string filename)
        {
            Stream file = OpenDataFile(filename);
            if (!Exists)
                return;

            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Big, file);

            CMaterial currentMaterial = null;

            while (true)
            {
                int blockLength = 0;
                MaterialBlockType blockType = (MaterialBlockType)reader.ReadInt32();
                blockLength = reader.ReadInt32();

                byte[] flags;

                switch (blockType)
                {
                    case MaterialBlockType.Attributes:
                        currentMaterial = new CMaterial();
                        _materials.Add(currentMaterial);

                        byte[] color = reader.ReadBytes(4);
                        byte[] otherColors = reader.ReadBytes(16);
                        flags = reader.ReadBytes(2);
                        byte[] transform = reader.ReadBytes(24);
                        currentMaterial.SimpMatPixelIndex = reader.ReadByte();
                        currentMaterial.SimpMatGradientCount = reader.ReadByte();

                        currentMaterial.DoubleSided = flags[0] == 0x10;
                        currentMaterial.Name = ReadNullTerminatedString(reader);

                        break;

                    case MaterialBlockType.AttributesV2:
                        currentMaterial = new CMaterial();
                        _materials.Add(currentMaterial);

                        reader.ReadBytes(4); //color
                        reader.ReadBytes(16); //othercolors
                        flags = reader.ReadBytes(4); // flags
                        reader.ReadBytes(24); //transform
                        reader.ReadBytes(4); //unk
                        currentMaterial.DoubleSided = flags[0] == 0x10;
                        reader.BaseStream.Position += 13;
                        currentMaterial.Name = ReadNullTerminatedString(reader);

                        break;

                    case MaterialBlockType.TextureName:
                        currentMaterial.PixName = ReadNullTerminatedString(reader);
                        break;

                    case MaterialBlockType.TabName:
                        string tabName = ReadNullTerminatedString(reader);
                        break;

                    case MaterialBlockType.Null:
                        break;

                    default:
                        reader.Seek(blockLength, SeekOrigin.Current);
                        break;
                }
                if (reader.BaseStream.Position == reader.BaseStream.Length)
                    break;
            }

            reader.Close();
        }
Example #7
0
        public void Load(Stream inputStream)
        {
            this.SetInternalStream(inputStream);

            DisposeLevels();
            _zoomLevels.Clear();

            _reader = new EndianBinaryReader(EndianBitConverter.Little, inputStream);

            // Check magic bytes
            if (_reader.ReadInt64() != 0x08002b4949)
                throw new Exception("Invalid ZIF file");

            while (_reader.BaseStream.Position < MAX_ZIF_BYTES)
            {
                ulong offset = _reader.ReadUInt64();
                if (offset == 0)
                    break;

                _reader.Seek(offset, SeekOrigin.Begin);
                var numTags = _reader.ReadUInt64();

                var level = new ZoomLevel(_reader);
                for (ulong i = 0; i < numTags; ++i)
                {
                    var key = _reader.ReadUInt16();
                    var notUsed = _reader.ReadUInt16();
                    var val1 = _reader.ReadUInt64();
                    var val2 = _reader.ReadUInt64();
                    level.AddTag(key, val1, val2);
                }
                _zoomLevels.Insert(0, level);
            }            
        }
        public void EndianReaderWriterPerformance()
        {
            // store the logging value
            var oldThreshold = LoggingConfiguration.GlobalLoggingLevel;

            // disable logging to improve performance
            LoggingConfiguration.GlobalLoggingLevel = log4net.Core.Level.Off;

            byte[] bytes;
            int testRuns;
            int startIndex;
            int expectedValue;
            ByteSetupMethods.Setup(out bytes, out testRuns, out startIndex,
                                   out expectedValue);

            var memStream = new MemoryStream(bytes);
            var endianReader = new EndianBinaryReader(EndianBitConverter.Big, memStream);

            var startTime = DateTime.Now;

            for(int i = 0; i < testRuns; i++)
            {
                endianReader.Seek(startIndex, SeekOrigin.Begin);
                var actualValue = endianReader.ReadInt32();

                // NOTE: Assert.AreEqual() significantly slows, by a factor of ~6x
                //       the execution of this loop, so we perform ourself and
                //       then call Assert.AreEqual() if the comparison fails.
                //       This doesn't reduce performance by a noticable amount
                if(actualValue != expectedValue)
                {
                    Assert.AreEqual(expectedValue, actualValue);
                }
            }

            var endTime = DateTime.Now;

            // restore logging
            LoggingConfiguration.GlobalLoggingLevel = oldThreshold;

            var rate = new Rate(startTime, endTime, testRuns, "Test runs");

            Console.WriteLine(rate.ToString());
        }
Example #9
0
        public DatFile(string filename, bool deformMainModel)
        {
            if (filename.EndsWith(".ACT", StringComparison.InvariantCultureIgnoreCase))
                filename = filename.ToUpper().Replace(".ACT", ".DAT"); //fix up some 3rd party vehicle weirdness

            CModel currentModel = null;

            Stream file = OpenDataFile(filename);
            if (!Exists)
                return;

            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Big, file);

            while (true)
            {
                int type = reader.ReadInt32();
                int size = 0;
                if (type != (int)BlockType.Null)
                    size = reader.ReadInt32();

                switch (type)
                {
                    case (int)BlockType.Null:
                        break;

                    case (int)BlockType.ModelName:
                        reader.Seek(2, SeekOrigin.Current);
                        string name = ReadNullTerminatedString(reader);

                        if (deformMainModel && Path.GetFileNameWithoutExtension(name).Equals(Path.GetFileNameWithoutExtension(filename), StringComparison.InvariantCultureIgnoreCase))
                            currentModel = new CDeformableModel();
                        else
                            currentModel = new CModel();

                        currentModel.Name = name;
                        _models.Add(currentModel);

                        break;

                    case (int)BlockType.Vertices:
                        ReadVertexBlock(reader, currentModel);
                        break;

                    case (int)BlockType.Faces:
                        ReadPolygonBlock(reader, currentModel);
                        break;

                    case (int)BlockType.TextureCoords:
                        ReadTextureMapBlock(reader, currentModel);
                        break;

                    case (int)BlockType.Materials:
                        ReadMaterialsBlock(reader, currentModel);
                        break;

                    case (int)BlockType.FaceMaterials:
                        ReadFaceMaterialsBlock(reader, currentModel);
                        break;

                    default:
                        Debug.WriteLine("Unknown section: " + type);
                        reader.Seek(size, SeekOrigin.Current);
                        break;
                }

                if (reader.BaseStream.Position == reader.BaseStream.Length)
                    break;
            }

            reader.Close();
            if (filename == "FAUST.DAT")
            {
            }
            _models.Resolve(true);
        }
Example #10
0
        public PixFile(string filename)
        {
            Stream file = OpenDataFile(filename);
            if (!Exists)
                return;

            EndianBinaryReader reader = new EndianBinaryReader(EndianBitConverter.Big, file);
            PixMap currentPix=null;

            while (true)
            {
                int blockLength = 0;
                PixBlockType blockType = (PixBlockType)reader.ReadInt32();

                if (blockType == PixBlockType.Null && reader.BaseStream.Position + 3 >= reader.BaseStream.Length)
                    break;

                blockLength = reader.ReadInt32();

                switch (blockType)
                {
                    case PixBlockType.Attributes:

                        int type = reader.ReadByte();
                        if (type == 7)
                        {
                            /* bmp palette data? - seen in some splat pack textures. Jump over it, and its pixel data */
                            reader.Seek(blockLength-1, SeekOrigin.Current);
                            _skipNextPixelSection = true;
                            break;
                        }
                        currentPix = new PixMap();
                        currentPix.Width = reader.ReadInt16();
                        int width2 = reader.ReadInt16();
                        currentPix.Height = reader.ReadInt16();

                        byte[] unk2 = reader.ReadBytes(4);
                        currentPix.Name = ReadNullTerminatedString(reader);
                        _pixMaps.Add(currentPix);
                        break;

                    case PixBlockType.PixelData:

                        if (_skipNextPixelSection)
                        {
                            reader.Seek(blockLength, SeekOrigin.Current);
                            _skipNextPixelSection = false;
                            break;
                        }
                        int pixelCount = reader.ReadInt32();
                        int bytesPerPixel = reader.ReadInt32();
                        if (bytesPerPixel > 3)
                        {
                            bytesPerPixel = 1;  //PixEd sometimes doesnt get this right
                        }

                        if (currentPix == null)
                        {
                            int size = (int)Math.Sqrt(pixelCount);
                            currentPix = new PixMap();
                            currentPix.Name = Path.GetFileName(filename);
                            currentPix.Width = size;
                            currentPix.Height = size;
                            _pixMaps.Add(currentPix);
                        }

                        byte[] pixels = reader.ReadBytes(pixelCount * bytesPerPixel);
                        Texture2D texture=null;

                        if (bytesPerPixel == 1)
                        {
                            texture = new Texture2D(Engine.Device, currentPix.Width, currentPix.Height, 1, TextureUsage.None, SurfaceFormat.Color);
                            texture.SetData<byte>(Helpers.GetBytesForImage(pixels, currentPix.Width, currentPix.Height, GameVars.Palette));
                        }
                        else if (bytesPerPixel == 2)
                        {
                            texture = new Texture2D(Engine.Device, currentPix.Width, currentPix.Height, 1, TextureUsage.None, SurfaceFormat.Bgr565);
                            int j = 0;
                            byte[] px = new byte[2];
                            for (int i = 0; i < pixels.Length; i += 2)
                            {
                                byte tmp = pixels[i + 1];
                                pixels[i + 1] = pixels[i];
                                pixels[i] = tmp;
                            }
                            texture.SetData<byte>(pixels);
                        }
                        else if (bytesPerPixel ==3 )
                        {
                            texture = new Texture2D(Engine.Device, currentPix.Width, currentPix.Height, 1, TextureUsage.None, SurfaceFormat.Color);
                            int j = 0;
                            byte[] px2 = new byte[pixels.Length * 4];
                            for (int i = 0; i < pixels.Length; i += 3)
                            {
                                px2[j++] = pixels[i];
                                px2[j++] = pixels[i+1];
                                px2[j++] = pixels[i+2];
                                px2[j++] = 255;
                            }
                            texture.SetData<byte>(px2);
                        }

                        currentPix.Texture = texture;
                        break;

                    case PixBlockType.Null:
                        break;

                    default:
                        reader.Seek(blockLength, SeekOrigin.Current);
                        break;
                }
                if (reader.BaseStream.Position+3 >= reader.BaseStream.Length)
                    break;
            }

            reader.Close();
        }