DocFX + Singulink = ♥

Class JsonRegistry<TKey, TValue>

Represents a collection of values with unique IDs that can be loaded from JSON.

Inheritance
object
JsonRegistry<TKey, TValue>
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: SlugBase
Assembly: SlugBase.dll
Syntax
public class JsonRegistry<TKey, TValue> where TKey : ExtEnum<TKey>
Type Parameters
Name Description
TKey

The type of the ExtEnum`1 keys.

TValue

The type of the values.

Constructors

View Source

JsonRegistry(Func<TKey, JsonObject, TValue>)

Create a new registry.

Declaration
public JsonRegistry(Func<TKey, JsonObject, TValue> fromJson)
Parameters
Type Name Description
Func<TKey, JsonObject, TValue> fromJson

The factory that creates values from JSON.

Properties

View Source

Keys

A collection of all keys used by this registry.

Declaration
public IEnumerable<TKey> Keys { get; }
Property Value
Type Description
IEnumerable<TKey>
View Source

Values

A collection of all values registered.

Declaration
public IEnumerable<TValue> Values { get; }
Property Value
Type Description
IEnumerable<TValue>
View Source

WatchForChanges

Whether this registry should track files to reload. Call ReloadChangedFiles() to apply changes.

Declaration
public bool WatchForChanges { get; set; }
Property Value
Type Description
bool

Methods

View Source

Add(JsonObject)

Load a TValue from JSON and link it to a new ExtEnum`1 ID.

Declaration
public KeyValuePair<TKey, TValue> Add(JsonObject json)
Parameters
Type Name Description
JsonObject json

The json data for the new object.

Returns
Type Description
KeyValuePair<TKey, TValue>

The registered key and value.

View Source

AddFromFile(string)

Parse a file as JSON and link it to a new ExtEnum`1 ID.

Declaration
public KeyValuePair<TKey, TValue> AddFromFile(string path)
Parameters
Type Name Description
string path

The file path to the json.

Returns
Type Description
KeyValuePair<TKey, TValue>

The registered key and value.

View Source

GetOrDefault(TKey)

Get a registered value by key.

Declaration
public TValue GetOrDefault(TKey id)
Parameters
Type Name Description
TKey id

The unique ID of the value.

Returns
Type Description
TValue

The registered value with the unique ID.

View Source

ReloadChangedFiles()

Reload all JSON files that have been modified.

Declaration
public void ReloadChangedFiles()
View Source

Remove(TKey)

Unregister a value.

Declaration
public void Remove(TKey id)
Parameters
Type Name Description
TKey id
Exceptions
Type Condition
ArgumentNullException
ArgumentException
View Source

ScanDirectory(string)

Register all JSON files in a directory using AddFromFile(string).

Declaration
public void ScanDirectory(string directory)
Parameters
Type Name Description
string directory

The directory to search.

View Source

TryAddFromFile(string)

Parse a file as JSON and link it to a new ExtEnum`1 ID. Load errors can be monitored with LoadFailed.

Declaration
public KeyValuePair<TKey, TValue>? TryAddFromFile(string path)
Parameters
Type Name Description
string path

The file path to the json.

Returns
Type Description
KeyValuePair<TKey, TValue>?

The registered key and value, or null if loading failed.

View Source

TryGet(TKey, out TValue)

Get a registered value by key.

Declaration
public bool TryGet(TKey id, out TValue value)
Parameters
Type Name Description
TKey id

The unique ID of the value.

TValue value

The registered value with the unique ID.

Returns
Type Description
bool

true if the value was found, false otherwise.

View Source

TryGetPath(TKey, out string)

Gets the path to a registered value by key.

Declaration
public bool TryGetPath(TKey id, out string path)
Parameters
Type Name Description
TKey id

The unique ID of the value.

string path

The path to the value's JSON file.

Returns
Type Description
bool

true if the value was found and has a path, false otherwise.

Events

View Source

EntryReloaded

Occurs when a loaded JSON file is modified while WatchForChanges is set.

Declaration
public event EventHandler<JsonRegistry<TKey, TValue>.ReloadedEventArgs> EntryReloaded
Event Type
Type Description
EventHandler<JsonRegistry<TKey, TValue>.ReloadedEventArgs>
View Source

LoadFailed

Occurs when a JSON file fails to load or reload.

Declaration
public event EventHandler<JsonRegistry<TKey, TValue>.LoadErrorEventArgs> LoadFailed
Event Type
Type Description
EventHandler<JsonRegistry<TKey, TValue>.LoadErrorEventArgs>