2021-11-29 09:05:36 +00: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 10:09:17 +00:00
|
|
|
using System;
|
2021-11-29 09:05:36 +00:00
|
|
|
using System.Collections.Generic;
|
2022-03-24 10:09:17 +00:00
|
|
|
using System.Diagnostics;
|
2022-03-24 13:53:49 +00:00
|
|
|
using System.Linq;
|
2021-11-29 09:05:36 +00:00
|
|
|
using osu.Framework.Extensions;
|
|
|
|
using osu.Framework.IO.Stores;
|
2022-03-22 09:30:54 +00:00
|
|
|
using osu.Game.Database;
|
2021-11-29 09:05:36 +00:00
|
|
|
using osu.Game.Extensions;
|
2022-03-24 10:09:17 +00:00
|
|
|
using Realms;
|
2021-11-29 09:05:36 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
{
|
2022-03-24 10:09:17 +00:00
|
|
|
public class RealmBackedResourceStore<T> : ResourceStore<byte[]>
|
|
|
|
where T : RealmObject, IHasRealmFiles, IHasGuidPrimaryKey
|
2021-11-29 09:05:36 +00:00
|
|
|
{
|
2022-03-24 10:09:17 +00:00
|
|
|
private Lazy<Dictionary<string, string>> fileToStoragePathMapping;
|
|
|
|
|
|
|
|
private readonly Live<T> liveSource;
|
2022-03-24 13:53:49 +00:00
|
|
|
private readonly IDisposable? realmSubscription;
|
2021-11-30 17:27:41 +00:00
|
|
|
|
2022-04-04 20:22:55 +00:00
|
|
|
public RealmBackedResourceStore(Live<T> source, IResourceStore<byte[]> underlyingStore, RealmAccess? realm)
|
2021-11-29 09:05:36 +00:00
|
|
|
: base(underlyingStore)
|
|
|
|
{
|
2022-03-24 10:09:17 +00:00
|
|
|
liveSource = source;
|
2022-03-23 04:36:33 +00:00
|
|
|
|
2022-03-24 10:09:17 +00:00
|
|
|
invalidateCache();
|
|
|
|
Debug.Assert(fileToStoragePathMapping != null);
|
2022-03-24 13:53:49 +00:00
|
|
|
|
2024-09-25 07:45:37 +00:00
|
|
|
Guid id = source.ID;
|
|
|
|
|
|
|
|
realmSubscription = realm?.RegisterForNotifications(r => r.All<T>().Where(s => s.ID == id), skinChanged);
|
2022-03-24 13:53:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
base.Dispose(disposing);
|
|
|
|
realmSubscription?.Dispose();
|
2021-11-30 17:27:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 04:37:42 +00:00
|
|
|
private void skinChanged(IRealmCollection<T> sender, ChangeSet? changes) => invalidateCache();
|
2021-11-29 09:05:36 +00:00
|
|
|
|
|
|
|
protected override IEnumerable<string> GetFilenames(string name)
|
|
|
|
{
|
|
|
|
foreach (string filename in base.GetFilenames(name))
|
|
|
|
{
|
2022-03-24 10:09:17 +00:00
|
|
|
string? path = getPathForFile(filename.ToStandardisedPath());
|
2021-11-29 09:05:36 +00:00
|
|
|
if (path != null)
|
|
|
|
yield return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 10:09:17 +00:00
|
|
|
private string? getPathForFile(string filename)
|
|
|
|
{
|
2022-12-16 09:16:26 +00:00
|
|
|
if (fileToStoragePathMapping.Value.TryGetValue(filename.ToLowerInvariant(), out string? path))
|
2022-03-24 10:09:17 +00:00
|
|
|
return path;
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-04-04 11:28:43 +00:00
|
|
|
private void invalidateCache() => fileToStoragePathMapping = new Lazy<Dictionary<string, string>>(initialiseFileCache);
|
2022-03-24 10:09:17 +00: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 09:05:36 +00:00
|
|
|
|
2022-03-24 10:09:17 +00:00
|
|
|
public override IEnumerable<string> GetAvailableResources() => fileToStoragePathMapping.Value.Keys;
|
2021-11-29 09:05:36 +00:00
|
|
|
}
|
|
|
|
}
|