示例#1
0
        public static unsafe string LoadString(DataStreamPtr reader)
        {
            byte[] bytes = new byte[1];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 1);
            }

            byte b = bytes[0];

            bytes = new byte[3];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 3);
            }

            bytes = new byte[b];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, b);
            }

            string str = Encoding.UTF8.GetString(bytes);

            return(str);
        }
示例#2
0
        public static unsafe string LoadStringMaybe(DataStreamPtr reader, string ifnot)
        {
            byte[] bytes = new byte[1];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 1);
            }

            byte b = bytes[0];

            bytes = new byte[3];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 3);
            }

            if (b < 99 && b > 0)
            {
                bytes = new byte[b];
                fixed(byte *buff = bytes)
                {
                    reader.Read(buff, b);
                }

                string str = Encoding.UTF8.GetString(bytes);
                return(str);
            }
            else
            {
                uint pos = reader.Tell();
                pos = pos - 4;
                reader.Seek(pos);
                return(ifnot);
            }
        }
示例#3
0
        public static unsafe uint LoadUInt32(DataStreamPtr reader)
        {
            byte[] bytes = new byte[4];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 4);
            }

            return(BitConverter.ToUInt32(bytes, 0));
        }
示例#4
0
        public static unsafe byte LoadByte(DataStreamPtr reader)
        {
            byte[] bytes = new byte[1];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 1);
            }

            return(bytes[0]);
        }
示例#5
0
        public static unsafe float LoadFloat(DataStreamPtr reader)
        {
            byte[] bytes = new byte[4];
            fixed(byte *buff = bytes)
            {
                reader.Read(buff, 4);
            }

            return(BitConverter.ToSingle(bytes, 0));
        }
示例#6
0
 public static MemoryStream DataPtrToStream(DataStreamPtr ptr)
 {
     if (ptr.Size() != 0)
     {
         byte[] buffer = new byte[ptr.Size()];
         unsafe
         {
             fixed(byte *bufferPtr = &buffer[0])
             {
                 ptr.Read(bufferPtr, (uint)buffer.Length);
             }
         }
         MemoryStream memoryStream = new MemoryStream(buffer);
         return(memoryStream);
     }
     return(null);
 }
示例#7
0
    //See here: http://www.ogre3d.org/addonforums/viewtopic.php?f=8&t=29287&p=98241#p98241
    public static MemoryStream DataPtrToStream(DataStreamPtr dataPtr)
    {
        if (dataPtr.Size() != 0)
        {
            byte[] buffer = new byte[dataPtr.Size()];

            unsafe
            {
                //Get the pointer to the first element of our buffer of bytes (in C++, can just use 'buffer')
                fixed(byte *bufferPtr = &buffer[0])
                {
                    //Read buffer.Length amount of data into bufferPtr
                    dataPtr.Read(bufferPtr, (uint)buffer.Length);
                }
            }

            MemoryStream stream = new MemoryStream(buffer);

            return(stream);
        }
        return(null);
    }
示例#8
0
        /************************************************************************/
        /* read a resource completely as a byte array                           */
        /************************************************************************/
        public byte[] ReadResource(string _groupName, string _fileName)
        {
            // if the file does not exist there is no way to load it
            if (!CheckResourceExists(_groupName, _fileName))
            {
                return(null);
            }

            // open resource for reading
            DataStreamPtr streamPtr  = ResourceGroupManager.Singleton.OpenResource(_fileName, _groupName);
            uint          length     = streamPtr.Size();
            uint          readLength = 0;

            // create buffer to load resource into
            byte[] buffer = new byte[(int)length];
            if (length != 0)
            {
                unsafe
                {
                    fixed(byte *bufferPtr = &buffer[0])
                    {
                        // read resource into buffer
                        readLength = streamPtr.Read(bufferPtr, length);
                    }
                }
            }

            // if reading the resource failed, just get rid of the buffer
            if (readLength != length)
            {
                buffer = null;
            }

            // close the resource stream
            streamPtr.Close();

            // return buffer of loaded resource or null if failed
            return(buffer);
        }