예제 #1
0
        /// <summary>
        /// Sets a string-valued setting.
        /// </summary>
        /// <param name="Key">Key name.</param>
        /// <param name="Value">New value.</param>
        /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns>
        public static async Task <bool> SetAsync(string Key, string Value)
        {
            using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
            {
                StringSetting Setting = await Database.FindFirstDeleteRest <StringSetting>(new FilterFieldEqualTo("Key", Key));

                if (Setting is null)
                {
                    Setting = new StringSetting(Key, Value);
                    await Database.Insert(Setting);

                    return(true);
                }
                else
                {
                    if (Setting.Value != Value)
                    {
                        Setting.Value = Value;
                        await Database.Update(Setting);

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Sets a string-valued setting.
        /// </summary>
        /// <param name="Key">Key name.</param>
        /// <param name="Value">New value.</param>
        /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns>
        public static async Task <bool> SetAsync(string Key, string Value)
        {
            using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
            {
                StringSetting Setting = await Database.FindFirstDeleteRest <StringSetting>(new FilterFieldEqualTo("Key", Key));

                return(await SetAsyncLocked(Key, Value, Setting));
            }
        }
예제 #3
0
        /// <summary>
        /// Deletes a runtime setting
        /// </summary>
        /// <param name="Key">Key name.</param>
        /// <returns>If a setting was found with the given name and deleted.</returns>
        public static async Task <bool> DeleteAsync(string Key)
        {
            using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
            {
                bool Found = false;

                foreach (Setting Setting in await Database.FindDelete <Setting>(new FilterFieldEqualTo("Key", Key)))
                {
                    Found = true;
                }

                return(Found);
            }
        }
예제 #4
0
        /// <summary>
        /// Sets a object-valued setting.
        /// </summary>
        /// <param name="Key">Key name.</param>
        /// <param name="Value">New value.</param>
        /// <returns>If the setting was saved (true). If the setting existed, and had the same value, false is returned.</returns>
        public static async Task <bool> SetAsync(string Key, object Value)
        {
            using (Semaphore Semaphore = await Semaphores.BeginWrite("setting:" + Key))
            {
                Setting Setting = await Database.FindFirstDeleteRest <Setting>(new FilterFieldEqualTo("Key", Key));

                if (Value is null)
                {
                    if (!(Setting is ObjectSetting))
                    {
                        await Database.Delete(Setting);

                        Setting = null;
                    }
                }
                else
                {
                    if (Value is string s)
                    {
                        if (!(Setting is StringSetting StringSetting))
                        {
                            await Database.Delete(Setting);

                            StringSetting = null;
                        }

                        return(await SetAsyncLocked(Key, s, StringSetting));
                    }
                    else if (Value is long l)
                    {
                        if (!(Setting is Int64Setting Int64Setting))
                        {
                            await Database.Delete(Setting);

                            Int64Setting = null;
                        }

                        return(await SetAsyncLocked(Key, l, Int64Setting));
                    }
                    else if (Value is double d)
                    {
                        if (!(Setting is DoubleSetting DoubleSetting))
                        {
                            await Database.Delete(Setting);

                            DoubleSetting = null;
                        }

                        return(await SetAsyncLocked(Key, d, DoubleSetting));
                    }
                    else if (Value is bool b)
                    {
                        if (!(Setting is BooleanSetting BooleanSetting))
                        {
                            await Database.Delete(Setting);

                            BooleanSetting = null;
                        }

                        return(await SetAsyncLocked(Key, b, BooleanSetting));
                    }
                    else if (Value is DateTime TP)
                    {
                        if (!(Setting is DateTimeSetting DateTimeSetting))
                        {
                            await Database.Delete(Setting);

                            DateTimeSetting = null;
                        }

                        return(await SetAsyncLocked(Key, TP, DateTimeSetting));
                    }
                    else if (Value is TimeSpan TS)
                    {
                        if (!(Setting is TimeSpanSetting TimeSpanSetting))
                        {
                            await Database.Delete(Setting);

                            TimeSpanSetting = null;
                        }

                        return(await SetAsyncLocked(Key, TS, TimeSpanSetting));
                    }
                    else
                    {
                        Type     T  = Value.GetType();
                        TypeInfo TI = T.GetTypeInfo();

                        if (!(TI.GetCustomAttribute(typeof(CollectionNameAttribute)) is null))
                        {
                            throw new InvalidOperationException("Object setting values cannot be stored separate collections. (CollectionName attribute found.)");
                        }

                        TypeNameAttribute TypeNameAttribute = TI.GetCustomAttribute <TypeNameAttribute>();
                        if (TypeNameAttribute.TypeNameSerialization != TypeNameSerialization.FullName)
                        {
                            throw new InvalidOperationException("Full Type names must be serialized when persisting object setting values. (TypeName attribute.). Exceptions for the types: Boolean, Int64, String, DateTime, TimeSpan, Double.");
                        }
                    }
                }

                if (Setting is ObjectSetting ObjectSetting)
                {
                    if (((ObjectSetting.Value is null) ^ (Value is null)) || !ObjectSetting.Value.Equals(Value))
                    {
                        ObjectSetting.Value = Value;
                        await Database.Update(ObjectSetting);

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }