1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 07:42:57 +08:00

Simplify LegacySkinResourceStore by deriving from ResourceStore

This commit is contained in:
Dean Herbert 2019-12-16 14:01:08 +09:00
parent 9062fe1935
commit befb78f83b

View File

@ -3,75 +3,39 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.IO.Stores;
using osu.Game.Database;
namespace osu.Game.Skinning
{
public class LegacySkinResourceStore<T> : IResourceStore<byte[]>
public class LegacySkinResourceStore<T> : ResourceStore<byte[]>
where T : INamedFileInfo
{
private readonly IHasFiles<T> source;
private readonly IResourceStore<byte[]> underlyingStore;
private string getPathForFile(string filename)
{
if (source.Files == null)
return null;
var file = source.Files.Find(f =>
string.Equals(f.Filename, filename, StringComparison.InvariantCultureIgnoreCase));
return file?.FileInfo.StoragePath;
}
public LegacySkinResourceStore(IHasFiles<T> source, IResourceStore<byte[]> underlyingStore)
: base(underlyingStore)
{
this.source = source;
this.underlyingStore = underlyingStore;
}
public Stream GetStream(string name)
protected override IEnumerable<string> GetFilenames(string name)
{
string path = getPathForFile(name);
return path == null ? null : underlyingStore.GetStream(path);
}
if (source.Files == null)
yield break;
public IEnumerable<string> GetAvailableResources() => source.Files.Select(f => f.Filename);
byte[] IResourceStore<byte[]>.Get(string name) => GetAsync(name).Result;
public Task<byte[]> GetAsync(string name)
{
string path = getPathForFile(name);
return path == null ? Task.FromResult<byte[]>(null) : underlyingStore.GetAsync(path);
}
#region IDisposable Support
private bool isDisposed;
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
foreach (var filename in base.GetFilenames(name))
{
isDisposed = true;
var path = getPathForFile(filename);
if (path != null)
yield return path;
}
}
~LegacySkinResourceStore()
{
Dispose(false);
}
private string getPathForFile(string filename) =>
source.Files.Find(f => string.Equals(f.Filename, filename, StringComparison.InvariantCultureIgnoreCase))?.FileInfo.StoragePath;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
public override IEnumerable<string> GetAvailableResources() => source.Files.Select(f => f.Filename);
}
}