Example #1
0
        public T Read <T>(MemoryAddress address)
        {
            if (!IsValidAddress(address))
            {
                throw new ArgumentOutOfRangeException();
            }
            if (!IsValidAddress(address + TypeHelper <T> .SizeOf))
            {
                throw new ArgumentException("address");
            }

            if (TypeHelper <T> .HasKnownStructLayout)
            {
                byte[] buffer = GetBuffer(TypeHelper <T> .SizeOf);
                UnsafeReadBytes(address, buffer, 0, TypeHelper <T> .SizeOf);
                return(StructHelper <T> .UnsafeRead(buffer, 0));
            }
            else if (TypeHelper <T> .IsMemoryObjectType)
            {
                return((T)(object)MemoryObjectFactory.UnsafeCreate(typeof(T), Memory, address));
            }
            else if (TypeHelper <T> .IsMemoryPointerType)
            {
                return((T)(object)MemoryPointerFactory.UnsafeCreate <T>(Memory, ReadMemoryAddress(address)));
            }
            else if (TypeHelper <T> .IsMemoryAddressType)
            {
                return((T)(object)ReadMemoryAddress(address));
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Example #2
0
        public T[] Read <T>(MemoryAddress address, int count)
        {
            if (!IsValidAddress(address))
            {
                throw new ArgumentOutOfRangeException("address");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (!IsValidAddress(address + TypeHelper <T> .SizeOf * count))
            {
                throw new ArgumentException();
            }

            T[] array = new T[count];

            if (TypeHelper <T> .HasKnownStructLayout)
            {
                int    bufferSize = TypeHelper <T> .SizeOf * count;
                byte[] buffer     = GetBuffer(bufferSize);
                UnsafeReadBytes(address, buffer, 0, bufferSize);
                StructHelper <T> .CopyFromBuffer(buffer, 0, array, 0, count);
            }
            else if (TypeHelper <T> .IsMemoryObjectType)
            {
                for (int i = 0; i < count; i++)
                {
                    array[i] = (T)(object)MemoryObjectFactory.UnsafeCreate(typeof(T), Memory, address + i * TypeHelper <T> .SizeOf);
                }
            }
            else if (TypeHelper <T> .IsMemoryPointerType)
            {
                var buffer = new byte[count * PointerSize];
                UnsafeReadBytes(address, buffer, 0, buffer.Length);
                var bufferReader = new BufferMemoryReader(buffer, 0, buffer.Length, PointerSize);

                for (int i = 0; i < count; i++)
                {
                    array[i] = (T)(object)MemoryPointerFactory.UnsafeCreate <T>(Memory, bufferReader.ReadMemoryAddress(i * PointerSize));
                }
            }
            else if (TypeHelper <T> .IsMemoryAddressType)
            {
                for (int i = 0; i < count; i++)
                {
                    array[i] = (T)(object)ReadMemoryAddress(address + i * PointerSize);
                }
            }
            else
            {
                throw new NotSupportedException();
            }

            return(array);
        }