Exemplo n.º 1
0
        /// <summary>
        /// Opens a settings provider stored at the given filename or creates a new one if the file does not exist
        /// </summary>
        /// <param name="filename">File from which to load the settings</param>
        /// <param name="manager">Instance of the current plugin manager</param>
        /// <returns>A settings provider corresponding to the given file</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="manager"/> is null</exception>
        public static SettingsProvider Open(string filename, PluginManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }

            if (manager.CurrentFileSystem.FileExists(filename))
            {
                var provider = Deserialize(manager.CurrentFileSystem.ReadAllText(filename), manager);
                provider.Filename = filename;
                return(provider);
            }
            else
            {
                var provider = new SettingsProvider(manager);
                provider.Filename = filename;
                return(provider);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deserializes the given data into a settings provider
        /// </summary>
        /// <param name="data">Data to deserialize.  If null, a new provider will be created.</param>
        /// <param name="manager">Instance of the current plugin manager</param>
        /// <returns>A settings provider corresponding to the given data</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="manager"/> is null</exception>
        public static SettingsProvider Deserialize(string data, PluginManager manager)
        {
            if (manager == null)
            {
                throw new ArgumentNullException(nameof(manager));
            }

            var provider = new SettingsProvider(manager);
            SerializedSettings s;

            if (string.IsNullOrEmpty(data))
            {
                s = new SerializedSettings();
            }
            else
            {
                s = Json.Deserialize <SerializedSettings>(data);
            }

            foreach (var item in s.Settings)
            {
                var valueType = ReflectionHelpers.GetTypeByName(item.Value.TypeName, manager);
                if (valueType == null)
                {
                    // If the type cannot be loaded, then it's possible that the PluginManager hasn't fully loaded everything yet.
                    // Store the serialized value and try this part again if anyone requests the property.
                    provider.UnloadableSettings[item.Key] = item.Value;
                }
                else
                {
                    provider.Settings[item.Key] = Json.Deserialize(valueType.AsType(), item.Value.Value);
                }
            }

            return(provider);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates an instance of the <see cref="ISettingsProvider"/> for the application environment.
 /// </summary>
 /// <param name="manager"></param>
 /// <returns>An instance of the <see cref="ISettingsProvider"/> for the application environment.</returns>
 public virtual ISettingsProvider GetSettingsProvider(PluginManager manager)
 {
     return(SettingsProvider.Open(GetSettingsFilename(), manager));
 }