Merge pull request #4861 from peppy/settings-flexibility

Store databased settings based on string keys rather than ints
This commit is contained in:
Dan Balasescu 2019-05-28 14:23:22 +09:00 committed by GitHub
commit 2d4ef1bec9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Game.Rulesets;
@ -19,6 +20,8 @@ public abstract class DatabasedConfigManager<T> : ConfigManager<T>
private readonly RulesetInfo ruleset;
private readonly bool legacySettingsExist;
protected DatabasedConfigManager(SettingsStore settings, RulesetInfo ruleset = null, int? variant = null)
{
this.settings = settings;
@ -26,6 +29,7 @@ protected DatabasedConfigManager(SettingsStore settings, RulesetInfo ruleset = n
this.variant = variant;
databasedSettings = settings.Query(ruleset?.ID, variant);
legacySettingsExist = databasedSettings.Any(s => int.TryParse(s.Key, out var _));
InitialiseDefaults();
}
@ -43,7 +47,18 @@ protected override void AddBindable<TBindable>(T lookup, Bindable<TBindable> bin
{
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)
{
@ -53,7 +68,7 @@ protected override void AddBindable<TBindable>(T lookup, Bindable<TBindable> bin
{
settings.Update(setting = new DatabasedSetting
{
Key = lookup,
Key = lookup.ToString(),
Value = bindable.Value,
RulesetID = ruleset?.ID,
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.
using System.ComponentModel.DataAnnotations.Schema;
@ -16,11 +16,7 @@ public class DatabasedSetting : IHasPrimaryKey
public int? Variant { get; set; }
[Column("Key")]
public int IntKey
{
get => (int)Key;
private set => Key = value;
}
public string Key { get; set; }
[Column("Value")]
public string StringValue
@ -29,10 +25,9 @@ public string StringValue
set => Value = value;
}
public object Key;
public object Value;
public DatabasedSetting(object key, object value)
public DatabasedSetting(string key, object value)
{
Key = key;
Value = value;

View File

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

View File

@ -16,7 +16,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
{
ID = table.Column<int>(type: "INTEGER", nullable: false)
.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),
Value = table.Column<string>(type: "TEXT", nullable: true),
Variant = table.Column<int>(type: "INTEGER", nullable: true)

View File

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