2021-11-29 17:05:36 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using osu.Framework.Extensions;
|
|
|
|
using osu.Framework.IO.Stores;
|
2022-03-22 17:30:54 +08:00
|
|
|
using osu.Game.Database;
|
2021-11-29 17:05:36 +08:00
|
|
|
using osu.Game.Extensions;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
2022-03-22 17:30:54 +08:00
|
|
|
public class RealmBackedResourceStore : ResourceStore<byte[]>
|
2021-11-29 17:05:36 +08:00
|
|
|
{
|
2021-12-01 01:27:41 +08:00
|
|
|
private readonly Dictionary<string, string> fileToStoragePathMapping = new Dictionary<string, string>();
|
|
|
|
|
2022-03-23 12:36:33 +08:00
|
|
|
public RealmBackedResourceStore(IHasRealmFiles source, IResourceStore<byte[]> underlyingStore, string[] extensions = null)
|
2021-11-29 17:05:36 +08:00
|
|
|
: base(underlyingStore)
|
|
|
|
{
|
2022-03-23 12:36:33 +08:00
|
|
|
// Must be initialised before the file cache.
|
|
|
|
if (extensions != null)
|
|
|
|
{
|
|
|
|
foreach (string extension in extensions)
|
|
|
|
AddExtension(extension);
|
|
|
|
}
|
|
|
|
|
2021-12-01 01:27:41 +08:00
|
|
|
initialiseFileCache(source);
|
|
|
|
}
|
|
|
|
|
2022-03-22 17:30:54 +08:00
|
|
|
private void initialiseFileCache(IHasRealmFiles source)
|
2021-12-01 01:27:41 +08:00
|
|
|
{
|
|
|
|
fileToStoragePathMapping.Clear();
|
|
|
|
foreach (var f in source.Files)
|
|
|
|
fileToStoragePathMapping[f.Filename.ToLowerInvariant()] = f.File.GetStoragePath();
|
2021-11-29 17:05:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override IEnumerable<string> GetFilenames(string name)
|
|
|
|
{
|
|
|
|
foreach (string filename in base.GetFilenames(name))
|
|
|
|
{
|
|
|
|
string path = getPathForFile(filename.ToStandardisedPath());
|
|
|
|
if (path != null)
|
|
|
|
yield return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 01:27:41 +08:00
|
|
|
private string getPathForFile(string filename) =>
|
|
|
|
fileToStoragePathMapping.TryGetValue(filename.ToLower(), out string path) ? path : null;
|
2021-11-29 17:05:36 +08:00
|
|
|
|
2021-12-01 01:27:41 +08:00
|
|
|
public override IEnumerable<string> GetAvailableResources() => fileToStoragePathMapping.Keys;
|
2021-11-29 17:05:36 +08:00
|
|
|
}
|
|
|
|
}
|