1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 22:33:05 +08:00

Merge pull request #16890 from peppy/beatmap-decoder-ruleset-store

Fix `LegacyBeatmapDecoder` not populating correct rulesets
This commit is contained in:
Dan Balasescu 2022-02-25 19:03:43 +09:00 committed by GitHub
commit 3a03833912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 242 additions and 165 deletions

View File

@ -77,10 +77,9 @@ namespace osu.Desktop.LegacyIpc
case LegacyIpcDifficultyCalculationRequest req: case LegacyIpcDifficultyCalculationRequest req:
try try
{ {
var ruleset = getLegacyRulesetFromID(req.RulesetId); WorkingBeatmap beatmap = new FlatFileWorkingBeatmap(req.BeatmapFile);
var ruleset = beatmap.BeatmapInfo.Ruleset.CreateInstance();
Mod[] mods = ruleset.ConvertFromLegacyMods((LegacyMods)req.Mods).ToArray(); Mod[] mods = ruleset.ConvertFromLegacyMods((LegacyMods)req.Mods).ToArray();
WorkingBeatmap beatmap = new FlatFileWorkingBeatmap(req.BeatmapFile, _ => ruleset);
return new LegacyIpcDifficultyCalculationResponse return new LegacyIpcDifficultyCalculationResponse
{ {

View File

@ -41,7 +41,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using (var importer = new BeatmapModelManager(realm, storage)) using (var importer = new BeatmapModelManager(realm, storage))
using (new RulesetStore(realm, storage)) using (new RealmRulesetStore(realm, storage))
{ {
Live<BeatmapSetInfo>? beatmapSet; Live<BeatmapSetInfo>? beatmapSet;
@ -85,7 +85,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using (var importer = new BeatmapModelManager(realm, storage)) using (var importer = new BeatmapModelManager(realm, storage))
using (new RulesetStore(realm, storage)) using (new RealmRulesetStore(realm, storage))
{ {
Live<BeatmapSetInfo>? beatmapSet; Live<BeatmapSetInfo>? beatmapSet;
@ -142,7 +142,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using (var importer = new BeatmapModelManager(realm, storage)) using (var importer = new BeatmapModelManager(realm, storage))
using (new RulesetStore(realm, storage)) using (new RealmRulesetStore(realm, storage))
{ {
Live<BeatmapSetInfo>? imported; Live<BeatmapSetInfo>? imported;
@ -171,7 +171,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
await LoadOszIntoStore(importer, realm.Realm); await LoadOszIntoStore(importer, realm.Realm);
}); });
@ -183,7 +183,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -201,7 +201,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -215,7 +215,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? tempPath = TestResources.GetTestBeatmapForImport(); string? tempPath = TestResources.GetTestBeatmapForImport();
@ -245,7 +245,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
var importedSecondTime = await LoadOszIntoStore(importer, realm.Realm); var importedSecondTime = await LoadOszIntoStore(importer, realm.Realm);
@ -265,7 +265,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -314,7 +314,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -366,7 +366,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -414,7 +414,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -463,7 +463,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -496,7 +496,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var progressNotification = new ImportProgressNotification(); var progressNotification = new ImportProgressNotification();
@ -532,7 +532,7 @@ namespace osu.Game.Tests.Database
}; };
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -582,7 +582,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -606,7 +606,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realmFactory, storage) => RunTestWithRealmAsync(async (realmFactory, storage) =>
{ {
using var importer = new BeatmapModelManager(realmFactory, storage); using var importer = new BeatmapModelManager(realmFactory, storage);
using var store = new RulesetStore(realmFactory, storage); using var store = new RealmRulesetStore(realmFactory, storage);
var imported = await LoadOszIntoStore(importer, realmFactory.Realm); var imported = await LoadOszIntoStore(importer, realmFactory.Realm);
@ -638,7 +638,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new NonOptimisedBeatmapImporter(realm, storage); using var importer = new NonOptimisedBeatmapImporter(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -662,7 +662,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var imported = await LoadOszIntoStore(importer, realm.Realm); var imported = await LoadOszIntoStore(importer, realm.Realm);
@ -688,7 +688,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealm((realm, storage) => RunTestWithRealm((realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
var metadata = new BeatmapMetadata var metadata = new BeatmapMetadata
{ {
@ -734,7 +734,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
using (File.OpenRead(temp)) using (File.OpenRead(temp))
@ -751,7 +751,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -787,7 +787,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -829,7 +829,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
@ -880,7 +880,7 @@ namespace osu.Game.Tests.Database
RunTestWithRealmAsync(async (realm, storage) => RunTestWithRealmAsync(async (realm, storage) =>
{ {
using var importer = new BeatmapModelManager(realm, storage); using var importer = new BeatmapModelManager(realm, storage);
using var store = new RulesetStore(realm, storage); using var store = new RealmRulesetStore(realm, storage);
string? temp = TestResources.GetTestBeatmapForImport(); string? temp = TestResources.GetTestBeatmapForImport();
await importer.Import(temp); await importer.Import(temp);

View File

@ -14,7 +14,7 @@ namespace osu.Game.Tests.Database
{ {
RunTestWithRealm((realm, storage) => RunTestWithRealm((realm, storage) =>
{ {
var rulesets = new RulesetStore(realm, storage); var rulesets = new RealmRulesetStore(realm, storage);
Assert.AreEqual(4, rulesets.AvailableRulesets.Count()); Assert.AreEqual(4, rulesets.AvailableRulesets.Count());
Assert.AreEqual(4, realm.Realm.All<RulesetInfo>().Count()); Assert.AreEqual(4, realm.Realm.All<RulesetInfo>().Count());
@ -26,8 +26,8 @@ namespace osu.Game.Tests.Database
{ {
RunTestWithRealm((realm, storage) => RunTestWithRealm((realm, storage) =>
{ {
var rulesets = new RulesetStore(realm, storage); var rulesets = new RealmRulesetStore(realm, storage);
var rulesets2 = new RulesetStore(realm, storage); var rulesets2 = new RealmRulesetStore(realm, storage);
Assert.AreEqual(4, rulesets.AvailableRulesets.Count()); Assert.AreEqual(4, rulesets.AvailableRulesets.Count());
Assert.AreEqual(4, rulesets2.AvailableRulesets.Count()); Assert.AreEqual(4, rulesets2.AvailableRulesets.Count());
@ -42,7 +42,7 @@ namespace osu.Game.Tests.Database
{ {
RunTestWithRealm((realm, storage) => RunTestWithRealm((realm, storage) =>
{ {
var rulesets = new RulesetStore(realm, storage); var rulesets = new RealmRulesetStore(realm, storage);
Assert.IsFalse(rulesets.AvailableRulesets.First().IsManaged); Assert.IsFalse(rulesets.AvailableRulesets.First().IsManaged);
Assert.IsFalse(rulesets.GetRuleset(0)?.IsManaged); Assert.IsFalse(rulesets.GetRuleset(0)?.IsManaged);

View File

@ -49,7 +49,7 @@ namespace osu.Game.Tests.Online
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(AudioManager audio, GameHost host) private void load(AudioManager audio, GameHost host)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default)); Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default));
Dependencies.CacheAs<BeatmapModelDownloader>(beatmapDownloader = new TestBeatmapModelDownloader(beatmaps, API)); Dependencies.CacheAs<BeatmapModelDownloader>(beatmapDownloader = new TestBeatmapModelDownloader(beatmaps, API));
} }

View File

@ -47,7 +47,7 @@ namespace osu.Game.Tests.Visual.Background
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(new OsuConfigManager(LocalStorage)); Dependencies.Cache(new OsuConfigManager(LocalStorage));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -36,7 +36,7 @@ namespace osu.Game.Tests.Visual.Collections
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host) private void load(GameHost host)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesets, null, Audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesets, null, Audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -43,7 +43,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -40,7 +40,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -58,7 +58,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -42,7 +42,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -46,7 +46,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -33,7 +33,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -34,7 +34,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -40,7 +40,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -41,7 +41,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -33,7 +33,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -38,7 +38,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -39,7 +39,7 @@ namespace osu.Game.Tests.Visual.Playlists
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
} }

View File

@ -43,7 +43,7 @@ namespace osu.Game.Tests.Visual.SongSelect
{ {
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.Cache(rulesetStore = new RulesetStore(Realm)); dependencies.Cache(rulesetStore = new RealmRulesetStore(Realm));
dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesetStore, null, dependencies.Get<AudioManager>(), Resources, dependencies.Get<GameHost>(), Beatmap.Default)); dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesetStore, null, dependencies.Get<AudioManager>(), Resources, dependencies.Get<GameHost>(), Beatmap.Default));
dependencies.Cache(scoreManager = new ScoreManager(rulesetStore, () => beatmapManager, LocalStorage, Realm, Scheduler)); dependencies.Cache(scoreManager = new ScoreManager(rulesetStore, () => beatmapManager, LocalStorage, Realm, Scheduler));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -36,7 +36,7 @@ namespace osu.Game.Tests.Visual.SongSelect
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host) private void load(GameHost host)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesets, null, Audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesets, null, Audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -47,7 +47,7 @@ namespace osu.Game.Tests.Visual.SongSelect
{ {
// These DI caches are required to ensure for interactive runs this test scene doesn't nuke all user beatmaps in the local install. // These DI caches are required to ensure for interactive runs this test scene doesn't nuke all user beatmaps in the local install.
// At a point we have isolated interactive test runs enough, this can likely be removed. // At a point we have isolated interactive test runs enough, this can likely be removed.
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, defaultBeatmap = Beatmap.Default)); Dependencies.Cache(manager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, defaultBeatmap = Beatmap.Default));

View File

@ -28,7 +28,7 @@ namespace osu.Game.Tests.Visual.SongSelect
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio) private void load(GameHost host, AudioManager audio)
{ {
Dependencies.Cache(rulesets = new RulesetStore(Realm)); Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default)); Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
Dependencies.Cache(scoreManager = new ScoreManager(rulesets, () => beatmapManager, LocalStorage, Realm, Scheduler)); Dependencies.Cache(scoreManager = new ScoreManager(rulesets, () => beatmapManager, LocalStorage, Realm, Scheduler));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -87,7 +87,7 @@ namespace osu.Game.Tests.Visual.UserInterface
{ {
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent)); var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.Cache(rulesetStore = new RulesetStore(Realm)); dependencies.Cache(rulesetStore = new RealmRulesetStore(Realm));
dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesetStore, null, dependencies.Get<AudioManager>(), Resources, dependencies.Get<GameHost>(), Beatmap.Default)); dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, rulesetStore, null, dependencies.Get<AudioManager>(), Resources, dependencies.Get<GameHost>(), Beatmap.Default));
dependencies.Cache(scoreManager = new ScoreManager(dependencies.Get<RulesetStore>(), () => beatmapManager, LocalStorage, Realm, Scheduler)); dependencies.Cache(scoreManager = new ScoreManager(dependencies.Get<RulesetStore>(), () => beatmapManager, LocalStorage, Realm, Scheduler));
Dependencies.Cache(Realm); Dependencies.Cache(Realm);

View File

@ -152,18 +152,6 @@ namespace osu.Game.Beatmaps
#region Compatibility properties #region Compatibility properties
[Ignored]
public int RulesetID
{
set
{
if (!string.IsNullOrEmpty(Ruleset.InstantiationInfo))
throw new InvalidOperationException($"Cannot set a {nameof(RulesetID)} when {nameof(Ruleset)} is already set to an actual ruleset.");
Ruleset.OnlineID = value;
}
}
[Ignored] [Ignored]
[Obsolete("Use BeatmapInfo.Difficulty instead.")] // can be removed 20220719 [Obsolete("Use BeatmapInfo.Difficulty instead.")] // can be removed 20220719
public BeatmapDifficulty BaseDifficulty public BeatmapDifficulty BaseDifficulty

View File

@ -7,7 +7,6 @@ using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps.Formats; using osu.Game.Beatmaps.Formats;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Skinning; using osu.Game.Skinning;
namespace osu.Game.Beatmaps namespace osu.Game.Beatmaps
@ -20,18 +19,16 @@ namespace osu.Game.Beatmaps
{ {
private readonly Beatmap beatmap; private readonly Beatmap beatmap;
public FlatFileWorkingBeatmap(string file, Func<int, Ruleset> rulesetProvider, int? beatmapId = null) public FlatFileWorkingBeatmap(string file, int? beatmapId = null)
: this(readFromFile(file), rulesetProvider, beatmapId) : this(readFromFile(file), beatmapId)
{ {
} }
private FlatFileWorkingBeatmap(Beatmap beatmap, Func<int, Ruleset> rulesetProvider, int? beatmapId = null) private FlatFileWorkingBeatmap(Beatmap beatmap, int? beatmapId = null)
: base(beatmap.BeatmapInfo, null) : base(beatmap.BeatmapInfo, null)
{ {
this.beatmap = beatmap; this.beatmap = beatmap;
beatmap.BeatmapInfo.Ruleset = rulesetProvider(beatmap.BeatmapInfo.Ruleset.OnlineID).RulesetInfo;
if (beatmapId.HasValue) if (beatmapId.HasValue)
beatmap.BeatmapInfo.OnlineID = beatmapId.Value; beatmap.BeatmapInfo.OnlineID = beatmapId.Value;
} }

View File

@ -5,7 +5,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.Rulesets;
namespace osu.Game.Beatmaps.Formats namespace osu.Game.Beatmaps.Formats
{ {
@ -37,6 +39,15 @@ namespace osu.Game.Beatmaps.Formats
LegacyStoryboardDecoder.Register(); LegacyStoryboardDecoder.Register();
} }
/// <summary>
/// Register dependencies for use with static decoder classes.
/// </summary>
/// <param name="rulesets">A store containing all available rulesets (used by <see cref="LegacyBeatmapDecoder"/>).</param>
public static void RegisterDependencies([NotNull] RulesetStore rulesets)
{
LegacyBeatmapDecoder.RulesetStore = rulesets ?? throw new ArgumentNullException(nameof(rulesets));
}
/// <summary> /// <summary>
/// Retrieves a <see cref="Decoder"/> to parse a <see cref="Beatmap"/>. /// Retrieves a <see cref="Decoder"/> to parse a <see cref="Beatmap"/>.
/// </summary> /// </summary>

View File

@ -7,16 +7,20 @@ using System.IO;
using System.Linq; using System.Linq;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Extensions.EnumExtensions; using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Logging;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Beatmaps.Legacy; using osu.Game.Beatmaps.Legacy;
using osu.Game.Beatmaps.Timing; using osu.Game.Beatmaps.Timing;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Objects.Legacy; using osu.Game.Rulesets.Objects.Legacy;
namespace osu.Game.Beatmaps.Formats namespace osu.Game.Beatmaps.Formats
{ {
public class LegacyBeatmapDecoder : LegacyDecoder<Beatmap> public class LegacyBeatmapDecoder : LegacyDecoder<Beatmap>
{ {
internal static RulesetStore RulesetStore;
private Beatmap beatmap; private Beatmap beatmap;
private ConvertHitObjectParser parser; private ConvertHitObjectParser parser;
@ -40,6 +44,12 @@ namespace osu.Game.Beatmaps.Formats
public LegacyBeatmapDecoder(int version = LATEST_VERSION) public LegacyBeatmapDecoder(int version = LATEST_VERSION)
: base(version) : base(version)
{ {
if (RulesetStore == null)
{
Logger.Log($"A {nameof(RulesetStore)} was not provided via {nameof(Decoder)}.{nameof(RegisterDependencies)}; falling back to default {nameof(AssemblyRulesetStore)}.");
RulesetStore = new AssemblyRulesetStore();
}
// BeatmapVersion 4 and lower had an incorrect offset (stable has this set as 24ms off) // BeatmapVersion 4 and lower had an incorrect offset (stable has this set as 24ms off)
offset = FormatVersion < 5 ? 24 : 0; offset = FormatVersion < 5 ? 24 : 0;
} }
@ -158,7 +168,7 @@ namespace osu.Game.Beatmaps.Formats
case @"Mode": case @"Mode":
int rulesetID = Parsing.ParseInt(pair.Value); int rulesetID = Parsing.ParseInt(pair.Value);
beatmap.BeatmapInfo.RulesetID = rulesetID; beatmap.BeatmapInfo.Ruleset = RulesetStore.GetRuleset(rulesetID) ?? throw new ArgumentException("Ruleset is not available locally.");
switch (rulesetID) switch (rulesetID)
{ {

View File

@ -22,6 +22,7 @@ using osu.Framework.Logging;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Audio; using osu.Game.Audio;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Graphics; using osu.Game.Graphics;
@ -111,7 +112,7 @@ namespace osu.Game
protected SkinManager SkinManager { get; private set; } protected SkinManager SkinManager { get; private set; }
protected RulesetStore RulesetStore { get; private set; } protected RealmRulesetStore RulesetStore { get; private set; }
protected RealmKeyBindingStore KeyBindingStore { get; private set; } protected RealmKeyBindingStore KeyBindingStore { get; private set; }
@ -202,9 +203,11 @@ namespace osu.Game
dependencies.Cache(realm = new RealmAccess(Storage, "client", EFContextFactory)); dependencies.Cache(realm = new RealmAccess(Storage, "client", EFContextFactory));
dependencies.Cache(RulesetStore = new RulesetStore(realm, Storage)); dependencies.CacheAs<RulesetStore>(RulesetStore = new RealmRulesetStore(realm, Storage));
dependencies.CacheAs<IRulesetStore>(RulesetStore); dependencies.CacheAs<IRulesetStore>(RulesetStore);
Decoder.RegisterDependencies(RulesetStore);
// Backup is taken here rather than in EFToRealmMigrator to avoid recycling realm contexts // Backup is taken here rather than in EFToRealmMigrator to avoid recycling realm contexts
// after initial usages below. It can be moved once a direction is established for handling re-subscription. // after initial usages below. It can be moved once a direction is established for handling re-subscription.
// See https://github.com/ppy/osu/pull/16547 for more discussion. // See https://github.com/ppy/osu/pull/16547 for more discussion.

View File

@ -0,0 +1,51 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Platform;
#nullable enable
namespace osu.Game.Rulesets
{
/// <summary>
/// A ruleset store that populates from loaded assemblies (and optionally, assemblies in a storage).
/// </summary>
public class AssemblyRulesetStore : RulesetStore
{
public override IEnumerable<RulesetInfo> AvailableRulesets => availableRulesets;
private readonly List<RulesetInfo> availableRulesets = new List<RulesetInfo>();
/// <summary>
/// Create an assembly ruleset store that populates from loaded assemblies and an external location.
/// </summary>
/// <param name="path">An path containing ruleset DLLs.</param>
public AssemblyRulesetStore(string path)
: this(new NativeStorage(path))
{
}
/// <summary>
/// Create an assembly ruleset store that populates from loaded assemblies and an optional storage source.
/// </summary>
/// <param name="storage">An optional storage containing ruleset DLLs.</param>
public AssemblyRulesetStore(Storage? storage = null)
: base(storage)
{
List<Ruleset> instances = LoadedAssemblies.Values
.Select(r => Activator.CreateInstance(r) as Ruleset)
.Where(r => r != null)
.Select(r => r.AsNonNull())
.ToList();
// add all legacy rulesets first to ensure they have exclusive choice of primary key.
foreach (var r in instances.Where(r => r is ILegacyRuleset))
availableRulesets.Add(new RulesetInfo(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.OnlineID));
}
}
}

View File

@ -0,0 +1,101 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Database;
#nullable enable
namespace osu.Game.Rulesets
{
public class RealmRulesetStore : RulesetStore
{
public override IEnumerable<RulesetInfo> AvailableRulesets => availableRulesets;
private readonly List<RulesetInfo> availableRulesets = new List<RulesetInfo>();
public RealmRulesetStore(RealmAccess realm, Storage? storage = null)
: base(storage)
{
prepareDetachedRulesets(realm);
}
private void prepareDetachedRulesets(RealmAccess realmAccess)
{
realmAccess.Write(realm =>
{
var rulesets = realm.All<RulesetInfo>();
List<Ruleset> instances = LoadedAssemblies.Values
.Select(r => Activator.CreateInstance(r) as Ruleset)
.Where(r => r != null)
.Select(r => r.AsNonNull())
.ToList();
// add all legacy rulesets first to ensure they have exclusive choice of primary key.
foreach (var r in instances.Where(r => r is ILegacyRuleset))
{
if (realm.All<RulesetInfo>().FirstOrDefault(rr => rr.OnlineID == r.RulesetInfo.OnlineID) == null)
realm.Add(new RulesetInfo(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.OnlineID));
}
// add any other rulesets which have assemblies present but are not yet in the database.
foreach (var r in instances.Where(r => !(r is ILegacyRuleset)))
{
if (rulesets.FirstOrDefault(ri => ri.InstantiationInfo.Equals(r.RulesetInfo.InstantiationInfo, StringComparison.Ordinal)) == null)
{
var existingSameShortName = rulesets.FirstOrDefault(ri => ri.ShortName == r.RulesetInfo.ShortName);
if (existingSameShortName != null)
{
// even if a matching InstantiationInfo was not found, there may be an existing ruleset with the same ShortName.
// this generally means the user or ruleset provider has renamed their dll but the underlying ruleset is *likely* the same one.
// in such cases, update the instantiation info of the existing entry to point to the new one.
existingSameShortName.InstantiationInfo = r.RulesetInfo.InstantiationInfo;
}
else
realm.Add(new RulesetInfo(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.OnlineID));
}
}
List<RulesetInfo> detachedRulesets = new List<RulesetInfo>();
// perform a consistency check and detach final rulesets from realm for cross-thread runtime usage.
foreach (var r in rulesets.OrderBy(r => r.OnlineID))
{
try
{
var resolvedType = Type.GetType(r.InstantiationInfo)
?? throw new RulesetLoadException(@"Type could not be resolved");
var instanceInfo = (Activator.CreateInstance(resolvedType) as Ruleset)?.RulesetInfo
?? throw new RulesetLoadException(@"Instantiation failure");
// If a ruleset isn't up-to-date with the API, it could cause a crash at an arbitrary point of execution.
// To eagerly handle cases of missing implementations, enumerate all types here and mark as non-available on throw.
resolvedType.Assembly.GetTypes();
r.Name = instanceInfo.Name;
r.ShortName = instanceInfo.ShortName;
r.InstantiationInfo = instanceInfo.InstantiationInfo;
r.Available = true;
detachedRulesets.Add(r.Clone());
}
catch (Exception ex)
{
r.Available = false;
Logger.Log($"Could not load ruleset {r}: {ex.Message}");
}
}
availableRulesets.AddRange(detachedRulesets.OrderBy(r => r));
});
}
}
}

View File

@ -7,34 +7,26 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using osu.Framework; using osu.Framework;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Database;
#nullable enable #nullable enable
namespace osu.Game.Rulesets namespace osu.Game.Rulesets
{ {
public class RulesetStore : IDisposable, IRulesetStore public abstract class RulesetStore : IDisposable, IRulesetStore
{ {
private readonly RealmAccess realmAccess;
private const string ruleset_library_prefix = @"osu.Game.Rulesets"; private const string ruleset_library_prefix = @"osu.Game.Rulesets";
private readonly Dictionary<Assembly, Type> loadedAssemblies = new Dictionary<Assembly, Type>(); protected readonly Dictionary<Assembly, Type> LoadedAssemblies = new Dictionary<Assembly, Type>();
/// <summary> /// <summary>
/// All available rulesets. /// All available rulesets.
/// </summary> /// </summary>
public IEnumerable<RulesetInfo> AvailableRulesets => availableRulesets; public abstract IEnumerable<RulesetInfo> AvailableRulesets { get; }
private readonly List<RulesetInfo> availableRulesets = new List<RulesetInfo>(); protected RulesetStore(Storage? storage = null)
public RulesetStore(RealmAccess realm, Storage? storage = null)
{ {
realmAccess = realm;
// On android in release configuration assemblies are loaded from the apk directly into memory. // On android in release configuration assemblies are loaded from the apk directly into memory.
// We cannot read assemblies from cwd, so should check loaded assemblies instead. // We cannot read assemblies from cwd, so should check loaded assemblies instead.
loadFromAppDomain(); loadFromAppDomain();
@ -53,8 +45,6 @@ namespace osu.Game.Rulesets
var rulesetStorage = storage?.GetStorageForDirectory(@"rulesets"); var rulesetStorage = storage?.GetStorageForDirectory(@"rulesets");
if (rulesetStorage != null) if (rulesetStorage != null)
loadUserRulesets(rulesetStorage); loadUserRulesets(rulesetStorage);
addMissingRulesets();
} }
/// <summary> /// <summary>
@ -95,80 +85,7 @@ namespace osu.Game.Rulesets
if (domainAssembly != null) if (domainAssembly != null)
return domainAssembly; return domainAssembly;
return loadedAssemblies.Keys.FirstOrDefault(a => a.FullName == asm.FullName); return LoadedAssemblies.Keys.FirstOrDefault(a => a.FullName == asm.FullName);
}
private void addMissingRulesets()
{
realmAccess.Write(realm =>
{
var rulesets = realm.All<RulesetInfo>();
List<Ruleset> instances = loadedAssemblies.Values
.Select(r => Activator.CreateInstance(r) as Ruleset)
.Where(r => r != null)
.Select(r => r.AsNonNull())
.ToList();
// add all legacy rulesets first to ensure they have exclusive choice of primary key.
foreach (var r in instances.Where(r => r is ILegacyRuleset))
{
if (realm.All<RulesetInfo>().FirstOrDefault(rr => rr.OnlineID == r.RulesetInfo.OnlineID) == null)
realm.Add(new RulesetInfo(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.OnlineID));
}
// add any other rulesets which have assemblies present but are not yet in the database.
foreach (var r in instances.Where(r => !(r is ILegacyRuleset)))
{
if (rulesets.FirstOrDefault(ri => ri.InstantiationInfo.Equals(r.RulesetInfo.InstantiationInfo, StringComparison.Ordinal)) == null)
{
var existingSameShortName = rulesets.FirstOrDefault(ri => ri.ShortName == r.RulesetInfo.ShortName);
if (existingSameShortName != null)
{
// even if a matching InstantiationInfo was not found, there may be an existing ruleset with the same ShortName.
// this generally means the user or ruleset provider has renamed their dll but the underlying ruleset is *likely* the same one.
// in such cases, update the instantiation info of the existing entry to point to the new one.
existingSameShortName.InstantiationInfo = r.RulesetInfo.InstantiationInfo;
}
else
realm.Add(new RulesetInfo(r.RulesetInfo.ShortName, r.RulesetInfo.Name, r.RulesetInfo.InstantiationInfo, r.RulesetInfo.OnlineID));
}
}
List<RulesetInfo> detachedRulesets = new List<RulesetInfo>();
// perform a consistency check and detach final rulesets from realm for cross-thread runtime usage.
foreach (var r in rulesets.OrderBy(r => r.OnlineID))
{
try
{
var resolvedType = Type.GetType(r.InstantiationInfo)
?? throw new RulesetLoadException(@"Type could not be resolved");
var instanceInfo = (Activator.CreateInstance(resolvedType) as Ruleset)?.RulesetInfo
?? throw new RulesetLoadException(@"Instantiation failure");
// If a ruleset isn't up-to-date with the API, it could cause a crash at an arbitrary point of execution.
// To eagerly handle cases of missing implementations, enumerate all types here and mark as non-available on throw.
resolvedType.Assembly.GetTypes();
r.Name = instanceInfo.Name;
r.ShortName = instanceInfo.ShortName;
r.InstantiationInfo = instanceInfo.InstantiationInfo;
r.Available = true;
detachedRulesets.Add(r.Clone());
}
catch (Exception ex)
{
r.Available = false;
Logger.Log($"Could not load ruleset {r}: {ex.Message}");
}
}
availableRulesets.AddRange(detachedRulesets.OrderBy(r => r));
});
} }
private void loadFromAppDomain() private void loadFromAppDomain()
@ -214,7 +131,7 @@ namespace osu.Game.Rulesets
{ {
string? filename = Path.GetFileNameWithoutExtension(file); string? filename = Path.GetFileNameWithoutExtension(file);
if (loadedAssemblies.Values.Any(t => Path.GetFileNameWithoutExtension(t.Assembly.Location) == filename)) if (LoadedAssemblies.Values.Any(t => Path.GetFileNameWithoutExtension(t.Assembly.Location) == filename))
return; return;
try try
@ -229,17 +146,17 @@ namespace osu.Game.Rulesets
private void addRuleset(Assembly assembly) private void addRuleset(Assembly assembly)
{ {
if (loadedAssemblies.ContainsKey(assembly)) if (LoadedAssemblies.ContainsKey(assembly))
return; return;
// the same assembly may be loaded twice in the same AppDomain (currently a thing in certain Rider versions https://youtrack.jetbrains.com/issue/RIDER-48799). // the same assembly may be loaded twice in the same AppDomain (currently a thing in certain Rider versions https://youtrack.jetbrains.com/issue/RIDER-48799).
// as a failsafe, also compare by FullName. // as a failsafe, also compare by FullName.
if (loadedAssemblies.Any(a => a.Key.FullName == assembly.FullName)) if (LoadedAssemblies.Any(a => a.Key.FullName == assembly.FullName))
return; return;
try try
{ {
loadedAssemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset))); LoadedAssemblies[assembly] = assembly.GetTypes().First(t => t.IsPublic && t.IsSubclassOf(typeof(Ruleset)));
} }
catch (Exception e) catch (Exception e)
{ {