Exemplo n.º 1
0
 public void RegisterServices(DependencyInjection.ServiceCollection services)
 {
     AssemblyHelper.Types.GetTypesWithAutoLoadAttribute(typeof(LoaderService <, ,>)).ForEach(s =>
     {
         services.RegisterTypeFactory(s, p => ActivatorUtilities.CreateInstance(p, s));
         services.RegisterSingleton(ServiceConfigurationKey.From(s));
     });
 }
Exemplo n.º 2
0
        public static T BuildGame <T>(this GuppyLoader guppy, ServiceConfigurationKey?key = null)
            where T : Game
        {
            if (!guppy.Initialized)
            {
                throw new Exception("Please initialize Guppy before building a game instance.");
            }

            return(guppy.BuildServiceProvider().GetService <T>(key ?? ServiceConfigurationKey.From <T>()));
        }
Exemplo n.º 3
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));
            });
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
0
        public void RegisterServices(ServiceCollection services)
        {
            #region Settings Setup
            services.RegisterSetup <Settings>((s, p, c) =>
            { // Configure the default settings...
                s.Set <NetworkAuthorization>(NetworkAuthorization.Slave);
                s.Set <HostType>(HostType.Remote);
            }, -10);

            services.RegisterSetup <ServerPeer>((server, p, c) =>
            {
                var settings = p.GetService <Settings>();
                settings.Set <NetworkAuthorization>(NetworkAuthorization.Master);
            });
            #endregion

            services.RegisterTypeFactory <NetOutgoingMessageService>(p => new NetOutgoingMessageService());
            services.RegisterTypeFactory <ChannelList>(p => new ChannelList());
            services.RegisterTypeFactory <PipeList>(p => new PipeList());
            services.RegisterTypeFactory <UserList>(p => new UserList());
            services.RegisterTypeFactory <ServiceList <INetworkEntity> >(p => new ServiceList <INetworkEntity>());
            services.RegisterTypeFactory <NetworkEntityList>(p => new NetworkEntityList());
            services.RegisterTypeFactory <ServerPeer>(p => new ServerPeer());
            services.RegisterTypeFactory <NetServer>(p => new NetServer(p.GetService <NetPeerConfiguration>()));
            services.RegisterTypeFactory <ClientPeer>(p => new ClientPeer());
            services.RegisterTypeFactory <NetClient>(p => new NetClient(p.GetService <NetPeerConfiguration>()));
            services.RegisterTypeFactory <NetPeerConfiguration>(p => new NetPeerConfiguration("guppy"));
            services.RegisterTypeFactory <ServerChannel>(p => new ServerChannel());
            services.RegisterTypeFactory <ClientChannel>(p => new ClientChannel());
            services.RegisterTypeFactory <IPipe>(p => new Pipe());
            services.RegisterTypeFactory <IUser>(p => new User());

            services.RegisterSingleton <NetOutgoingMessageService>();
            services.RegisterSingleton <ChannelList>();
            services.RegisterScoped <PipeList>();
            services.RegisterSingleton <UserList>();
            services.RegisterTransient(Constants.ServiceConfigurations.TransientUserList);
            services.RegisterTransient <ServiceList <INetworkEntity> >();
            services.RegisterScoped <NetworkEntityList>();
            services.RegisterSingleton <ServerPeer>(baseLookupType: typeof(IPeer));
            services.RegisterSingleton <NetServer>(baseLookupType: typeof(NetPeer));
            services.RegisterSingleton <ClientPeer>(baseLookupType: typeof(IPeer));
            services.RegisterSingleton <NetClient>(baseLookupType: typeof(NetPeer));
            services.RegisterSingleton <NetPeerConfiguration>();
            services.RegisterScoped <ServerChannel>();
            services.RegisterScoped <ClientChannel>();
            services.RegisterTransient <IPipe>();
            services.RegisterTransient <IUser>();

            services.RegisterSetup <UserList>(Constants.ServiceConfigurations.TransientUserList, (users, p, c) =>
            { // Automatically try to add the current user when connecting to a new client.
                // TODO: Investigate what happens if CurrentUser is not yet defined?
                users.TryAdd(p.GetService <IPeer>().CurrentUser);
            }, Guppy.Core.Constants.Priorities.Initialize + 1);

            services.RegisterSetup <INetworkEntity>((service, p, c) =>
            { // Automatically add any network services into the scoped service list.
                p.GetService <NetworkEntityList>().TryAdd(service);
            }, Guppy.Core.Constants.Priorities.Initialize + 1);

            #region Components
            services.RegisterTypeFactory <ChannelBaseCRUDComponent>(p => new ChannelBaseCRUDComponent());
            services.RegisterTypeFactory <PipeMasterCRUDComponent>(p => new PipeMasterCRUDComponent());

            services.RegisterTransient <ChannelBaseCRUDComponent>();
            services.RegisterTransient <PipeMasterCRUDComponent>();

            services.RegisterComponent <ChannelBaseCRUDComponent, IChannel>();
            services.RegisterComponent <PipeMasterCRUDComponent, IPipe>();

            services.RegisterComponentFilter(
                ServiceConfigurationKey.From(type: typeof(RemoteHostComponent <>)),
                (e, p, t) =>
            {
                return(t.GetCustomAttribute <NetworkAuthorizationRequiredAttribute>().NetworkAuthorization
                       == p.GetService <Settings>().Get <NetworkAuthorization>());
            },
                (cc, ec) =>
            {
                var hasAttribute = cc.TypeFactory.Type.GetCustomAttribute <NetworkAuthorizationRequiredAttribute>() != default;
                return(hasAttribute);
            });
            #endregion
        }