2023-10-04 22:07:36 +00:00
|
|
|
|
using Dalamud.Plugin;
|
2023-10-01 20:50:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
2023-10-04 22:07:36 +00:00
|
|
|
|
using Dalamud.Plugin.Services;
|
2023-10-01 20:50:21 +00:00
|
|
|
|
|
|
|
|
|
namespace Workshoppa.External;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Originally part of ECommons by NightmareXIV.
|
|
|
|
|
///
|
|
|
|
|
/// https://github.com/NightmareXIV/ECommons/blob/master/ECommons/Reflection/DalamudReflector.cs
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal sealed class DalamudReflector : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly DalamudPluginInterface _pluginInterface;
|
2023-10-04 22:07:36 +00:00
|
|
|
|
private readonly IFramework _framework;
|
|
|
|
|
private readonly IPluginLog _pluginLog;
|
2023-10-01 20:50:21 +00:00
|
|
|
|
private readonly Dictionary<string, IDalamudPlugin> _pluginCache = new();
|
|
|
|
|
private bool _pluginsChanged = false;
|
|
|
|
|
|
2023-10-04 22:07:36 +00:00
|
|
|
|
public DalamudReflector(DalamudPluginInterface pluginInterface, IFramework framework, IPluginLog pluginLog)
|
2023-10-01 20:50:21 +00:00
|
|
|
|
{
|
|
|
|
|
_pluginInterface = pluginInterface;
|
|
|
|
|
_framework = framework;
|
2023-10-04 22:07:36 +00:00
|
|
|
|
_pluginLog = pluginLog;
|
2023-10-01 20:50:21 +00:00
|
|
|
|
var pm = GetPluginManager();
|
|
|
|
|
pm.GetType().GetEvent("OnInstalledPluginsChanged")!.AddEventHandler(pm, OnInstalledPluginsChanged);
|
|
|
|
|
|
|
|
|
|
_framework.Update += FrameworkUpdate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_framework.Update -= FrameworkUpdate;
|
|
|
|
|
|
|
|
|
|
var pm = GetPluginManager();
|
|
|
|
|
pm.GetType().GetEvent("OnInstalledPluginsChanged")!.RemoveEventHandler(pm, OnInstalledPluginsChanged);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 22:07:36 +00:00
|
|
|
|
private void FrameworkUpdate(IFramework framework)
|
2023-10-01 20:50:21 +00:00
|
|
|
|
{
|
|
|
|
|
if (_pluginsChanged)
|
|
|
|
|
{
|
|
|
|
|
_pluginsChanged = false;
|
|
|
|
|
_pluginCache.Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private object GetPluginManager()
|
|
|
|
|
{
|
|
|
|
|
return _pluginInterface.GetType().Assembly.GetType("Dalamud.Service`1", true)!
|
|
|
|
|
.MakeGenericType(
|
|
|
|
|
_pluginInterface.GetType().Assembly.GetType("Dalamud.Plugin.Internal.PluginManager", true)!)
|
|
|
|
|
.GetMethod("Get")!.Invoke(null, BindingFlags.Default, null, Array.Empty<object>(), null)!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryGetDalamudPlugin(string internalName, out IDalamudPlugin? instance, bool suppressErrors = false,
|
|
|
|
|
bool ignoreCache = false)
|
|
|
|
|
{
|
|
|
|
|
if (!ignoreCache && _pluginCache.TryGetValue(internalName, out instance))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var pluginManager = GetPluginManager();
|
|
|
|
|
var installedPlugins =
|
|
|
|
|
(System.Collections.IList)pluginManager.GetType().GetProperty("InstalledPlugins")!.GetValue(
|
|
|
|
|
pluginManager)!;
|
|
|
|
|
|
|
|
|
|
foreach (var t in installedPlugins)
|
|
|
|
|
{
|
|
|
|
|
if ((string?)t.GetType().GetProperty("Name")!.GetValue(t) == internalName)
|
|
|
|
|
{
|
|
|
|
|
var type = t.GetType().Name == "LocalDevPlugin" ? t.GetType().BaseType : t.GetType();
|
|
|
|
|
var plugin = (IDalamudPlugin?)type!
|
|
|
|
|
.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(t);
|
|
|
|
|
if (plugin == null)
|
|
|
|
|
{
|
2023-10-04 22:07:36 +00:00
|
|
|
|
_pluginLog.Warning($"[DalamudReflector] Found requested plugin {internalName} but it was null");
|
2023-10-01 20:50:21 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
instance = plugin;
|
|
|
|
|
_pluginCache[internalName] = plugin;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
if (!suppressErrors)
|
|
|
|
|
{
|
2023-10-04 22:07:36 +00:00
|
|
|
|
_pluginLog.Error(e, $"Can't find {internalName} plugin: {e.Message}");
|
2023-10-01 20:50:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instance = null;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInstalledPluginsChanged()
|
|
|
|
|
{
|
2023-10-04 22:07:36 +00:00
|
|
|
|
_pluginLog.Verbose("Installed plugins changed event fired");
|
2023-10-01 20:50:21 +00:00
|
|
|
|
_pluginsChanged = true;
|
|
|
|
|
}
|
|
|
|
|
}
|