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.

167 lines
6.0KB

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