Exemplo n.º 1
0
        GameHostBuilderContext GetGameHostBuilderContext(HostBuilderContext context)
        {
            if (!context.Properties.TryGetValue(typeof(GameHostBuilderContext), out var contextVal))
            {
                var options = new GameHostOptions(context.Configuration, Assembly.GetEntryAssembly()?.GetName().Name);
                var gameHostBuilderContext = new GameHostBuilderContext
                {
                    Configuration      = context.Configuration,
                    HostingEnvironment = new HostingEnvironment(),
                };
                gameHostBuilderContext.HostingEnvironment.Initialize(context.HostingEnvironment.ContentRootPath, options);
                context.Properties[typeof(GameHostBuilderContext)] = gameHostBuilderContext;
                context.Properties[typeof(GameHostOptions)]        = options;
                return(gameHostBuilderContext);
            }

            // Refresh config, it's periodically updated/replaced
            var gameHostContext = (GameHostBuilderContext)contextVal;

            gameHostContext.Configuration = context.Configuration;
            return(gameHostContext);
        }
Exemplo n.º 2
0
        void ExecuteHostingStartups()
        {
            var gameHostOptions = new GameHostOptions(_config, Assembly.GetEntryAssembly()?.GetName().Name);

            if (gameHostOptions.PreventHostingStartup)
            {
                return;
            }

            var exceptions = new List <Exception>();

            _hostingStartupGameHostBuilder = new HostingStartupGameHostBuilder(this);

            // Execute the hosting startup assemblies
            foreach (var assemblyName in gameHostOptions.GetFinalHostingStartupAssemblies().Distinct(StringComparer.OrdinalIgnoreCase))
            {
                try
                {
                    var assembly = Assembly.Load(new AssemblyName(assemblyName));

                    foreach (var attribute in assembly.GetCustomAttributes <HostingStartupAttribute>())
                    {
                        var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType);
                        hostingStartup.Configure(_hostingStartupGameHostBuilder);
                    }
                }
                catch (Exception ex)
                {
                    // Capture any errors that happen during startup
                    exceptions.Add(new InvalidOperationException($"Startup assembly {assemblyName} failed to execute. See the inner exception for more details.", ex));
                }
            }

            if (exceptions.Count > 0)
            {
                _hostingStartupErrors = new AggregateException(exceptions);
            }
        }
Exemplo n.º 3
0
        public GameHost(
            IServiceCollection appServices,
            IServiceProvider hostingServiceProvider,
            GameHostOptions options,
            IConfiguration config,
            AggregateException hostingStartupErrors)
        {
            _config = config ?? throw new ArgumentNullException(nameof(config));
            _hostingStartupErrors = hostingStartupErrors;
            Options = options;
            _applicationServiceCollection = appServices ?? throw new ArgumentNullException(nameof(appServices));
            _hostingServiceProvider       = hostingServiceProvider ?? throw new ArgumentNullException(nameof(hostingServiceProvider));
            _applicationServiceCollection.AddSingleton <ApplicationLifetime>();
            // There's no way to to register multiple service types per definition. See https://github.com/aspnet/DependencyInjection/issues/360
#pragma warning disable CS0618 // Type or member is obsolete
            _applicationServiceCollection.AddSingleton(services => services.GetService <ApplicationLifetime>() as IHostApplicationLifetime);
            _applicationServiceCollection.AddSingleton(services => services.GetService <ApplicationLifetime>() as GameNetCore.Hosting.IApplicationLifetime);
#if !NET2
            _applicationServiceCollection.AddSingleton(services => services.GetService <ApplicationLifetime>() as Extensions.Hosting.IApplicationLifetime);
#endif
#pragma warning restore CS0618 // Type or member is obsolete
            _applicationServiceCollection.AddSingleton <HostedServiceExecutor>();
        }
        public static void Initialize(this IGameHostEnvironment hostingEnvironment, string contentRootPath, GameHostOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrEmpty(contentRootPath))
            {
                throw new ArgumentException("A valid non-empty content root must be provided.", nameof(contentRootPath));
            }
            if (!Directory.Exists(contentRootPath))
            {
                throw new ArgumentException($"The content root '{contentRootPath}' does not exist.", nameof(contentRootPath));
            }

            hostingEnvironment.ApplicationName         = options.ApplicationName;
            hostingEnvironment.ContentRootPath         = contentRootPath;
            hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath);

            var gameRoot = options.GameRoot;

            if (gameRoot == null)
            {
                // Default to /wwwroot if it exists.
                var gameroot = Path.Combine(hostingEnvironment.ContentRootPath, "gameroot");
                if (Directory.Exists(gameroot))
                {
                    hostingEnvironment.GameRootPath = gameroot;
                }
            }
            else
            {
                hostingEnvironment.GameRootPath = Path.Combine(hostingEnvironment.ContentRootPath, gameRoot);
            }

            if (!string.IsNullOrEmpty(hostingEnvironment.GameRootPath))
            {
                hostingEnvironment.GameRootPath = Path.GetFullPath(hostingEnvironment.GameRootPath);
                if (!Directory.Exists(hostingEnvironment.GameRootPath))
                {
                    Directory.CreateDirectory(hostingEnvironment.GameRootPath);
                }
                hostingEnvironment.GameRootFileProvider = new PhysicalFileProvider(hostingEnvironment.GameRootPath);
            }
            else
            {
                hostingEnvironment.GameRootFileProvider = new NullFileProvider();
            }

            hostingEnvironment.EnvironmentName =
                options.Environment ??
                hostingEnvironment.EnvironmentName;
        }