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;
|
|
|
|
using System.Collections.Generic;
|
2021-12-01 12:50:19 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
using osu.Framework.Development;
|
2021-11-29 17:05:36 +08:00
|
|
|
using osu.Framework.Extensions;
|
|
|
|
using osu.Framework.IO.Stores;
|
|
|
|
using osu.Game.Extensions;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
|
|
|
public class LegacyDatabasedSkinResourceStore : ResourceStore<byte[]>
|
|
|
|
{
|
2021-12-01 01:27:41 +08:00
|
|
|
private readonly Dictionary<string, string> fileToStoragePathMapping = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
private readonly IDisposable subscription;
|
2021-11-29 17:05:36 +08:00
|
|
|
|
|
|
|
public LegacyDatabasedSkinResourceStore(SkinInfo source, IResourceStore<byte[]> underlyingStore)
|
|
|
|
: base(underlyingStore)
|
|
|
|
{
|
2021-12-01 12:50:19 +08:00
|
|
|
// Subscribing to non-managed instances doesn't work.
|
|
|
|
// In this usage, the skin may be non-managed in tests.
|
|
|
|
if (source.IsManaged)
|
2021-12-01 01:27:41 +08:00
|
|
|
{
|
2021-12-01 12:50:19 +08:00
|
|
|
// Subscriptions can only work on the main thread.
|
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
|
|
|
subscription = source.Files
|
|
|
|
.AsRealmCollection().SubscribeForNotifications((sender, changes, error) =>
|
|
|
|
{
|
|
|
|
if (changes == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If a large number of changes are made on skin files, this may be better suited to being cleared here
|
|
|
|
// and reinitialised on next usage.
|
|
|
|
initialiseFileCache(source);
|
|
|
|
});
|
|
|
|
}
|
2021-12-01 01:27:41 +08:00
|
|
|
|
|
|
|
initialiseFileCache(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
~LegacyDatabasedSkinResourceStore()
|
|
|
|
{
|
|
|
|
Dispose(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initialiseFileCache(SkinInfo source)
|
|
|
|
{
|
|
|
|
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
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
base.Dispose(disposing);
|
2021-11-29 17:05:36 +08:00
|
|
|
|
2021-12-01 01:27:41 +08:00
|
|
|
subscription?.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|