MelonLoader

MelonLoader

242 Downloads

[Bug]: MelonPreferences LoadFromFile and SaveToFile does not generate Exception

MeitziQ opened this issue ยท 0 comments

commented

All of the following criteria must be met

  • All Requirements must be installed.
  • Full Latest.log file included. If no file exists then leave this unchecked and state so.

All of the following are optional to answer

  • Tried reinstalling the Game.
  • Tried reinstalling MelonLoader.
  • Tried restarting PC.
  • Was able to see the Start Screen.

Describe the issue.

When you are using MelonPreferences_Category methods LoadFromFile() and SaveToFile() you have no idea if they fail.
You can't fix or know situation where configuration files is broken and there is no real connection between file and mod anymore.

Example code:

using System;
using MelonLoader;
[assembly: MelonInfo(typeof(MelonTest.Main), "MelonTest", "0.2", "Me")]
namespace MelonTest {
    public class Main : MelonMod {
        private MelonPreferences_Category settingsCategory;
        private MelonPreferences_Entry<bool> modEnabled;
        public override void OnApplicationLateStart() {
            MelonLogger.Msg("Initializing settings..");
            settingsCategory = MelonPreferences.CreateCategory("OurFirstCategory");
            modEnabled = settingsCategory.CreateEntry<bool>("MODenabled", true, "", "break this file by setting this to example 'tre'");
            settingsCategory.SetFilePath("Mods/MelonTest.cfg", autoload: false);
            try {
                settingsCategory.LoadFromFile(); //This should generate Exception if load does not success. (or creating file first time does not success)
                settingsCategory.SaveToFile(); //This should generate Exception if saving does not happpen, but it does not
                MelonLogger.Msg("We got no errors. Value of modEnabled=" + modEnabled.Value.ToString());
            } catch (Exception exception) {
                MelonLogger.Msg("We got Exception: " + exception.ToString());
            }
        }       
    }   
}

After first run, corrupt cfg file somehow, example "MODenabled = tre" (real life scenario)
After this, there is no way know inside mod that file is corrupted.

Console will show:

[11:57:55.146] [MelonTest] Initializing settings..
[11:57:55.154] [MelonTest] [ERROR] Error while Loading Preferences from Mods/MelonTest.cfg: Tomlet.Exceptions.TomlInvalidValueException: Expected the start of a number, string literal, boolean, array, or table on line 3, found 't'

  at Tomlet.TomlParser.ReadValue (Tomlet.TomletStringReader reader) [0x002b9] in <239dd078bb4e49c8a1ff8200d76f2e32>:0 

  at Tomlet.TomlParser.ReadKeyValuePair (Tomlet.TomletStringReader reader, System.String& key, Tomlet.Models.TomlValue& value) [0x00045] in <239dd078bb4e49c8a1ff8200d76f2e32>:0 

  at Tomlet.TomlParser.Parse (System.String input) [0x0008c] in <239dd078bb4e49c8a1ff8200d76f2e32>:0 

  at Tomlet.TomlParser.ParseFile (System.String filePath) [0x0000c] in <239dd078bb4e49c8a1ff8200d76f2e32>:0 

  at MelonLoader.Preferences.IO.File.Load () [0x00017] in <239dd078bb4e49c8a1ff8200d76f2e32>:0 

  at MelonLoader.MelonPreferences.LoadFileAndRefreshCategories (MelonLoader.Preferences.IO.File file, System.Boolean printmsg) [0x00000] in <239dd078bb4e49c8a1ff8200d76f2e32>:0 
[11:57:55.155] [WARNING] Defaulting Mods/MelonTest.cfg to Fallback Functionality to further avoid File Corruption...
[11:57:55.156] [MelonTest] MelonPreferences Saved to Mods/MelonTest.cfg
[11:57:55.157] [MelonTest] We got no errors. Value of modEnabled=True

Please notice line "[MelonTest] MelonPreferences Saved to Mods/MelonTest.cfg" which is not incorrect. It is not true.
But in code, no errors all.
Expected result is raised Exception which would allow fix situation.

Did you attach your log file?

  • Yes, I attached my log file to the text box above.
  • No, I could not find a log file at {Game_Directory}\MelonLoader\Latest.log