Exemple #1
0
        protected override void AddBindable <TBindable>(TLookup lookup, Bindable <TBindable> bindable)
        {
            base.AddBindable(lookup, bindable);

            var setting = databasedSettings.Find(s => s.Key == lookup.ToString());

            if (setting != null)
            {
                bindable.Parse(setting.Value);
            }
            else
            {
                setting = new RealmRulesetSetting
                {
                    Key       = lookup.ToString(),
                    Value     = bindable.Value.ToString(),
                    RulesetID = rulesetId,
                    Variant   = variant,
                };

                realmFactory?.Context.Write(() => realmFactory.Context.Add(setting));

                databasedSettings.Add(setting);
            }

            bindable.ValueChanged += b =>
            {
                realmFactory?.Context.Write(() => setting.Value = b.NewValue.ToString());
            };
        }
        private IBindable <TValue>?genericLookup <TLookup, TValue>(TLookup lookup)
            where TLookup : notnull
            where TValue : notnull
        {
            try
            {
                if (Configuration.ConfigDictionary.TryGetValue(lookup.ToString(), out string val))
                {
                    // special case for handling skins which use 1 or 0 to signify a boolean state.
                    if (typeof(TValue) == typeof(bool))
                    {
                        val = val == "1" ? "true" : "false";
                    }

                    var bindable = new Bindable <TValue>();
                    if (val != null)
                    {
                        bindable.Parse(val);
                    }
                    return(bindable);
                }
            }
            catch
            {
            }

            return(null);
        }
        protected override void AddBindable <TBindable>(T lookup, Bindable <TBindable> bindable)
        {
            base.AddBindable(lookup, bindable);

            var setting = databasedSettings.Find(s => (int)s.Key == (int)(object)lookup);

            if (setting != null)
            {
                bindable.Parse(setting.Value);
            }
            else
            {
                settings.Update(setting = new DatabasedSetting
                {
                    Key       = lookup,
                    Value     = bindable.Value,
                    RulesetID = ruleset?.ID,
                    Variant   = variant,
                });

                databasedSettings.Add(setting);
            }

            bindable.ValueChanged += b =>
            {
                setting.Value = b.NewValue;
                settings.Update(setting);
            };
        }
Exemple #4
0
        protected override void AddBindable <TBindable>(TLookup lookup, Bindable <TBindable> bindable)
        {
            base.AddBindable(lookup, bindable);

            var setting = databasedSettings.Find(s => s.Key == lookup.ToString());

            if (setting != null)
            {
                bindable.Parse(setting.Value);
            }
            else
            {
                setting = new RealmRulesetSetting
                {
                    Key         = lookup.ToString(),
                    Value       = bindable.Value.ToString(),
                    RulesetName = rulesetName,
                    Variant     = variant,
                };

                realm?.Realm.Write(() => realm.Realm.Add(setting));

                databasedSettings.Add(setting);
            }

            bindable.ValueChanged += b =>
            {
                lock (pendingWrites)
                    pendingWrites.Add(lookup);
            };
        }
Exemple #5
0
        private IBindable <TValue>?genericLookup <TLookup, TValue>(TLookup lookup)
            where TLookup : notnull
            where TValue : notnull
        {
            try
            {
                if (Configuration.ConfigDictionary.TryGetValue(lookup.ToString(), out string val))
                {
                    // special case for handling skins which use 1 or 0 to signify a boolean state.
                    // ..or in some cases 2 (https://github.com/ppy/osu/issues/18579).
                    if (typeof(TValue) == typeof(bool))
                    {
                        val = bool.TryParse(val, out bool boolVal)
                            ? Convert.ChangeType(boolVal, typeof(bool)).ToString()
                            : Convert.ChangeType(Convert.ToInt32(val), typeof(bool)).ToString();
                    }

                    var bindable = new Bindable <TValue>();
                    if (val != null)
                    {
                        bindable.Parse(val);
                    }
                    return(bindable);
                }
            }
            catch
            {
            }

            return(null);
        }
Exemple #6
0
        protected override void AddBindable <TBindable>(T lookup, Bindable <TBindable> bindable)
        {
            base.AddBindable(lookup, bindable);

            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)
            {
                bindable.Parse(setting.Value);
            }
            else
            {
                settings.Update(setting = new DatabasedSetting
                {
                    Key       = lookup.ToString(),
                    Value     = bindable.Value,
                    RulesetID = ruleset?.ID,
                    Variant   = variant,
                });

                databasedSettings.Add(setting);
            }

            bindable.ValueChanged += b =>
            {
                setting.Value = b.NewValue;

                lock (dirtySettings)
                {
                    if (!dirtySettings.Contains(setting))
                    {
                        dirtySettings.Add(setting);
                    }
                }
            };
        }
