/// <summary>Function to determine if there are any existing files that match the texture names or sprite names.</summary> /// <param name="atlas">The texture atlas to evaluate.</param> /// <returns> /// <b>true</b> if there are any sprites or textures with the same name in the output area, or <b>false</b> if not.</returns> public bool HasExistingFiles(GorgonTextureAtlas atlas) { string directory = Path.GetDirectoryName(atlas.Textures[0].Texture.Name).FormatDirectory('/'); if (!_fileSystem.DirectoryExists(directory)) { return(false); } foreach (GorgonTexture2DView texture in atlas.Textures) { if (_fileSystem.GetFile(texture.Texture.Name) != null) { return(true); } } return(false); }
/// <summary>Function to save the atlas data.</summary> /// <param name="atlas">The atlas data to save.</param> public void SaveAtlas(IReadOnlyDictionary <IContentFile, GorgonSprite> spriteFiles, GorgonTextureAtlas atlas) { IGorgonImage image = null; string directory = Path.GetDirectoryName(atlas.Textures[0].Texture.Name).FormatDirectory('/'); Stream outStream = null; try { _fileSystem.BeginBatch(); if (!_fileSystem.DirectoryExists(directory)) { _fileSystem.CreateDirectory(directory); } void WriteImageFile(Stream stream) => _defaultImageCodec.SaveToStream(image, stream); // Check the files to ensure they're not open for editing. foreach (GorgonTexture2DView texture in atlas.Textures) { IContentFile textureFile = _fileSystem.GetFile(texture.Texture.Name); if ((textureFile != null) && (textureFile.IsOpen)) { throw new IOException(string.Format(Resources.GORTAG_ERR_IMAGE_OPEN, textureFile.Path)); } } foreach ((GorgonSprite original, GorgonSprite sprite) in atlas.Sprites) { IContentFile oldFile = spriteFiles.FirstOrDefault(item => item.Value == original).Key; if ((oldFile != null) && (oldFile.IsOpen)) { throw new IOException(string.Format(Resources.GORTAG_ERR_IMAGE_OPEN, oldFile.Path)); } } // Write textures. foreach (GorgonTexture2DView texture in atlas.Textures) { image = texture.Texture.ToImage(); _fileSystem.WriteFile(texture.Texture.Name, WriteImageFile); image.Dispose(); } // Write out the updated sprites. foreach ((GorgonSprite original, GorgonSprite sprite) in atlas.Sprites) { IContentFile oldTextureFile = null; IContentFile textureFile = _fileSystem.GetFile(sprite.Texture.Texture.Name); IContentFile oldFile = spriteFiles.FirstOrDefault(item => item.Value == original).Key; if (oldFile.Metadata.DependsOn.TryGetValue(CommonEditorContentTypes.ImageType, out string oldTexturePath)) { oldTextureFile = _fileSystem.GetFile(oldTexturePath); } outStream = oldFile.OpenWrite(); _defaultSpriteCodec.Save(sprite, outStream); textureFile.LinkContent(oldFile); oldTextureFile?.UnlinkContent(oldFile); oldFile.RefreshMetadata(); oldFile.Refresh(); outStream.Dispose(); } } finally { outStream?.Dispose(); image?.Dispose(); _fileSystem.EndBatch(); } }