SettingsFileAccess<TSettings>
A class in Root.Township, Alta.Utilities.
Loads a settings class from a JSON file (with Resources defaults), watches the file for changes and saves it back. SettingsManager.GameConfig is one. See Settings and files.
Constructors
Fields
5 private and internal fields
| Member | What it is |
|---|
private IAltaFile file | The settings file (on disk, or a database settings file). |
private FileSystemWatcher fileSystemWatcher | Watcher for changes to the settings file. |
private readonly bool isDummySettings | True for the no-file constructor: settings live in memory only and saving does nothing. |
private static readonly Logger logger | NLog logger for this class. |
private TSettings settings | The loaded settings; read through Settings, which waits for a pending read to finish. |
Properties
| Member | What it is |
|---|
TSettings Settings { get; } | The current settings. Blocks to finish a read that is still in progress. |
1 private and internal property
| Member | What it is |
|---|
private ISettings Alta.Core.Settings.ISettingsFileAccess.BaseSettings { get; } | The loaded settings as plain ISettings. |
Events
| Member | What it is |
|---|
event Action<ISettings> Changed | Raised with the settings as ISettings after each read or SetAllSettings. |
event Action<TSettings> SettingsChanged | Raised on the main thread after each read or SetAllSettings. |
Methods
| Member | What it is |
|---|
void ChangeFile(IAltaFolder folder, string name = null) | Loads defaults, then reads name.json from the folder and watches it for changes. |
void NotifySettingsChanged() | Raises SettingsChanged on the main thread. Does nothing for dummy settings. |
void Save() | Writes the settings file. Does nothing for dummy settings. |
async void SaveAsync() | Waits for any read in progress, then writes the file. |
void SetAllSettings(TSettings newSettings) | Replaces the whole settings object, saves it and raises the change events. |
bool TryGet<TValue>(string name, out TValue value) | Reads a settings property by name. |
protected override void Finalize() | Destructor: stops and disposes the file watcher. |
4 private and internal methods
| Member | What it is |
|---|
private void CreateWatcher(IAltaFolder folder, string fileName) | Watches the settings file on disk so edits to it are reloaded. |
private void LoadDefaults(string name) | Loads defaults from a Resources text asset named after the file plus _Defaults, or a new instance if there is none. |
private void OnFileChanged(object sender, FileSystemEventArgs e) | File watcher handler that reloads the file when it is written. |
private async void StartRead() | Reads the file into the settings; if there is no file yet, writes the defaults. Then raises SettingsChanged. |
Used in
Members of other types that take, return or hold a SettingsFileAccess<TSettings>: