// WingsEmu // // Developed by NosWings Team using System; using System.Reflection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using PhoenixLib.Extensions; namespace WingsAPI.Plugins.Extensions { public static class AssemblyExtensions { public static void AddTypesImplementingInterfaceInAssembly(this IServiceCollection services, Assembly assembly) { Type[] types = assembly.GetTypesImplementingInterface(); foreach (Type handlerType in types) { services.AddTransient(handlerType); } } } public static class FeatureToggleExtensions { public static void TryAddSingletonFeatureToggleEnabledByDefault(this IServiceCollection services, string envVarName) where TInterface : class where TImplementation : class, TInterface { services.TryAddSingletonFeatureToggle(envVarName, true); } public static void TryAddSingletonFeatureToggleDisabledByDefault(this IServiceCollection services, string envVarName) where TInterface : class where TImplementation : class, TInterface { services.TryAddSingletonFeatureToggle(envVarName, false); } public static void TryAddSingletonFeatureToggle(this IServiceCollection services, string envVarName, bool defaultActivationState) where TInterface : class where TImplementation : class, TInterface { if (!bool.TryParse(Environment.GetEnvironmentVariable(envVarName) ?? defaultActivationState.ToString(), out bool isActivated)) { return; } if (!isActivated) { return; } services.TryAddSingleton(); } public static void TryAddTransientFeatureToggleEnabledByDefault(this IServiceCollection services, string envVarName) where TInterface : class where TImplementation : class, TInterface { services.TryAddTransientFeatureToggle(envVarName, true); } public static void TryAddTransientFeatureToggleDisabledByDefault(this IServiceCollection services, string envVarName) where TInterface : class where TImplementation : class, TInterface { services.TryAddTransientFeatureToggle(envVarName, false); } public static void TryAddTransientFeatureToggle(this IServiceCollection services, string envVarName, bool defaultActivationState) where TInterface : class where TImplementation : class, TInterface { if (!bool.TryParse(Environment.GetEnvironmentVariable(envVarName) ?? defaultActivationState.ToString(), out bool isActivated)) { return; } if (!isActivated) { return; } services.TryAddTransient(); } } }