Code/SboxSuperuser/Superuser.Storage.cs

Part of the Superuser admin library's public facade. ConfigureStorage hands a storage backend factory to the scene's SuperuserSession on the host's main thread, and GetStore returns the active IRecoverableAdminStore from the session runtime, or null after the main-thread check fails or when there is no valid scene.

using Sandbox;
using SboxSuperuser.Systems;

namespace SboxSuperuser;

public static partial class Superuser
{
	/// <summary>Selects storage for this scene before any bootstrap is enabled; trusted host setup only, never an RPC.</summary>
	public static void ConfigureStorage( Func<AdminStorageContext, IAdminStorageBackend> factory )
	{
		ArgumentNullException.ThrowIfNull( factory );
		if ( !ThreadSafe.IsMainThread || !Game.ActiveScene.IsValid() || !Networking.IsHost )
			throw new InvalidOperationException( "Configure storage on the active host scene's main thread." );
		Game.ActiveScene.GetSystem<SuperuserSession>().ConfigureStorage( factory );
	}

	/// <summary>Gets the active host store on the main thread, or null outside an eligible bootstrap runtime.</summary>
	public static IRecoverableAdminStore GetStore()
	{
		if ( !ThreadSafe.IsMainThread )
		{
			return null;
		}

		var scene = Game.ActiveScene;
		return scene.IsValid() ? scene.GetSystem<SuperuserSession>()?.GetRuntime()?.Store : null;
	}
}