Exemple #7
0
        public void TestParsingString(string value)
        {
            var bindable = new Bindable <string>();

            bindable.Parse(value);

            Assert.AreEqual(value, bindable.Value);
        }
        public void TestParsingEnumToNullableType(TestEnum value)
        {
            var bindable = new Bindable <TestEnum?>();

            bindable.Parse(value);

            Assert.AreEqual(value, bindable.Value);
        }
        public void TestParsingStringToNullableType(string value, TestEnum?expected)
        {
            var bindable = new Bindable <TestEnum?>();

            bindable.Parse(value);

            Assert.AreEqual(expected, bindable.Value);
        }
Exemple #10
0
        public void TestUnparsaebles(object value)
        {
            var bindable = new Bindable <TestEnum>();
            var nullable = new Bindable <TestEnum?>();

            Assert.Throws <ArgumentException>(() => bindable.Parse(value));
            Assert.Throws <ArgumentException>(() => nullable.Parse(value));
        }
Exemple #11
0
        public void TestParsing(TestEnum expected, params object[] values)
        {
            var bindable = new Bindable <TestEnum>();
            var nullable = new Bindable <TestEnum?>();

            foreach (object value in values.Append(expected))
            {
                bindable.Parse(value);
                nullable.Parse(value);

                Assert.AreEqual(expected, bindable.Value);
                Assert.AreEqual(expected, nullable.Value);
            }
        }
        public void TestParseBindableOfMatchingInterfaceType()
        {
            // both of these implement IBindable<int>
            var bindable1 = new BindableInt(10)
            {
                MaxValue = 15
            };
            var bindable2 = new Bindable <int>(20);

            bindable1.Parse(bindable2);
            // ensure MaxValue is still respected.
            Assert.That(bindable1.Value, Is.EqualTo(15));

            bindable2.Parse(bindable1);
            Assert.That(bindable2.Value, Is.EqualTo(15));
        }
        public override IBindable <TValue> GetConfig <TLookup, TValue>(TLookup lookup)
        {
            switch (lookup)
            {
            case GlobalSkinConfiguration global:
                switch (global)
                {
                case GlobalSkinConfiguration.ComboColours:
                    return(SkinUtils.As <TValue>(new Bindable <List <Color4> >(Configuration.ComboColours)));
                }

                break;

            case GlobalSkinColour colour:
                return(SkinUtils.As <TValue>(getCustomColour(colour.ToString())));

            case SkinCustomColourLookup customColour:
                return(SkinUtils.As <TValue>(getCustomColour(customColour.Lookup.ToString())));

            default:
                try
                {
                    if (Configuration.ConfigDictionary.TryGetValue(lookup.ToString(), out var val))
                    {
                        // special case for handling skins which use 1 or 0 to signify a boolean state.
                        if (typeof(TValue) == typeof(bool))
                        {
                            val = val == "1" ? "true" : "false";
                        }

                        var bindable = new Bindable <TValue>();
                        if (val != null)
                        {
                            bindable.Parse(val);
                        }
                        return(bindable);
                    }
                }
                catch
                {
                }

                break;
            }

            return(null);
        }
        public void TestParseBindableOfExactSameType()
        {
            var bindable1 = new BindableInt();
            var bindable2 = new BindableDouble();
            var bindable3 = new BindableBool();
            var bindable4 = new Bindable <string>();

            bindable1.Parse(new BindableInt(3));
            bindable2.Parse(new BindableDouble(2.5));
            bindable3.Parse(new BindableBool(true));
            bindable4.Parse(new Bindable <string>("string value"));

            Assert.That(bindable1.Value, Is.EqualTo(3));
            Assert.That(bindable2.Value, Is.EqualTo(2.5));
            Assert.That(bindable3.Value, Is.EqualTo(true));
            Assert.That(bindable4.Value, Is.EqualTo("string value"));

            // parsing bindable of different type should throw exception.
            Assert.Throws <ArgumentException>(() => bindable1.Parse(new BindableDouble(3.0)));
        }