コード例 #1
0
        //------------------------------------------------------------------------------
        static public bool Delete(ISettingsSerializer serializer,
                                  SettingBaseAttribute attr,
                                  string settingName, bool secure,
                                  FunctionalityHook functionalityHook)
        {
            try
            {
                serializer.Delete(settingName, secure, attr);
            }
            catch (ExceptionForUser exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         String.Format("Delete exception for setting '{0}'", settingName),
                                                         exc.InnerException);

                throw exc.InnerException;
            }
            catch (Exception exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         String.Format("Delete exception for setting '{0}'", settingName),
                                                         exc);
                return(false);
            }

            return(true);
        }
コード例 #2
0
        //------------------------------------------------------------------------------
        FunctionalityHook GetFunctionalityHook()
        {
            if (m_FunctionalityHook == null)
            {
                m_FunctionalityHook = new FunctionalityHook()
                {
                    TypeCorrector = new DynamicSettingTypeCorrector()
                };
            }

            return(m_FunctionalityHook);
        }
コード例 #3
0
        //------------------------------------------------------------------------------
        public static bool ProcessSetting(SettingsObjectInfo settingsObjectInfo,
                                          SettingInfo inSettingInfo,
                                          SettingsEnumerationMode mode,
                                          SettingsHelperData settingsHelperData,
                                          out SettingInfo outSettingInfo,
                                          FunctionalityHook functionalityHook)
        {
            bool processStatus = false;

            outSettingInfo = new SettingInfo();

            SettingBaseAttribute settingBaseAttribute = null;

            if (settingsObjectInfo.MemberInfo != null)
            {
                settingBaseAttribute = settingsObjectInfo.MemberInfo.GetCustomAttribute <SettingBaseAttribute>();
                if (settingBaseAttribute == null)
                {
                    return(false);
                }
            }

            ISettingsSerializer serializer = null;

            if (settingsObjectInfo.MemberInfo != null)
            {
                serializer = GetSerializerForMemberInfo(settingsObjectInfo.Settings,
                                                        settingsObjectInfo.ForegroundSerializer,
                                                        settingsObjectInfo.MemberInfo,
                                                        ref settingsHelperData.SerializersCache);
            }
            else
            {
                serializer = GetSerializerForSettingAttributeType(settingsObjectInfo.Settings,
                                                                  settingsObjectInfo.ForegroundSerializer,
                                                                  settingsObjectInfo.SettingAttributeType,
                                                                  ref settingsHelperData.SerializersCache);
            }

            Debug.Assert(serializer != null);

            if (settingsObjectInfo.MemberInfo != null)
            {
                inSettingInfo.SettingName = SettingNameFormatInfo.GetFullSettingName(settingsObjectInfo.Settings.GetType(),
                                                                                     settingsObjectInfo.MemberInfo,
                                                                                     settingBaseAttribute,
                                                                                     serializer as ISettingsSerializerWishes,
                                                                                     SettingNameMode.Static);
            }

            if (settingsObjectInfo.MemberInfo != null)
            {
                var settingValueType = SettingsSerializer.GetSettingType(settingsObjectInfo.MemberInfo);

                inSettingInfo.SettingValueType = settingValueType;
                inSettingInfo.SettingValue     = SettingsSerializer.GetSettingValue(settingsObjectInfo.MemberInfo, settingsObjectInfo.Settings);
            }

            bool secure = false;

            var settingSecureAttr = settingBaseAttribute as SettingAttribute;

            if (settingSecureAttr != null)
            {
                secure = settingSecureAttr.Secure;
            }

            switch (mode)
            {
            case SettingsEnumerationMode.Load:
            {
                Wrapper defaultValueWrapper = null;
                if (settingBaseAttribute != null && settingBaseAttribute.DefaultValueInUse)
                {
                    var defaultValue = SettingsSerializer.GetDefaultValue(settingBaseAttribute, inSettingInfo.SettingValueType);
                    defaultValueWrapper = new Wrapper()
                    {
                        Value = defaultValue
                    };
                }

                processStatus = SettingsSerializer.Load(serializer,
                                                        settingBaseAttribute,
                                                        inSettingInfo.SettingName, secure,
                                                        defaultValueWrapper,
                                                        inSettingInfo.SettingValueType,
                                                        ref outSettingInfo,
                                                        functionalityHook);

                // outSettingInfo.SettingValue type was corrected
                // in the SettingsSerializer.Load method
                if (processStatus && settingsObjectInfo.MemberInfo != null)
                {
                    SettingsSerializer.SetSettingValue(settingsObjectInfo.MemberInfo, settingsObjectInfo.Settings, outSettingInfo.SettingValue);
                }

#if __TDD__
                SettingsBaseConfiguration.TDDData.SerializerAction(serializer.GetType(),
                                                                   TDDData.SerializerActions.Load,
                                                                   inSettingInfo.SettingName,
                                                                   secure);
#endif // __TDD__

                break;
            }

            case SettingsEnumerationMode.Save:
            {
                if (settingsObjectInfo.MemberInfo != null)
                {
                    inSettingInfo.SettingValue = SettingsSerializer.GetSettingValue(settingsObjectInfo.MemberInfo, settingsObjectInfo.Settings);
                }

                processStatus = SettingsSerializer.Save(serializer,
                                                        settingBaseAttribute,
                                                        inSettingInfo.SettingName, secure,
                                                        inSettingInfo.SettingValue,
                                                        functionalityHook);

                settingsHelperData.SerializersToSync.Add(serializer);

                outSettingInfo = inSettingInfo;
#if __TDD__
                SettingsBaseConfiguration.TDDData.SerializerAction(serializer.GetType(),
                                                                   TDDData.SerializerActions.Save,
                                                                   inSettingInfo.SettingName,
                                                                   secure);
#endif // __TDD__

                break;
            }

            case SettingsEnumerationMode.Delete:
            {
                processStatus = SettingsSerializer.Delete(serializer,
                                                          settingBaseAttribute,
                                                          inSettingInfo.SettingName, secure,
                                                          functionalityHook);

                settingsHelperData.SerializersToSync.Add(serializer);

                object defaultValue = SettingsSerializer.GetDefaultValue(settingBaseAttribute, inSettingInfo.SettingValueType);

                outSettingInfo.SettingName         = inSettingInfo.SettingName;
                outSettingInfo.SettingValue        = defaultValue;
                outSettingInfo.SettingDefaultValue = defaultValue;
                outSettingInfo.SettingValueType    = inSettingInfo.SettingValueType;

                if (processStatus && settingsObjectInfo.MemberInfo != null)
                {
                    SettingsSerializer.SetSettingValue(settingsObjectInfo.MemberInfo,
                                                       settingsObjectInfo.Settings,
                                                       defaultValue);
                }
#if __TDD__
                SettingsBaseConfiguration.TDDData.SerializerAction(serializer.GetType(),
                                                                   TDDData.SerializerActions.Delete,
                                                                   inSettingInfo.SettingName,
                                                                   secure);
#endif // __TDD__

                break;
            }

            case SettingsEnumerationMode.LoadDefaults:
            {
                Debug.Assert(inSettingInfo.SettingValueType != null);
                var defaultValue = SettingsSerializer.GetDefaultValue(settingBaseAttribute, inSettingInfo.SettingValueType);

                if (settingsObjectInfo.MemberInfo != null)
                {
                    SettingsSerializer.SetSettingValue(settingsObjectInfo.MemberInfo,
                                                       settingsObjectInfo.Settings,
                                                       defaultValue);
                }

                outSettingInfo.SettingName         = inSettingInfo.SettingName;
                outSettingInfo.SettingValue        = defaultValue;
                outSettingInfo.SettingDefaultValue = defaultValue;
                outSettingInfo.SettingValueType    = inSettingInfo.SettingValueType;

                processStatus = true;
#if __TDD__
                SettingsBaseConfiguration.TDDData.SerializerAction(serializer.GetType(),
                                                                   TDDData.SerializerActions.LoadDefaults,
                                                                   inSettingInfo.SettingName,
                                                                   secure);
#endif // __TDD__
                break;
            }

            default:
                Debug.Assert(false);
                throw new NotImplementedException(String.Format("Not implemented for '{0}' mode", mode));
            }

            return(processStatus);
        }
コード例 #4
0
        /*
         * //------------------------------------------------------------------------------
         * static bool TryToMigrateFromPrevFormat(ISettingsSerializer serializer,
         *                                     string curentSettingName, bool secure,
         *                                     Type settingType,
         *                                     out object value)
         * {
         *  var delimeter = SettingNameFormatInfo.GetSettingNameDelimeter(serializer as ISettingsSerializerWishes);
         *  SettingNameFormatInfo fi = new SettingNameFormatInfo(curentSettingName, delimeter, SettingNameMode.Unknown);
         *
         *  try
         *  {
         *      InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Format migration",
         *                                               String.Format("Try to migrate '{0}' from previous format version", curentSettingName));
         *      bool loaded = serializer.Load(fi.V1SettingName, secure, out value);
         *
         *      if (loaded)
         *      {
         *          value = InternalConfiguration.PlatformHelper.CorrectSettingType_V1(value, settingType);
         *
         *          InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Format migration",
         *                                                   String.Format("From '{0}' to '{1}' - success",
         *                                                                 fi.V1SettingName, curentSettingName));
         *
         *          return true;
         *      }
         *      else
         *      {
         *          InternalConfiguration.PlatformHelper.Log(LogLevel.Info, "Format migration",
         *                                                   String.Format("'{0}' skip migration, no previous value", curentSettingName));
         *      }
         *  }
         *  catch (Exception exc)
         *  {
         *      InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
         *                                               String.Format("Exception during format migration from '{0}' to '{1}'",
         *                                                             fi.V1SettingName, curentSettingName),
         *                                               exc);
         *  }
         *
         *  value = null;
         *
         *  return false;
         * }
         */

        //------------------------------------------------------------------------------
        static public bool Load(ISettingsSerializer serializer,
                                SettingBaseAttribute attr,
                                string settingName, bool secure,
                                Wrapper attributeDefaultValueWrapper,
                                Type destinationType,
                                ref SettingInfo outSettingInfo,
                                FunctionalityHook functionalityHook)
        {
            bool   valueWasLoaded = false;
            object loadedValue    = null;

            object defaultValue;

            if (attributeDefaultValueWrapper != null)
            {
                defaultValue = attributeDefaultValueWrapper.Value;
            }
            else
            {
                defaultValue = GetTypeDefaultValue(destinationType);
            }

            try
            {
                valueWasLoaded = serializer.Load(settingName, secure, attr, out loadedValue);

                bool valueWasSet = false;

                if (valueWasLoaded)
                {
                    // CorrectValueType in case of exception default value will be returned
                    var outTuple = CorrectValueType(loadedValue, destinationType, defaultValue, functionalityHook);

                    outSettingInfo.SettingValue        = outTuple.Item1;
                    outSettingInfo.SettingValueType    = outTuple.Item2;
                    outSettingInfo.SettingDefaultValue = outTuple.Item3;
                    outSettingInfo.SettingName         = settingName;

                    valueWasSet = true;
                }
                else
                {
                    bool   valueWasMigrated = false;
                    object migratedValue    = null;

                    /*
                     * if (SettingsBaseConfiguration.EnableFormatMigration)
                     * {
                     *  try
                     *  {
                     *      valueWasMigrated = TryToMigrateFromPrevFormat(serializer, settingName, secure, destinationType, out migratedValue);
                     *  }
                     *  catch(Exception exc)
                     *  {
                     *      InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                     *                                           String.Format("Format migration exception for setting '{0}'", settingName),
                     *                                           exc);
                     *
                     *      return false;
                     *  }
                     * }
                     */

                    if (valueWasMigrated)
                    {
                        // in success, object with non-curent type can be returned
                        // correct setting type
                        // CorrectValueType function in case of exception return default value
                        var outTuple = CorrectValueType(migratedValue, destinationType, defaultValue, functionalityHook);

                        outSettingInfo.SettingValue        = outTuple.Item1;
                        outSettingInfo.SettingValueType    = outTuple.Item2;
                        outSettingInfo.SettingDefaultValue = outTuple.Item3;
                        outSettingInfo.SettingName         = settingName;

                        valueWasSet = true;
                    }
                    else if (attributeDefaultValueWrapper != null)
                    {
                        // Use default value only if it was set
                        outSettingInfo.SettingValue = defaultValue;

                        outSettingInfo.SettingValueType    = destinationType;
                        outSettingInfo.SettingDefaultValue = defaultValue;
                        outSettingInfo.SettingName         = settingName;

                        valueWasSet = true;
                    }
                }

                return(valueWasSet);
            }
            catch (ExceptionForUser exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         String.Format("Load exception for setting '{0}'", settingName),
                                                         exc.InnerException);

                throw exc.InnerException;
            }
            catch (Exception exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error,
                                                         String.Format("Load exception for setting '{0}'", settingName),
                                                         exc);
            }

            return(false);
        }
コード例 #5
0
        //------------------------------------------------------------------------------
        public static Tuple <object /*value*/, Type /*destinationType*/, object /*defaultValue*/> CorrectValueType(object value,
                                                                                                                   Type destinationType,
                                                                                                                   object defaultValue,
                                                                                                                   FunctionalityHook functionalityHook)
        {
            Tuple <object, Type, object> result;

            try
            {
                if (functionalityHook != null && functionalityHook.TypeCorrector != null)
                {
                    result = functionalityHook.TypeCorrector.CorrectType(value, destinationType, defaultValue);
                }
                else
                {
                    object settingValue = SettingsSerializerHelper.CorrectSettingType(value, destinationType);
                    result = Tuple.Create(settingValue, destinationType, defaultValue);
                }
            }
            catch (Exception exc)
            {
                InternalConfiguration.PlatformHelper.Log(LogLevel.Error, exc);

                //throw;
                result = Tuple.Create(defaultValue, destinationType, defaultValue);
            }

            return(result);
        }