A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Collections.Generic;
6using System.IO;
7using System.Threading.Tasks;
8using osu.Framework.IO.Stores;
9using SixLabors.ImageSharp;
10using SixLabors.ImageSharp.PixelFormats;
11
12namespace osu.Framework.Graphics.Textures
13{
14 public class TextureLoaderStore : IResourceStore<TextureUpload>
15 {
16 private IResourceStore<byte[]> store { get; }
17
18 public TextureLoaderStore(IResourceStore<byte[]> store)
19 {
20 this.store = store;
21 (store as ResourceStore<byte[]>)?.AddExtension(@"png");
22 (store as ResourceStore<byte[]>)?.AddExtension(@"jpg");
23 }
24
25 public Task<TextureUpload> GetAsync(string name) => Task.Run(() => Get(name));
26
27 public TextureUpload Get(string name)
28 {
29 try
30 {
31 using (var stream = store.GetStream(name))
32 {
33 if (stream != null)
34 return new TextureUpload(ImageFromStream<Rgba32>(stream));
35 }
36 }
37 catch
38 {
39 }
40
41 return null;
42 }
43
44 public Stream GetStream(string name) => store.GetStream(name);
45
46 protected virtual Image<TPixel> ImageFromStream<TPixel>(Stream stream) where TPixel : unmanaged, IPixel<TPixel>
47 => TextureUpload.LoadFromStream<TPixel>(stream);
48
49 public IEnumerable<string> GetAvailableResources() => store.GetAvailableResources();
50
51 #region IDisposable Support
52
53 private bool isDisposed;
54
55 public void Dispose()
56 {
57 Dispose(true);
58 GC.SuppressFinalize(this);
59 }
60
61 protected virtual void Dispose(bool disposing)
62 {
63 if (isDisposed)
64 return;
65
66 store.Dispose();
67
68 isDisposed = true;
69 }
70
71 #endregion
72 }
73}