@ -1,6 +1,8 @@
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.IO ;
using System.IO ;
using System.Linq ;
using System.Reflection ;
using System.Text ;
using System.Text ;
namespace IllusionPlugin
namespace IllusionPlugin
@ -8,21 +10,15 @@ namespace IllusionPlugin
/// <summary>
/// <summary>
/// Allows to get and set preferences for your mod.
/// Allows to get and set preferences for your mod.
/// </summary>
/// </summary>
public static class ModPrefs
{
private static IniFile _instance ;
private static IniFile Instance
{
get
{
if ( _instance = = null )
{
_instance = new IniFile ( Path . Combine ( Environment . CurrentDirectory , "UserData/modprefs.ini" ) ) ;
}
return _instance ;
}
}
public class ModPrefs {
internal static Dictionary < IPlugin , ModPrefs > ModPrefses { get ; set ; } = new Dictionary < IPlugin , ModPrefs > ( ) ;
private IniFile Instance ;
public ModPrefs ( IPlugin plugin ) {
Instance = new IniFile ( Path . Combine ( Environment . CurrentDirectory , $"UserData/ModPrefs/{plugin.Name}.ini" ) ) ;
ModPrefses . Add ( plugin , this ) ;
}
/// <summary>
/// <summary>
/// Gets a string from the ini.
/// Gets a string from the ini.
@ -32,9 +28,9 @@ namespace IllusionPlugin
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
/// <returns></returns>
public static string GetString ( string section , string name , string defaultValue = "" , bool autoSave = false )
public string GetString ( string section , string name , string defaultValue = "" , bool autoSave = false )
{
{
string value = Instance . IniReadValue ( section , name ) ;
var value = Instance . IniReadValue ( section , name ) ;
if ( value ! = "" )
if ( value ! = "" )
return value ;
return value ;
else if ( autoSave )
else if ( autoSave )
@ -51,10 +47,9 @@ namespace IllusionPlugin
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
/// <returns></returns>
public static int GetInt ( string section , string name , int defaultValue = 0 , bool autoSave = false )
public int GetInt ( string section , string name , int defaultValue = 0 , bool autoSave = false )
{
{
int value ;
if ( int . TryParse ( Instance . IniReadValue ( section , name ) , out value ) )
if ( int . TryParse ( Instance . IniReadValue ( section , name ) , out var value ) )
return value ;
return value ;
else if ( autoSave )
else if ( autoSave )
SetInt ( section , name , defaultValue ) ;
SetInt ( section , name , defaultValue ) ;
@ -71,10 +66,9 @@ namespace IllusionPlugin
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
/// <returns></returns>
public static float GetFloat ( string section , string name , float defaultValue = 0f , bool autoSave = false )
public float GetFloat ( string section , string name , float defaultValue = 0f , bool autoSave = false )
{
{
float value ;
if ( float . TryParse ( Instance . IniReadValue ( section , name ) , out value ) )
if ( float . TryParse ( Instance . IniReadValue ( section , name ) , out var value ) )
return value ;
return value ;
else if ( autoSave )
else if ( autoSave )
SetFloat ( section , name , defaultValue ) ;
SetFloat ( section , name , defaultValue ) ;
@ -90,7 +84,7 @@ namespace IllusionPlugin
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="defaultValue">Value that should be used when no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <param name="autoSave">Whether or not the default value should be written if no value is found.</param>
/// <returns></returns>
/// <returns></returns>
public static bool GetBool ( string section , string name , bool defaultValue = false , bool autoSave = false )
public bool GetBool ( string section , string name , bool defaultValue = false , bool autoSave = false )
{
{
string sVal = GetString ( section , name , null ) ;
string sVal = GetString ( section , name , null ) ;
if ( sVal = = "1" | | sVal = = "0" )
if ( sVal = = "1" | | sVal = = "0" )
@ -111,7 +105,7 @@ namespace IllusionPlugin
/// <param name="section">Section of the key.</param>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="name">Name of the key.</param>
/// <returns></returns>
/// <returns></returns>
public static bool HasKey ( string section , string name )
public bool HasKey ( string section , string name )
{
{
return Instance . IniReadValue ( section , name ) ! = null ;
return Instance . IniReadValue ( section , name ) ! = null ;
}
}
@ -122,7 +116,7 @@ namespace IllusionPlugin
/// <param name="section">Section of the key.</param>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
/// <param name="value">Value that should be written.</param>
public static void SetFloat ( string section , string name , float value )
public void SetFloat ( string section , string name , float value )
{
{
Instance . IniWriteValue ( section , name , value . ToString ( ) ) ;
Instance . IniWriteValue ( section , name , value . ToString ( ) ) ;
}
}
@ -133,7 +127,7 @@ namespace IllusionPlugin
/// <param name="section">Section of the key.</param>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
/// <param name="value">Value that should be written.</param>
public static void SetInt ( string section , string name , int value )
public void SetInt ( string section , string name , int value )
{
{
Instance . IniWriteValue ( section , name , value . ToString ( ) ) ;
Instance . IniWriteValue ( section , name , value . ToString ( ) ) ;
@ -145,7 +139,7 @@ namespace IllusionPlugin
/// <param name="section">Section of the key.</param>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
/// <param name="value">Value that should be written.</param>
public static void SetString ( string section , string name , string value )
public void SetString ( string section , string name , string value )
{
{
Instance . IniWriteValue ( section , name , value ) ;
Instance . IniWriteValue ( section , name , value ) ;
@ -157,10 +151,17 @@ namespace IllusionPlugin
/// <param name="section">Section of the key.</param>
/// <param name="section">Section of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="name">Name of the key.</param>
/// <param name="value">Value that should be written.</param>
/// <param name="value">Value that should be written.</param>
public static void SetBool ( string section , string name , bool value )
public void SetBool ( string section , string name , bool value )
{
{
Instance . IniWriteValue ( section , name , value ? "1" : "0" ) ;
Instance . IniWriteValue ( section , name , value ? "1" : "0" ) ;
}
}
}
public static class ModPrefsExtensions {
public static ModPrefs GetModPrefs ( this IPlugin plugin ) {
return ModPrefs . ModPrefses . First ( o = > o . Key = = plugin ) . Value ;
}
}
}
}
}