Files
LightlessClient/LightlessSync/LightlessPlugin.cs
2025-10-16 23:44:15 +02:00

192 lines
12 KiB
C#

using LightlessSync.FileCache;
using LightlessSync.LightlessConfiguration;
using LightlessSync.PlayerData.Pairs;
using LightlessSync.PlayerData.Services;
using LightlessSync.Services;
using LightlessSync.Services.Mediator;
using LightlessSync.Services.ServerConfiguration;
using LightlessSync.UI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using System.Reflection;
namespace LightlessSync;
#pragma warning disable S125 // Sections of code should not be commented out
/*
(..,,...,,,,,+/, ,,.....,,+
..,,+++/((###%%%&&%%#(+,,.,,,+++,,,,//,,#&@@@@%+.
...+//////////(/,,,,++,.,(###((//////////,.. .,#@@%/./
,..+/////////+///,.,. ,&@@@@,,/////////////+,.. ,(##+,.
,,.+//////////++++++.. ./#%#,+/////////////+,....,/((,..,
+..////////////+++++++... .../##(,,////////////////++,,,+/(((+,
+,.+//////////////+++++++,.,,,/(((+.,////////////////////////((((#/,,
/+.+//////////++++/++++++++++,,...,++///////////////////////////((((##,
/,.////////+++++++++++++++++++++////////+++//////++/+++++//////////((((#(+,
/+.+////////+++++++++++++++++++++++++++++++++++++++++++++++++++++/////((((##+
+,.///////////////+++++++++++++++++++++++++++++++++++++++++++++++++++///((((%/
/.,/////////////////+++++++++++++++++++++++++++++++++++++++++++++++++++///+/(#+
+,./////////////////+++++++++++++++++++++++++++++++++++++++++++++++,,+++++///((,
...////////++/++++++++++++++++++++++++,,++++++++++++++++++++++++++++++++++++//(,,
..//+,+///++++++++++++++++++,,,,+++,,,,,,,,,,,,++++++++,,+++++++++++++++++++//,,+
..,++,.++++++++++++++++++++++,,,,,,,,,,,,,,,,,,,++++++++,,,,,,,,,,++++++++++...
..+++,.+++++++++++++++++++,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,++,..,.
..,++++,,+++++++++++,+,,,,,,,,,,..,+++++++++,,,,,,.....................,//+,+
....,+++++,.,+++++++++++,,,,,,,,.+///(((((((((((((///////////////////////(((+,,,
.....,++++++++++..,+++++++++++,,.,,,.////////(((((((((((((((////////////////////+,,/
.....,++++++++++++,..,,+++++++++,,.,../////////////////((((((((((//////////////////,,+
...,,+++++++++++++,.,,.,,,+++++++++,.,/////////////////(((//++++++++++++++//+++++++++/,,
....,++++++++++++++,.,++.,++++++++++++.,+////////////////////+++++++++++++++++++++++++///,,..
...,++++++++++++++++..+++..+++++++++++++.,//////////////////////////++++++++++++///////++++......
...++++++++++++++++++..++++.,++,++++++++++.+///////////////////////////////////////////++++++..,,,..
...+++++++++++++++++++..+++++..,+,,+++++++++.+//////////////////////////////////////////+++++++...,,,,..
..++++++++++++++++++++..++++++..,+,,+++++++++.+//////////////////////////////////////++++++++++,....,,,,..
...+++//(//////+++++++++..++++++,.,+++++++++++++,..,....,,,+++///////////////////////++++++++++++..,,,,,,,,...
..,++/(((((//////+++++++,.,++++++,,.,,,+++++++++++++++++++++++,.++////////////////////+++++++++++.....,,,,,,,...
..,//#(((((///////+++++++..++++++++++,...,++,++++++++++++++++,...+++/////////////////////+,,,+++... ....,,,,,,...
...+//(((((//////////++++++..+++++++++++++++,......,,,,++++++,,,..+++////////////////////////+,.... ...,,,,,,,...
..,//((((////////////++++++..++++++/+++++++++++++,,...,,........,+/+//////////////////////((((/+,.. ....,.,,,,..
...+/////////////////////+++..++++++/+///+++++++++++++++++++++///+/+////////////////////////(((((/+... .......,,...
..++////+++//////////////++++.+++++++++///////++++++++////////////////////////////////////+++/(((((/+.. .....,,...
.,++++++++///////////////++++..++++//////////////////////////////////////////////////////++++++/((((++.. ........
.+++++++++////////////////++++,.+++/////////////////////////////////////////////////////+++++++++/((/++..
.,++++++++//////////////////++++,.+++//////////////////////////////////////////////////+++++++++++++//+++..
.++++++++//////////////////////+/,.,+++////((((////////////////////////////////////////++++++++++++++++++...
.++++++++///////////////////////+++..++++//((((((((///////////////////////////////////++++++++++++++++++++ .
.++++++///////////////////////////++,.,+++++/(((((((((/////////////////////////////+++++++++++++++++++++++,..
.++++++////////////////////////////+++,.,+++++++/((((((((//////////////////////////++++++++++++++++++++++++..
.+++++++///////////////////++////////++++,.,+++++++++///////////+////////////////+++++++++++++++++++++++++,..
..++++++++++//////////////////////+++++++..+...,+++++++++++++++/++++++++++++++++++++++++++++++++++++++++++,...
..++++++++++++///////////////+++++++,...,,,,,.,....,,,,+++++++++++++++++++++++++++++++++++++++++++++++,,,,...
...++++++++++++++++++++++++++,,,,...,,,,,,,,,..,,++,,,.,,,,,,,,,,,,,,,,,,+++++++++++++++++++++++++,,,,,,,,..
...+++++++++++++++,,,,,,,,....,,,,,,,,,,,,,,,..,,++++++,,,,,,,,,,,,,,,,+++++++++++++++++++++++++,,,,,,,,,..
...++++++++++++,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...,++++++++++++++++++++++++++++++++++++++++++++,,,,,,,,,,...
,....,++++++++++++++,,,+++++++,,,,,,,,,,,,,,,,,.,++++++++++++++++++++++++++++++++++++++++++++,,,,,,,,..
*/
#pragma warning restore S125 // Sections of code should not be commented out
// thank you dark 🙏
public class LightlessPlugin : MediatorSubscriberBase, IHostedService
{
private readonly DalamudUtilService _dalamudUtil;
private readonly LightlessConfigService _lightlessConfigService;
private readonly ServerConfigurationManager _serverConfigurationManager;
private readonly IServiceScopeFactory _serviceScopeFactory;
private IServiceScope? _runtimeServiceScope;
private Task? _launchTask = null;
public LightlessPlugin(ILogger<LightlessPlugin> logger, LightlessConfigService lightlessConfigService,
ServerConfigurationManager serverConfigurationManager,
DalamudUtilService dalamudUtil,
IServiceScopeFactory serviceScopeFactory, LightlessMediator mediator) : base(logger, mediator)
{
_lightlessConfigService = lightlessConfigService;
_serverConfigurationManager = serverConfigurationManager;
_dalamudUtil = dalamudUtil;
_serviceScopeFactory = serviceScopeFactory;
}
public Task StartAsync(CancellationToken cancellationToken)
{
var version = Assembly.GetExecutingAssembly().GetName().Version!;
Logger.LogInformation("Launching {name} {major}.{minor}.{build}", "Lightless Sync", version.Major, version.Minor, version.Build);
Mediator.Publish(new EventMessage(new Services.Events.Event(nameof(LightlessPlugin), Services.Events.EventSeverity.Informational,
$"Starting Lightless Sync {version.Major}.{version.Minor}.{version.Build}")));
Mediator.Subscribe<SwitchToMainUiMessage>(this, (msg) => { if (_launchTask == null || _launchTask.IsCompleted) _launchTask = Task.Run(WaitForPlayerAndLaunchCharacterManager); });
Mediator.Subscribe<DalamudLoginMessage>(this, (_) => DalamudUtilOnLogIn());
Mediator.Subscribe<DalamudLogoutMessage>(this, (_) => DalamudUtilOnLogOut());
UIColors.Initialize(_lightlessConfigService);
Mediator.StartQueueProcessing();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
UnsubscribeAll();
DalamudUtilOnLogOut();
Logger.LogDebug("Halting LightlessPlugin");
return Task.CompletedTask;
}
private void CheckVersion()
{
var ver = Assembly.GetExecutingAssembly().GetName().Version;
var currentVersion = ver == null ? string.Empty : $"{ver.Major}.{ver.Minor}.{ver.Build}";
var lastSeen = _lightlessConfigService.Current.LastSeenVersion ?? string.Empty;
Logger.LogInformation("Last seen version: {lastSeen}, current version: {currentVersion}", lastSeen, currentVersion);
Logger.LogInformation("User has valid setup: {hasValidSetup}", _lightlessConfigService.Current.HasValidSetup());
Logger.LogInformation("Server has valid config: {hasValidConfig}", _serverConfigurationManager.HasValidConfig());
// Show update notes if version has changed and user has valid setup
if (!string.Equals(lastSeen, currentVersion, StringComparison.Ordinal) &&
_lightlessConfigService.Current.HasValidSetup() &&
_serverConfigurationManager.HasValidConfig())
{
Mediator.Publish(new UiToggleMessage(typeof(UpdateNotesUi)));
}
}
private void DalamudUtilOnLogIn()
{
Logger?.LogDebug("Client login");
if (_launchTask == null || _launchTask.IsCompleted) _launchTask = Task.Run(WaitForPlayerAndLaunchCharacterManager);
}
private void DalamudUtilOnLogOut()
{
Logger?.LogDebug("Client logout");
_runtimeServiceScope?.Dispose();
}
private async Task WaitForPlayerAndLaunchCharacterManager()
{
while (!await _dalamudUtil.GetIsPlayerPresentAsync().ConfigureAwait(false))
{
await Task.Delay(100).ConfigureAwait(false);
}
try
{
Logger?.LogDebug("Launching Managers");
_runtimeServiceScope?.Dispose();
_runtimeServiceScope = _serviceScopeFactory.CreateScope();
_runtimeServiceScope.ServiceProvider.GetRequiredService<UiService>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<CommandManagerService>();
if (!_lightlessConfigService.Current.HasValidSetup() || !_serverConfigurationManager.HasValidConfig())
{
Mediator.Publish(new SwitchToIntroUiMessage());
return;
}
_runtimeServiceScope.ServiceProvider.GetRequiredService<CacheCreationService>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<TransientResourceManager>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<VisibleUserDataDistributor>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<NotificationService>();
_runtimeServiceScope.ServiceProvider.GetRequiredService<NameplateService>();
CheckVersion();
#if !DEBUG
if (_lightlessConfigService.Current.LogLevel != LogLevel.Information)
{
Mediator.Publish(new NotificationMessage("Abnormal Log Level",
$"Your log level is set to '{_lightlessConfigService.Current.LogLevel}' which is not recommended for normal usage. Set it to '{LogLevel.Information}' in \"Lightless Settings -> Debug\" unless instructed otherwise.",
LightlessConfiguration.Models.NotificationType.Error, TimeSpan.FromSeconds(15000)));
}
#endif
}
catch (Exception ex)
{
Logger?.LogCritical(ex, "Error during launch of managers");
}
}
}