Jump to content

Problems with ini file


AlwayzPatty

Recommended Posts

So I am making a plugin but when I try to load my INI file it just says that the object isn't set.

internal static class Settings
{
  public static Keys OpenMenuKey = Keys.U;
  public static Keys ModOpenMenuKey = Keys.None;
  static KeysConverter kc = new KeysConverter();

  public static void Load()
  {
    Common.Log("Loading config.");
    Common.Log(GetIniString("Keybindings", "MenuKey"));
    Common.Log(GetIniString("Keybindings", "MenuKeyModifier"));
    OpenMenuKey = (Keys)kc.ConvertFromString(GetIniString("Keybindings", "MenuKey", "U"));
    ModOpenMenuKey = (Keys)kc.ConvertFromString(GetIniString("Keybindings", "MenuKeyModifier", "None"));
  }

  private static InitializationFile initialiseFile()
  {
    InitializationFile ini = new InitializationFile(@"Plugins\LSPDFR\LevelSystem.ini");
    ini.Create();
    return ini;
  }

  private static string GetIniString(string section, string key, string defaultVal = "")
  {
    if (initialiseFile().Exists() && ValidateIni(section, key))
    {
      return initialiseFile().ReadString(section, key, defaultVal);
    }
    else
    {
      Common.Log("Ini file doesn't exist.");
      Game.DisplayNotification("mprpsymbol", "rp", "~b~LSPDFR Level System", "~r~Ini file error", $"Ini File doesnt exist!");
      return defaultVal;
    }
  }

  private static bool ValidateIni(string section, string key)
  {
    return initialiseFile().DoesSectionExist(section) && initialiseFile().DoesKeyExist(section, key);
  }
}

Also my Common Logs are empty where I request the value. Am I doing something wrong?

Also the "initialiseFile().Exists()" returns true but the "ValidateIni()" returns false

 

The INI I'm using:

[Keybindings]

MenuKey = U
MenuKeyModifier = None

 

Edited by AlwayzPatty

AlwayzPatty (Patrick)

Founder Overpunch Studios

 

The sun is always shining :)

Link to comment
Share on other sites

  • Management Team

It looks to me like you are creating the same ini file over and over again. You should initialize it once and then always use that instance. I'd suggest making a (static) variable such as "InitializationFile myIni" and in "initialiseFile" check if it is null. If so, you initialize it once. If it is not null, you return it instead.

Please do not PM me unless really necessary (knowing you helps). If you think you need my attention in a topic, tag me.

Link to comment
Share on other sites

The new code:

internal static class Settings
    {
        private static InitializationFile iniFile;

        #region User Settings
            public static Keys OpenMenuKey = Keys.U;
            public static Keys ModOpenMenuKey = Keys.None;
        #endregion

        #region System Settings
            public static float PercentageIncrementExpNeeded = .35f;
        #endregion

        static KeysConverter kc = new KeysConverter();

        public static void Load()
        {
            Common.Log("Loading config.");
            Common.Log(GetIniString("Keybindings", "MenuKey"));
            Common.Log(GetIniString("Keybindings", "MenuKeyModifier"));
            OpenMenuKey = (Keys)kc.ConvertFromString(GetIniString("Keybindings", "MenuKey", "U"));
            ModOpenMenuKey = (Keys)kc.ConvertFromString(GetIniString("Keybindings", "MenuKeyModifier", "None"));
        }

        private static InitializationFile initialiseFile()
        {
            if (iniFile != null && iniFile.Exists())
                return iniFile;

            iniFile = new InitializationFile(@"Plugins\LSPDFR\LevelSystem.ini");
            iniFile.Create();
            if (iniFile.Exists())
                return iniFile;
            else
                throw new Exception("Ini doesn't exist.");
        }

        private static string GetIniString(string section, string key, string defaultVal = "")
        {
            try
            {
                Common.Log($"[Requesting INI String] Section: {section}, Key: {key}, FileLocation: {initialiseFile().FileName}, IniValidation: {ValidateIni(section, key).ToString()}");
                if (ValidateIni(section, key))
                {
                    return initialiseFile().ReadString(section, key, defaultVal);
                }
                else
                {
                    Common.Log("Ini file doesn't exist.");
                    Game.DisplayNotification("mprpsymbol", "rp", "~b~LSPDFR Level System", "~r~Ini file error", $"Ini File doesnt exist!");
                    return defaultVal;
                }
            }
            catch(Exception e)
            {
                Common.Log($"[ERROR] {e.Message}");
                return defaultVal;
            }
        }

        private static bool ValidateIni(string section, string key)
        {
            return initialiseFile().DoesSectionExist(section) && initialiseFile().DoesKeyExist(section, key);
        }
    }

This results to this being said in the logs:

image.thumb.png.ce3ba3cc2d04a8bd4cf8ca93eb99450f.png

 

So your solution doesn't work.

I will try some stuff today myself and you will hear from me if I found the solution.

 

AlwayzPatty (Patrick)

Founder Overpunch Studios

 

The sun is always shining :)

Link to comment
Share on other sites

Fixed the issue it was apperantly an encoding issue that was already mentioned as a bug to RPH.
 

Thanks to PNWParksFan

Quote

just discovered a very interesting bug in InitializationFile

if the file format is UTF-8 with BOM, and your first INI section starts on line 0, then it won't parse it as a section correctly

and it seems that UTF-8 with BOM is the default encoding when you create a new empty data file using VS

if I change it to regular UTF-8 encoding, or add a blank line at the beginning of the file, the problem goes away

 

Edited by AlwayzPatty
added solution

AlwayzPatty (Patrick)

Founder Overpunch Studios

 

The sun is always shining :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...