diff --git a/osu.Game/Database/RealmContextFactory.cs b/osu.Game/Database/RealmContextFactory.cs
index 2a2c94ee7d..db8bb14b4a 100644
--- a/osu.Game/Database/RealmContextFactory.cs
+++ b/osu.Game/Database/RealmContextFactory.cs
@@ -14,7 +14,6 @@ using osu.Framework.Statistics;
using osu.Game.Configuration;
using osu.Game.Input.Bindings;
using osu.Game.Models;
-using osu.Game.Rulesets;
using Realms;
#nullable enable
@@ -33,7 +32,7 @@ namespace osu.Game.Database
///
public readonly string Filename;
- private readonly RulesetStore? rulesets;
+ private readonly IDatabaseContextFactory? efContextFactory;
///
/// Version history:
@@ -51,8 +50,8 @@ namespace osu.Game.Database
///
private readonly SemaphoreSlim contextCreationLock = new SemaphoreSlim(1);
- private static readonly GlobalStatistic refreshes = GlobalStatistics.Get("Realm", "Dirty Refreshes");
- private static readonly GlobalStatistic contexts_created = GlobalStatistics.Get("Realm", "Contexts (Created)");
+ private static readonly GlobalStatistic refreshes = GlobalStatistics.Get(@"Realm", @"Dirty Refreshes");
+ private static readonly GlobalStatistic contexts_created = GlobalStatistics.Get(@"Realm", @"Contexts (Created)");
private readonly object contextLock = new object();
private Realm? context;
@@ -62,14 +61,14 @@ namespace osu.Game.Database
get
{
if (!ThreadSafety.IsUpdateThread)
- throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
+ throw new InvalidOperationException(@$"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
lock (contextLock)
{
if (context == null)
{
context = CreateContext();
- Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}");
+ Logger.Log(@$"Opened realm ""{context.Config.DatabasePath}"" at version {context.Config.SchemaVersion}");
}
// creating a context will ensure our schema is up-to-date and migrated.
@@ -78,14 +77,20 @@ namespace osu.Game.Database
}
}
- public RealmContextFactory(Storage storage, string filename, RulesetStore? rulesets = null)
+ ///
+ /// Construct a new instance of a realm context factory.
+ ///
+ /// The game storage which will be used to create the realm backing file.
+ /// The filename to use for the realm backing file. A ".realm" extension will be added automatically if not specified.
+ /// An EF factory used only for migration purposes.
+ public RealmContextFactory(Storage storage, string filename, IDatabaseContextFactory? efContextFactory = null)
{
this.storage = storage;
- this.rulesets = rulesets;
+ this.efContextFactory = efContextFactory;
Filename = filename;
- const string realm_extension = ".realm";
+ const string realm_extension = @".realm";
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
Filename += realm_extension;
@@ -253,7 +258,7 @@ namespace osu.Game.Database
var newItem = newSettings.ElementAt(i);
long rulesetId = oldItem.RulesetID;
- string? rulesetName = rulesets?.GetRuleset((int)rulesetId)?.ShortName;
+ string? rulesetName = getRulesetShortNameFromLegacyID(rulesetId);
if (string.IsNullOrEmpty(rulesetName))
migration.NewRealm.Remove(newItem);
@@ -279,8 +284,7 @@ namespace osu.Game.Database
continue;
long rulesetId = oldItem.RulesetID;
-
- string? rulesetName = rulesets?.GetRuleset((int)rulesetId)?.ShortName;
+ string? rulesetName = getRulesetShortNameFromLegacyID(rulesetId);
if (string.IsNullOrEmpty(rulesetName))
migration.NewRealm.Remove(newItem);
@@ -292,6 +296,15 @@ namespace osu.Game.Database
}
}
+ private string? getRulesetShortNameFromLegacyID(long rulesetId)
+ {
+ if (efContextFactory == null)
+ return null;
+
+ using (var efContext = efContextFactory.Get())
+ return efContext.RulesetInfo.First(r => r.ID == rulesetId)?.ShortName;
+ }
+
///
/// Flush any active contexts and block any further writes.
///
@@ -306,7 +319,7 @@ namespace osu.Game.Database
throw new ObjectDisposedException(nameof(RealmContextFactory));
if (!ThreadSafety.IsUpdateThread)
- throw new InvalidOperationException($"{nameof(BlockAllOperations)} must be called from the update thread.");
+ throw new InvalidOperationException(@$"{nameof(BlockAllOperations)} must be called from the update thread.");
Logger.Log(@"Blocking realm operations.", LoggingTarget.Database);
@@ -330,7 +343,7 @@ namespace osu.Game.Database
timeout -= sleep_length;
if (timeout < 0)
- throw new TimeoutException("Took too long to acquire lock");
+ throw new TimeoutException(@"Took too long to acquire lock");
}
}
catch
diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs
index a2dd417491..1890fd7d24 100644
--- a/osu.Game/OsuGameBase.cs
+++ b/osu.Game/OsuGameBase.cs
@@ -192,7 +192,7 @@ namespace osu.Game
dependencies.Cache(RulesetStore = new RulesetStore(contextFactory, Storage));
- dependencies.Cache(realmFactory = new RealmContextFactory(Storage, "client", RulesetStore));
+ dependencies.Cache(realmFactory = new RealmContextFactory(Storage, "client", contextFactory));
dependencies.CacheAs(Storage);