1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 10:02:59 +08:00

Store databased settings based on string keys rather than ints

Allows for rearranging/removal from enums without consequence.
This commit is contained in:
Dean Herbert 2019-05-25 15:00:53 +09:00
parent 05c0df53dc
commit 127858d398
5 changed files with 29 additions and 13 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Game.Rulesets; using osu.Game.Rulesets;
@ -19,6 +20,8 @@ namespace osu.Game.Configuration
private readonly RulesetInfo ruleset; private readonly RulesetInfo ruleset;
private readonly bool legacySettingsExist;
protected DatabasedConfigManager(SettingsStore settings, RulesetInfo ruleset = null, int? variant = null) protected DatabasedConfigManager(SettingsStore settings, RulesetInfo ruleset = null, int? variant = null)
{ {
this.settings = settings; this.settings = settings;
@ -26,6 +29,7 @@ namespace osu.Game.Configuration
this.variant = variant; this.variant = variant;
databasedSettings = settings.Query(ruleset?.ID, variant); databasedSettings = settings.Query(ruleset?.ID, variant);
legacySettingsExist = databasedSettings.Any(s => int.TryParse(s.Key, out var _));
InitialiseDefaults(); InitialiseDefaults();
} }
@ -43,7 +47,18 @@ namespace osu.Game.Configuration
{ {
base.AddBindable(lookup, bindable); base.AddBindable(lookup, bindable);
var setting = databasedSettings.Find(s => (int)s.Key == (int)(object)lookup); if (legacySettingsExist)
{
var legacySetting = databasedSettings.Find(s => s.Key == ((int)(object)lookup).ToString());
if (legacySetting != null)
{
bindable.Parse(legacySetting.Value);
settings.Delete(legacySetting);
}
}
var setting = databasedSettings.Find(s => s.Key == lookup.ToString());
if (setting != null) if (setting != null)
{ {
@ -53,7 +68,7 @@ namespace osu.Game.Configuration
{ {
settings.Update(setting = new DatabasedSetting settings.Update(setting = new DatabasedSetting
{ {
Key = lookup, Key = lookup.ToString(),
Value = bindable.Value, Value = bindable.Value,
RulesetID = ruleset?.ID, RulesetID = ruleset?.ID,
Variant = variant, Variant = variant,

View File

@ -1,4 +1,4 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
@ -16,11 +16,7 @@ namespace osu.Game.Configuration
public int? Variant { get; set; } public int? Variant { get; set; }
[Column("Key")] [Column("Key")]
public int IntKey public string Key { get; set; }
{
get => (int)Key;
private set => Key = value;
}
[Column("Value")] [Column("Value")]
public string StringValue public string StringValue
@ -29,10 +25,9 @@ namespace osu.Game.Configuration
set => Value = value; set => Value = value;
} }
public object Key;
public object Value; public object Value;
public DatabasedSetting(object key, object value) public DatabasedSetting(string key, object value)
{ {
Key = key; Key = key;
Value = value; Value = value;

View File

@ -37,5 +37,11 @@ namespace osu.Game.Configuration
SettingChanged?.Invoke(); SettingChanged?.Invoke();
} }
public void Delete(DatabasedSetting setting)
{
using (var usage = ContextFactory.GetForWrite())
usage.Context.Remove(setting);
}
} }
} }

View File

@ -16,7 +16,7 @@ namespace osu.Game.Migrations
{ {
ID = table.Column<int>(type: "INTEGER", nullable: false) ID = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true), .Annotation("Sqlite:Autoincrement", true),
Key = table.Column<int>(type: "INTEGER", nullable: false), Key = table.Column<int>(type: "TEXT", nullable: false),
RulesetID = table.Column<int>(type: "INTEGER", nullable: true), RulesetID = table.Column<int>(type: "INTEGER", nullable: true),
Value = table.Column<string>(type: "TEXT", nullable: true), Value = table.Column<string>(type: "TEXT", nullable: true),
Variant = table.Column<int>(type: "INTEGER", nullable: true) Variant = table.Column<int>(type: "INTEGER", nullable: true)

View File

@ -14,7 +14,7 @@ namespace osu.Game.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "2.2.1-servicing-10028"); .HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity("osu.Game.Beatmaps.BeatmapDifficulty", b => modelBuilder.Entity("osu.Game.Beatmaps.BeatmapDifficulty", b =>
{ {
@ -198,7 +198,7 @@ namespace osu.Game.Migrations
b.Property<int>("ID") b.Property<int>("ID")
.ValueGeneratedOnAdd(); .ValueGeneratedOnAdd();
b.Property<int>("IntKey") b.Property<string>("Key")
.HasColumnName("Key"); .HasColumnName("Key");
b.Property<int?>("RulesetID"); b.Property<int?>("RulesetID");