PalacePal/Pal.Client/DependencyInjectionContext.cs

189 lines
8.0 KiB
C#
Raw Normal View History

using System;
2023-02-16 18:51:54 +00:00
using System.IO;
2023-02-15 22:17:19 +00:00
using Dalamud.Data;
2023-02-15 13:35:11 +00:00
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.ClientState.Conditions;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Game.Command;
using Dalamud.Game.Gui;
2023-02-15 22:17:19 +00:00
using Dalamud.Interface.Windowing;
2023-02-15 13:35:11 +00:00
using Dalamud.Plugin;
2023-02-16 18:51:54 +00:00
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
2023-02-15 13:35:11 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
2023-02-15 22:17:19 +00:00
using Pal.Client.Commands;
using Pal.Client.Configuration;
using Pal.Client.Configuration.Legacy;
2023-02-16 18:51:54 +00:00
using Pal.Client.Database;
using Pal.Client.DependencyInjection;
using Pal.Client.DependencyInjection.Logging;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
2023-02-15 22:17:19 +00:00
using Pal.Client.Net;
2023-02-15 13:35:11 +00:00
using Pal.Client.Properties;
2023-02-15 22:17:19 +00:00
using Pal.Client.Rendering;
using Pal.Client.Scheduled;
using Pal.Client.Windows;
2023-02-15 13:35:11 +00:00
2023-02-16 18:51:54 +00:00
namespace Pal.Client
2023-02-15 13:35:11 +00:00
{
/// <summary>
/// DI-aware Plugin.
/// </summary>
internal sealed class DependencyInjectionContext : IDisposable
2023-02-15 13:35:11 +00:00
{
public static DalamudLoggerProvider LoggerProvider { get; } = new();
/// <summary>
/// Initialized as temporary logger, will be overriden once context is ready with a logger that supports scopes.
/// </summary>
private ILogger _logger = LoggerProvider.CreateLogger<DependencyInjectionContext>();
2023-02-16 18:51:54 +00:00
private readonly string _sqliteConnectionString;
private readonly ServiceCollection _serviceCollection = new();
2023-02-15 13:35:11 +00:00
private ServiceProvider? _serviceProvider;
public string Name => Localization.Palace_Pal;
public DependencyInjectionContext(
DalamudPluginInterface pluginInterface,
2023-02-15 13:35:11 +00:00
ClientState clientState,
GameGui gameGui,
ChatGui chatGui,
ObjectTable objectTable,
Framework framework,
Condition condition,
CommandManager commandManager,
DataManager dataManager,
Plugin plugin)
2023-02-15 13:35:11 +00:00
{
_logger.LogInformation("Building dalamud service container for {Assembly}",
2023-02-18 20:12:36 +00:00
typeof(DependencyInjectionContext).Assembly.FullName);
2023-02-16 18:51:54 +00:00
// set up legacy services
#pragma warning disable CS0612
JsonFloorState.SetContextProperties(pluginInterface.GetPluginConfigDirectory());
#pragma warning restore CS0612
// set up logging
_serviceCollection.AddLogging(builder =>
builder.AddFilter("Pal", LogLevel.Trace)
.AddFilter("Microsoft.EntityFrameworkCore.Database", LogLevel.Warning)
.AddFilter("Grpc", LogLevel.Debug)
.ClearProviders()
.AddProvider(LoggerProvider));
2023-02-15 13:35:11 +00:00
// dalamud
_serviceCollection.AddSingleton<IDalamudPlugin>(plugin);
_serviceCollection.AddSingleton(pluginInterface);
_serviceCollection.AddSingleton(clientState);
_serviceCollection.AddSingleton(gameGui);
_serviceCollection.AddSingleton(chatGui);
_serviceCollection.AddSingleton<Chat>();
_serviceCollection.AddSingleton(objectTable);
_serviceCollection.AddSingleton(framework);
_serviceCollection.AddSingleton(condition);
_serviceCollection.AddSingleton(commandManager);
_serviceCollection.AddSingleton(dataManager);
_serviceCollection.AddSingleton(new WindowSystem(typeof(DependencyInjectionContext).AssemblyQualifiedName));
2023-02-15 13:35:11 +00:00
2023-02-16 18:51:54 +00:00
_sqliteConnectionString =
$"Data Source={Path.Join(pluginInterface.GetPluginConfigDirectory(), "palace-pal.data.sqlite3")}";
}
public IServiceProvider BuildServiceContainer()
{
_logger.LogInformation("Building async service container for {Assembly}",
typeof(DependencyInjectionContext).Assembly.FullName);
// EF core
_serviceCollection.AddDbContext<PalClientContext>(o => o.UseSqlite(_sqliteConnectionString));
_serviceCollection.AddTransient<JsonMigration>();
_serviceCollection.AddScoped<Cleanup>();
2023-02-16 18:51:54 +00:00
2023-02-15 22:17:19 +00:00
// plugin-specific
_serviceCollection.AddScoped<DependencyContextInitializer>();
_serviceCollection.AddScoped<DebugState>();
2023-02-22 22:58:05 +00:00
_serviceCollection.AddScoped<GameHooks>();
_serviceCollection.AddScoped<RemoteApi>();
_serviceCollection.AddScoped<ConfigurationManager>();
_serviceCollection.AddScoped<IPalacePalConfiguration>(sp =>
sp.GetRequiredService<ConfigurationManager>().Load());
_serviceCollection.AddTransient<RepoVerification>();
// commands
_serviceCollection.AddScoped<PalConfigCommand>();
_serviceCollection.AddScoped<PalNearCommand>();
_serviceCollection.AddScoped<PalStatsCommand>();
_serviceCollection.AddScoped<PalTestConnectionCommand>();
2023-02-15 22:17:19 +00:00
2023-02-16 18:51:54 +00:00
// territory & marker related services
_serviceCollection.AddScoped<TerritoryState>();
_serviceCollection.AddScoped<FrameworkService>();
_serviceCollection.AddScoped<ChatService>();
_serviceCollection.AddScoped<FloorService>();
_serviceCollection.AddScoped<ImportService>();
2023-02-15 22:17:19 +00:00
// windows & related services
_serviceCollection.AddScoped<AgreementWindow>();
_serviceCollection.AddScoped<ConfigWindow>();
_serviceCollection.AddScoped<StatisticsService>();
_serviceCollection.AddScoped<StatisticsWindow>();
2023-02-15 22:17:19 +00:00
2023-02-21 16:32:13 +00:00
// rendering
_serviceCollection.AddScoped<SimpleRenderer>();
_serviceCollection.AddScoped<SplatoonRenderer>();
_serviceCollection.AddScoped<RenderAdapter>();
2023-02-15 22:17:19 +00:00
// queue handling
_serviceCollection.AddTransient<IQueueOnFrameworkThread.Handler<QueuedImport>, QueuedImport.Handler>();
_serviceCollection
.AddTransient<IQueueOnFrameworkThread.Handler<QueuedUndoImport>, QueuedUndoImport.Handler>();
_serviceCollection
.AddTransient<IQueueOnFrameworkThread.Handler<QueuedConfigUpdate>, QueuedConfigUpdate.Handler>();
_serviceCollection
.AddTransient<IQueueOnFrameworkThread.Handler<QueuedSyncResponse>, QueuedSyncResponse.Handler>();
2023-02-15 13:35:11 +00:00
// build
_serviceProvider = _serviceCollection.BuildServiceProvider(new ServiceProviderOptions
2023-02-15 13:35:11 +00:00
{
ValidateOnBuild = true,
ValidateScopes = true,
});
2023-02-16 18:51:54 +00:00
2023-02-15 22:17:19 +00:00
#if RELEASE
// You're welcome to remove this code in your fork, but please make sure that:
// - none of the links accessible within FFXIV open the original repo (e.g. in the plugin installer), and
// - you host your own server instance
//
// This is mainly to avoid this plugin being included in 'mega-repos' that, for whatever reason, decide
// that collecting all plugins is a good idea (and break half in the process).
_serviceProvider.GetService<RepoVerification>();
#endif
2023-02-16 18:51:54 +00:00
// This is not ideal as far as loading the plugin goes, because there's no way to check for errors and
// tell Dalamud that no, the plugin isn't ready -- so the plugin will count as properly initialized,
// even if it's not.
//
// There's 2-3 seconds of slowdown primarily caused by the sqlite init, but that needs to happen for
// config stuff.
_logger = _serviceProvider.GetRequiredService<ILogger<DependencyInjectionContext>>();
_logger.LogInformation("Service container built");
return _serviceProvider;
2023-02-15 13:35:11 +00:00
}
public void Dispose()
{
_logger.LogInformation("Disposing DI Context");
_serviceProvider?.Dispose();
// ensure we're not keeping the file open longer than the plugin is loaded
using (SqliteConnection sqliteConnection = new(_sqliteConnectionString))
SqliteConnection.ClearPool(sqliteConnection);
2023-02-15 13:35:11 +00:00
}
}
}