Ejemplo n.º 1
0
 public static void RegisterScene <TScene>(
     this ServiceCollection services,
     ServiceConfigurationKey?key            = default,
     Func <ServiceProvider, TScene> factory = null,
     Int32 priority = 0)
     where TScene : class, IScene
 => services.RegisterScene(
     scene : typeof(TScene),
     key : key,
     factory : factory,
     priority : priority);
Ejemplo n.º 2
0
 public static void RegisterGame <TGame>(
     this ServiceCollection services,
     ServiceConfigurationKey?key           = default,
     Func <ServiceProvider, TGame> factory = default,
     Int32 priority = 0)
     where TGame : Game
 => services.RegisterGame(
     game : typeof(TGame),
     key : key,
     factory : factory,
     priority : priority);
        public void RegisterServices(ServiceCollection services)
        {
            services.RegisterTypeFactory <IServiceScopeFactory>(p => new ServiceScopeFactory(p));
            services.RegisterScoped <IServiceScopeFactory>();

            services.RegisterTypeFactory <IServiceScope>(p => new ServiceScope(p));
            services.RegisterScoped <IServiceScope>();

            services.RegisterTypeFactory <ServiceProvider>(p => p);
            services.RegisterScoped <IServiceProvider>(typeof(ServiceProvider));
            services.RegisterScoped <ServiceProvider>(typeof(ServiceProvider));

            services.RegisterTypeFactory <Settings>(p => new Settings());
            services.RegisterSingleton <Settings>();
        }
Ejemplo n.º 4
0
        public void RegisterServices(ServiceCollection services)
        {
            services.AddFactory <UIService>(p => new UIService());
            services.AddFactory <Stage>(p => new Stage());
            services.AddFactory <ScreenLayer>(p => new ScreenLayer());

            services.AddSingleton <UIService>();
            services.AddTransient <Stage>();
            services.AddTransient <ScreenLayer>();
            services.AddTransient <Camera2D>("screen-camera");

            // Register default elements
            services.AddFactory(typeof(ElementList <>), (p, t) => ActivatorUtilities.CreateInstance(p, t));
            services.AddFactory(typeof(Container <>), (p, t) => ActivatorUtilities.CreateInstance(p, t));
            services.AddFactory <Container>(p => new Container());
            services.AddFactory <PageContainer>(p => new PageContainer());
            services.AddFactory(typeof(StackContainer <>), (p, t) => ActivatorUtilities.CreateInstance(p, t));
            services.AddFactory(typeof(InnerStackContainer <>), (p, t) => ActivatorUtilities.CreateInstance(p, t));
            services.AddFactory <StackContainer>(p => new StackContainer());
            services.AddFactory <Element>(p => new Element());
            services.AddFactory <TextElement>(p => new TextElement());
            services.AddFactory <TextInput>(p => new TextInput());

            services.AddTransient(typeof(ElementList <>));
            services.AddTransient(typeof(Container <>));
            services.AddTransient <Container>();
            services.AddTransient <PageContainer>();
            services.AddTransient(typeof(StackContainer <>));
            services.AddTransient(typeof(InnerStackContainer <>));
            services.AddTransient <StackContainer>();
            services.AddTransient <Element>();
            services.AddTransient <TextElement>();
            services.AddTransient <TextInput>();

            // Register Content
            services.AddSetup <ContentService>((content, p, c) =>
            {
                content.TryRegister("ui:font", "UI/Font");
            });

            services.AddSetup <TextElement>((text, p, c) =>
            {
                text.Font      = p.GetContent <SpriteFont>("ui:font");
                text.Alignment = Alignment.CenterLeft;
            }, -1);
        }
Ejemplo n.º 5
0
        public void RegisterServices(ServiceCollection services)
        {
            services.RegisterTypeFactory <SpriteBatch>(p => new SpriteBatch(p.GetService <GraphicsDevice>()));
            services.RegisterTypeFactory <Camera2D>(p => new Camera2D());

            services.RegisterScoped <SpriteBatch>();
            services.RegisterTransient(Guppy.Constants.ServiceConfigurationKeys.TransientSpritebatch);
            services.RegisterScoped <Camera2D>();
            services.RegisterTransient(Guppy.Constants.ServiceConfigurationKeys.TransientCamera);

            AssemblyHelper.AddAssembly(typeof(GraphicsDevice).Assembly);
            AssemblyHelper.Types.GetTypesAssignableFrom <IVertexType>().Where(t => t.IsValueType).ForEach(vt =>
            {
                var primitiveBatchType = typeof(PrimitiveBatch <>).MakeGenericType(vt);
                services.RegisterTypeFactory(primitiveBatchType, (p, t) => ActivatorUtilities.CreateInstance(p, t));
                services.RegisterSingleton(ServiceConfigurationKey.From(type: primitiveBatchType));
            });
        }
Ejemplo n.º 6
0
        public static void RegisterScene(
            this ServiceCollection services,
            Type scene,
            ServiceConfigurationKey?key            = default,
            Func <ServiceProvider, Object> factory = default,
            Int32 priority = 0)
        {
            ExceptionHelper.ValidateAssignableFrom <IScene>(scene);

            factory ??= p => ActivatorUtilities.CreateInstance(p, scene);
            services.RegisterTypeFactory(
                type: scene,
                method: factory,
                priority: priority);
            services.RegisterScoped(
                key: key ?? ServiceConfigurationKey.From(type: scene),
                priority: priority,
                baseLookupType: typeof(IScene));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Parse an incoming service descriptor instance &
        /// convert it into a useable Guppy TypeFactory/ServiceConfiguration
        /// </summary>
        /// <param name="descriptor"></param>
        /// <param name="services"></param>
        internal static void ConvertServiceDescriptor(this ServiceDescriptor descriptor, ServiceCollection services)
        {
            var implementationType = descriptor.ImplementationType ?? descriptor.ServiceType;
            var key   = ServiceConfigurationKey.From(descriptor.ServiceType);
            var count = services.ServiceConfigurations.Count(c => c.Key == key);

            services.RegisterTypeFactory(
                type: implementationType,
                typeImplementation: implementationType,
                method: descriptor.CreateFactoryMethod(),
                priority: count);

            services.RegisterServiceConfiguration(
                key,
                descriptor.Lifetime,
                implementationType,
                ServiceConfigurationKey.From(implementationType).Yield(),
                priority: count);
        }