private bool ValidateBlamPointer(BlamPointer blamPointer, ElementArray info, MapStream stream)
        {
            var stringWriter = new StringWriter();

            if (blamPointer.Count == 0 && blamPointer.Address == 0)
            {
                return(true);
            }
            if (blamPointer.Count == 0 ^ blamPointer.Address == 0)
            {
                stringWriter.WriteLine(string.Format("-> null-value count({0}) address({1}) is invalid", blamPointer.Count, blamPointer.Address));
            }
            if (blamPointer.Count < 0)
            {
                stringWriter.WriteLine(string.Format("-> count({0}) is invalid", blamPointer.Count));
            }
            if (blamPointer.Count > info.MaxElementCount && info.MaxElementCount > 0)
            {
                stringWriter.WriteLine(string.Format("-> count({0}) > max-count({1})", blamPointer.Count, info.MaxElementCount));
            }
            if (!stream.ContainsPointer(blamPointer))
            {
                stringWriter.WriteLine(string.Format("-> address({0}) not contained in stream({1})", blamPointer.Address, stream.Name));
            }

            var errors = stringWriter.ToString();

            if (!string.IsNullOrWhiteSpace(errors))
            {
                OnWriteMessage(string.Format("Pointer ({0})\n{1}", info.Name, errors));
                return(false);
            }
            return(true);
        }
        public bool Contains(BlamPointer pointer)
        {
            var previousAddressIsContained = true;

            foreach (var address in pointer)
            {
                if (this.Contains(address, true) ^ previousAddressIsContained)
                {
                    previousAddressIsContained = false;
                    break;
                }
                else
                {
                    previousAddressIsContained = true;
                }
            }
            if (previousAddressIsContained)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #3
0
        public virtual T[] ReadBlockArrayData <T>(BinaryReader binaryReader, BlamPointer blamPointer)
            where T : GuerillaBlock
        {
            var array = new T[blamPointer.ElementCount];

            if (!BlamPointer.IsNull(blamPointer) && binaryReader.BaseStream.Position != blamPointer.StartAddress)
            {
                var offset = blamPointer.StartAddress - binaryReader.BaseStream.Position;
                binaryReader.BaseStream.Seek(offset, SeekOrigin.Current);
            }
            var pointerArray = new Queue <BlamPointer> [blamPointer.ElementCount];

            var ctor = GetObjectActivator <T>(typeof(T));

            for (var i = 0; i < blamPointer.ElementCount; ++i)
            {
                var element = (T)ctor();
                array[i]        = element;
                pointerArray[i] = array[i].ReadFields(binaryReader);
            }
            for (var i = 0; i < blamPointer.ElementCount; ++i)
            {
                array[i].ReadInstances(binaryReader, pointerArray[i]);
            }
            return(array);
        }
 public bool ContainsPointer(BlamPointer blamPointer)
 {
     foreach (var block in this.MemoryBlocks)
     {
         var previousAddressIsContained = true;
         foreach (var address in blamPointer)
         {
             if (block.Contains(address, true) ^ previousAddressIsContained)
             {
                 previousAddressIsContained = false;
                 break;
             }
             else
             {
                 previousAddressIsContained = true;
             }
         }
         if (previousAddressIsContained)
         {
             return(true);
         }
         else
         {
             continue;
         }
     }
     return(false);
 }
        private void ValidateTagBlock(ElementArray info, BlamPointer pointer, BinaryReader reader, ref int address)
        {
            using (reader.BaseStream.Pin())
            {
                // If owned by tag and memory has not been allocated yet*
                var allocated = from item in PointersList
                                where item.Item1.Equals(pointer)
                                select item;
                var partiallyAllocated = from item in PointersList
                                         where item.Item1.Intersects(pointer)
                                         select item;
                if (OwnedByTag(pointer))
                {
                    if (!allocated.Any())
                    {
                        var alignedAddress = (address - startOffset) + Padding.GetCount(address - startOffset, info.Alignment);
                        if (pointer.Address - startOffset != alignedAddress)
                        {
                            MapStream mapStream = reader.BaseStream as MapStream;
                            if (mapStream != null)
                            {
                                OnWriteMessage(string.Format("{2}: Expected address \"{0}\"  - actually was \"{1}\"", address - startOffset, pointer.Address - startOffset, info.Name));
                            }
                        }
                        address = pointer.Address + pointer.PointedSize;
                    }
                    if (allocated.Any())
                    {
                    }
                    else if (partiallyAllocated.Any())
                    {
                        foreach (var overlappingItem in partiallyAllocated)
                        {
                            var overlap = pointer.Address - overlappingItem.Item1.Address - overlappingItem.Item1.PointedSize;
                            OnWriteMessage(string.Format("Overlap of ({0})[{3}] with ({1}) by ({2}) bytes", overlappingItem.Item2.ToHierarchyString(), info.ToHierarchyString(), overlap, overlappingItem.Item1.Count));
                        }
                    }
                }
                else if (!IsValid(pointer))
                {
                    OnWriteMessage(string.Format("INVALID POINTER"));
                    return;
                }
                else
                {
                    OnWriteMessage(string.Format("WILLLLLSOOON SHARE"));
                }

                PointersList.Add(new Tuple <BlamPointer, ElementArray>(pointer, info));

                foreach (var elementAddress in pointer)
                {
                    reader.BaseStream.Position = elementAddress;
                    ValidateChildren(info, reader, ref address);
                }
            }
        }
Exemple #6
0
 public virtual byte[] ReadDataByteArray(BinaryReader binaryReader, BlamPointer blamPointer)
 {
     if (BlamPointer.IsNull(blamPointer))
     {
         return(new byte[0]);
     }
     binaryReader.BaseStream.Position = blamPointer.StartAddress;
     return(binaryReader.ReadBytes(blamPointer.ElementCount));
 }
Exemple #7
0
        public static short[] ReadDataShortArray(BinaryReader binaryReader, BlamPointer blamPointer)
        {
            binaryReader.BaseStream.Position = blamPointer.StartAddress;
            var elements = new short[blamPointer.ElementCount];
            var buffer   = binaryReader.ReadBytes(blamPointer.ElementCount * 2);

            Buffer.BlockCopy(buffer, 0, elements, 0, buffer.Length);
            return(elements);
        }
        private void Enqueue(short[] data, BlamPointer dataPointer)
        {
            var dataQueueItem = new ShortDataQueueItem(data)
            {
                Pointer = dataPointer
            };

            _lookupDictionary[data] = dataQueueItem;
            _queue.Enqueue(dataQueueItem);
        }
        public void QueueRead(GuerillaBlock[] dataBlocks, BlamPointer dataPointer)
        {
            //  if the array is empty there's nothing to read, so return
            if (dataBlocks.Length <= 0)
            {
                return;
            }

            Enqueue(dataBlocks, dataPointer);
        }
        public void QueueRead(short[] data, BlamPointer dataPointer)
        {
            //  if the array is empty there's nothing to write, so return
            if (data.Length <= 0)
            {
                return;
            }

            Enqueue(data, dataPointer);
        }
        private void Enqueue(GuerillaBlock[] dataBlocks, BlamPointer dataPointer)
        {
            var guerillaQueueItem = new GuerillaQueueItem(dataBlocks)
            {
                Pointer = dataPointer
            };

            _lookupDictionary[dataBlocks] = guerillaQueueItem;
            _queue.Enqueue(guerillaQueueItem);
        }
 private bool IsValid(BlamPointer pointer)
 {
     if (isValidDelegate != null)
     {
         return(isValidDelegate(pointer));
     }
     else
     {
         return(false);
     }
 }
 public bool OwnedByTag(BlamPointer pointer)
 {
     if (IsPointerOwnedByTagDelegate != null)
     {
         return(IsPointerOwnedByTagDelegate(pointer));
     }
     else
     {
         return(false);
     }
 }
Exemple #14
0
        private void Enqueue(short[] data)
        {
            var blamPointer   = new BlamPointer(data.Length, _queueAddress, 2);
            var dataQueueItem = new ShortDataQueueItem(data)
            {
                Pointer = blamPointer
            };

            _lookupDictionary[data] = dataQueueItem;
            _queue.Enqueue(dataQueueItem);
            _queueAddress = blamPointer.EndAddress;
        }
Exemple #15
0
        public static T[] ReadBlockArrayData <T>(BinaryReader binaryReader, BlamPointer blamPointer)
            where T : GuerillaBlock, new()
        {
            var array = new T[blamPointer.ElementCount];

            binaryReader.BaseStream.Position = blamPointer.StartAddress;
            for (var i = 0; i < blamPointer.ElementCount; ++i)
            {
                array[i] = (T)Activator.CreateInstance(typeof(T), binaryReader);
            }
            return(array);
        }
Exemple #16
0
        private void Enqueue(byte[] data, int alignment = 4)
        {
            var blamPointer   = new BlamPointer(data.Length, _queueAddress, 1, alignment);
            var dataQueueItem = new ByteDataQueueItem(data)
            {
                Pointer = blamPointer
            };

            _lookupDictionary[data] = dataQueueItem;
            _queue.Enqueue(dataQueueItem);
            _queueAddress = blamPointer.EndAddress;
        }
Exemple #17
0
        public void WriteQueue( )
        {
            while (_queue.Count > 0)
            {
                var item = Dequeue( );
                //  if the pointer has data, and the stream is not already at the data start address
                //  then seek the data start address using a current stream offset to preserve the read/write
                //  cache.
                if (!BlamPointer.IsNull(item.Pointer) && BaseStream.Position != item.Pointer.StartAddress)
                {
#if DEBUG
                    var offset = item.Pointer.StartAddress - BaseStream.Position;
                    if (offset < 0)
                    {
                        throw new Exception("That breaks the maps");
                    }
#endif
                    BaseStream.Seek(item.Pointer.StartAddress);
                }

                var dataQueueItem = item as ByteDataQueueItem;
                if (dataQueueItem != null)
                {
                    Write(dataQueueItem.Data);
                    continue;
                }

                var shortDataQueueItem = item as ShortDataQueueItem;
                if (shortDataQueueItem != null)
                {
                    var buffer = shortDataQueueItem.Data;
                    for (var i = 0; i < buffer.Length; ++i)
                    {
                        Write(shortDataQueueItem.Data[i]);
                    }
                    continue;
                }

                var guerillaBlockQueueItem = item as GuerillaQueueItem;
                if (guerillaBlockQueueItem == null)
                {
                    continue;
                }

                //  then foreach element in the block array call the write method
                foreach (GuerillaBlock block in guerillaBlockQueueItem.DataBlocks)
                {
                    block.Write_(this);
                }
            }
        }
Exemple #18
0
        public static int WriteData(BinaryWriter binaryWriter, byte[] data, int nextAddress)
        {
            var blamPointer = new BlamPointer(data.Length, nextAddress, sizeof(byte));

            if (blamPointer.ElementCount <= 0)
            {
                return(nextAddress);
            }
            using (binaryWriter.BaseStream.Pin())
            {
                binaryWriter.BaseStream.Position = blamPointer[0];
                binaryWriter.Write(data);
            }
            return(blamPointer.EndAddress);
        }
Exemple #19
0
        private void Enqueue(GuerillaBlock[] dataBlocks)
        {
            var elementSize = dataBlocks.GetElementSize( );
            var alignment   = dataBlocks.GetAlignment( );
            var blamPointer = new BlamPointer(dataBlocks.Length, _queueAddress, elementSize,
                                              alignment);

            var guerillaQueueItem = new GuerillaQueueItem(dataBlocks)
            {
                Pointer = blamPointer
            };

            _lookupDictionary[dataBlocks] = guerillaQueueItem;
            _queue.Enqueue(guerillaQueueItem);
            _queueAddress = blamPointer.EndAddress;
        }
        public bool Contains(BlamPointer pointer)
        {
            var failed = false;

            foreach (var address in pointer)
            {
                failed |= !Contains(address);
                if (failed)
                {
                    break;
                }
            }

            failed |= !Contains(pointer.EndAddress - 1);

            return(!failed);
        }
        public void ReadQueue( )
        {
            while (_queue.Count > 0)
            {
                var item = Dequeue( );
                //  if the pointer has data, and the stream is not already at the data start address
                //  then seek the data start address using a current stream offset to preserve the read/write
                //  cache.
                if (!BlamPointer.IsNull(item.Pointer) && BaseStream.Position != item.Pointer.StartAddress)
                {
                    var offset = item.Pointer.StartAddress - BaseStream.Position;
                    BaseStream.Seek(offset, SeekOrigin.Current);
                }

                var dataQueueItem = item as ByteDataQueueItem;
                if (dataQueueItem != null)
                {
                    dataQueueItem.Data = ReadBytes(item.Pointer.ElementCount);
                    continue;
                }

                var shortDataQueueItem = item as ShortDataQueueItem;
                if (shortDataQueueItem != null)
                {
                    for (var i = 0; i < item.Pointer.ElementCount; ++i)
                    {
                        shortDataQueueItem.Data[i] = ReadInt16( );
                    }
                    continue;
                }

                var guerillaBlockQueueItem = item as GuerillaQueueItem;
                if (guerillaBlockQueueItem == null)
                {
                    continue;
                }

                //  then foreach element in the block array call the write method
                foreach (GuerillaBlock block in guerillaBlockQueueItem.DataBlocks)
                {
                    block.ReadFields(this);
                }
            }
        }
Exemple #22
0
        public static int WriteBlockArray <T>(BinaryWriter binaryWriter, IList <T> blocks, int nextAddress)
            where T : GuerillaBlock
        {
            var elementType = blocks.GetType().GetElementType();
            var blamPointer = new BlamPointer(blocks.Count, nextAddress, SizeOf(elementType),
                                              AlignmentOf(elementType));

            binaryWriter.Write(blamPointer);
            nextAddress = blamPointer.EndAddress;
            binaryWriter.BaseStream.Pin();
            {
                for (var i = 0; i < blamPointer.ElementCount; ++i)
                {
                    binaryWriter.BaseStream.Position = blamPointer[i];
                    nextAddress = blocks[i].Write(binaryWriter, nextAddress);
                }
            }
            return(nextAddress);
        }
Exemple #23
0
 public bool ContainsPointer(BlamPointer blamPointer)
 {
     return(DefaultMemoryBlock.Contains(blamPointer) || ActiveStructureMemoryAllocation.Contains(blamPointer));
 }
Exemple #24
0
 public static byte[] ReadDataByteArray(BinaryReader binaryReader, BlamPointer blamPointer)
 {
     binaryReader.BaseStream.Position = blamPointer.StartAddress;
     return(binaryReader.ReadBytes(blamPointer.ElementCount));
 }
Exemple #25
0
 private bool IsValid(BlamPointer pointer)
 {
     return(_isValidDelegate != null && _isValidDelegate(pointer));
 }
 public bool OwnedByTag(BlamPointer pointer)
 {
     if (IsPointerOwnedByTagDelegate != null) return IsPointerOwnedByTagDelegate(pointer);
     else return false;
 }
        private void ValidateTagBlock(ElementArray info, BlamPointer pointer, BinaryReader reader, ref int address)
        {

            using (reader.BaseStream.Pin())
            {
                // If owned by tag and memory has not been allocated yet*
                var allocated = from item in PointersList
                                where item.Item1.Equals(pointer)
                                select item;
                var partiallyAllocated = from item in PointersList
                                         where item.Item1.Intersects(pointer)
                                         select item;
                if (OwnedByTag(pointer))
                {
                    if (!allocated.Any())
                    {
                        var alignedAddress = (address - startOffset) + Padding.GetCount(address - startOffset, info.Alignment);
                        if (pointer.Address - startOffset != alignedAddress)
                        {
                            MapStream mapStream = reader.BaseStream as MapStream;
                            if (mapStream != null)
                            {
                                OnWriteMessage(string.Format("{2}: Expected address \"{0}\"  - actually was \"{1}\"", address - startOffset, pointer.Address - startOffset, info.Name));
                            }
                        }
                        address = pointer.Address + pointer.PointedSize;
                    }
                    if (allocated.Any()) { }
                    else if (partiallyAllocated.Any())
                    {
                        foreach (var overlappingItem in partiallyAllocated)
                        {
                            var overlap = pointer.Address - overlappingItem.Item1.Address - overlappingItem.Item1.PointedSize;
                            OnWriteMessage(string.Format("Overlap of ({0})[{3}] with ({1}) by ({2}) bytes", overlappingItem.Item2.ToHierarchyString(), info.ToHierarchyString(), overlap, overlappingItem.Item1.Count));
                        }
                    }
                }
                else if (!IsValid(pointer))
                {
                    OnWriteMessage(string.Format("INVALID POINTER"));
                    return;
                }
                else
                    OnWriteMessage(string.Format("WILLLLLSOOON SHARE"));

                PointersList.Add(new Tuple<BlamPointer, ElementArray>(pointer, info));

                foreach (var elementAddress in pointer)
                {
                    reader.BaseStream.Position = elementAddress;
                    ValidateChildren(info, reader, ref address);
                }
            }
        }
 private bool IsValid(BlamPointer pointer)
 {
     if (isValidDelegate != null) return isValidDelegate(pointer);
     else return false;
 }
        private bool ValidateBlamPointer(BlamPointer blamPointer, ElementArray info, MapStream stream)
        {
            var stringWriter = new StringWriter();
            if (blamPointer.Count == 0 && blamPointer.Address == 0) return true;
            if (blamPointer.Count == 0 ^ blamPointer.Address == 0)
                stringWriter.WriteLine(string.Format("-> null-value count({0}) address({1}) is invalid", blamPointer.Count, blamPointer.Address));
            if (blamPointer.Count < 0)
                stringWriter.WriteLine(string.Format("-> count({0}) is invalid", blamPointer.Count));
            if (blamPointer.Count > info.MaxElementCount && info.MaxElementCount > 0)
                stringWriter.WriteLine(string.Format("-> count({0}) > max-count({1})", blamPointer.Count, info.MaxElementCount));
            if (!stream.ContainsPointer(blamPointer))
                stringWriter.WriteLine(string.Format("-> address({0}) not contained in stream({1})", blamPointer.Address, stream.Name));

            var errors = stringWriter.ToString();
            if (!string.IsNullOrWhiteSpace(errors))
            {
                OnWriteMessage(string.Format("Pointer ({0})\n{1}", info.Name, errors));
                return false;
            }
            return true;
        }
Exemple #30
0
        private void ValidateTagBlock(ElementArray info, BlamPointer pointer, BinaryReader reader, ref int address)
        {
            using (reader.BaseStream.Pin())
            {
                // If owned by tag and memory has not been allocated yet*
                var allocated = from item in _pointersList
                                where item.Item1.Equals(pointer)
                                select item;
                var partiallyAllocated = from item in _pointersList
                                         where item.Item1.Intersects(pointer)
                                         select item;
                if (IsOwnedByTag(pointer))
                {
                    var enumerable = allocated as IList <Tuple <BlamPointer, ElementArray> > ?? allocated.ToList();
                    if (!enumerable.Any())
                    {
                        var alignedAddress = (address) +
                                             Padding.GetCount(address, info.alignment);
                        if (pointer.StartAddress != alignedAddress)
                        {
                            var mapStream = reader.BaseStream as CacheStream;
                            if (mapStream != null)
                            {
                                error = true;
                                OnWriteMessage(string.Format("{2}: Expected address \"{0}\"  - actually was \"{1}\" delta \"{3}\"",
                                                             (uint)alignedAddress, (uint)pointer.StartAddress, info.name, alignedAddress - pointer.StartAddress));
                            }
                        }
                        address = pointer.StartAddress + pointer.PointedSize;
                    }
                    if (enumerable.Any())
                    {
                    }
                    else
                    {
                        var overlappingItems = partiallyAllocated as IList <Tuple <BlamPointer, ElementArray> > ??
                                               partiallyAllocated.ToList();
                        if (overlappingItems.Any())
                        {
                            foreach (var overlappingItem in overlappingItems)
                            {
                                error = true;
                                var overlap = pointer.StartAddress - overlappingItem.Item1.StartAddress -
                                              overlappingItem.Item1.PointedSize;

                                OnWriteMessage(
                                    string.Format(
                                        "Overlap of ({0})\n{3} elements sized '{5}' at '{4}'\nwith ({1})\n{6} elements sized '{8}' at '{7}'\nby ({2}) bytes",
                                        overlappingItem.Item2.ToHierarchyString(), info.ToHierarchyString(), overlap,
                                        overlappingItem.Item1.ElementCount, overlappingItem.Item1.StartAddress,
                                        overlappingItem.Item1.ElementSize,
                                        pointer.ElementCount, pointer.StartAddress, pointer.ElementSize));
                            }
                        }
                    }
                }
                else if (!IsValid(pointer))
                {
                    //error = true;
                    //OnWriteMessage(string.Format("INVALID POINTER {1} -> {0}", info.name, pointer));
                    return;
                }
                else
                {
                    // OnWriteMessage( string.Format( "EXTERNAL SHARE" ) );
                }

                _pointersList.Add(new Tuple <BlamPointer, ElementArray>(pointer, info));

                foreach (var elementAddress in pointer)
                {
                    reader.BaseStream.Position = elementAddress;
                    ValidateChildren(info, reader, ref address);
                }
            }
        }
Exemple #31
0
 private bool IsOwnedByTag(BlamPointer pointer)
 {
     return(_isPointerOwnedByTagDelegate != null && _isPointerOwnedByTagDelegate(pointer));
 }