// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using osu.Framework.IO.Stores; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; namespace osu.Framework.Graphics.Textures { public class TextureLoaderStore : IResourceStore { private IResourceStore store { get; } public TextureLoaderStore(IResourceStore store) { this.store = store; (store as ResourceStore)?.AddExtension(@"png"); (store as ResourceStore)?.AddExtension(@"jpg"); } public Task GetAsync(string name) => Task.Run(() => Get(name)); public TextureUpload Get(string name) { try { using (var stream = store.GetStream(name)) { if (stream != null) return new TextureUpload(ImageFromStream(stream)); } } catch { } return null; } public Stream GetStream(string name) => store.GetStream(name); protected virtual Image ImageFromStream(Stream stream) where TPixel : unmanaged, IPixel => TextureUpload.LoadFromStream(stream); public IEnumerable GetAvailableResources() => store.GetAvailableResources(); #region IDisposable Support private bool isDisposed; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (isDisposed) return; store.Dispose(); isDisposed = true; } #endregion } }