public MelonPreferences_Entry <T> CreateEntry <T>(string identifier, T default_value, string display_name = null,
                                                          string description = null, bool is_hidden = false, bool dont_save_default = false, Preferences.ValueValidator validator = null, string oldIdentifier = null)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new Exception("identifier is null or empty when calling CreateEntry");
            }

            if (display_name == null)
            {
                display_name = identifier;
            }

            var entry = GetEntry <T>(identifier);

            if (entry != null)
            {
                throw new Exception($"Calling CreateEntry for { display_name } when it Already Exists");
            }

            if (validator != null && !validator.IsValid(default_value))
            {
                throw new ArgumentException($"Default value '{default_value}' is invalid according to the provided ValueValidator!");
            }

            if (oldIdentifier != null)
            {
                if (HasEntry(oldIdentifier))
                {
                    throw new Exception($"Unable to rename '{oldIdentifier}' when it got already loaded");
                }

                RenameEntry(oldIdentifier, identifier);
            }

            entry = new MelonPreferences_Entry <T>
            {
                Identifier      = identifier,
                DisplayName     = display_name,
                Description     = description,
                IsHidden        = is_hidden,
                DontSaveDefault = dont_save_default,
                Category        = this,
                DefaultValue    = default_value,
                Value           = default_value,
                Validator       = validator,
            };

            Preferences.IO.File currentFile = File;
            if (currentFile == null)
            {
                currentFile = MelonPreferences.DefaultFile;
            }
            currentFile.SetupEntryFromRawValue(entry);

            Entries.Add(entry);

            return(entry);
        }
Exemplo n.º 2
0
        public static string GetString(string section, string name)
        {
            MelonPreferences_Category category = MelonPreferences.GetCategory(section);

            if (category == null)
            {
                return(null);
            }
            MelonPreferences_Entry entry = category.GetEntry(name);

            if (entry == null)
            {
                return(null);
            }
            return(entry.GetValueAsString());
        }
        public bool DeleteEntry(string identifier)
        {
            MelonPreferences_Entry entry = GetEntry(identifier);

            if (entry != null)
            {
                Entries.Remove(entry);
            }

            Preferences.IO.File currentfile = File;
            if (currentfile == null)
            {
                currentfile = MelonPreferences.DefaultFile;
            }

            return(currentfile.RemoveFromDocument(Identifier, identifier));
        }
        public bool RenameEntry(string identifier, string newIdentifier)
        {
            MelonPreferences_Entry entry = GetEntry(identifier);

            if (entry != null)
            {
                entry.Identifier = newIdentifier;
            }

            Preferences.IO.File currentfile = File;
            if (currentfile == null)
            {
                currentfile = MelonPreferences.DefaultFile;
            }

            return(currentfile.RenameEntryInDocument(Identifier, identifier, newIdentifier));
        }
Exemplo n.º 5
0
        public static void SetString(string section, string name, string value)
        {
            MelonPreferences_Category category = MelonPreferences.GetCategory(section);

            if (category == null)
            {
                return;
            }
            MelonPreferences_Entry entry = category.GetEntry(name);

            if (entry == null)
            {
                return;
            }
            switch (entry)
            {
            case MelonPreferences_Entry <string> stringEntry:
                stringEntry.Value = value;
                break;

            case MelonPreferences_Entry <int> intEntry:
                if (int.TryParse(value, out var parsedInt))
                {
                    intEntry.Value = parsedInt;
                }
                break;

            case MelonPreferences_Entry <float> floatEntry:
                if (float.TryParse(value, out var parsedFloat))
                {
                    floatEntry.Value = parsedFloat;
                }
                break;

            case MelonPreferences_Entry <bool> boolEntry:
                if (value.ToLower().StartsWith("true") || value.ToLower().StartsWith("false"))
                {
                    boolEntry.Value = value.ToLower().StartsWith("true");
                }
                break;
            }
        }
Exemplo n.º 6
0
 public PrefDesc(MelonPreferences_Entry entry) : base(entry)
 {
 }