A fork of Eusth's IPA
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

168 satır
6.1KB

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