Merge pull request #1153 from Ottermandias/DataShare

This commit is contained in:
goat 2023-03-11 20:39:12 +01:00 committed by GitHub
commit 9cda360334
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 159 additions and 47 deletions

View file

@ -182,6 +182,7 @@ internal class DataWindow : Window
Aetherytes, Aetherytes,
Dtr_Bar, Dtr_Bar,
UIColor, UIColor,
DataShare,
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -378,6 +379,9 @@ internal class DataWindow : Window
case DataKind.UIColor: case DataKind.UIColor:
this.DrawUIColor(); this.DrawUIColor();
break; break;
case DataKind.DataShare:
this.DrawDataShareTab();
break;
} }
} }
else else
@ -1765,6 +1769,35 @@ internal class DataWindow : Window
} }
} }
private void DrawDataShareTab()
{
if (!ImGui.BeginTable("###DataShareTable", 4, ImGuiTableFlags.SizingFixedFit | ImGuiTableFlags.RowBg))
return;
try
{
ImGui.TableSetupColumn("Shared Tag");
ImGui.TableSetupColumn("Creator Assembly");
ImGui.TableSetupColumn("#", ImGuiTableColumnFlags.WidthFixed, 30 * ImGuiHelpers.GlobalScale);
ImGui.TableSetupColumn("Consumers");
ImGui.TableHeadersRow();
foreach (var share in Service<DataShare>.Get().GetAllShares())
{
ImGui.TableNextColumn();
ImGui.TextUnformatted(share.Tag);
ImGui.TableNextColumn();
ImGui.TextUnformatted(share.CreatorAssembly);
ImGui.TableNextColumn();
ImGui.TextUnformatted(share.Users.Length.ToString());
ImGui.TableNextColumn();
ImGui.TextUnformatted(string.Join(", ", share.Users));
}
} finally
{
ImGui.EndTable();
}
}
private void DrawUIColor() private void DrawUIColor()
{ {
var colorSheet = Service<DataManager>.Get().GetExcelSheet<UIColor>(); var colorSheet = Service<DataManager>.Get().GetExcelSheet<UIColor>();

View file

@ -1,9 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection; using System.Reflection;
using Dalamud.Plugin.Ipc.Exceptions; using Dalamud.Plugin.Ipc.Exceptions;
using Serilog;
namespace Dalamud.Plugin.Ipc.Internal; namespace Dalamud.Plugin.Ipc.Internal;
@ -32,30 +35,41 @@ internal class DataShare : IServiceType
/// <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="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="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> /// <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 public T GetOrCreateData<T>(string tag, Func<T> dataGenerator)
where T : class
{ {
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty; var callerName = GetCallerName();
if (this.caches.TryGetValue(tag, out var cache)) lock (this.caches)
{ {
if (!cache.Type.IsAssignableTo(typeof(T))) if (this.caches.TryGetValue(tag, out var cache))
throw new DataCacheTypeMismatchError(tag, cache.CreatorAssemblyName, typeof(T), cache.Type); {
cache.UserAssemblyNames.Add(callerName); if (!cache.Type.IsAssignableTo(typeof(T)))
return cache.Data as T ?? throw new DataCacheValueNullError(tag, cache.Type); {
} throw new DataCacheTypeMismatchError(tag, cache.CreatorAssemblyName, typeof(T), cache.Type);
}
try cache.UserAssemblyNames.Add(callerName);
{ return cache.Data as T ?? throw new DataCacheValueNullError(tag, cache.Type);
var obj = dataGenerator.Invoke(); }
if (obj == null)
throw new Exception("Returned data was null.");
cache = new DataCache(callerName, obj, typeof(T)); try
this.caches[tag] = cache; {
return obj; var obj = dataGenerator.Invoke();
} if (obj == null)
catch (Exception e) {
{ throw new Exception("Returned data was null.");
throw new DataCacheCreationError(tag, callerName, typeof(T), e); }
cache = new DataCache(callerName, obj, typeof(T));
this.caches[tag] = cache;
Log.Verbose("[DataShare] Created new data for [{Tag:l}] for creator {Creator:l}.", tag, callerName);
return obj;
}
catch (Exception e)
{
throw new DataCacheCreationError(tag, callerName, typeof(T), e);
}
} }
} }
@ -66,16 +80,35 @@ internal class DataShare : IServiceType
/// <param name="tag">The name for the data cache.</param> /// <param name="tag">The name for the data cache.</param>
public void RelinquishData(string tag) public void RelinquishData(string tag)
{ {
if (!this.caches.TryGetValue(tag, out var cache)) lock (this.caches)
return; {
if (!this.caches.TryGetValue(tag, out var cache))
{
return;
}
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty; var callerName = GetCallerName();
if (!cache.UserAssemblyNames.Remove(callerName) || cache.UserAssemblyNames.Count > 0) lock (this.caches)
return; {
if (!cache.UserAssemblyNames.Remove(callerName) || cache.UserAssemblyNames.Count > 0)
{
return;
}
this.caches.Remove(tag); if (this.caches.Remove(tag))
if (cache.Data is IDisposable disposable) {
disposable.Dispose(); if (cache.Data is IDisposable disposable)
{
disposable.Dispose();
Log.Verbose("[DataShare] Disposed [{Tag:l}] after it was removed from all shares.", tag);
}
else
{
Log.Verbose("[DataShare] Removed [{Tag:l}] from all shares.", tag);
}
}
}
}
} }
/// <summary> /// <summary>
@ -86,19 +119,27 @@ internal class DataShare : IServiceType
/// <param name="tag">The name for the data cache.</param> /// <param name="tag">The name for the data cache.</param>
/// <param name="data">The requested data on success, null otherwise.</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> /// <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 public bool TryGetData<T>(string tag, [NotNullWhen(true)] out T? data)
where T : class
{ {
data = null; data = null;
if (!this.caches.TryGetValue(tag, out var cache) || !cache.Type.IsAssignableTo(typeof(T))) lock (this.caches)
return false; {
if (!this.caches.TryGetValue(tag, out var cache) || !cache.Type.IsAssignableTo(typeof(T)))
{
return false;
}
var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty; var callerName = GetCallerName();
data = cache.Data as T; data = cache.Data as T;
if (data == null) if (data == null)
return false; {
return false;
}
cache.UserAssemblyNames.Add(callerName); cache.UserAssemblyNames.Add(callerName);
return true; return true;
}
} }
/// <summary> /// <summary>
@ -111,19 +152,57 @@ internal class DataShare : IServiceType
/// <exception cref="KeyNotFoundException">Thrown if <paramref name="tag"/> is not registered.</exception> /// <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="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="DataCacheValueNullError">Thrown if the stored data for a cache is null.</exception>
public T GetData<T>(string tag) where T : class public T GetData<T>(string tag)
where T : class
{ {
if (!this.caches.TryGetValue(tag, out var cache)) lock (this.caches)
throw new KeyNotFoundException($"The data cache {tag} is not registered."); {
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; var callerName = Assembly.GetCallingAssembly().GetName().Name ?? string.Empty;
if (!cache.Type.IsAssignableTo(typeof(T))) if (!cache.Type.IsAssignableTo(typeof(T)))
throw new DataCacheTypeMismatchError(tag, callerName, typeof(T), cache.Type); {
throw new DataCacheTypeMismatchError(tag, callerName, typeof(T), cache.Type);
}
if (cache.Data is not T data) if (cache.Data is not T data)
throw new DataCacheValueNullError(tag, typeof(T)); {
throw new DataCacheValueNullError(tag, typeof(T));
}
cache.UserAssemblyNames.Add(callerName); cache.UserAssemblyNames.Add(callerName);
return data; return data;
}
}
/// <summary>
/// Obtain a read-only list of data shares.
/// </summary>
/// <returns>All currently subscribed tags, their creator names and all their users.</returns>
internal IEnumerable<(string Tag, string CreatorAssembly, string[] Users)> GetAllShares()
{
lock (this.caches)
{
return this.caches.Select(kvp => (kvp.Key, kvp.Value.CreatorAssemblyName, kvp.Value.UserAssemblyNames.ToArray()));
}
}
/// <summary> Obtain the last assembly name in the stack trace that is not a system or dalamud assembly. </summary>
private static string GetCallerName()
{
var frames = new StackTrace().GetFrames();
foreach (var frame in frames.Reverse())
{
var name = frame.GetMethod()?.DeclaringType?.Assembly.GetName().Name ?? "Unknown";
if (!name.StartsWith("System") && !name.StartsWith("Dalamud"))
{
return name;
}
}
return "Unknown";
} }
} }