Ejemplo n.º 1
0
        public bool FileExists(string name, FilePath options, out string filePath)
        {
            string path = options == FilePath.Base ? name : UserPath + name;

            filePath = path + "0";
            if (FileProvider.FileExists(filePath))
            {
                return(true);
            }
            else
            {
                filePath = path + "1";
                if (FileProvider.FileExists(filePath))
                {
                    return(true);
                }
                else
                {
                    filePath = null;
                    return(false);
                }
            }
        }
Ejemplo n.º 2
0
 public static IEnumerable <object[]> GetDynamicArchiveData(string fileName, SearchOption searchOption, params string[] directories)
 {
     foreach (var directory in GetTestDataDirectories(directories))
     {
         if (Directory.Exists(directory))
         {
             foreach (var searchPattern in _archiveFileExtensions.Select(extension => $"*{extension}"))
             {
                 foreach (var archive in Directory.EnumerateFiles(directory, searchPattern, searchOption))
                 {
                     var file = Path.Combine(archive, fileName);
                     if (FileProvider.FileExists(file))
                     {
                         yield return(new[] { file });
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
        internal ChunkHeader ReadChunkHeader(string url)
        {
            if (!FileProvider.FileExists(url))
            {
                HandleAssetNotFound(url);
                return(null);
            }

            using (var stream = FileProvider.OpenStream(url, VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                // File does not exist
                // TODO/Benlitz: Add a log entry for that, it's not expected to happen
                if (stream == null)
                {
                    return(null);
                }

                // Read header
                var streamReader = new BinarySerializationReader(stream);
                return(ChunkHeader.Read(streamReader));
            }
        }
Ejemplo n.º 4
0
        private object DeserializeObject(Queue <DeserializeOperation> serializeOperations, Reference parentReference, string url, Type objType, object obj, ContentManagerLoaderSettings settings)
        {
            // Try to find already loaded object
            Reference reference = FindDeserializedObject(url, objType);

            if (reference != null && reference.Deserialized)
            {
                // Add reference
                bool isRoot = parentReference == null;
                if (isRoot || parentReference.References.Add(reference))
                {
                    IncrementReference(reference, isRoot);
                }

                // Check if need to fully stream resource
                if (!settings.AllowContentStreaming)
                {
                    var streamingManager = services.GetService <IStreamingManager>();
                    streamingManager?.FullyLoadResource(reference.Object);
                }

                return(reference.Object);
            }

            if (!FileProvider.FileExists(url))
            {
                HandleAssetNotFound(url);
                return(null);
            }

            ContentSerializerContext contentSerializerContext;
            object result;

            // Open asset binary stream
            try
            {
                using (var stream = FileProvider.OpenStream(url, VirtualFileMode.Open, VirtualFileAccess.Read))
                {
                    // File does not exist
                    // TODO/Benlitz: Add a log entry for that, it's not expected to happen
                    if (stream == null)
                    {
                        return(null);
                    }

                    Type headerObjType = null;

                    // Read header
                    var streamReader = new BinarySerializationReader(stream);
                    var chunkHeader  = ChunkHeader.Read(streamReader);
                    if (chunkHeader != null)
                    {
                        headerObjType = AssemblyRegistry.GetType(chunkHeader.Type);
                    }

                    // Find serializer
                    var serializer = Serializer.GetSerializer(headerObjType, objType);
                    if (serializer == null)
                    {
                        throw new InvalidOperationException($"Content serializer for {headerObjType}/{objType} could not be found.");
                    }
                    contentSerializerContext = new ContentSerializerContext(url, ArchiveMode.Deserialize, this)
                    {
                        LoadContentReferences = settings.LoadContentReferences,
                        AllowContentStreaming = settings.AllowContentStreaming,
                    };

                    // Read chunk references
                    if (chunkHeader != null && chunkHeader.OffsetToReferences != -1)
                    {
                        // Seek to where references are stored and deserialize them
                        streamReader.NativeStream.Seek(chunkHeader.OffsetToReferences, SeekOrigin.Begin);
                        contentSerializerContext.SerializeReferences(streamReader);
                        streamReader.NativeStream.Seek(chunkHeader.OffsetToObject, SeekOrigin.Begin);
                    }

                    if (reference == null)
                    {
                        // Create Reference
                        reference = new Reference(url, parentReference == null);
                        result    = obj ?? serializer.Construct(contentSerializerContext);
                        SetAssetObject(reference, result);
                    }
                    else
                    {
                        result = reference.Object;
                    }

                    reference.Deserialized = true;

                    PrepareSerializerContext(contentSerializerContext, streamReader.Context);

                    contentSerializerContext.SerializeContent(streamReader, serializer, result);

                    // Add reference
                    parentReference?.References.Add(reference);
                }
            }
            catch (Exception exception)
            {
                throw new ContentManagerException($"Unexpected exception while loading asset [{url}]. Reason: {exception.Message}. Check inner-exception for details.", exception);
            }

            if (settings.LoadContentReferences)
            {
                // Process content references
                // TODO: Should we work at ChunkReference level?
                foreach (var contentReference in contentSerializerContext.ContentReferences)
                {
                    bool shouldBeLoaded = true;

                    //Reference childReference;

                    settings.ContentFilter?.Invoke(contentReference, ref shouldBeLoaded);

                    if (shouldBeLoaded)
                    {
                        serializeOperations.Enqueue(new DeserializeOperation(reference, contentReference.Location, contentReference.Type, contentReference.ObjectValue));
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
 public void TestFileProviderFileExists(string path, bool expected)
 {
     Assert.AreEqual(expected, FileProvider.FileExists(path));
 }
Ejemplo n.º 6
0
        private object DeserializeObject(Queue <DeserializeOperation> serializeOperations, AssetReference parentAssetReference, string url, Type objType, object obj, AssetManagerLoaderSettings settings)
        {
            // Try to find already loaded object
            AssetReference assetReference = FindDeserializedObject(url, objType);

            if (assetReference != null && assetReference.Deserialized)
            {
                // Add reference
                bool isRoot = parentAssetReference == null;
                if (isRoot || parentAssetReference.References.Add(assetReference))
                {
                    IncrementReference(assetReference, isRoot);
                }

                return(assetReference.Object);
            }

            if (!FileProvider.FileExists(url))
            {
                HandleAssetNotFound(url);
                return(null);
            }

            ContentSerializerContext contentSerializerContext;
            object result;

            // Open asset binary stream
            using (var stream = FileProvider.OpenStream(url, VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                // File does not exist
                // TODO/Benlitz: Add a log entry for that, it's not expected to happen
                if (stream == null)
                {
                    return(null);
                }

                Type headerObjType = null;

                // Read header
                var streamReader = new BinarySerializationReader(stream);
                var chunkHeader  = ChunkHeader.Read(streamReader);
                if (chunkHeader != null)
                {
                    headerObjType = Type.GetType(chunkHeader.Type);
                }

                // Find serializer
                var serializer = Serializer.GetSerializer(headerObjType, objType);
                if (serializer == null)
                {
                    throw new InvalidOperationException(string.Format("Content serializer for {0}/{1} could not be found.", headerObjType, objType));
                }
                contentSerializerContext = new ContentSerializerContext(url, ArchiveMode.Deserialize, this)
                {
                    LoadContentReferences = settings.LoadContentReferences
                };

                // Read chunk references
                if (chunkHeader != null && chunkHeader.OffsetToReferences != -1)
                {
                    // Seek to where references are stored and deserialize them
                    streamReader.NativeStream.Seek(chunkHeader.OffsetToReferences, SeekOrigin.Begin);
                    contentSerializerContext.SerializeReferences(streamReader);
                    streamReader.NativeStream.Seek(chunkHeader.OffsetToObject, SeekOrigin.Begin);
                }

                if (assetReference == null)
                {
                    // Create AssetReference
                    assetReference = new AssetReference(url, parentAssetReference == null);
                    contentSerializerContext.AssetReference = assetReference;
                    result = obj ?? serializer.Construct(contentSerializerContext);
                    SetAssetObject(assetReference, result);
                }
                else
                {
                    result = assetReference.Object;
                    contentSerializerContext.AssetReference = assetReference;
                }

                assetReference.Deserialized = true;

                PrepareSerializerContext(contentSerializerContext, streamReader.Context);

                contentSerializerContext.SerializeContent(streamReader, serializer, result);

                // Add reference
                if (parentAssetReference != null)
                {
                    parentAssetReference.References.Add(assetReference);
                }
            }

            if (settings.LoadContentReferences)
            {
                // Process content references
                // TODO: Should we work at ChunkReference level?
                foreach (var contentReference in contentSerializerContext.ContentReferences)
                {
                    bool shouldBeLoaded = true;

                    //AssetReference childReference;

                    if (settings.ContentFilter != null)
                    {
                        settings.ContentFilter(contentReference, ref shouldBeLoaded);
                    }

                    if (shouldBeLoaded)
                    {
                        serializeOperations.Enqueue(new DeserializeOperation(assetReference, contentReference.Location, contentReference.Type, contentReference.ObjectValue));
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 7
0
        internal object DeserializeObject(AssetReference parentAssetReference, out AssetReference assetReference, string url, Type objType, AssetManagerLoaderSettings settings, ConverterContext converterContext = null)
        {
            // Resolve URL
            ObjectId objectId;

            if (!FileProvider.AssetIndexMap.TryGetValue(url, out objectId))
            {
                throw new InvalidOperationException(string.Format("Asset [{0}] not found.", url));
            }

            // Try to find already loaded object
            if (loadedAssetsByUrl.TryGetValue(objectId, out assetReference))
            {
                while (assetReference != null && !objType.GetTypeInfo().IsAssignableFrom(assetReference.Object.GetType().GetTypeInfo()))
                {
                    assetReference = assetReference.Next;
                }

                if (assetReference != null)
                {
                    // Add reference
                    bool isRoot = parentAssetReference == null;
                    if (isRoot || parentAssetReference.References.Add(assetReference))
                    {
                        IncrementReference(assetReference, isRoot);
                    }

                    return(assetReference.Object);
                }
            }

            if (!FileProvider.FileExists(url))
            {
                throw new InvalidOperationException(string.Format("Asset [{0}] not found.", url));
            }

            ContentSerializerContext contentSerializerContext;
            object result;

            // Open asset binary stream
            using (var stream = FileProvider.OpenStream(url, VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                // File does not exist
                // TODO/Benlitz: Add a log entry for that, it's not expected to happen
                if (stream == null)
                {
                    return(null);
                }

                Type headerObjType = null;

                // Read header
                var streamReader = new BinarySerializationReader(stream);
                var chunkHeader  = ChunkHeader.Read(streamReader);
                if (chunkHeader != null)
                {
                    headerObjType = Type.GetType(chunkHeader.Type);
                }

                // Find serializer
                var serializer = Serializer.GetSerializer(headerObjType, objType);
                if (serializer == null)
                {
                    throw new InvalidOperationException(string.Format("Content serializer for {0}/{1} could not be found.", headerObjType, objType));
                }
                contentSerializerContext = new ContentSerializerContext(url, ArchiveMode.Deserialize, this);

                // Read chunk references
                if (chunkHeader != null && chunkHeader.OffsetToReferences != -1)
                {
                    // Seek to where references are stored and deserialize them
                    streamReader.NativeStream.Seek(chunkHeader.OffsetToReferences, SeekOrigin.Begin);
                    contentSerializerContext.SerializeReferences(streamReader);
                    streamReader.NativeStream.Seek(chunkHeader.OffsetToObject, SeekOrigin.Begin);
                }

                // Create AssetReference
                assetReference = new AssetReference(objectId, url, parentAssetReference == null);
                contentSerializerContext.AssetReference = assetReference;

                result = serializer.Construct(contentSerializerContext);

                PrepareSerializerContext(contentSerializerContext, streamReader.Context);
                contentSerializerContext.ConverterContext = converterContext;

                result = contentSerializerContext.SerializeContent(streamReader, serializer, result);

                SetAssetObject(assetReference, result);

                // Add reference
                if (parentAssetReference != null)
                {
                    parentAssetReference.References.Add(assetReference);
                }
            }

            if (settings.LoadContentReferences)
            {
                // Process content references
                // TODO: Should we work at ChunkReference level?
                foreach (var contentReference in contentSerializerContext.ContentReferences)
                {
                    bool shouldBeLoaded = true;

                    AssetReference childReference;

                    if (settings.ContentFilter != null)
                    {
                        settings.ContentFilter(contentReference, ref shouldBeLoaded);
                    }

                    if (shouldBeLoaded)
                    {
                        contentReference.ObjectValue = DeserializeObject(assetReference, out childReference, contentReference.Location, contentReference.Type, settings);
                    }
                }
            }

            return(result);
        }