Ejemplo n.º 1
0
 public void SaveToFile(bool printmsg = true)
 {
     Preferences.IO.File currentfile = File;
     if (currentfile == null)
     {
         currentfile = MelonPreferences.DefaultFile;
     }
     foreach (MelonPreferences_Entry entry in Entries)
     {
         if (!(entry.DontSaveDefault && entry.GetValueAsString() == entry.GetDefaultValueAsString()) && entry.GetValueAsString() != null)
         {
             currentfile.InsertIntoDocument(Identifier, entry.Identifier, entry.Save());
         }
     }
     try
     {
         currentfile.Save();
     }
     catch (Exception ex)
     {
         MelonLogger.Error($"Error while Saving Preferences to {currentfile.FilePath}: {ex}");
         currentfile.WasError = true;
     }
     if (printmsg)
     {
         MelonLogger.Msg($"MelonPreferences Saved to {currentfile.FilePath}");
     }
     MelonHandler.OnPreferencesSaved();
 }
Ejemplo n.º 2
0
 public void SetFilePath(string filepath, bool autoload = true)
 {
     if (File != null)
     {
         Preferences.IO.File oldfile = File;
         File = null;
         if (!MelonPreferences.IsFileInUse(oldfile))
         {
             oldfile.FileWatcher.Destroy();
             MelonPreferences.PrefFiles.Remove(oldfile);
         }
     }
     if (!string.IsNullOrEmpty(filepath) && !MelonPreferences.IsFilePathDefault(filepath))
     {
         File = MelonPreferences.GetPrefFileFromFilePath(filepath);
         if (File == null)
         {
             File = new Preferences.IO.File(filepath);
             MelonPreferences.PrefFiles.Add(File);
         }
     }
     if (autoload)
     {
         MelonPreferences.LoadFileAndRefreshCategories(File);
     }
 }
        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);
        }
Ejemplo n.º 4
0
 public void LoadFromFile(bool printmsg = true)
 {
     Preferences.IO.File currentfile = File;
     if (currentfile == null)
     {
         currentfile = MelonPreferences.DefaultFile;
     }
     MelonPreferences.LoadFileAndRefreshCategories(currentfile, printmsg);
 }
        private MelonPreferences_ReflectiveCategory(Type type, string categoryName, string displayName)
        {
            SystemType  = type;
            Identifier  = categoryName;
            DisplayName = displayName;

            IO.File currentFile = File;
            if (currentFile == null)
            {
                currentFile = MelonPreferences.DefaultFile;
            }
            if (!(currentFile.TryGetCategoryTable(Identifier) is { } table))
            {
                LoadDefaults();
            }
Ejemplo n.º 6
0
 public void ResetFilePath()
 {
     if (File == null)
     {
         return;
     }
     Preferences.IO.File oldfile = File;
     File = null;
     if (!MelonPreferences.IsFileInUse(oldfile))
     {
         oldfile.FileWatcher.Destroy();
         MelonPreferences.PrefFiles.Remove(oldfile);
     }
     MelonPreferences.LoadFileAndRefreshCategories(MelonPreferences.DefaultFile);
 }
        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));
        }