1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 06:52:55 +08:00

Merge pull request #7228 from peppy/fix-skin-crash

Fix crashes on custom skins due to extension-less file lookups
This commit is contained in:
Dan Balasescu 2019-12-16 15:34:29 +09:00 committed by GitHub
commit c3c0356963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,77 +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;
bool hasExtension = filename.Contains('.');
var file = source.Files.Find(f =>
string.Equals(hasExtension ? f.Filename : Path.ChangeExtension(f.Filename, null), 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);
}
}