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.
|
|
|
|
|
2022-03-24 18:09:17 +08:00
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
using System;
|
2021-11-29 17:05:36 +08:00
|
|
|
using System.Collections.Generic;
|
2022-03-24 18:09:17 +08:00
|
|
|
using System.Diagnostics;
|
2022-03-24 21:53:49 +08:00
|
|
|
using System.Linq;
|
2022-03-24 18:09:17 +08:00
|
|
|
using System.Threading;
|
2021-11-29 17:05:36 +08:00
|
|
|
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;
|
2022-03-24 18:09:17 +08:00
|
|
|
using Realms;
|
2021-11-29 17:05:36 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
2022-03-24 18:09:17 +08:00
|
|
|
public class RealmBackedResourceStore<T> : ResourceStore<byte[]>
|
|
|
|
where T : RealmObject, IHasRealmFiles, IHasGuidPrimaryKey
|
2021-11-29 17:05:36 +08:00
|
|
|
{
|
2022-03-24 18:09:17 +08:00
|
|
|
private Lazy<Dictionary<string, string>> fileToStoragePathMapping;
|
|
|
|
|
|
|
|
private readonly Live<T> liveSource;
|
2022-03-24 21:53:49 +08:00
|
|
|
private readonly IDisposable? realmSubscription;
|
2021-12-01 01:27:41 +08:00
|
|
|
|
2022-04-04 19:27:03 +08:00
|
|
|
public RealmBackedResourceStore(Live<T> source, IResourceStore<byte[]> underlyingStore, RealmAccess realm)
|
2021-11-29 17:05:36 +08:00
|
|
|
: base(underlyingStore)
|
|
|
|
{
|
2022-03-24 18:09:17 +08:00
|
|
|
liveSource = source;
|
2022-03-23 12:36:33 +08:00
|
|
|
|
2022-03-24 18:09:17 +08:00
|
|
|
invalidateCache();
|
|
|
|
Debug.Assert(fileToStoragePathMapping != null);
|
2022-03-24 21:53:49 +08:00
|
|
|
|
2022-04-04 19:27:03 +08:00
|
|
|
realmSubscription = realm.RegisterForNotifications(r => r.All<T>().Where(s => s.ID == source.ID), skinChanged);
|
2022-03-24 21:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
base.Dispose(disposing);
|
|
|
|
realmSubscription?.Dispose();
|
2021-12-01 01:27:41 +08:00
|
|
|
}
|
|
|
|
|
2022-03-24 21:53:49 +08:00
|
|
|
private void skinChanged(IRealmCollection<T> sender, ChangeSet changes, Exception error) => invalidateCache();
|
2021-11-29 17:05:36 +08:00
|
|
|
|
|
|
|
protected override IEnumerable<string> GetFilenames(string name)
|
|
|
|
{
|
|
|
|
foreach (string filename in base.GetFilenames(name))
|
|
|
|
{
|
2022-03-24 18:09:17 +08:00
|
|
|
string? path = getPathForFile(filename.ToStandardisedPath());
|
2021-11-29 17:05:36 +08:00
|
|
|
if (path != null)
|
|
|
|
yield return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 18:09:17 +08:00
|
|
|
private string? getPathForFile(string filename)
|
|
|
|
{
|
|
|
|
if (fileToStoragePathMapping.Value.TryGetValue(filename.ToLowerInvariant(), out string path))
|
|
|
|
return path;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:28:43 +08:00
|
|
|
private void invalidateCache() => fileToStoragePathMapping = new Lazy<Dictionary<string, string>>(initialiseFileCache);
|
2022-03-24 18:09:17 +08:00
|
|
|
|
|
|
|
private Dictionary<string, string> initialiseFileCache() => liveSource.PerformRead(source =>
|
|
|
|
{
|
|
|
|
var dictionary = new Dictionary<string, string>();
|
|
|
|
dictionary.Clear();
|
|
|
|
foreach (var f in source.Files)
|
|
|
|
dictionary[f.Filename.ToLowerInvariant()] = f.File.GetStoragePath();
|
|
|
|
|
|
|
|
return dictionary;
|
|
|
|
});
|
2021-11-29 17:05:36 +08:00
|
|
|
|
2022-03-24 18:09:17 +08:00
|
|
|
public override IEnumerable<string> GetAvailableResources() => fileToStoragePathMapping.Value.Keys;
|
2021-11-29 17:05:36 +08:00
|
|
|
}
|
|
|
|
}
|