Пример #1
0
        /// <summary>
        /// Function to find the image associated with the sprite file.
        /// </summary>
        /// <param name="spriteFile">The sprite file to evaluate.</param>
        /// <param name="fileManager">The file manager used to handle content files.</param>
        /// <returns>The file representing the image associated with the sprite.</returns>
        private IContentFile FindImage(IContentFile spriteFile, IContentFileManager fileManager)
        {
            if ((spriteFile.Metadata.DependsOn.Count == 0) ||
                (!spriteFile.Metadata.DependsOn.TryGetValue(CommonEditorContentTypes.ImageType, out string texturePath)))
            {
                return(null);
            }

            IContentFile textureFile = fileManager.GetFile(texturePath);

            if (textureFile == null)
            {
                CommonServices.Log.Print($"[ERROR] Sprite '{spriteFile.Path}' has texture '{texturePath}', but the file was not found on the file system.", LoggingLevel.Verbose);
                return(null);
            }

            string textureFileContentType = textureFile.Metadata.ContentMetadata?.ContentTypeID;

            if (string.IsNullOrWhiteSpace(textureFileContentType))
            {
                CommonServices.Log.Print($"[ERROR] Sprite texture '{texturePath}' was found but has no content type ID.", LoggingLevel.Verbose);
                return(null);
            }

            if ((!textureFile.Metadata.Attributes.TryGetValue(CommonEditorConstants.ContentTypeAttr, out string imageType)) ||
                (!string.Equals(imageType, textureFileContentType, StringComparison.OrdinalIgnoreCase)))
            {
                CommonServices.Log.Print($"[ERROR] Sprite '{spriteFile.Path}' has texture '{texturePath}', but the texture has a content type ID of '{textureFileContentType}', and the sprite requires a content type ID of '{imageType}'.", LoggingLevel.Verbose);
                return(null);
            }

            return(textureFile);
        }
Пример #2
0
        /// <summary>Function to load the sprites used to generate the atlas texture(s).</summary>
        /// <param name="files">The sprite files to load.</param>
        /// <returns>A list of sprites and their associated files.</returns>
        public IReadOnlyDictionary <IContentFile, GorgonSprite> LoadSprites(IEnumerable <IContentFile> files)
        {
            var    result   = new Dictionary <IContentFile, GorgonSprite>();
            Stream stream   = null;
            var    textures = new Dictionary <string, GorgonTexture2DView>(StringComparer.OrdinalIgnoreCase);

            try
            {
                foreach (IContentFile file in files)
                {
                    GorgonTexture2DView texture = null;

                    if (file.Metadata.DependsOn.TryGetValue(CommonEditorContentTypes.ImageType, out string texturePath))
                    {
                        if (!textures.TryGetValue(texturePath, out texture))
                        {
                            texture = LoadSpriteTexture(_fileSystem.GetFile(texturePath));
                            textures[texturePath] = texture;
                        }
                    }

                    stream       = file.OpenRead();
                    result[file] = _defaultSpriteCodec.FromStream(stream, texture);
                    stream.Dispose();
                }
            }
            catch
            {
                // If we loaded in a bunch of textures already, then dump them since we can't do it outside of here.
                foreach (KeyValuePair <IContentFile, GorgonSprite> sprite in result)
                {
                    sprite.Value.Texture?.Dispose();
                }

                throw;
            }
            finally
            {
                stream?.Dispose();
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Function to update the dependencies for the sprite.
        /// </summary>
        /// <param name="fileStream">The stream for the file.</param>
        /// <param name="dependencyList">The list of dependency file paths.</param>
        /// <param name="fileManager">The content file management system.</param>
        private void UpdateDependencies(Stream fileStream, Dictionary <string, string> dependencyList, IContentFileManager fileManager)
        {
            string textureName = _defaultCodec.GetAssociatedTextureName(fileStream);

            if (string.IsNullOrWhiteSpace(textureName))
            {
                return;
            }
            IContentFile textureFile = fileManager.GetFile(textureName);

            // If we lack the texture (e.g. it's been deleted or something), then reset the value from the metadata.
            if (textureFile == null)
            {
                dependencyList.Remove(textureName);
                return;
            }

            dependencyList[CommonEditorContentTypes.ImageType] = textureName;
        }
Пример #4
0
        /// <summary>
        /// Function to save the sprites to the file system.
        /// </summary>
        /// <param name="path">The path to save the sprites into.</param>
        /// <param name="baseFileName">The base file name.</param>
        /// <param name="sprites">The sprites to save.</param>
        /// <param name="textureFile">The texture file associated with the sprites.</param>
        /// <exception cref="IOException">Thrown when the file system is locked by another thread (after 10 seconds).</exception>
        public void SaveSprites(string path, string baseFileName, IEnumerable <GorgonSprite> sprites, IContentFile textureFile)
        {
            int spriteIndex = 1;

            try
            {
                string fileName  = Path.GetFileNameWithoutExtension(baseFileName);
                string extension = Path.GetExtension(baseFileName);

                if ((!string.IsNullOrWhiteSpace(extension)) && (extension[0] != '.'))
                {
                    extension = "." + extension;
                }

                if (!_fileManager.BeginBatch())
                {
                    throw new IOException(Resources.GOREST_ERR_CANNOT_ACCESS_FILESYSTEM_LOCK);
                }

                foreach (GorgonSprite sprite in sprites)
                {
                    string filePath = $"{path}{fileName} ({spriteIndex++}){(string.IsNullOrWhiteSpace(extension) ? string.Empty : extension)}";

                    while (_fileManager.GetFile(filePath) != null)
                    {
                        filePath = $"{path}{fileName} ({spriteIndex++}){(string.IsNullOrWhiteSpace(extension) ? string.Empty : extension)}";
                    }

                    IContentFile outputFile = _fileManager.WriteFile(filePath, stream =>
                    {
                        _defaultCodec.Save(sprite, stream);
                    });
                    textureFile.LinkContent(outputFile);
                }
            }
            finally
            {
                _fileManager.EndBatch();
            }
        }