using Dalamud.Game.Command; using Dalamud.Plugin; using MareSynchronos.FileCacheDB; using MareSynchronos.Factories; using System.Threading.Tasks; using Dalamud.Game; using Dalamud.Game.ClientState.Objects; using Dalamud.Game.ClientState; using System; using Dalamud.Interface.ImGuiFileDialog; using MareSynchronos.Managers; using MareSynchronos.WebAPI; using Dalamud.Interface.Windowing; using MareSynchronos.UI; using MareSynchronos.Utils; using Penumbra.PlayerWatch; namespace MareSynchronos { public sealed class Plugin : IDalamudPlugin { private const string CommandName = "/mare"; private readonly ApiController _apiController; private readonly ClientState _clientState; private readonly CommandManager _commandManager; private readonly Framework _framework; private readonly Configuration _configuration; private readonly FileCacheManager _fileCacheManager; private readonly IntroUi _introUi; private readonly IpcManager _ipcManager; public static DalamudPluginInterface PluginInterface { get; set; } private readonly PluginUi _pluginUi; private readonly WindowSystem _windowSystem; private PlayerManager? _playerManager; private readonly DalamudUtil _dalamudUtil; private CachedPlayersManager? _characterCacheManager; private readonly DownloadUi _downloadUi; private readonly FileDialogManager _fileDialogManager; public Plugin(DalamudPluginInterface pluginInterface, CommandManager commandManager, Framework framework, ObjectTable objectTable, ClientState clientState) { Logger.Debug("Launching " + Name); PluginInterface = pluginInterface; _commandManager = commandManager; _framework = framework; _clientState = clientState; _configuration = PluginInterface.GetPluginConfig() as Configuration ?? new Configuration(); _configuration.Initialize(PluginInterface); _configuration.Migrate(); _windowSystem = new WindowSystem("MareSynchronos"); new FileCacheContext().Dispose(); // make sure db is initialized I guess // those can be initialized outside of game login _apiController = new ApiController(_configuration); _ipcManager = new IpcManager(PluginInterface); _fileCacheManager = new FileCacheManager(_ipcManager, _configuration); _fileDialogManager = new FileDialogManager(); var uiSharedComponent = new UiShared(_ipcManager, _apiController, _fileCacheManager, _fileDialogManager, _configuration); _pluginUi = new PluginUi(_windowSystem, uiSharedComponent, _configuration, _apiController); _introUi = new IntroUi(_windowSystem, uiSharedComponent, _configuration, _fileCacheManager); _introUi.FinishedRegistration += (_, _) => { _introUi.IsOpen = false; _pluginUi.IsOpen = true; ReLaunchCharacterManager(); }; _downloadUi = new DownloadUi(_windowSystem, _configuration, _apiController); _dalamudUtil = new DalamudUtil(_clientState, objectTable, PlayerWatchFactory.Create(framework, _clientState, objectTable)); clientState.Login += ClientState_Login; clientState.Logout += ClientState_Logout; _apiController.ChangingServers += ApiControllerOnChangingServers; if (clientState.IsLoggedIn) { ClientState_Login(null, null!); } } private void ApiControllerOnChangingServers(object? sender, EventArgs e) { _pluginUi.IsOpen = false; _introUi.IsOpen = true; } public string Name => "Mare Synchronos"; public void Dispose() { Logger.Debug("Disposing " + Name); _apiController.ChangingServers -= ApiControllerOnChangingServers; _apiController?.Dispose(); _commandManager.RemoveHandler(CommandName); _clientState.Login -= ClientState_Login; _clientState.Logout -= ClientState_Logout; _pluginUi?.Dispose(); _introUi?.Dispose(); _downloadUi?.Dispose(); _fileCacheManager?.Dispose(); _ipcManager?.Dispose(); _playerManager?.Dispose(); _characterCacheManager?.Dispose(); _dalamudUtil.Dispose(); } private void ClientState_Login(object? sender, EventArgs e) { Logger.Debug("Client login"); PluginInterface.UiBuilder.Draw += Draw; PluginInterface.UiBuilder.OpenConfigUi += OpenConfigUi; _commandManager.AddHandler(CommandName, new CommandInfo(OnCommand) { HelpMessage = "Opens the Mare Synchronos UI" }); if (!_configuration.HasValidSetup()) { _introUi.IsOpen = true; return; } ReLaunchCharacterManager(); } private void ClientState_Logout(object? sender, EventArgs e) { Logger.Debug("Client logout"); _characterCacheManager?.Dispose(); _playerManager?.Dispose(); PluginInterface.UiBuilder.Draw -= Draw; PluginInterface.UiBuilder.OpenConfigUi -= OpenConfigUi; _commandManager.RemoveHandler(CommandName); } public void ReLaunchCharacterManager() { _characterCacheManager?.Dispose(); _playerManager?.Dispose(); Task.Run(WaitForPlayerAndLaunchCharacterManager); } private async Task WaitForPlayerAndLaunchCharacterManager() { while (!_dalamudUtil.IsPlayerPresent) { await Task.Delay(100); } try { var characterCacheFactory = new CharacterDataFactory(_dalamudUtil, _ipcManager); _characterCacheManager = new CachedPlayersManager(_clientState, _framework, _apiController, _dalamudUtil, _ipcManager); _playerManager = new PlayerManager(_apiController, _ipcManager, characterCacheFactory, _characterCacheManager, _dalamudUtil); } catch (Exception ex) { Logger.Debug(ex.Message); } } private void Draw() { _windowSystem.Draw(); _fileDialogManager.Draw(); } private void OnCommand(string command, string args) { if (string.IsNullOrEmpty(args)) { _pluginUi.Toggle(); } } private void OpenConfigUi() { if (_configuration.HasValidSetup()) _pluginUi.Toggle(); else _introUi.Toggle(); } } }