From 4a1b220d4a031a13304fbc798a19f342251f4af6 Mon Sep 17 00:00:00 2001 From: goat Date: Sat, 30 Sep 2023 01:09:25 +0200 Subject: [PATCH] fix: register interfaces for provided services --- Dalamud.CorePlugin/PluginImpl.cs | 5 ++- Dalamud.Injector/Hacks.cs | 20 +++++++++++ .../Internal/DalamudConfiguration.cs | 1 + Dalamud/Dalamud.cs | 1 + Dalamud/DalamudStartInfo.cs | 1 + Dalamud/Game/TargetSigScanner.cs | 1 + Dalamud/IoC/Internal/ServiceContainer.cs | 1 + Dalamud/ServiceManager.cs | 35 +++++++++---------- Dalamud/Storage/ReliableFileStorage.cs | 1 + 9 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 Dalamud.Injector/Hacks.cs diff --git a/Dalamud.CorePlugin/PluginImpl.cs b/Dalamud.CorePlugin/PluginImpl.cs index 5ed672f2d..03f11e989 100644 --- a/Dalamud.CorePlugin/PluginImpl.cs +++ b/Dalamud.CorePlugin/PluginImpl.cs @@ -2,6 +2,7 @@ using System; using System.IO; using Dalamud.Configuration.Internal; +using Dalamud.Game; using Dalamud.Game.Command; using Dalamud.Interface.Windowing; using Dalamud.Plugin; @@ -55,7 +56,7 @@ namespace Dalamud.CorePlugin /// /// Dalamud plugin interface. /// Logging service. - public PluginImpl(DalamudPluginInterface pluginInterface, IPluginLog log) + public PluginImpl(DalamudPluginInterface pluginInterface, IPluginLog log, ISigScanner scanner) { try { @@ -65,6 +66,8 @@ namespace Dalamud.CorePlugin this.windowSystem.AddWindow(new PluginWindow()); + this.pluginLog.Information(scanner.ToString()); + this.Interface.UiBuilder.Draw += this.OnDraw; this.Interface.UiBuilder.OpenConfigUi += this.OnOpenConfigUi; this.Interface.UiBuilder.OpenMainUi += this.OnOpenMainUi; diff --git a/Dalamud.Injector/Hacks.cs b/Dalamud.Injector/Hacks.cs new file mode 100644 index 000000000..7bc4468af --- /dev/null +++ b/Dalamud.Injector/Hacks.cs @@ -0,0 +1,20 @@ +using System; + +// ReSharper disable once CheckNamespace +namespace Dalamud; + +// TODO: Get rid of this! Move StartInfo to another assembly, make this good + +/// +/// Class to initialize Service<T>s. +/// +internal static class ServiceManager +{ + /// + /// Indicates that the class is a service. + /// + [AttributeUsage(AttributeTargets.Class)] + public class Service : Attribute + { + } +} diff --git a/Dalamud/Configuration/Internal/DalamudConfiguration.cs b/Dalamud/Configuration/Internal/DalamudConfiguration.cs index 63494931c..1e9bc1523 100644 --- a/Dalamud/Configuration/Internal/DalamudConfiguration.cs +++ b/Dalamud/Configuration/Internal/DalamudConfiguration.cs @@ -19,6 +19,7 @@ namespace Dalamud.Configuration.Internal; /// Class containing Dalamud settings. /// [Serializable] +[ServiceManager.Service] internal sealed class DalamudConfiguration : IServiceType, IDisposable { private static readonly JsonSerializerSettings SerializerSettings = new() diff --git a/Dalamud/Dalamud.cs b/Dalamud/Dalamud.cs index 2187f0da2..a9a3a511a 100644 --- a/Dalamud/Dalamud.cs +++ b/Dalamud/Dalamud.cs @@ -29,6 +29,7 @@ namespace Dalamud; /// /// The main Dalamud class containing all subsystems. /// +[ServiceManager.Service] internal sealed class Dalamud : IServiceType { #region Internals diff --git a/Dalamud/DalamudStartInfo.cs b/Dalamud/DalamudStartInfo.cs index 63a61c97e..f22388e9b 100644 --- a/Dalamud/DalamudStartInfo.cs +++ b/Dalamud/DalamudStartInfo.cs @@ -10,6 +10,7 @@ namespace Dalamud; /// Struct containing information needed to initialize Dalamud. /// [Serializable] +[ServiceManager.Service] public record DalamudStartInfo : IServiceType { /// diff --git a/Dalamud/Game/TargetSigScanner.cs b/Dalamud/Game/TargetSigScanner.cs index 0360f95cc..9242c5e83 100644 --- a/Dalamud/Game/TargetSigScanner.cs +++ b/Dalamud/Game/TargetSigScanner.cs @@ -11,6 +11,7 @@ namespace Dalamud.Game; /// [PluginInterface] [InterfaceVersion("1.0")] +[ServiceManager.Service] #pragma warning disable SA1015 [ResolveVia] #pragma warning restore SA1015 diff --git a/Dalamud/IoC/Internal/ServiceContainer.cs b/Dalamud/IoC/Internal/ServiceContainer.cs index 3dd76473f..ce7ce25a1 100644 --- a/Dalamud/IoC/Internal/ServiceContainer.cs +++ b/Dalamud/IoC/Internal/ServiceContainer.cs @@ -16,6 +16,7 @@ namespace Dalamud.IoC.Internal; /// This is only used to resolve dependencies for plugins. /// Dalamud services are constructed via Service{T}.ConstructObject at the moment. /// +[ServiceManager.Service] internal class ServiceContainer : IServiceProvider, IServiceType { private static readonly ModuleLog Log = new("SERVICECONTAINER"); diff --git a/Dalamud/ServiceManager.cs b/Dalamud/ServiceManager.cs index 57e4ace10..bb680127c 100644 --- a/Dalamud/ServiceManager.cs +++ b/Dalamud/ServiceManager.cs @@ -92,28 +92,24 @@ internal static class ServiceManager var cacheDir = new DirectoryInfo(Path.Combine(startInfo.WorkingDirectory!, "cachedSigs")); if (!cacheDir.Exists) cacheDir.Create(); - + lock (LoadedServices) { - Service.Provide(dalamud); - LoadedServices.Add(typeof(Dalamud)); - - Service.Provide(startInfo); - LoadedServices.Add(typeof(DalamudStartInfo)); + void ProvideService(T service) where T : IServiceType + { + Debug.Assert(typeof(T).GetServiceKind().HasFlag(ServiceKind.ManualService), "Provided service must have Service attribute"); + Service.Provide(service); + LoadedServices.Add(typeof(T)); + } - Service.Provide(fs); - LoadedServices.Add(typeof(ReliableFileStorage)); - - Service.Provide(configuration); - LoadedServices.Add(typeof(DalamudConfiguration)); - - Service.Provide(new ServiceContainer()); - LoadedServices.Add(typeof(ServiceContainer)); - - Service.Provide( + ProvideService(dalamud); + ProvideService(startInfo); + ProvideService(fs); + ProvideService(configuration); + ProvideService(new ServiceContainer()); + ProvideService( new TargetSigScanner( - true, new FileInfo(Path.Combine(cacheDir.FullName, $"{startInfo.GameVersion}.json")))); - LoadedServices.Add(typeof(TargetSigScanner)); + true, new FileInfo(Path.Combine(cacheDir.FullName, $"{startInfo.GameVersion}.json")))); } using (Timings.Start("CS Resolver Init")) @@ -149,7 +145,8 @@ internal static class ServiceManager serviceContainer.RegisterInterfaces(serviceType); // Scoped service do not go through Service and are never early loaded - if (serviceKind.HasFlag(ServiceKind.ScopedService)) + // Manual services are provided + if (serviceKind.HasFlag(ServiceKind.ScopedService) || serviceKind.HasFlag(ServiceKind.ManualService)) continue; Debug.Assert( diff --git a/Dalamud/Storage/ReliableFileStorage.cs b/Dalamud/Storage/ReliableFileStorage.cs index 7fdd04880..6bc6cabcc 100644 --- a/Dalamud/Storage/ReliableFileStorage.cs +++ b/Dalamud/Storage/ReliableFileStorage.cs @@ -23,6 +23,7 @@ namespace Dalamud.Storage; /// /// This is not an early-loaded service, as it is needed before they are initialized. /// +[ServiceManager.Service] public class ReliableFileStorage : IServiceType, IDisposable { private static readonly ModuleLog Log = new("VFS");