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
|
|
|
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;
|
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-05 04:22:55 +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
|
|
|
|
2024-09-26 15:48:42 +08:00
|
|
|
// Required local for iOS. Will cause runtime crash if inlined.
|
2024-09-25 15:45:37 +08:00
|
|
|
Guid id = source.ID;
|
|
|
|
|
|
|
|
realmSubscription = realm?.RegisterForNotifications(r => r.All<T>().Where(s => s.ID == 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
|
|
|
}
|
|
|
|
|
2023-07-06 12:37:42 +08:00
|
|
|
private void skinChanged(IRealmCollection<T> sender, ChangeSet? changes) => 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)
|
|
|
|
{
|
2022-12-16 17:16:26 +08:00
|
|
|
if (fileToStoragePathMapping.Value.TryGetValue(filename.ToLowerInvariant(), out string? path))
|
2022-03-24 18:09:17 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|