Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
ServiceCollectionExtensions.cs
2using Microsoft.Extensions.DependencyInjection;
3using Microsoft.Extensions.Logging;
4
6
10public static class ServiceCollectionExtensions
11{
18 public static IServiceCollection AddEngineLogging(this IServiceCollection services)
19 {
20 ArgumentNullException.ThrowIfNull(services);
21
22 // Ensure the standard Microsoft logging services exist.
23 services.AddLogging();
24
25 // Capture the ILoggerFactory registration that DI would normally resolve.
26 // We cannot resolve ILoggerFactory from inside another ILoggerFactory
27 // registration because that creates a circular dependency.
28 var factoryDescriptor = services.Last(descriptor =>
29 descriptor.ServiceType == typeof(ILoggerFactory)
30 && !descriptor.IsKeyedService);
31
32 // If the application supplied an ILoggerFactory instance directly,
33 // there is nothing to defer: initialize Gondwana with that same instance
34 // and leave DI ownership/registration unchanged.
35 if (factoryDescriptor.ImplementationInstance is ILoggerFactory existingFactory)
36 {
37 EngineLogger.Initialize(existingFactory);
38 return services;
39 }
40
41 // Replace the effective registration with a wrapper that constructs the
42 // original factory exactly as DI would have, then exposes that same factory
43 // to Gondwana.
44 services.Remove(factoryDescriptor);
45
46 services.Add(ServiceDescriptor.Describe(
47 typeof(ILoggerFactory),
48 provider =>
49 {
50 var factory = CreateLoggerFactory(provider, factoryDescriptor);
51 EngineLogger.Initialize(factory);
52
53 return factory;
54 },
55 factoryDescriptor.Lifetime));
56
57 return services;
58 }
59
60 private static ILoggerFactory CreateLoggerFactory(
61 IServiceProvider provider,
62 ServiceDescriptor descriptor)
63 {
64 if (descriptor.ImplementationFactory is not null)
65 {
66 return (ILoggerFactory)descriptor.ImplementationFactory(provider);
67 }
68
69 if (descriptor.ImplementationType is not null)
70 {
71 return (ILoggerFactory)ActivatorUtilities.CreateInstance(
72 provider,
73 descriptor.ImplementationType);
74 }
75
76 throw new InvalidOperationException(
77 "The registered ILoggerFactory service could not be constructed.");
78 }
79}