2019-01-24 08:43:03 +00:00
|
|
|
|
// 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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-12-06 13:32:31 +00:00
|
|
|
|
using System;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-05-25 06:00:53 +00:00
|
|
|
|
using System.Linq;
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Configuration
|
|
|
|
|
{
|
2019-12-06 13:32:31 +00:00
|
|
|
|
public abstract class DatabasedConfigManager<TLookup> : ConfigManager<TLookup>
|
|
|
|
|
where TLookup : struct, Enum
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly SettingsStore settings;
|
|
|
|
|
|
2018-06-11 04:13:36 +00:00
|
|
|
|
private readonly int? variant;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-09-04 17:12:17 +00:00
|
|
|
|
private List<DatabasedSetting> databasedSettings;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
private readonly RulesetInfo ruleset;
|
|
|
|
|
|
2019-09-04 17:12:17 +00:00
|
|
|
|
private bool legacySettingsExist;
|
2019-05-25 06:00:53 +00:00
|
|
|
|
|
2018-06-11 04:13:36 +00:00
|
|
|
|
protected DatabasedConfigManager(SettingsStore settings, RulesetInfo ruleset = null, int? variant = null)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
this.settings = settings;
|
|
|
|
|
this.ruleset = ruleset;
|
|
|
|
|
this.variant = variant;
|
|
|
|
|
|
2019-09-04 17:12:17 +00:00
|
|
|
|
Load();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
InitialiseDefaults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PerformLoad()
|
|
|
|
|
{
|
2019-09-04 17:12:17 +00:00
|
|
|
|
databasedSettings = settings.Query(ruleset?.ID, variant);
|
2019-11-12 12:03:21 +00:00
|
|
|
|
legacySettingsExist = databasedSettings.Any(s => int.TryParse(s.Key, out _));
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool PerformSave()
|
|
|
|
|
{
|
2019-09-04 17:12:17 +00:00
|
|
|
|
lock (dirtySettings)
|
|
|
|
|
{
|
2019-09-05 04:36:37 +00:00
|
|
|
|
foreach (var setting in dirtySettings)
|
|
|
|
|
settings.Update(setting);
|
|
|
|
|
dirtySettings.Clear();
|
2019-09-04 17:12:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 17:12:17 +00:00
|
|
|
|
private readonly List<DatabasedSetting> dirtySettings = new List<DatabasedSetting>();
|
|
|
|
|
|
2019-12-06 13:32:31 +00:00
|
|
|
|
protected override void AddBindable<TBindable>(TLookup lookup, Bindable<TBindable> bindable)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
base.AddBindable(lookup, bindable);
|
|
|
|
|
|
2019-05-25 06:00:53 +00:00
|
|
|
|
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());
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (setting != null)
|
|
|
|
|
{
|
|
|
|
|
bindable.Parse(setting.Value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
settings.Update(setting = new DatabasedSetting
|
|
|
|
|
{
|
2019-05-25 06:00:53 +00:00
|
|
|
|
Key = lookup.ToString(),
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Value = bindable.Value,
|
|
|
|
|
RulesetID = ruleset?.ID,
|
|
|
|
|
Variant = variant,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
databasedSettings.Add(setting);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-22 08:51:39 +00:00
|
|
|
|
bindable.ValueChanged += b =>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-02-22 08:51:39 +00:00
|
|
|
|
setting.Value = b.NewValue;
|
2019-09-05 04:37:11 +00:00
|
|
|
|
|
2019-09-04 17:12:17 +00:00
|
|
|
|
lock (dirtySettings)
|
2019-09-05 04:37:11 +00:00
|
|
|
|
{
|
2019-09-04 17:12:17 +00:00
|
|
|
|
if (!dirtySettings.Contains(setting))
|
|
|
|
|
dirtySettings.Add(setting);
|
2019-09-05 04:37:11 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|