Exemple #1
0
        public static unsafe Image LoadFromMemory(IntPtr pSource, int size, bool makeACopy, GCHandle? handle)
        {
            var stream = new BinarySerializationReader(new NativeMemoryStream((byte*)pSource, size));

            // Read and check magic code
            var magicCode = stream.ReadUInt32();
            if (magicCode != MagicCode)
                return null;

            // Read header
            var imageDescription = new ImageDescription();
            imageDescriptionSerializer.Serialize(ref imageDescription, ArchiveMode.Deserialize, stream);

            if (makeACopy)
            {
                var buffer = Utilities.AllocateMemory(size);
                Utilities.CopyMemory(buffer, pSource, size);
                pSource = buffer;
                makeACopy = false;
            }

            var image = new Image(imageDescription, pSource, 0, handle, !makeACopy);

            var totalSizeInBytes = stream.ReadInt32();
            if (totalSizeInBytes != image.TotalSizeInBytes)
                throw new InvalidOperationException("Image size is different than expected.");

            // Read image data
            stream.Serialize(image.DataPointer, image.TotalSizeInBytes);

            return image;
        }
        private KeyValuePair<EffectBytecode, EffectBytecodeCacheLoadSource> LoadEffectBytecode(DatabaseFileProvider database, ObjectId bytecodeId)
        {
            KeyValuePair<EffectBytecode, EffectBytecodeCacheLoadSource> bytecodePair;

            if (!bytecodes.TryGetValue(bytecodeId, out bytecodePair))
            {
                if (!bytecodesByPassingStorage.Contains(bytecodeId) && database.ObjectDatabase.Exists(bytecodeId))
                {
                    using (var stream = database.ObjectDatabase.OpenStream(bytecodeId))
                    {
                        var bytecode = EffectBytecode.FromStream(stream);

                        // Try to read an integer that would specify what kind of cache it belongs to (if undefined because of old versions, mark it as dynamic cache)
                        var cacheSource = EffectBytecodeCacheLoadSource.DynamicCache;
                        if (stream.Position < stream.Length)
                        {
                            var binaryReader = new BinarySerializationReader(stream);
                            cacheSource = (EffectBytecodeCacheLoadSource)binaryReader.ReadInt32();
                        }
                        bytecodePair = new KeyValuePair<EffectBytecode, EffectBytecodeCacheLoadSource>(bytecode, cacheSource);
                    }
                }
                if (bytecodePair.Key != null)
                {
                    bytecodes.Add(bytecodeId, bytecodePair);
                }
            }

            // Always check that the bytecode is in sync with hash sources on all platforms
            if (bytecodePair.Key != null && IsBytecodeObsolete(bytecodePair.Key))
            {
                bytecodes.Remove(bytecodeId);
                bytecodePair = new KeyValuePair<EffectBytecode, EffectBytecodeCacheLoadSource>(null, EffectBytecodeCacheLoadSource.JustCompiled);
            }

            return bytecodePair;
        }