Exemplo n.º 1
0
 public ServiceConfigurationContext(
     ServiceConfigurationKey key,
     ServiceLifetime lifetime,
     Type typeFactory,
     Int32 priority = 0) : this(key, lifetime, typeFactory, key.Yield(), priority)
 {
 }
Exemplo n.º 2
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.º 3
0
 public ServiceConfigurationContext(
     ServiceConfigurationKey key,
     ServiceLifetime lifetime,
     Type typeFactory,
     ServiceConfigurationKey baseCacheKey,
     Int32 priority = 0) : this(key, lifetime, typeFactory, key.GetAncestors(baseCacheKey), priority)
 {
 }
Exemplo n.º 4
0
        protected override T Create <T>(ServiceProvider provider, ServiceConfigurationKey configurationKey, Action <T, ServiceProvider, ServiceConfiguration> setup = null, Guid?id = null)
        {
            return(base.Create <T>(provider, configurationKey, (pipe, p, c) =>
            {
                pipe.Channel = this.channel;

                setup?.Invoke(pipe, p, c);
            }, id));
        }
Exemplo n.º 5
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.º 6
0
        public ComponentConfigurationContext(
            ServiceConfigurationKey componentServiceConfigurationKey,
            ServiceConfigurationKey entityServicConfigurationeKey)
        {
            ExceptionHelper.ValidateAssignableFrom <IComponent>(componentServiceConfigurationKey.Type);
            ExceptionHelper.ValidateAssignableFrom <IEntity>(entityServicConfigurationeKey.Type);

            this.ComponentServiceConfigurationKey = componentServiceConfigurationKey;
            this.EntityServiceConfigurationKey    = entityServicConfigurationeKey;
        }
Exemplo n.º 7
0
 public ServiceConfigurationContext(
     ServiceConfigurationKey key,
     ServiceLifetime lifetime,
     Type typeFactory,
     IEnumerable <ServiceConfigurationKey> cacheKeys,
     Int32 priority = 0)
 {
     this.Key         = key;
     this.Lifetime    = lifetime;
     this.TypeFactory = typeFactory ?? key.Type;
     this.CacheKeys   = cacheKeys.Where(k => k != key).ToArray();
     this.Priority    = priority;
 }
Exemplo n.º 8
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.º 9
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.º 10
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.º 11
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
        }
Exemplo n.º 12
0
 protected override T Create <T>(ServiceProvider provider, ServiceConfigurationKey configurationKey, Action <T, ServiceProvider, ServiceConfiguration> setup = null, Guid?id = null)
 => base.Create(provider.CreateScope().ServiceProvider as ServiceProvider, configurationKey, setup, id);