Files
stream-cinema/CinemaJellyfin/CinemaServiceRegistrator.cs

64 lines
2.4 KiB
C#

using MediaBrowser.Controller;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Providers;
using Microsoft.Extensions.DependencyInjection;
namespace Jellyfin.Plugin.Cinema;
/// <summary>
/// Register Cinema services.
/// </summary>
///
public class CinemaServiceRegistrator : IPluginServiceRegistrator
{
/// <inheritdoc />
public void RegisterServices(IServiceCollection serviceCollection, IServerApplicationHost applicationHost)
{
//serviceCollection.AddSingleton<IImageProvider, CinemaImageProvider>();
serviceCollection.AddHostedService<CinemaHost>();
// HACK: Replace IMediaSourceManager as CinemaMediaSourceProvider is not called soon enough while
// entering a media item page.
Type tMsm = typeof(IMediaSourceManager);
int count = serviceCollection.Count;
for (int i = 0; i < count; i++)
{
ServiceDescriptor a = serviceCollection[i];
if (a.ServiceType == tMsm && !a.IsKeyedService)
{
Type oldImplType = a.ImplementationType!;
serviceCollection.RemoveAt(i);
serviceCollection.AddSingleton(new CinemaInnerMediaSourceManager(oldImplType));
serviceCollection.AddSingleton(oldImplType, oldImplType);
// Replace IMediaSourceManager but also need direct access to the CinemaMediaSourceManager if it itself
// also gets replaced in a longer chain
serviceCollection.AddSingleton<CinemaMediaSourceManager>();
serviceCollection.AddSingleton<IMediaSourceManager>(x => x.GetRequiredService<CinemaMediaSourceManager>());
break;
}
}
// HACK: Replace ILibraryManager as Series.GetEpisodes is not virtual
Type tLm = typeof(ILibraryManager);
int count2 = serviceCollection.Count;
for (int i = 0; i < count2; i++)
{
ServiceDescriptor a = serviceCollection[i];
if (a.ServiceType == tLm && !a.IsKeyedService)
{
Type oldImplType = a.ImplementationType!;
serviceCollection.RemoveAt(i);
serviceCollection.AddSingleton(new CinemaInnerLibraryManager(oldImplType));
serviceCollection.AddSingleton(oldImplType, oldImplType);
// Replace IMediaSourceManager but also need direct access to the CinemaLibraryManager if it itself
// also gets replaced in a longer chain
serviceCollection.AddSingleton<CinemaLibraryManager>();
serviceCollection.AddSingleton<ILibraryManager>(x => x.GetRequiredService<CinemaLibraryManager>());
break;
}
}
}
}