DP-PatMe2Mqtt/PatMe2Mqtt/Service.cs

111 lines
3.8 KiB
C#
Raw Normal View History

2023-05-26 15:26:16 +02:00
using Dalamud.Data;
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
using Dalamud.Game.Text;
using Dalamud.IoC;
using Dalamud.Logging;
using Dalamud.Plugin;
using System.Reflection;
using System;
using System.Collections;
2023-11-14 00:07:11 +01:00
using Dalamud.Plugin.Services;
2023-05-26 15:26:16 +02:00
namespace PatMe2Mqtt
{
public sealed class Service
{
public Service()
{
// THIS. IS. UGLY.
try
{
if (typeof(PluginServiceAttribute).Assembly.GetType("Dalamud.Service`1") is not { } serviceContainerContainer)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information("PatMe2Mqtt couldn't find the service container types.");
2023-05-26 15:26:16 +02:00
return;
}
if (typeof(PluginServiceAttribute).Assembly.GetType("Dalamud.Plugin.Internal.PluginManager") is not { } pluginManagerType)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information("PatMe2Mqtt couldn't find the plugin manager type.");
2023-05-26 15:26:16 +02:00
return;
}
if (typeof(PluginServiceAttribute).Assembly.GetType("Dalamud.Plugin.Internal.Types.LocalPlugin") is not { } localPluginType ||
localPluginType.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance) is not { } localPluginInstanceField)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information("PatMe2Mqtt couldn't find the local plugin type or important members.");
2023-05-26 15:26:16 +02:00
return;
}
serviceContainerContainer = serviceContainerContainer.MakeGenericType(pluginManagerType);
if (serviceContainerContainer.GetMethod("Get")?.Invoke(null, Array.Empty<object>()) is not object manager)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information("PatMe2Mqtt couldn't obtain the plugin manager.");
2023-05-26 15:26:16 +02:00
return;
}
if (pluginManagerType.GetProperty("InstalledPlugins") is not { } installedPluginsProperty)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information("PatMe2Mqtt couldn't obtain the plugin list property.");
2023-05-26 15:26:16 +02:00
return;
}
GetPluginInstance = name =>
{
if (installedPluginsProperty?.GetValue(manager) is not IList installedPlugins)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information("PatMe2Mqtt couldn't obtain the plugin list.");
2023-05-26 15:26:16 +02:00
return null;
}
foreach (var plugin in installedPlugins)
{
if (localPluginInstanceField.GetValue(plugin) is { } instance && instance.GetType().Assembly.GetName().Name == name)
{
return instance;
}
}
return null;
};
}
catch (Exception e)
{
2023-11-14 00:07:11 +01:00
PluginLog.Information($"PatMe2Mqtt couldn't obtain the plugin manager service: {e}");
2023-05-26 15:26:16 +02:00
}
}
public static Plugin Plugin { get; internal set; } = null!;
public static PatMeProxyApi.PatMe PatMe { get; internal set; } = null!;
[PluginService]
2024-07-02 10:15:50 +02:00
public static IDalamudPluginInterface PluginInterface { get; private set; } = null!;
2023-05-26 15:26:16 +02:00
[PluginService]
2023-11-14 00:07:11 +01:00
public static ICommandManager CommandManager { get; private set; } = null!;
2023-05-26 15:26:16 +02:00
[PluginService]
2023-11-14 00:07:11 +01:00
public static IFramework Framework { get; private set; } = null!;
2023-05-26 15:26:16 +02:00
[PluginService]
2023-11-14 00:07:11 +01:00
public static IClientState ClientState { get; private set; } = null!;
[PluginService]
public static IPluginLog PluginLog { get; private set; } = null!;
2023-05-26 15:26:16 +02:00
public static Func<string, object?>? GetPluginInstance { get; private set; }
}
}