Use plugin internal name for datashare tracking

This commit is contained in:
Critical Impact 2026-02-02 16:56:31 +10:00
parent 33a7cdefa8
commit dc783e0c2b
3 changed files with 17 additions and 33 deletions

View file

@ -87,7 +87,7 @@ internal class DataShareWidget : IDataWindowWidget
try
{
var dataShare = Service<DataShare>.Get();
var data2 = dataShare.GetData<object>(name);
var data2 = dataShare.GetData<object>(name, "DataShareWidget");
try
{
data = Encoding.UTF8.GetBytes(
@ -98,7 +98,7 @@ internal class DataShareWidget : IDataWindowWidget
}
finally
{
dataShare.RelinquishData(name);
dataShare.RelinquishData(name, "DataShareWidget");
}
}
catch (Exception e)

View file

@ -227,19 +227,19 @@ internal sealed class DalamudPluginInterface : IDalamudPluginInterface, IDisposa
/// <inheritdoc/>
public T GetOrCreateData<T>(string tag, Func<T> dataGenerator) where T : class
=> Service<DataShare>.Get().GetOrCreateData(tag, dataGenerator);
=> Service<DataShare>.Get().GetOrCreateData(tag, this.plugin.InternalName, dataGenerator);
/// <inheritdoc/>
public void RelinquishData(string tag)
=> Service<DataShare>.Get().RelinquishData(tag);
=> Service<DataShare>.Get().RelinquishData(tag, this.plugin.InternalName);
/// <inheritdoc/>
public bool TryGetData<T>(string tag, [NotNullWhen(true)] out T? data) where T : class
=> Service<DataShare>.Get().TryGetData(tag, out data);
=> Service<DataShare>.Get().TryGetData(tag, this.plugin.InternalName, out data);
/// <inheritdoc/>
public T? GetData<T>(string tag) where T : class
=> Service<DataShare>.Get().GetData<T>(tag);
=> Service<DataShare>.Get().GetData<T>(tag, this.plugin.InternalName);
/// <inheritdoc/>
public ICallGateProvider<TRet> GetIpcProvider<TRet>(string name)

View file

@ -33,16 +33,15 @@ internal class DataShare : IServiceType
/// </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="callerName">The name of the caller.</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)
public T GetOrCreateData<T>(string tag, string callerName, Func<T> dataGenerator)
where T : class
{
var callerName = GetCallerName();
Lazy<DataCache> cacheLazy;
lock (this.caches)
{
@ -58,7 +57,8 @@ internal class DataShare : IServiceType
/// 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)
/// <param name="callerName">The name of the caller.</param>
public void RelinquishData(string tag, string callerName)
{
DataCache cache;
lock (this.caches)
@ -66,8 +66,6 @@ internal class DataShare : IServiceType
if (!this.caches.TryGetValue(tag, out var cacheLazy))
return;
var callerName = GetCallerName();
cache = cacheLazy.Value;
if (!cache.UserAssemblyNames.Remove(callerName) || cache.UserAssemblyNames.Count > 0)
return;
@ -84,7 +82,7 @@ internal class DataShare : IServiceType
}
catch (Exception e)
{
Log.Error(e, "[DataShare] Failed to dispose [{Tag:l}] after it was removed from all shares.", tag);
Log.Error(e, "[DataShare] Failed to dispose [{Tag:l}] after it was removed from all shares.", tag);
}
}
else
@ -99,9 +97,10 @@ internal class DataShare : IServiceType
/// </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="callerName">The name of the caller.</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)
public bool TryGetData<T>(string tag, string callerName, [NotNullWhen(true)] out T? data)
where T : class
{
data = null;
@ -112,7 +111,7 @@ internal class DataShare : IServiceType
return false;
}
return cacheLazy.Value.TryGetData(GetCallerName(), out data, out _);
return cacheLazy.Value.TryGetData(callerName, out data, out _);
}
/// <summary>
@ -121,11 +120,12 @@ internal class DataShare : IServiceType
/// </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="callerName">The name of the caller.</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)
public T GetData<T>(string tag, string callerName)
where T : class
{
Lazy<DataCache> cacheLazy;
@ -135,7 +135,7 @@ internal class DataShare : IServiceType
throw new KeyNotFoundException($"The data cache [{tag}] is not registered.");
}
return cacheLazy.Value.TryGetData<T>(GetCallerName(), out var value, out var ex) ? value : throw ex;
return cacheLazy.Value.TryGetData<T>(callerName, out var value, out var ex) ? value : throw ex;
}
/// <summary>
@ -150,20 +150,4 @@ internal class DataShare : IServiceType
kvp => (kvp.Key, kvp.Value.Value.CreatorAssemblyName, kvp.Value.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";
}
}