From 505fede44d93ff68c5ad150d1641b25143bef701 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Nov 2021 16:27:28 +0900 Subject: [PATCH 1/2] Pass the full EF context rather than a legacy `RulesetStore` --- osu.Game/Database/RealmContextFactory.cs | 18 +++++++++++++----- osu.Game/OsuGameBase.cs | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/osu.Game/Database/RealmContextFactory.cs b/osu.Game/Database/RealmContextFactory.cs index 70ea24b581..f04492d0e8 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: @@ -77,10 +76,10 @@ namespace osu.Game.Database } } - public RealmContextFactory(Storage storage, string filename, RulesetStore? rulesets = null) + public RealmContextFactory(Storage storage, string filename, IDatabaseContextFactory? efContextFactory = null) { this.storage = storage; - this.rulesets = rulesets; + this.efContextFactory = efContextFactory; Filename = filename; @@ -252,7 +251,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); @@ -264,6 +263,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. /// 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); From b1b67238268bed5945aa1ae90d21dac16629114a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 23 Nov 2021 17:47:43 +0900 Subject: [PATCH 2/2] Add xmldoc and verbatim string markers --- osu.Game/Database/RealmContextFactory.cs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/osu.Game/Database/RealmContextFactory.cs b/osu.Game/Database/RealmContextFactory.cs index f04492d0e8..3949494766 100644 --- a/osu.Game/Database/RealmContextFactory.cs +++ b/osu.Game/Database/RealmContextFactory.cs @@ -49,8 +49,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; @@ -60,14 +60,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. @@ -76,6 +76,12 @@ namespace osu.Game.Database } } + /// + /// 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; @@ -83,7 +89,7 @@ namespace osu.Game.Database Filename = filename; - const string realm_extension = ".realm"; + const string realm_extension = @".realm"; if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal)) Filename += realm_extension; @@ -286,7 +292,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); @@ -310,7 +316,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