Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        public static void AddUnityEngineConverters()
        {
            var colorConverter = new TypeConverter
            {
                ConvertToString = (obj, type) => ColorUtility.ToHtmlStringRGBA((Color)obj),
                ConvertToObject = (str, type) =>
                {
                    if (!ColorUtility.TryParseHtmlString("#" + str.Trim('#', ' '), out var c))
                    {
                        throw new FormatException("Invalid color string, expected hex #RRGGBBAA");
                    }
                    return(c);
                },
            };

            TomlTypeConverter.AddConverter(typeof(Color), colorConverter);

            var jsonConverter = new TypeConverter
            {
                ConvertToString = (obj, type) => JsonUtility.ToJson(obj),
                ConvertToObject = (str, type) => JsonUtility.FromJson(type: type, json: str),
            };

            TomlTypeConverter.AddConverter(typeof(Vector2), jsonConverter);
            TomlTypeConverter.AddConverter(typeof(Vector3), jsonConverter);
            TomlTypeConverter.AddConverter(typeof(Vector4), jsonConverter);
            TomlTypeConverter.AddConverter(typeof(Quaternion), jsonConverter);
        }
Exemplo n.º 3
0
 static KeyboardShortcut()
 {
     TomlTypeConverter.AddConverter(
         typeof(KeyboardShortcut),
         new TypeConverter
     {
         ConvertToString = (o, type) => ((KeyboardShortcut)o).Serialize(),
         ConvertToObject = (s, type) => Deserialize(s)
     });
 }
Exemplo n.º 4
0
        public ConfigWrapper <T> Wrap <T>(ConfigDefinition configDefinition, T defaultValue = default(T))
        {
            if (!Cache.ContainsKey(configDefinition))
            {
                Cache.Add(configDefinition, TomlTypeConverter.ConvertToString(defaultValue));
                Save();
            }
            else
            {
                var original = Cache.Keys.First(x => x.Equals(configDefinition));

                if (original.Description != configDefinition.Description)
                {
                    original.Description = configDefinition.Description;
                    Save();
                }
            }

            return(new ConfigWrapper <T>(this, configDefinition));
        }
Exemplo n.º 5
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;
                }
            }
        }