示例#1
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> Bind <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.TryGetValue(configDefinition, out var rawEntry))
                {
                    return((ConfigEntry <T>)rawEntry);
                }

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

                Entries[configDefinition] = entry;

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

                if (SaveOnConfigSet)
                {
                    Save();
                }

                return(entry);
            }
        }
示例#2
0
        /// <summary>
        /// Reloads the config from disk. Unsaved changes are lost.
        /// </summary>
        public void Reload()
        {
            lock (_ioLock)
            {
                OrphanedEntries.Clear();

                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(new[] { '=' }, 2);                     //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
                    {
                        OrphanedEntries[definition] = currentValue;
                    }
                }
            }

            OnConfigReloaded();
        }
示例#3
0
        /// <summary>
        /// Writes the config to disk.
        /// </summary>
        public void Save()
        {
            lock (_ioLock)
            {
                string directoryName = Path.GetDirectoryName(ConfigFilePath);
                if (directoryName != null)
                {
                    Directory.CreateDirectory(directoryName);
                }

                using (var writer = new StreamWriter(ConfigFilePath, false, 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(OrphanedEntries.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();
                    }
                }
            }
        }