Merge pull request #1029 from Ottermandias/DataShare

This commit is contained in:
goat 2022-10-29 13:53:19 +02:00 committed by GitHub
commit ce8b4702b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 245 additions and 1 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
@ -174,6 +175,22 @@ namespace Dalamud.Plugin
#region IPC
/// <inheritdoc cref="DataShare.GetOrCreateData{T}"/>
public T GetOrCreateData<T>(string tag, Func<T> dataGenerator) where T : class
=> Service<DataShare>.Get().GetOrCreateData(tag, dataGenerator);
/// <inheritdoc cref="DataShare.RelinquishData"/>
public void RelinquishData(string tag)
=> Service<DataShare>.Get().RelinquishData(tag);
/// <inheritdoc cref="DataShare.TryGetData{T}"/>
public bool TryGetData<T>(string tag, [NotNullWhen(true)] out T? data) where T : class
=> Service<DataShare>.Get().TryGetData(tag, out data);
/// <inheritdoc cref="DataShare.GetData{T}"/>
public T? GetData<T>(string tag) where T : class
=> Service<DataShare>.Get().GetData<T>(tag);
/// <summary>
/// Gets an IPC provider.
/// </summary>

View file

@ -0,0 +1,21 @@
using System;
namespace Dalamud.Plugin.Ipc.Exceptions;
/// <summary>
/// This exception is thrown when a null value is provided for a data cache or it does not implement the expected type.
/// </summary>
public class DataCacheCreationError : IpcError
{
/// <summary>
/// Initializes a new instance of the <see cref="DataCacheCreationError"/> class.
/// </summary>
/// <param name="tag">Tag of the data cache.</param>
/// <param name="creator">The assembly name of the caller.</param>
/// <param name="expectedType">The type expected.</param>
/// <param name="ex">The thrown exception.</param>
public DataCacheCreationError(string tag, string creator, Type expectedType, Exception ex)
: base($"The creation of the {expectedType} data cache {tag} initialized by {creator} was unsuccessful.", ex)
{
}
}

View file

@ -0,0 +1,21 @@
using System;
namespace Dalamud.Plugin.Ipc.Exceptions;
/// <summary>
/// This exception is thrown when a data cache is accessed with the wrong type.
/// </summary>
public class DataCacheTypeMismatchError : IpcError
{
/// <summary>
/// Initializes a new instance of the <see cref="DataCacheTypeMismatchError"/> class.
/// </summary>
/// <param name="tag">Tag of the data cache.</param>
/// <param name="creator">Assembly name of the plugin creating the cache.</param>
/// <param name="requestedType">The requested type.</param>
/// <param name="actualType">The stored type.</param>
public DataCacheTypeMismatchError(string tag, string creator, Type requestedType, Type actualType)
: base($"Data cache {tag} was requested with type {requestedType}, but {creator} created type {actualType}.")
{
}
}

View file

@ -0,0 +1,19 @@
using System;
namespace Dalamud.Plugin.Ipc.Exceptions;
/// <summary>
/// This exception is thrown when a null value is provided for a data cache or it does not implement the expected type.
/// </summary>
public class DataCacheValueNullError : IpcError
{
/// <summary>
/// Initializes a new instance of the <see cref="DataCacheValueNullError"/> class.
/// </summary>
/// <param name="tag">Tag of the data cache.</param>
/// <param name="expectedType">The type expected.</param>
public DataCacheValueNullError(string tag, Type expectedType)
: base($"The data cache {tag} expects a type of {expectedType} but does not implement it.")
{
}
}

View file

@ -1,4 +1,4 @@
using System;
using System;
namespace Dalamud.Plugin.Ipc.Exceptions;

View file

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
namespace Dalamud.Plugin.Ipc.Internal;
/// <summary>
/// A helper struct for reference-counted, type-safe shared access across plugin boundaries.
/// </summary>
internal readonly struct DataCache
{
/// <summary> The assembly name of the initial creator. </summary>
internal readonly string CreatorAssemblyName;
/// <summary> A not-necessarily distinct list of current users. </summary>
internal readonly List<string> UserAssemblyNames;
/// <summary> The type the data was registered as. </summary>
internal readonly Type Type;
/// <summary> A reference to data. </summary>
internal readonly object? Data;
/// <summary>
/// Initializes a new instance of the <see cref="DataCache"/> struct.
/// </summary>
/// <param name="creatorAssemblyName">The assembly name of the initial creator.</param>
/// <param name="data">A reference to data.</param>
/// <param name="type">The type of the data.</param>
public DataCache(string creatorAssemblyName, object? data, Type type)
{
this.CreatorAssemblyName = creatorAssemblyName;
this.UserAssemblyNames = new List<string> { creatorAssemblyName };
this.Data = data;
this.Type = type;
}
}

