Skip to main content

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​

MemberWhat it is
SettingsFileAccess(string databaseIdentifier)Creates a SettingsFileAccess with default values.
SettingsFileAccess(IAltaFolder folder, string name = null)Creates a SettingsFileAccess with default values.
SettingsFileAccess()Creates a SettingsFileAccess with default values.

Fields​

5 private and internal fields
MemberWhat it is
private IAltaFile fileThe settings file (on disk, or a database settings file).
private FileSystemWatcher fileSystemWatcherWatcher for changes to the settings file.
private readonly bool isDummySettingsTrue for the no-file constructor: settings live in memory only and saving does nothing.
private static readonly Logger loggerNLog logger for this class.
private TSettings settingsThe loaded settings; read through Settings, which waits for a pending read to finish.

Properties​

MemberWhat it is
TSettings Settings { get; }The current settings. Blocks to finish a read that is still in progress.
1 private and internal property
MemberWhat it is
private ISettings Alta.Core.Settings.ISettingsFileAccess.BaseSettings { get; }The loaded settings as plain ISettings.

Events​

MemberWhat it is
event Action<ISettings> ChangedRaised with the settings as ISettings after each read or SetAllSettings.
event Action<TSettings> SettingsChangedRaised on the main thread after each read or SetAllSettings.

Methods​

MemberWhat 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
MemberWhat 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&lt;TSettings>: