2020-04-26 02:19:58 +02:00
|
|
|
using System;
|
2020-03-21 00:49:43 +01:00
|
|
|
using System.Collections.Generic;
|
2020-11-28 22:48:30 +01:00
|
|
|
using System.Runtime.Serialization;
|
2020-03-21 00:49:43 +01:00
|
|
|
|
|
|
|
namespace MLEM.Misc {
|
2020-12-18 16:39:52 +01:00
|
|
|
/// <inheritdoc />
|
2020-11-28 22:48:30 +01:00
|
|
|
[DataContract]
|
2020-12-18 16:39:52 +01:00
|
|
|
public class GenericDataHolder : IGenericDataHolder {
|
2020-03-21 00:49:43 +01:00
|
|
|
|
2020-12-18 16:39:52 +01:00
|
|
|
[DataMember(EmitDefaultValue = false)]
|
2020-03-21 00:49:43 +01:00
|
|
|
private Dictionary<string, object> data;
|
|
|
|
|
2020-12-18 16:39:52 +01:00
|
|
|
/// <inheritdoc />
|
2020-03-21 00:49:43 +01:00
|
|
|
public void SetData(string key, object data) {
|
|
|
|
if (data == default) {
|
2020-04-26 02:19:58 +02:00
|
|
|
if (this.data != null)
|
|
|
|
this.data.Remove(key);
|
2020-03-21 00:49:43 +01:00
|
|
|
} else {
|
2020-04-26 02:19:58 +02:00
|
|
|
if (this.data == null)
|
|
|
|
this.data = new Dictionary<string, object>();
|
2020-03-21 00:49:43 +01:00
|
|
|
this.data[key] = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:39:52 +01:00
|
|
|
/// <inheritdoc />
|
2020-03-21 00:49:43 +01:00
|
|
|
public T GetData<T>(string key) {
|
2021-02-02 11:01:30 +01:00
|
|
|
if (this.data != null && this.data.TryGetValue(key, out var val))
|
|
|
|
return (T) val;
|
2020-03-21 00:49:43 +01:00
|
|
|
return default;
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:39:52 +01:00
|
|
|
/// <inheritdoc />
|
2020-03-21 00:49:43 +01:00
|
|
|
public IReadOnlyCollection<string> GetDataKeys() {
|
2020-04-26 02:19:58 +02:00
|
|
|
if (this.data == null)
|
|
|
|
return Array.Empty<string>();
|
2020-03-21 00:49:43 +01:00
|
|
|
return this.data.Keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-12-18 16:39:52 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents an object that can hold generic key-value based data.
|
|
|
|
/// A lot of MLEM components extend this class to allow for users to add additional data to them easily.
|
|
|
|
/// </summary>
|
|
|
|
public interface IGenericDataHolder {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Store a piece of generic data on this object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key to store the data by</param>
|
|
|
|
/// <param name="data">The data to store in the object</param>
|
|
|
|
void SetData(string key, object data);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a piece of generic data of the given type on this object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key that the data is stored by</param>
|
|
|
|
/// <typeparam name="T">The type of the data stored</typeparam>
|
|
|
|
/// <returns>The data, or default if it doesn't exist</returns>
|
|
|
|
T GetData<T>(string key);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns all of the generic data that this object stores.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>The generic data on this object</returns>
|
|
|
|
IReadOnlyCollection<string> GetDataKeys();
|
|
|
|
|
|
|
|
}
|
2020-03-21 00:49:43 +01:00
|
|
|
}
|