Framework/Bootstrap/FrameworkSystem.cs

Bootstrap entry point for the framework. A GameObjectSystem that runs on scene startup, calls FrameworkHost.EnsureBooted for non-editor scenes, starts hosting on a dedicated server when no PlayerManager exists, and has remote clients send a hello RPC so the host spawns their player.

namespace RoleplayFramework.Bootstrap;

/// <summary>
/// Created by the engine for every game scene. Boots the framework the first time any game scene starts,
/// so it works no matter which scene is the entry point (menu, test map, or a dedicated server's map).
/// Editor scenes are ignored.
/// </summary>
public sealed class FrameworkSystem : GameObjectSystem<FrameworkSystem>, ISceneStartup
{
	public FrameworkSystem( Scene scene ) : base( scene )
	{
	}

	void ISceneStartup.OnHostPreInitialize( SceneFile scene ) { }

	void ISceneStartup.OnHostInitialize()
	{
		if ( Scene.IsEditor ) return;
		FrameworkHost.EnsureBooted();

		// A dedicated server starts in the startup (menu) scene: host the world straight away.
		if ( Application.IsDedicatedServer && FrameworkHost.State == StartupState.Ready && !Scene.GetAllComponents<Players.PlayerManager>().Any() )
			Net.SessionService.StartDedicated();
	}

	void ISceneStartup.OnClientInitialize()
	{
		if ( Scene.IsEditor ) return;
		FrameworkHost.EnsureBooted();

		// A remote client has loaded the host's scene: introduce ourselves. The host spawns our
		// player only after this handshake succeeds.
		if ( Networking.IsClient && !Networking.IsHost )
			Net.FrameworkRpc.SendHello();
	}
}