A fork of Eusth's IPA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 line
6.0KB

  1. using System;
  2. using System.IO;
  3. namespace IllusionPlugin
  4. {
  5. /// <summary>
  6. /// Allows to get and set preferences for your mod.
  7. /// </summary>
  8. public static class ModPrefs
  9. {
  10. private static IniFile _instance;
  11. private static IniFile Instance
  12. {
  13. get
  14. {
  15. if (_instance == null)
  16. {
  17. _instance = new IniFile(Path.Combine(Environment.CurrentDirectory, "UserData/modprefs.ini"));
  18. }
  19. return _instance;
  20. }
  21. }
  22. /// <summary>
  23. /// Gets a string from the ini.
  24. /// </summary>
  25. /// <param name="section">Section of the key.</param>
  26. /// <param name="name">Name of the key.</param>
  27. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  28. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  29. /// <returns></returns>
  30. public static string GetString(string section, string name, string defaultValue = "", bool autoSave = false)
  31. {
  32. string value = Instance.IniReadValue(section, name);
  33. if (value != "")
  34. return value;
  35. else if (autoSave)
  36. SetString(section, name, defaultValue);
  37. return defaultValue;
  38. }
  39. /// <summary>
  40. /// Gets an int from the ini.
  41. /// </summary>
  42. /// <param name="section">Section of the key.</param>
  43. /// <param name="name">Name of the key.</param>
  44. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  45. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  46. /// <returns></returns>
  47. public static int GetInt(string section, string name, int defaultValue = 0, bool autoSave = false)
  48. {
  49. int value;
  50. if (int.TryParse(Instance.IniReadValue(section, name), out value))
  51. return value;
  52. else if (autoSave)
  53. SetInt(section, name, defaultValue);
  54. return defaultValue;
  55. }
  56. /// <summary>
  57. /// Gets a float from the ini.
  58. /// </summary>
  59. /// <param name="section">Section of the key.</param>
  60. /// <param name="name">Name of the key.</param>
  61. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  62. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  63. /// <returns></returns>
  64. public static float GetFloat(string section, string name, float defaultValue = 0f, bool autoSave = false)
  65. {
  66. float value;
  67. if (float.TryParse(Instance.IniReadValue(section, name), out value))
  68. return value;
  69. else if (autoSave)
  70. SetFloat(section, name, defaultValue);
  71. return defaultValue;
  72. }
  73. /// <summary>
  74. /// Gets a bool from the ini.
  75. /// </summary>
  76. /// <param name="section">Section of the key.</param>
  77. /// <param name="name">Name of the key.</param>
  78. /// <param name="defaultValue">Value that should be used when no value is found.</param>
  79. /// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
  80. /// <returns></returns>
  81. public static bool GetBool(string section, string name, bool defaultValue = false, bool autoSave = false)
  82. {
  83. string sVal = GetString(section, name, null);
  84. if (sVal == "1" || sVal == "0")
  85. {
  86. return sVal == "1";
  87. }
  88. else if (autoSave)
  89. {
  90. SetBool(section, name, defaultValue);
  91. }
  92. return defaultValue;
  93. }
  94. /// <summary>
  95. /// Checks whether or not a key exists in the ini.
  96. /// </summary>
  97. /// <param name="section">Section of the key.</param>
  98. /// <param name="name">Name of the key.</param>
  99. /// <returns></returns>
  100. public static bool HasKey(string section, string name)
  101. {
  102. return Instance.IniReadValue(section, name) != null;
  103. }
  104. /// <summary>
  105. /// Sets a float in the ini.
  106. /// </summary>
  107. /// <param name="section">Section of the key.</param>
  108. /// <param name="name">Name of the key.</param>
  109. /// <param name="value">Value that should be written.</param>
  110. public static void SetFloat(string section, string name, float value)
  111. {
  112. Instance.IniWriteValue(section, name, value.ToString());
  113. }
  114. /// <summary>
  115. /// Sets an int in the ini.
  116. /// </summary>
  117. /// <param name="section">Section of the key.</param>
  118. /// <param name="name">Name of the key.</param>
  119. /// <param name="value">Value that should be written.</param>
  120. public static void SetInt(string section, string name, int value)
  121. {
  122. Instance.IniWriteValue(section, name, value.ToString());
  123. }
  124. /// <summary>
  125. /// Sets a string in the ini.
  126. /// </summary>
  127. /// <param name="section">Section of the key.</param>
  128. /// <param name="name">Name of the key.</param>
  129. /// <param name="value">Value that should be written.</param>
  130. public static void SetString(string section, string name, string value)
  131. {
  132. Instance.IniWriteValue(section, name, value);
  133. }
  134. /// <summary>
  135. /// Sets a bool in the ini.
  136. /// </summary>
  137. /// <param name="section">Section of the key.</param>
  138. /// <param name="name">Name of the key.</param>
  139. /// <param name="value">Value that should be written.</param>
  140. public static void SetBool(string section, string name, bool value)
  141. {
  142. Instance.IniWriteValue(section, name, value ? "1" : "0");
  143. }
  144. }
  145. }