mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-17 13:27:43 +01:00
Use plugin internal name for datashare tracking
This commit is contained in:
parent
33a7cdefa8
commit
dc783e0c2b
3 changed files with 17 additions and 33 deletions
|
|
@ -87,7 +87,7 @@ internal class DataShareWidget : IDataWindowWidget
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var dataShare = Service<DataShare>.Get();
|
var dataShare = Service<DataShare>.Get();
|
||||||
var data2 = dataShare.GetData<object>(name);
|
var data2 = dataShare.GetData<object>(name, "DataShareWidget");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
data = Encoding.UTF8.GetBytes(
|
data = Encoding.UTF8.GetBytes(
|
||||||
|
|
@ -98,7 +98,7 @@ internal class DataShareWidget : IDataWindowWidget
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
dataShare.RelinquishData(name);
|
dataShare.RelinquishData(name, "DataShareWidget");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
||||||
|
|
@ -227,19 +227,19 @@ internal sealed class DalamudPluginInterface : IDalamudPluginInterface, IDisposa
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public T GetOrCreateData<T>(string tag, Func<T> dataGenerator) where T : class
|
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/>
|
/// <inheritdoc/>
|
||||||
public void RelinquishData(string tag)
|
public void RelinquishData(string tag)
|
||||||
=> Service<DataShare>.Get().RelinquishData(tag);
|
=> Service<DataShare>.Get().RelinquishData(tag, this.plugin.InternalName);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
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
|
||||||
=> Service<DataShare>.Get().TryGetData(tag, out data);
|
=> Service<DataShare>.Get().TryGetData(tag, this.plugin.InternalName, out data);
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public T? GetData<T>(string tag) where T : class
|
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/>
|
/// <inheritdoc/>
|
||||||
public ICallGateProvider<TRet> GetIpcProvider<TRet>(string name)
|
public ICallGateProvider<TRet> GetIpcProvider<TRet>(string name)
|
||||||
|
|
|
||||||
|
|
@ -33,16 +33,15 @@ internal class DataShare : IServiceType
|
||||||
/// </summary>
|
/// </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>
|
/// <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="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>
|
/// <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>
|
/// <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="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)
|
public T GetOrCreateData<T>(string tag, string callerName, Func<T> dataGenerator)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
var callerName = GetCallerName();
|
|
||||||
|
|
||||||
Lazy<DataCache> cacheLazy;
|
Lazy<DataCache> cacheLazy;
|
||||||
lock (this.caches)
|
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.
|
/// 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>
|
/// </summary>
|
||||||
/// <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)
|
/// <param name="callerName">The name of the caller.</param>
|
||||||
|
public void RelinquishData(string tag, string callerName)
|
||||||
{
|
{
|
||||||
DataCache cache;
|
DataCache cache;
|
||||||
lock (this.caches)
|
lock (this.caches)
|
||||||
|
|
@ -66,8 +66,6 @@ internal class DataShare : IServiceType
|
||||||
if (!this.caches.TryGetValue(tag, out var cacheLazy))
|
if (!this.caches.TryGetValue(tag, out var cacheLazy))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var callerName = GetCallerName();
|
|
||||||
|
|
||||||
cache = cacheLazy.Value;
|
cache = cacheLazy.Value;
|
||||||
if (!cache.UserAssemblyNames.Remove(callerName) || cache.UserAssemblyNames.Count > 0)
|
if (!cache.UserAssemblyNames.Remove(callerName) || cache.UserAssemblyNames.Count > 0)
|
||||||
return;
|
return;
|
||||||
|
|
@ -84,7 +82,7 @@ internal class DataShare : IServiceType
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
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
|
else
|
||||||
|
|
@ -99,9 +97,10 @@ internal class DataShare : IServiceType
|
||||||
/// </summary>
|
/// </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>
|
/// <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="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>
|
/// <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)
|
public bool TryGetData<T>(string tag, string callerName, [NotNullWhen(true)] out T? data)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
data = null;
|
data = null;
|
||||||
|
|
@ -112,7 +111,7 @@ internal class DataShare : IServiceType
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cacheLazy.Value.TryGetData(GetCallerName(), out data, out _);
|
return cacheLazy.Value.TryGetData(callerName, out data, out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -121,11 +120,12 @@ internal class DataShare : IServiceType
|
||||||
/// </summary>
|
/// </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>
|
/// <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="tag">The name for the data cache.</param>
|
||||||
|
/// <param name="callerName">The name of the caller.</param>
|
||||||
/// <returns>The requested data.</returns>
|
/// <returns>The requested data.</returns>
|
||||||
/// <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)
|
public T GetData<T>(string tag, string callerName)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
Lazy<DataCache> cacheLazy;
|
Lazy<DataCache> cacheLazy;
|
||||||
|
|
@ -135,7 +135,7 @@ internal class DataShare : IServiceType
|
||||||
throw new KeyNotFoundException($"The data cache [{tag}] is not registered.");
|
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>
|
/// <summary>
|
||||||
|
|
@ -150,20 +150,4 @@ internal class DataShare : IServiceType
|
||||||
kvp => (kvp.Key, kvp.Value.Value.CreatorAssemblyName, kvp.Value.Value.UserAssemblyNames.ToArray()));
|
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";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue