Ejemplo n.º 1
0
        public void Test_CacheStream()
        {
            //for (int i = 0; i < 10; i++)
            Parallel.For(0, 32, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 64
            }, i =>
            {
                //using (MemoryStream stream = new MemoryStream())
                using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    using (CacheStream stream = new CacheStream(bufferStream, 1024, _bufferManager))
                    {
                        byte[] buffer = _bufferManager.TakeBuffer(1024 * 1024); //new byte[_random.Next(128, 1024 * 1024 * 10)];
                        long seek     = _random.Next(64, buffer.Length);
                        //long seek = 0;

                        _random.NextBytes(buffer);

                        stream.Write(buffer, 0, buffer.Length);
                        stream.Position = seek;

                        byte[] buff2 = _bufferManager.TakeBuffer(buffer.Length); //new byte[buffer.Length];
                        stream.Read(buff2, (int)seek, buff2.Length - (int)seek);

                        if (!CollectionUtilities.Equals(buffer, (int)seek, buff2, (int)seek, buffer.Length - (int)seek))
                        {
                            Assert.IsTrue(CollectionUtilities.Equals(buffer, (int)seek, buff2, (int)seek, buffer.Length - (int)seek));
                        }

                        _bufferManager.ReturnBuffer(buffer);
                        _bufferManager.ReturnBuffer(buff2);
                    }
            });

            using (MemoryStream mstream = new MemoryStream())
                using (BufferStream bufferStream = new BufferStream(_bufferManager))
                    using (CacheStream stream = new CacheStream(bufferStream, 1024, _bufferManager))
                    {
                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            var v = (byte)_random.Next(0, 255);
                            mstream.WriteByte(v);
                            stream.WriteByte(v);
                        }

                        mstream.Seek(0, SeekOrigin.Begin);
                        stream.Seek(0, SeekOrigin.Begin);

                        Assert.IsTrue(mstream.Length == stream.Length);

                        for (int i = 0; i < 1024 * 1024; i++)
                        {
                            Assert.IsTrue(mstream.ReadByte() == stream.ReadByte());
                        }
                    }
        }
Ejemplo n.º 2
0
        public bool Validate(MoonfishTagGroup validateTag, IEnumerable <MoonfishTagGroup> tagPool, string[] filenames)
        {
            error         = false;
            _pointersList = new List <Tuple <BlamPointer, ElementArray> >();
            var filename = string.Format(@"{1}\test_analysis\analysis.txt",
                                         validateTag.Class.ToTokenString(), Local.MapsDirectory);
            var stringWriter = File.AppendText(filename);

            _writeMessage = (stringWriter.WriteLine);

            var offset       = 0;
            var elementArray = CompileElementArray(validateTag, tagPool, offset);

            elementArray.count = 1;

            foreach (var file in filenames)
            {
                using (var map = new CacheStream(file))
                {
                    var binaryReader = new BinaryReader(map);

                    //OnWriteMessage(string.Format("Begin ({0})", map.MapName));

                    foreach (var tag in map.Index)
                    {
                        error = false;
                        if (!(tag.Class == validateTag.Class))
                        {
                            continue;
                        }

                        var metaTableMemory = map.DefaultMemoryBlock;
                        if (tag.Class == (TagClass)"sbsp" || tag.Class == (TagClass)"ltmp")
                        {
                            metaTableMemory =
                                map.StructureMemoryBlocks[map.StructureMemoryBlockBindings[tag.Identifier]];
                            map.ActiveAllocation(StructureCache.VirtualStructureCache0 +
                                                 map.StructureMemoryBlockBindings[tag.Identifier]);
                        }
                        var virtualTagMemory = new VirtualMappedAddress
                        {
                            Address = tag.VirtualAddress,
                            Length  = tag.Length
                        };

                        _isValidDelegate             = metaTableMemory.Contains;
                        _isPointerOwnedByTagDelegate = virtualTagMemory.Contains;
                        _pointersList.Clear();

                        offset = (int)map.Seek(tag.Identifier);


                        elementArray.virtualAddress = map.Index[tag.Identifier].VirtualAddress;
                        ValidateTagBlock(elementArray, elementArray.ToFixedArrayPointer(), binaryReader, ref offset);

                        if (error)
                        {
                            OnWriteMessage(string.Format("Tag ({0}.{1})", tag.Path, validateTag.Class.ToTokenString()));
                        }

                        stringWriter.Flush();
                    }

                    Console.WriteLine(@"Parsed ({0})", map.Header.Name);
                }
            }
            stringWriter.Close();


            return(error);
        }