2023-02-15 22:51:35 +00:00
|
|
|
|
using Dalamud.Interface.Windowing;
|
2022-10-23 02:38:58 +00:00
|
|
|
|
using Dalamud.Plugin;
|
2023-02-08 15:06:43 +00:00
|
|
|
|
using Pal.Client.Rendering;
|
2022-10-26 18:43:24 +00:00
|
|
|
|
using Pal.Client.Windows;
|
2022-10-23 02:38:58 +00:00
|
|
|
|
using System;
|
2023-02-10 19:48:14 +00:00
|
|
|
|
using System.Globalization;
|
2022-10-23 02:38:58 +00:00
|
|
|
|
using System.Linq;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Dalamud.Game.ClientState;
|
|
|
|
|
using Dalamud.Game.Command;
|
2023-02-10 19:48:14 +00:00
|
|
|
|
using Pal.Client.Properties;
|
2023-02-11 13:31:43 +00:00
|
|
|
|
using ECommons;
|
2023-02-15 22:17:19 +00:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-02-17 18:12:44 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
using Pal.Client.Commands;
|
2023-02-15 01:38:04 +00:00
|
|
|
|
using Pal.Client.Configuration;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
using Pal.Client.DependencyInjection;
|
2022-10-23 02:38:58 +00:00
|
|
|
|
|
|
|
|
|
namespace Pal.Client
|
|
|
|
|
{
|
2023-02-16 12:17:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// With all DI logic elsewhere, this plugin shell really only takes care of a few things around events that
|
|
|
|
|
/// need to be sent to different receivers depending on priority or configuration .
|
|
|
|
|
/// </summary>
|
2023-02-18 03:34:49 +00:00
|
|
|
|
/// <see cref="DependencyInjectionContext"/>
|
2023-02-15 22:17:19 +00:00
|
|
|
|
internal sealed class Plugin : IDisposable
|
2022-10-23 02:38:58 +00:00
|
|
|
|
{
|
2023-02-15 22:17:19 +00:00
|
|
|
|
private readonly DalamudPluginInterface _pluginInterface;
|
2023-02-17 18:12:44 +00:00
|
|
|
|
private readonly ILogger<Plugin> _logger;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
private readonly CommandManager _commandManager;
|
|
|
|
|
private readonly Chat _chat;
|
2023-02-16 21:09:29 +00:00
|
|
|
|
private readonly WindowSystem _windowSystem;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
private readonly ClientState _clientState;
|
|
|
|
|
|
|
|
|
|
private readonly IServiceScope _rootScope;
|
|
|
|
|
private readonly DependencyInjectionLoader _loader;
|
|
|
|
|
|
|
|
|
|
private Action? _loginAction = null;
|
2022-10-23 02:38:58 +00:00
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
public Plugin(
|
|
|
|
|
DalamudPluginInterface pluginInterface,
|
2023-02-18 03:34:49 +00:00
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
CancellationToken cancellationToken)
|
2022-10-23 02:38:58 +00:00
|
|
|
|
{
|
2023-02-15 22:17:19 +00:00
|
|
|
|
_pluginInterface = pluginInterface;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
_logger = serviceProvider.GetRequiredService<ILogger<Plugin>>();
|
|
|
|
|
_commandManager = serviceProvider.GetRequiredService<CommandManager>();
|
|
|
|
|
_chat = serviceProvider.GetRequiredService<Chat>();
|
2023-02-17 18:12:44 +00:00
|
|
|
|
_windowSystem = serviceProvider.GetRequiredService<WindowSystem>();
|
2023-02-18 03:34:49 +00:00
|
|
|
|
_clientState = serviceProvider.GetRequiredService<ClientState>();
|
2022-11-30 21:15:26 +00:00
|
|
|
|
|
2023-02-18 03:34:49 +00:00
|
|
|
|
_rootScope = serviceProvider.CreateScope();
|
|
|
|
|
_loader = _rootScope.ServiceProvider.GetRequiredService<DependencyInjectionLoader>();
|
|
|
|
|
_loader.InitCompleted += InitCompleted;
|
|
|
|
|
var _ = Task.Run(async () => await _loader.InitializeAsync(cancellationToken));
|
2022-10-26 18:43:24 +00:00
|
|
|
|
|
2023-02-08 15:06:43 +00:00
|
|
|
|
pluginInterface.UiBuilder.Draw += Draw;
|
2023-02-11 20:10:45 +00:00
|
|
|
|
pluginInterface.UiBuilder.OpenConfigUi += OpenConfigUi;
|
2023-02-10 19:48:14 +00:00
|
|
|
|
pluginInterface.LanguageChanged += LanguageChanged;
|
2023-02-18 03:34:49 +00:00
|
|
|
|
_clientState.Login += Login;
|
|
|
|
|
|
|
|
|
|
_commandManager.AddHandler("/pal", new CommandInfo(OnCommand)
|
|
|
|
|
{
|
|
|
|
|
HelpMessage = Localization.Command_pal_HelpText
|
|
|
|
|
});
|
2022-10-23 02:38:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 03:34:49 +00:00
|
|
|
|
private void InitCompleted(Action? loginAction)
|
2022-10-23 02:38:58 +00:00
|
|
|
|
{
|
2023-02-18 03:34:49 +00:00
|
|
|
|
LanguageChanged(_pluginInterface.UiLanguage);
|
|
|
|
|
|
|
|
|
|
if (_clientState.IsLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
loginAction?.Invoke();
|
|
|
|
|
_loginAction = null;
|
|
|
|
|
}
|
2022-10-23 02:38:58 +00:00
|
|
|
|
else
|
2023-02-18 03:34:49 +00:00
|
|
|
|
_loginAction = loginAction;
|
|
|
|
|
}
|
2023-02-02 16:16:03 +00:00
|
|
|
|
|
2023-02-18 03:34:49 +00:00
|
|
|
|
private void Login(object? sender, EventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
_loginAction?.Invoke();
|
|
|
|
|
_loginAction = null;
|
2022-10-25 17:28:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 03:34:49 +00:00
|
|
|
|
private void OnCommand(string command, string arguments)
|
2022-10-23 02:38:58 +00:00
|
|
|
|
{
|
2023-02-18 03:34:49 +00:00
|
|
|
|
arguments = arguments.Trim();
|
|
|
|
|
|
|
|
|
|
IPalacePalConfiguration configuration =
|
|
|
|
|
_rootScope.ServiceProvider.GetRequiredService<IPalacePalConfiguration>();
|
|
|
|
|
if (configuration.FirstUse && arguments != "" && arguments != "config")
|
|
|
|
|
{
|
|
|
|
|
_chat.Error(Localization.Error_FirstTimeSetupRequired);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var sp = _rootScope.ServiceProvider;
|
|
|
|
|
|
|
|
|
|
switch (arguments)
|
|
|
|
|
{
|
|
|
|
|
case "":
|
|
|
|
|
case "config":
|
|
|
|
|
sp.GetRequiredService<PalConfigCommand>().Execute();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "stats":
|
|
|
|
|
sp.GetRequiredService<PalStatsCommand>().Execute();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "tc":
|
|
|
|
|
case "test-connection":
|
|
|
|
|
sp.GetRequiredService<PalTestConnectionCommand>().Execute();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "near":
|
|
|
|
|
case "tnear":
|
|
|
|
|
case "hnear":
|
|
|
|
|
sp.GetRequiredService<PalNearCommand>().Execute(arguments);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
_chat.Error(string.Format(Localization.Command_pal_UnknownSubcommand, arguments,
|
|
|
|
|
command));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_chat.Error(e.ToString());
|
|
|
|
|
}
|
2022-10-23 02:38:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-18 03:34:49 +00:00
|
|
|
|
private void OpenConfigUi()
|
|
|
|
|
=> _rootScope.ServiceProvider.GetRequiredService<PalConfigCommand>().Execute();
|
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
private void LanguageChanged(string languageCode)
|
2022-12-22 00:01:09 +00:00
|
|
|
|
{
|
2023-02-17 18:12:44 +00:00
|
|
|
|
_logger.LogInformation("Language set to '{Language}'", languageCode);
|
|
|
|
|
|
2023-02-15 22:17:19 +00:00
|
|
|
|
Localization.Culture = new CultureInfo(languageCode);
|
2023-02-16 21:09:29 +00:00
|
|
|
|
_windowSystem.Windows.OfType<ILanguageChanged>()
|
2023-02-16 12:17:55 +00:00
|
|
|
|
.Each(w => w.LanguageChanged());
|
2023-02-08 15:06:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Draw()
|
|
|
|
|
{
|
2023-02-18 03:34:49 +00:00
|
|
|
|
if (_loader.LoadState == DependencyInjectionLoader.ELoadState.Loaded)
|
|
|
|
|
{
|
|
|
|
|
_rootScope.ServiceProvider.GetRequiredService<RenderAdapter>().DrawLayers();
|
|
|
|
|
_windowSystem.Draw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_commandManager.RemoveHandler("/pal");
|
|
|
|
|
|
|
|
|
|
_pluginInterface.UiBuilder.Draw -= Draw;
|
|
|
|
|
_pluginInterface.UiBuilder.OpenConfigUi -= OpenConfigUi;
|
|
|
|
|
_pluginInterface.LanguageChanged -= LanguageChanged;
|
|
|
|
|
_clientState.Login -= Login;
|
|
|
|
|
|
|
|
|
|
_loader.InitCompleted -= InitCompleted;
|
|
|
|
|
_rootScope.Dispose();
|
2022-10-26 21:38:29 +00:00
|
|
|
|
}
|
2022-10-23 02:38:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|