Utility/AdminCommands.cs

Defines a single admin console command restart_scene that restarts the current Scene by reloading its source SceneFile. It checks that the caller is the listen-server host, validates the active scene and its source, stops sounds, and issues a scene Load with new SceneLoadOptions.

Networking
using Sandbox;

namespace BrickJam;

/// <summary>
/// Developer/admin console commands. The Scene System has no <c>[ConCmd.Admin]</c>, so "admin" is gated
/// as host-only (the listen-server host).
/// </summary>
public static class AdminCommands
{
	/// <summary>
	/// Reload the current scene from scratch (full restart). Host/admin only. Console: <c>restart_scene</c>.
	/// </summary>
	[ConCmd( "restart_scene" )]
	public static void RestartScene()
	{
		if ( !Networking.IsHost )
		{
			Log.Warning( "restart_scene: host/admin only." );
			return;
		}

		var scene = Game.ActiveScene;
		if ( !scene.IsValid() )
			return;

		if ( scene.Source is not SceneFile sceneFile )
		{
			Log.Warning( "restart_scene: the current scene has no source SceneFile to reload." );
			return;
		}

		Log.Info( "[admin] Restarting scene..." );

		Sound.StopAll( 0.4f );
		
		var options = new SceneLoadOptions();
		options.SetScene( sceneFile );
		scene.Load( options );
	}
}