Skip to content

Commit

Permalink
feat: ✨ Implement the possible separation of building the app context…
Browse files Browse the repository at this point in the history
… and booting
  • Loading branch information
Hobart2967 committed Oct 14, 2024
1 parent 2635684 commit 4e79708
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions Source/Runtime/AppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
using GoDough.Visuals;
using System;

namespace GoDough.Runtime {
public class AppHost {
namespace GoDough.Runtime
{
public class AppHost
{
#region Private Fields
public Node AutoLoadNode { get; private set; }
#endregion
Expand All @@ -29,21 +31,24 @@ public class AppHost {
public event InputEventHandler OnUnhandledInput;

private static AppHost? _instance = null;

Check warning on line 33 in Source/Runtime/AppHost.cs

View workflow job for this annotation

GitHub Actions / On Push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 33 in Source/Runtime/AppHost.cs

View workflow job for this annotation

GitHub Actions / On Push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
public static AppHost? Instance {
public static AppHost? Instance

Check warning on line 34 in Source/Runtime/AppHost.cs

View workflow job for this annotation

GitHub Actions / On Push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 34 in Source/Runtime/AppHost.cs

View workflow job for this annotation

GitHub Actions / On Push

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
get { return AppHost._instance; }
}
#endregion

#region Ctor
public AppHost(Node autoLoadNode) {
public AppHost(Node autoLoadNode)
{
this.AutoLoadNode = autoLoadNode;

AppHost._instance = AppHost.Instance ?? this;
}
#endregion

#region Public Methods
public void Start() {
public void Start(Boolean deferBoot = false)
{
GD.Print("[GoDough] Building AppHost");
var builder = Host.CreateDefaultBuilder(null);

Expand All @@ -55,39 +60,49 @@ public void Start() {
GD.Print("[GoDough] Sealing AppHost");
this.Application = builder.Build();

this.Boot();
if (!deferBoot)
{
this.Boot();
}
}

public virtual void ConfigureLogging(ILoggingBuilder loggingBuilder) {
public virtual void ConfigureLogging(ILoggingBuilder loggingBuilder)
{
loggingBuilder.AddGodotLogger();

if (!OS.IsDebugBuild()) {
if (!OS.IsDebugBuild())
{
return;
}

loggingBuilder.SetMinimumLevel(LogLevel.Trace);
}

public virtual void ConfigureServices(IServiceCollection services) {
public virtual void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton<IAppHostNodeProvider, AppHostNodeProvider>(x =>
new AppHostNodeProvider(() => this.AutoLoadNode))
.AddSingleton(typeof(SceneManagementService<>));
}

private void Boot() {
public void Boot()
{
var logger = this.Application.Services.GetService<ILogger<AppHost>>();

var bootstrappers = this.Application.Services.GetServices<IBootstrapper>();
logger.LogInformation("Booting App with {0} Bootstrappers", bootstrappers.Count());

foreach(var service in bootstrappers) {
foreach (var service in bootstrappers)
{
service.Run();
}
}

public void PhysicsProcess(double delta) {
if (this.OnPhysicsProcess != null) {
public void PhysicsProcess(double delta)
{
if (this.OnPhysicsProcess != null)
{
this.OnPhysicsProcess.Invoke(this, delta);
}

Expand All @@ -97,17 +112,21 @@ public void PhysicsProcess(double delta) {



public void UnhandledInput(InputEvent ev) {
if (this.OnUnhandledInput != null) {
public void UnhandledInput(InputEvent ev)
{
if (this.OnUnhandledInput != null)
{
this.OnUnhandledInput.Invoke(this, ev);
}

this.InvokeLifeCycleHooks<IOnUnhandledInput>(x =>
x.OnUnhandledInput(ev));
}

public void Input(InputEvent ev) {
if (this.OnInput != null) {
public void Input(InputEvent ev)
{
if (this.OnInput != null)
{
this.OnInput.Invoke(this, ev);
}

Expand All @@ -116,8 +135,10 @@ public void Input(InputEvent ev) {
}


public void Process(double delta) {
if (this.OnProcess != null) {
public void Process(double delta)
{
if (this.OnProcess != null)
{
this.OnProcess.Invoke(this, delta);
}

Expand All @@ -127,9 +148,11 @@ public void Process(double delta) {
#endregion

#region Private Methods
private void InvokeLifeCycleHooks<T>(Action<T> action) {
private void InvokeLifeCycleHooks<T>(Action<T> action)
{
var framebasedServices = this.Application.Services.GetServices<T>();
foreach(var service in framebasedServices) {
foreach (var service in framebasedServices)
{
action(service);
}
}
Expand Down

0 comments on commit 4e79708

Please sign in to comment.