API Reference | Settings

This section of documentation describes the SettingsManager class designed for managing module settings in the OutfitTool system.

SettingsManager<T> Class

Generic class for loading and saving module settings in a YAML file. All settings for all modules are stored in a single settings.yaml file in the %USERPROFILE%\\OutfitTools directory.

Class Definition

using System.Collections;
using System.IO;
using System.Reflection;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace OutfitTool.API1;

public class SettingsManager<T> where T : new()
{
    // Implementation...
}

Working Features

  • Each module stores its settings in a separate group within the YAML file, with the key being the settings class name
  • CamelCase is used for field naming in the YAML file
  • Supports caching of loaded settings for improved performance
  • Automatically creates the settings directory and file upon first use

Usage Example from Calculator Module

In the Calculator module, a CalculatorSettings settings class is implemented, which defines calculator window parameters and application behavior.

CalculatorSettings Class

namespace Calculator.Settings;

public class CalculatorSettings
{
    public const int DEFAULT_ROUND_TO = 10;

    public double Left = int.MaxValue;
    public double Top = int.MaxValue;
    public double Width = 247;
    public double Height = 288;
    public int RoundTo = DEFAULT_ROUND_TO;
    public bool CopyResultToClipboard = true;
}

Using SettingsManager in ModuleController

In the Calculator module, settings are loaded during initialization and saved upon shutdown:

public void Init(ILogger logger)
{
    // Create a settings manager instance
    var settingsManager = new SettingsManager<CalculatorSettings>();
    
    // Load settings
    var settings = settingsManager.LoadSettings();
    
    // Create a calculator window with loaded settings
    CalculatorWindow = new CalculatorForm(settings);
}

public void Shutdown()
{
    if (CalculatorWindow != null)
    {
        // Save current window settings
        CalculatorWindow.SaveWindowSettings();
        
        // Create a settings manager
        var settingsManager = new SettingsManager<CalculatorSettings>();
        
        // Get current settings from the window
        var settings = CalculatorWindow.GetSettings();
        
        // Save settings to file
        settingsManager.SaveSettings(settings);
        
        CalculatorWindow.Close(); 
    }
}

SettingsManager Methods

Constructor

Initializes the settings manager for the specified type. Automatically determines the settings file path and creates necessary directories.

T LoadSettings(bool force = false)

  • Loads settings from the YAML file
  • If force = true, forcibly re-reads the file, ignoring the cache
  • If the file does not exist or the settings group is not found, returns a new instance with default values
  • Supports fields and properties of types: bool, int, float, double, decimal, DateTime, string, arrays, lists, and dictionaries

void SaveSettings(T settings)

  • Saves the specified settings to the YAML file
  • Saves all existing settings groups, updating only the current type's group
  • Uses CamelCase for field naming in YAML

Usage Recommendations

  • Create a separate class for your module's settings
  • Use clear and understandable field and property names
  • Specify default values for all fields
  • Load settings during module initialization (Init)
  • Save settings upon module shutdown (Shutdown)
  • Consider saving settings when they change in the interface