예제 #1
0
        /// <summary>
        /// Reloads the config from disk. Unsaved changes are lost.
        /// </summary>
        public void Reload()
        {
            lock (_ioLock)
            {
                HomelessEntries.Clear();

                try
                {
                    _disableSaving = true;

                    string currentSection = string.Empty;

                    foreach (string rawLine in File.ReadAllLines(ConfigFilePath))
                    {
                        string line = rawLine.Trim();

                        if (line.StartsWith("#"))                         //comment
                        {
                            continue;
                        }

                        if (line.StartsWith("[") && line.EndsWith("]"))                         //section
                        {
                            currentSection = line.Substring(1, line.Length - 2);
                            continue;
                        }

                        string[] split = line.Split('=');                         //actual config line
                        if (split.Length != 2)
                        {
                            continue;                             //empty/invalid line
                        }
                        string currentKey   = split[0].Trim();
                        string currentValue = split[1].Trim();

                        var definition = new ConfigDefinition(currentSection, currentKey);

                        Entries.TryGetValue(definition, out ConfigEntryBase entry);

                        if (entry != null)
                        {
                            entry.SetSerializedValue(currentValue);
                        }
                        else
                        {
                            HomelessEntries[definition] = currentValue;
                        }
                    }
                }
                finally
                {
                    _disableSaving = false;
                }
            }

            OnConfigReloaded();
        }
예제 #2
0
        /// <summary>
        /// Writes the config to disk.
        /// </summary>
        public void Save()
        {
            lock (_ioLock)
            {
                if (_disableSaving)
                {
                    return;
                }

                string directoryName = Path.GetDirectoryName(ConfigFilePath);
                if (directoryName != null)
                {
                    Directory.CreateDirectory(directoryName);
                }

                using (var writer = new StreamWriter(File.Create(ConfigFilePath), Encoding.UTF8))
                {
                    if (_ownerMetadata != null)
                    {
                        writer.WriteLine($"## Settings file was created by plugin {_ownerMetadata.Name} v{_ownerMetadata.Version}");
                        writer.WriteLine($"## Plugin GUID: {_ownerMetadata.GUID}");
                        writer.WriteLine();
                    }

                    var allConfigEntries = Entries.Select(x => new { x.Key, entry = x.Value, value = x.Value.GetSerializedValue() })
                                           .Concat(HomelessEntries.Select(x => new { x.Key, entry = (ConfigEntryBase)null, value = x.Value }));

                    foreach (var sectionKv in allConfigEntries.GroupBy(x => x.Key.Section).OrderBy(x => x.Key))
                    {
                        // Section heading
                        writer.WriteLine($"[{sectionKv.Key}]");

                        foreach (var configEntry in sectionKv)
                        {
                            writer.WriteLine();

                            configEntry.entry?.WriteDescription(writer);

                            writer.WriteLine($"{configEntry.Key.Key} = {configEntry.value}");
                        }

                        writer.WriteLine();
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Create a new setting. The setting is saved to drive and loaded automatically.
        /// Each definition can be used to add only one setting, trying to add a second setting will throw an exception.
        /// </summary>
        /// <typeparam name="T">Type of the value contained in this setting.</typeparam>
        /// <param name="configDefinition">Section and Key of the setting.</param>
        /// <param name="defaultValue">Value of the setting if the setting was not created yet.</param>
        /// <param name="configDescription">Description of the setting shown to the user and other metadata.</param>
        public ConfigEntry <T> AddSetting <T>(ConfigDefinition configDefinition, T defaultValue, ConfigDescription configDescription = null)
        {
            if (!TomlTypeConverter.CanConvert(typeof(T)))
            {
                throw new ArgumentException($"Type {typeof(T)} is not supported by the config system. Supported types: {string.Join(", ", TomlTypeConverter.GetSupportedTypes().Select(x => x.Name).ToArray())}");
            }

            lock (_ioLock)
            {
                if (Entries.ContainsKey(configDefinition))
                {
                    throw new ArgumentException("The setting " + configDefinition + " has already been created. Use GetSetting to get it.");
                }

                try
                {
                    _disableSaving = true;

                    var entry = new ConfigEntry <T>(this, configDefinition, defaultValue, configDescription);
                    Entries[configDefinition] = entry;

                    if (HomelessEntries.TryGetValue(configDefinition, out string homelessValue))
                    {
                        entry.SetSerializedValue(homelessValue);
                        HomelessEntries.Remove(configDefinition);
                    }

                    _disableSaving = false;
                    if (SaveOnConfigSet)
                    {
                        Save();
                    }

                    return(entry);
                }
                finally
                {
                    _disableSaving = false;
                }
            }
        }