View file

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Dalamud.Plugin.Ipc.Exceptions;
namespace Dalamud.Plugin.Ipc.Internal;
/// <summary>
/// This class facilitates sharing data-references of standard types between plugins without using more expensive IPC.
/// </summary>
[ServiceManager.EarlyLoadedService]
internal class DataShare : IServiceType
{
private readonly Dictionary<string, DataCache> caches = new();
[ServiceManager.ServiceConstructor]
private DataShare()
{
}
/// <summary>
/// If a data cache for <paramref name="tag"/> exists, return the data.
/// Otherwise, call the function <paramref name="dataGenerator"/> to create data and store it as a new cache.
/// In either case, the calling assembly will be added to the current consumers on success.
/// </summary>
/// <typeparam name="T">The type of the stored data - needs to be a reference type that is shared through Dalamud itself, not loaded by the plugin.</typeparam>
/// <param name="tag">The name for the data cache.</param>
/// <param name="dataGenerator">The function that generates the data if it does not already exist.</param>
/// <returns>Either the existing data for <paramref name="tag"/> or the data generated by <paramref name="dataGenerator"/>.</returns>
/// <exception cref="DataCacheTypeMismatchError">Thrown if a cache for <paramref name="tag"/> exists, but contains data of a type not assignable to <typeparamref name="T>"/>.</exception>
/// <exception cref="DataCacheValueNullError">Thrown if the stored data for a cache is null.</exception>
/// <exception cref="DataCacheCreationError">Thrown if <paramref name="dataGenerator"/> throws an exception or returns null.</exception>
public T GetOrCreateData<T>(string tag, Func<T> dataGenerator) where T : class
{
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty;
if (this.caches.TryGetValue(tag, out var cache))
{
if (!cache.Type.IsAssignableTo(typeof(T)))
throw new DataCacheTypeMismatchError(tag, cache.CreatorAssemblyName, typeof(T), cache.Type);
cache.UserAssemblyNames.Add(callerName);
return cache.Data as T ?? throw new DataCacheValueNullError(tag, cache.Type);
}
try
{
var obj = dataGenerator.Invoke();
if (obj == null)
throw new Exception("Returned data was null.");
cache = new DataCache(callerName, obj, typeof(T));
this.caches[tag] = cache;
return obj;
}
catch (Exception e)
{
throw new DataCacheCreationError(tag, callerName, typeof(T), e);
}
}
/// <summary>
/// Notifies the DataShare that the calling assembly no longer uses the data stored for <paramref name="tag"/> (or uses it one time fewer).
/// If no assembly uses the data anymore, the cache will be removed from the data share and if it is an IDisposable, Dispose will be called on it.
/// </summary>
/// <param name="tag">The name for the data cache.</param>
public void RelinquishData(string tag)
{
if (!this.caches.TryGetValue(tag, out var cache))
return;
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty;
if (!cache.UserAssemblyNames.Remove(callerName) || cache.UserAssemblyNames.Count > 0)
return;
this.caches.Remove(tag);
if (cache.Data is IDisposable disposable)
disposable.Dispose();
}
/// <summary>
/// Obtain the data for the given <paramref name="tag"/>, if it exists and has the correct type.
/// Add the calling assembly to the current consumers if true is returned.
/// </summary>
/// <typeparam name="T">The type of the stored data - needs to be a reference type that is shared through Dalamud itself, not loaded by the plugin.</typeparam>
/// <param name="tag">The name for the data cache.</param>
/// <param name="data">The requested data on success, null otherwise.</param>
/// <returns>True if the requested data exists and is assignable to the requested type.</returns>
public bool TryGetData<T>(string tag, [NotNullWhen(true)] out T? data) where T : class
{
data = null;
if (!this.caches.TryGetValue(tag, out var cache) || !cache.Type.IsAssignableTo(typeof(T)))
return false;
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty;
data = cache.Data as T;
if (data == null)
return false;
cache.UserAssemblyNames.Add(callerName);
return true;
}
/// <summary>
/// Obtain the data for the given <paramref name="tag"/>, if it exists and has the correct type.
/// Add the calling assembly to the current consumers if non-null is returned.
/// </summary>
/// <typeparam name="T">The type of the stored data - needs to be a reference type that is shared through Dalamud itself, not loaded by the plugin.</typeparam>
/// <param name="tag">The name for the data cache.</param>
/// <returns>The requested data</returns>
/// <exception cref="KeyNotFoundException">Thrown if <paramref name="tag"/> is not registered.</exception>
/// <exception cref="DataCacheTypeMismatchError">Thrown if a cache for <paramref name="tag"/> exists, but contains data of a type not assignable to <typeparamref name="T>"/>.</exception>
/// <exception cref="DataCacheValueNullError">Thrown if the stored data for a cache is null.</exception>
public T GetData<T>(string tag) where T : class
{
if (!this.caches.TryGetValue(tag, out var cache))
throw new KeyNotFoundException($"The data cache {tag} is not registered.");
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty;
if (!cache.Type.IsAssignableTo(typeof(T)))
throw new DataCacheTypeMismatchError(tag, callerName, typeof(T), cache.Type);
if (cache.Data is not T data)
throw new DataCacheValueNullError(tag, typeof(T));
cache.UserAssemblyNames.Add(callerName);
return data;
}
}