mirror of
https://github.com/ppy/osu.git
synced 2025-01-27 00:23:01 +08:00
Correct and simplify RulesetStore
This commit is contained in:
parent
ec51314e37
commit
acc299c7b9
@ -474,10 +474,11 @@ namespace osu.Game.Beatmaps
|
||||
// TODO: Diff beatmap metadata with set metadata and leave it here if necessary
|
||||
beatmap.BeatmapInfo.Metadata = null;
|
||||
|
||||
RulesetInfo ruleset = rulesets.GetRuleset(beatmap.BeatmapInfo.RulesetID);
|
||||
|
||||
// TODO: this should be done in a better place once we actually need to dynamically update it.
|
||||
beatmap.BeatmapInfo.Ruleset = rulesets.QueryRulesetInfo(r => r.ID == beatmap.BeatmapInfo.RulesetID);
|
||||
beatmap.BeatmapInfo.StarDifficulty = rulesets.QueryRulesetInfo(r => r.ID == beatmap.BeatmapInfo.RulesetID)?.CreateInstance()?.CreateDifficultyCalculator(beatmap)
|
||||
.Calculate() ?? 0;
|
||||
beatmap.BeatmapInfo.Ruleset = ruleset;
|
||||
beatmap.BeatmapInfo.StarDifficulty = ruleset?.CreateInstance()?.CreateDifficultyCalculator(beatmap).Calculate() ?? 0;
|
||||
|
||||
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ namespace osu.Game.Input
|
||||
public KeyBindingStore(OsuDbContext connection, RulesetStore rulesets, Storage storage = null)
|
||||
: base(connection, storage)
|
||||
{
|
||||
foreach (var info in rulesets.AllRulesets)
|
||||
foreach (var info in rulesets.AvailableRulesets)
|
||||
{
|
||||
var ruleset = info.CreateInstance();
|
||||
foreach (var variant in ruleset.AvailableVariants)
|
||||
|
@ -39,7 +39,7 @@ namespace osu.Game.Overlays.Direct
|
||||
DisplayStyleControl.Dropdown.AccentColour = colours.BlueDark;
|
||||
|
||||
Ruleset.BindTo(game?.Ruleset ?? new Bindable<RulesetInfo> { Value = rulesets.GetRuleset(0) });
|
||||
foreach (var r in rulesets.AllRulesets)
|
||||
foreach (var r in rulesets.AvailableRulesets)
|
||||
{
|
||||
modeButtons.Add(new RulesetToggleButton(Ruleset, r));
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ namespace osu.Game.Overlays
|
||||
{
|
||||
AddSection(new GlobalKeyBindingsSection(global));
|
||||
|
||||
foreach (var ruleset in rulesets.AllRulesets)
|
||||
foreach (var ruleset in rulesets.AvailableRulesets)
|
||||
AddSection(new RulesetBindingsSection(ruleset));
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,7 @@ namespace osu.Game.Overlays.Mods
|
||||
if (osu != null)
|
||||
Ruleset.BindTo(osu.Ruleset);
|
||||
else
|
||||
Ruleset.Value = rulesets.AllRulesets.First();
|
||||
Ruleset.Value = rulesets.AvailableRulesets.First();
|
||||
|
||||
Ruleset.ValueChanged += rulesetChanged;
|
||||
Ruleset.TriggerChange();
|
||||
|
@ -27,7 +27,7 @@ namespace osu.Game.Overlays.Settings.Sections
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(RulesetStore rulesets)
|
||||
{
|
||||
foreach(Ruleset ruleset in rulesets.AllRulesets.Select(info => info.CreateInstance()))
|
||||
foreach(Ruleset ruleset in rulesets.AvailableRulesets.Select(info => info.CreateInstance()))
|
||||
{
|
||||
SettingsSubsection section = ruleset.CreateSettings();
|
||||
if (section != null)
|
||||
|
@ -27,7 +27,7 @@ namespace osu.Game.Overlays.Settings
|
||||
|
||||
var modes = new List<Drawable>();
|
||||
|
||||
foreach (var ruleset in rulesets.AllRulesets)
|
||||
foreach (var ruleset in rulesets.AvailableRulesets)
|
||||
{
|
||||
var icon = new ConstrainedIconContainer
|
||||
{
|
||||
|
@ -67,7 +67,7 @@ namespace osu.Game.Overlays.Toolbar
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(RulesetStore rulesets, OsuGame game)
|
||||
{
|
||||
foreach (var r in rulesets.AllRulesets)
|
||||
foreach (var r in rulesets.AvailableRulesets)
|
||||
{
|
||||
modeButtons.Add(new ToolbarModeButton
|
||||
{
|
||||
|
@ -18,13 +18,6 @@ namespace osu.Game.Rulesets
|
||||
{
|
||||
private static readonly Dictionary<Assembly, Type> loaded_assemblies = new Dictionary<Assembly, Type>();
|
||||
|
||||
public IEnumerable<RulesetInfo> AllRulesets => Connection.RulesetInfo.Where(r => r.Available);
|
||||
|
||||
public RulesetStore(OsuDbContext connection)
|
||||
: base(connection)
|
||||
{
|
||||
}
|
||||
|
||||
static RulesetStore()
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve += currentDomain_AssemblyResolve;
|
||||
@ -33,6 +26,23 @@ namespace osu.Game.Rulesets
|
||||
loadRulesetFromFile(file);
|
||||
}
|
||||
|
||||
public RulesetStore(OsuDbContext connection)
|
||||
: base(connection)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve a ruleset using a known ID.
|
||||
/// </summary>
|
||||
/// <param name="id">The ruleset's internal ID.</param>
|
||||
/// <returns>A ruleset, if available, else null.</returns>
|
||||
public RulesetInfo GetRuleset(int id) => AvailableRulesets.FirstOrDefault(r => r.ID == id);
|
||||
|
||||
/// <summary>
|
||||
/// All available rulesets.
|
||||
/// </summary>
|
||||
public IEnumerable<RulesetInfo> AvailableRulesets => Connection.RulesetInfo.Where(r => r.Available);
|
||||
|
||||
private static Assembly currentDomain_AssemblyResolve(object sender, ResolveEventArgs args) => loaded_assemblies.Keys.FirstOrDefault(a => a.FullName == args.Name);
|
||||
|
||||
private const string ruleset_library_prefix = "osu.Game.Rulesets";
|
||||
@ -44,7 +54,7 @@ namespace osu.Game.Rulesets
|
||||
Connection.Database.ExecuteSqlCommand("DELETE FROM RulesetInfo");
|
||||
}
|
||||
|
||||
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo()));
|
||||
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo())).ToList();
|
||||
|
||||
//add all legacy modes in correct order
|
||||
foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID))
|
||||
@ -55,6 +65,7 @@ namespace osu.Game.Rulesets
|
||||
Connection.RulesetInfo.Add(rulesetInfo);
|
||||
}
|
||||
}
|
||||
|
||||
Connection.SaveChanges();
|
||||
|
||||
//add any other modes
|
||||
@ -67,6 +78,7 @@ namespace osu.Game.Rulesets
|
||||
if (existing == null)
|
||||
Connection.RulesetInfo.Add(us);
|
||||
}
|
||||
|
||||
Connection.SaveChanges();
|
||||
|
||||
//perform a consistency check
|
||||
@ -82,6 +94,7 @@ namespace osu.Game.Rulesets
|
||||
r.Available = false;
|
||||
}
|
||||
}
|
||||
|
||||
Connection.SaveChanges();
|
||||
}
|
||||
|
||||
@ -108,20 +121,5 @@ namespace osu.Game.Rulesets
|
||||
InstantiationInfo = ruleset.GetType().AssemblyQualifiedName,
|
||||
ID = ruleset.LegacyID
|
||||
};
|
||||
|
||||
public RulesetInfo GetRuleset(int id) => Connection.RulesetInfo.First(r => r.ID == id);
|
||||
|
||||
public RulesetInfo QueryRulesetInfo(Func<RulesetInfo, bool> query)
|
||||
{
|
||||
return Connection.RulesetInfo.FirstOrDefault(query);
|
||||
}
|
||||
|
||||
public List<RulesetInfo> QueryRulesets(Func<RulesetInfo, bool> query = null)
|
||||
{
|
||||
var rulesets = Connection.RulesetInfo;
|
||||
if (query != null)
|
||||
return rulesets.Where(query).ToList();
|
||||
return rulesets.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ namespace osu.Game.Tests.Visual
|
||||
|
||||
AddStep("Toggle", modSelect.ToggleVisibility);
|
||||
|
||||
foreach (var ruleset in rulesets.AllRulesets)
|
||||
foreach (var ruleset in rulesets.AvailableRulesets)
|
||||
AddStep(ruleset.CreateInstance().Description, () => modSelect.Ruleset.Value = ruleset);
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ namespace osu.Game.Tests.Visual
|
||||
new BeatmapInfo
|
||||
{
|
||||
OnlineBeatmapID = 1234 + i,
|
||||
Ruleset = rulesets.QueryRulesets().First(),
|
||||
Ruleset = rulesets.AvailableRulesets.First(),
|
||||
Path = "normal.osu",
|
||||
Version = "Normal",
|
||||
Difficulty = new BeatmapDifficulty
|
||||
@ -86,7 +86,7 @@ namespace osu.Game.Tests.Visual
|
||||
new BeatmapInfo
|
||||
{
|
||||
OnlineBeatmapID = 1235 + i,
|
||||
Ruleset = rulesets.QueryRulesets().First(),
|
||||
Ruleset = rulesets.AvailableRulesets.First(),
|
||||
Path = "hard.osu",
|
||||
Version = "Hard",
|
||||
Difficulty = new BeatmapDifficulty
|
||||
@ -97,7 +97,7 @@ namespace osu.Game.Tests.Visual
|
||||
new BeatmapInfo
|
||||
{
|
||||
OnlineBeatmapID = 1236 + i,
|
||||
Ruleset = rulesets.QueryRulesets().First(),
|
||||
Ruleset = rulesets.AvailableRulesets.First(),
|
||||
Path = "insane.osu",
|
||||
Version = "Insane",
|
||||
Difficulty = new BeatmapDifficulty
|
||||
|
@ -50,7 +50,7 @@ namespace osu.Game.Tests.Visual
|
||||
|
||||
string instantiation = ruleset?.AssemblyQualifiedName;
|
||||
|
||||
foreach (var r in rulesets.QueryRulesets(rs => rs.Available && (instantiation == null || rs.InstantiationInfo == instantiation)))
|
||||
foreach (var r in rulesets.AvailableRulesets.Where(rs => instantiation == null || rs.InstantiationInfo == instantiation))
|
||||
AddStep(r.Name, () => loadPlayerFor(r));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user