Ejemplo n.º 1
0
        /// <summary>
        /// Reads bytes from a buffer, validates the expected serialized version, and deserializes them into a new blob asset.
        /// </summary>
        /// <param name="data">A byte stream of the blob data to read.</param>
        /// <param name="version">Expected version number of the blob data.</param>
        /// <param name="result">The resulting BlobAssetReference if the data was read successful.</param>
        /// <returns>A bool if the read was successful or not.</returns>
        public static bool TryRead(byte *data, int version, out BlobAssetReference <T> result)
        {
            var binaryReader  = new MemoryBinaryReader(data);
            var storedVersion = binaryReader.ReadInt();

            if (storedVersion != version)
            {
                result = default;
                return(false);
            }

            result = binaryReader.Read <T>();

            return(true);
        }
        public unsafe World Create()
        {
            World world;

            fixed(byte *ptr = bytes)
            {
                var data = new MemoryBinaryReader(ptr);
                var blob = data.Read <WorldDefinitionData>();

                world = new World(blob.Value.name.ToString());
                for (int i = 0; i < blob.Value.entries.Length; i++)
                {
                    world.CreateSystem(Type.GetType(blob.Value.entries[i].assemblyQualifiedName.ToString()));
                }
            }

            return(world);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads bytes from a buffer, validates the expected serialized version, and deserializes them into a new blob asset.
        /// </summary>
        /// <param name="buffer">Byte array of buffer</param>
        /// <param name="version">Expected version number of the blob data.</param>
        /// <param name="result">The resulting BlobAssetReference if the data was read successful.</param>
        /// <returns>A bool if the read was successful or not.</returns>
        public static bool TryRead(byte[] buffer, int version, out BlobAssetReference <T> result)
        {
            fixed(byte *fixedBuffer = buffer)
            {
                using (var binaryReader = new MemoryBinaryReader(fixedBuffer))
                {
                    var storedVersion = binaryReader.ReadInt();
                    if (storedVersion != version)
                    {
                        result = default;
                        return(false);
                    }

                    result = binaryReader.Read <T>();
                    return(true);
                }
            }
        }
        /// <summary>
        /// Create a clone of a BlobAssetReference
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static unsafe BlobAssetReference <T> Clone <T>(this BlobAssetReference <T> source) where T : struct
        {
            if (source == BlobAssetReference <T> .Null)
            {
                return(BlobAssetReference <T> .Null);
            }

            var writer = new MemoryBinaryWriter();

            writer.Write(source);
            var reader = new MemoryBinaryReader(writer.Data);
            var clone  = reader.Read <T>();

            writer.Dispose();
            reader.Dispose();

            return(clone);
        }
Ejemplo n.º 5
0
        public static unsafe BlobAssetReference <NodeBlob> ToBlob(this TextAsset file)
        {
            var dataPtr = UnsafeUtility.PinGCArrayAndGetDataAddress(file.bytes, out var gcHandle);
            var reader  = new MemoryBinaryReader((byte *)dataPtr);

            try
            {
                var storedVersion = reader.ReadInt();
                if (storedVersion != NodeBlob.VERSION)
                {
                    throw new FormatException("Version is not match.");
                }
                return(reader.Read <NodeBlob>());
            }
            finally
            {
                reader.Dispose();
                UnsafeUtility.ReleaseGCObject(gcHandle);
            }
        }