Composition root for the Superuser host-side admin runtime. Builds the storage backend, owner/policy service, command registry and dispatcher, audit, moderation, messages, content bans, alt detection, prop cleanup, votes, chat adapter and the optional Prefix module, then exposes health and player/identity resolution helpers to the rest of the engine layer.
using Sandbox;
namespace SboxSuperuser.Engine.Bootstrap;
internal sealed partial class BootstrapRuntime : IDisposable
{
private readonly Scene _scene;
private readonly Connection _host;
private readonly bool _allowEditorTesting;
private bool _disposed;
private DateTimeOffset _nextAuditMaintenance = DateTimeOffset.UtcNow.AddSeconds( 60 );
private Task _auditMaintenance;
internal Guid Id { get; } = Guid.NewGuid();
internal OwnerConfigurationFile Owners { get; }
internal Storage.ConfiguredAdminStore Store { get; }
internal Authority.HostPolicyService Policy { get; }
internal Commands.CommandRegistry Commands { get; }
internal Commands.HostCommandDispatcher Dispatcher { get; }
internal Audit.HostAuditService Audit { get; }
internal Core.Commands.ConsoleRootState ConsoleRoot { get; }
internal Commands.AdministrationCommands Administration { get; }
internal Players.PlayerCommands PlayerCommands { get; }
internal Modules.ModuleManager Modules { get; }
internal ChatIntegration.Platform.PlatformChatAdapter Chat { get; }
internal Moderation.HostModerationService Moderation { get; }
internal Messages.HostMessageService Messages { get; }
internal Votes.VoteSession Votes { get; }
internal Content.HostContentService Content { get; }
internal Core.Props.PropOwnership Props { get; } = new();
internal Props.IPropWorld PropWorld { get; } = new Props.ScenePropWorld();
internal Props.HostPropCleanup PropCleanup { get; }
internal Content.IContentCatalog ContentCatalog { get; } = new Content.EngineContentCatalog();
internal Alts.HostAltService Alts { get; }
internal Props.PropCommands PropCommands { get; }
internal Admin.HostAdminQueries AdminQueries { get; }
internal Admin.AdminRequestGate AdminRequests { get; } = new();
/// <summary>The shipped Prefix module, or null when the bootstrap's Prefixes option is off.</summary>
internal Prefixes.PrefixModule Prefixes { get; }
/// <summary>Recent public chat for moderators; memory only, cleared when this runtime stops.</summary>
internal Core.Chat.RecentChat RecentChat { get; } = new();
internal BootstrapState Health
{
get
{
if ( !Policy.IsUsable )
{
return Owners.State == BootstrapState.OwnersInvalid && Policy.OwnerSource == "bootstrap"
? BootstrapState.OwnersInvalid : BootstrapState.AuthorityDegraded;
}
if ( Audit.IsDegraded )
{
return BootstrapState.AuditDegraded;
}
if ( !Moderation.IsUsable ) return BootstrapState.ModerationDegraded;
if ( !ConsoleRoot.IsAvailable )
{
return BootstrapState.ConsoleUnavailable;
}
if ( Policy.OwnerSource == "bootstrap" )
{
return Owners.State;
}
return Policy.OwnerCount > 0 ? BootstrapState.Ready : BootstrapState.OwnersMissing;
}
}
internal bool IsUsable => !_disposed && _scene.IsValid() && _scene == Game.ActiveScene
&& Networking.IsActive && Networking.IsHost && _host is not null && _host.IsActive
&& ReferenceEquals( _host, Connection.Host )
&& (Application.IsDedicatedServer || (Game.IsEditor && _allowEditorTesting));
internal BootstrapRuntime( Scene scene, bool allowEditorTesting, string commandRoot = "admin",
Func<AdminStorageContext, IAdminStorageBackend> storageFactory = null, BootstrapModules modules = null )
{
_scene = scene;
_host = Connection.Host;
_allowEditorTesting = allowEditorTesting;
Owners = new OwnerConfigurationFile( Application.IsDedicatedServer );
var localStore = new Storage.HostAdminStore( Application.IsDedicatedServer, () => IsUsable );
IAdminStorageBackend backend = localStore;
try
{
if ( storageFactory is not null ) backend = storageFactory( new AdminStorageContext( Application.IsDedicatedServer, localStore ) );
if ( backend is null ) throw new InvalidOperationException( "The storage factory returned null." );
}
catch ( Exception error )
{
backend = null;
Log.Warning( $"[Superuser] Storage factory failed; type={error.GetType().Name}; no local fallback was selected." );
}
Store = new Storage.ConfiguredAdminStore( backend, () => ThreadSafe.IsMainThread && IsUsable,
error => Log.Warning( $"[Superuser] Storage adapter failed; type={error.GetType().Name}." ) );
Audit = new Audit.HostAuditService( Store, () => ThreadSafe.IsMainThread && IsUsable,
error => Log.Warning( $"[Superuser] Audit storage degraded; type={error.GetType().Name}; files preserved." ) );
Store.StorageChanged += Audit.OnStorageChanged;
var ownerStatus = Owners.State switch
{
BootstrapState.Ready => StoreReadStatus.Found,
BootstrapState.OwnersMissing => StoreReadStatus.Missing,
_ => StoreReadStatus.Corrupt
};
Policy = new Authority.HostPolicyService( Store, Owners.Configuration?.Ids ?? Array.Empty<string>(),
ownerStatus, () => IsUsable,
error => Log.Warning( $"[Superuser] Authority validation failed; type={error.GetType().Name}; files preserved." ), Audit );
Store.StorageChanged += Policy.OnStorageChanged;
Commands = new Commands.CommandRegistry( Policy.RegisteredPermissions, Policy.ReplaceRegisteredPermissions,
() => ThreadSafe.IsMainThread && IsUsable ) { ReadDocumentationFile = FileSystem.Mounted.ReadAllText };
Dispatcher = new Commands.HostCommandDispatcher( Policy, Commands, () => ThreadSafe.IsMainThread && IsUsable,
OnlinePlayers, error => Log.Warning( $"[Superuser] Command execution failed; type={error.GetType().Name}; outcome may be Unknown." ), audit: Audit );
ConsoleRoot = new Core.Commands.ConsoleRootState( commandRoot, TypeLibrary.GetMemberAttributes<ConVarAttribute>().Select( attribute => attribute.Name ) );
Modules = new Modules.ModuleManager( Commands, Policy, () => ThreadSafe.IsMainThread && IsUsable,
error => Log.Warning( $"[Superuser] Module lifecycle failed; type={error.GetType().Name}; inspect module status." ),
CommandDiscovery.AttributeCommandDiscovery.Discover );
Moderation = new Moderation.HostModerationService( Store, ModerationAvailable, Dispatcher.CanUse, CanModerateTarget, ReportModerationFailure );
Store.StorageChanged += Moderation.OnStorageChanged;
Messages = new Messages.HostMessageService( Store, () => IsUsable,
error => Log.Warning( $"[Superuser] Message templates failed to load; type={error.GetType().Name}; built-in wording is used." ) );
Store.StorageChanged += Messages.OnStorageChanged;
Content = new Content.HostContentService( Store, () => IsUsable,
error => Log.Warning( $"[Superuser] Content bans failed to load; type={error.GetType().Name}; nothing is banned until fixed." ) );
Store.StorageChanged += Content.OnStorageChanged;
Alts = new Alts.HostAltService( Store, () => IsUsable,
error => Log.Warning( $"[Superuser] Alt-account records failed to load; type={error.GetType().Name}; detection starts fresh." ),
wait => GameTask.DelayRealtime( (int)wait.TotalMilliseconds ) );
Store.StorageChanged += Alts.OnStorageChanged;
PropCleanup = new Props.HostPropCleanup( Store, () => IsUsable,
error => Log.Warning( $"[Superuser] Prop cleanup delay failed to load; type={error.GetType().Name}; the 2 minute default is used." ) );
Store.StorageChanged += PropCleanup.OnStorageChanged;
Votes = new Votes.VoteSession( Superuser.PublishVote, () => OnlinePlayers()?.Select( player => player.SteamId ).ToArray() ?? Array.Empty<string>(),
wait => GameTask.DelayRealtime( (int)wait.TotalMilliseconds ) );
AdminQueries = new Admin.HostAdminQueries( Policy, Moderation, Audit, OnlinePlayers, () => Modules.Statuses,
() => Commands.Descriptors, () => ConsoleDiagnostics().Data, RecentChat, Commands, Alts, Content, Props, PropCleanup, ContentCatalog );
Administration = new Commands.AdministrationCommands( Policy, Audit, Commands, Dispatcher, ConsoleDiagnostics, Modules,
Moderation, DisconnectModeratedAsync, NotifyModerationWarning, Superuser.Announce, Superuser.PlayerName,
new Messaging.EngineMessenger(), Messages, Votes, map => Superuser.PlayerActions.ChangeMap( map ), Content, Alts, ContentCatalog );
PlayerCommands = new Players.PlayerCommands( Commands, () => Superuser.PlayerActions,
() => OnlinePlayers()?.Select( player => player.SteamId ).ToArray() ?? Array.Empty<string>(), Dispatcher.CanTarget,
Superuser.PlayerName, Superuser.Announce, wait => GameTask.DelayRealtime( (int)wait.TotalMilliseconds ), Messages.Catalog,
map => Content.IsAllowed( Core.Content.ContentBanList.Map, map ) );
PropCommands = new Props.PropCommands( Commands, Props, PropWorld, Dispatcher.CanUse, Superuser.PlayerName, PropCleanup );
Chat = new ChatIntegration.Platform.PlatformChatAdapter( this );
modules ??= new BootstrapModules( true, true, "" );
if ( modules.Prefixes )
{
Prefixes = new Prefixes.PrefixModule( new Prefixes.PrefixHost( Store,
() => OnlinePlayers()?.Select( player => player.SteamId ).ToArray() ?? Array.Empty<string>(),
PrefixGroups, PrefixAllGroups, PrefixScriptProblem, key => Policy.RegisteredPermissions.Contains( key ), modules.PrefixedChat, modules.PrefixGuide ) );
}
if ( !ConsoleRoot.IsAvailable )
{
Log.Warning( $"[Superuser] {ConsoleRoot.Message}" );
}
}
// Owners count as the "owner" group, above every real group, so a prefix can be given to all of them.
private IReadOnlyList<Core.Prefixes.GroupRank> PrefixGroups( string steamId )
{
var ranks = new List<Core.Prefixes.GroupRank>();
if ( Policy.IsOwner( steamId ) ) ranks.Add( new( Engine.Prefixes.PrefixModule.OwnerGroup, Core.Permissions.TargetProtection.OwnerLevel ) );
var policy = Policy.CurrentPolicy;
if ( policy is null ) return ranks;
foreach ( var id in policy.ApplicableGroups( steamId, Policy.Claims ) ) ranks.Add( new( id, policy.Groups[ id ].ProtectionLevel ) );
return ranks;
}
private IReadOnlyList<Core.Prefixes.GroupRank> PrefixAllGroups()
{
var owner = new Core.Prefixes.GroupRank( Engine.Prefixes.PrefixModule.OwnerGroup, Core.Permissions.TargetProtection.OwnerLevel );
var groups = Policy.CurrentPolicy?.Groups.Values.Select( group => new Core.Prefixes.GroupRank( group.Id, group.ProtectionLevel ) )
?? Enumerable.Empty<Core.Prefixes.GroupRank>();
return groups.Prepend( owner ).ToArray();
}
private static string PrefixScriptProblem( string source )
{
var compiled = Core.Effects.EffectCompiler.Compile( source );
return compiled.Diagnostics.FirstOrDefault()?.Describe( source );
}
private CommandResult ConsoleDiagnostics() => new( CommandStatus.Success, "Host administration health.", new Dictionary<string, string>
{
[ "mode" ] = Application.IsDedicatedServer ? "DedicatedServer" : "EditorTesting",
[ "root" ] = ConsoleRoot.Name,
[ "console" ] = ConsoleRoot.Message,
[ "policy" ] = Policy.PolicyStatus.ToString(),
[ "owners" ] = Policy.OwnerStatus.ToString(),
[ "ownerSource" ] = Policy.OwnerSource,
[ "audit" ] = Audit.Diagnostics().ReadStatus.ToString(),
[ "auditDegraded" ] = Audit.IsDegraded.ToString(),
[ "auditWrite" ] = Audit.Diagnostics().LastWriteStatus.ToString(),
[ "retentionPending" ] = Audit.Diagnostics().RetentionPending.ToString(),
[ "moderation" ] = Moderation.Status.ToString(),
[ "migration" ] = "Player-hosted release games and host migration are unsupported."
} );
internal void MaintainAudit()
{
if ( !IsUsable || DateTimeOffset.UtcNow < _nextAuditMaintenance || _auditMaintenance is { IsCompleted: false } )
{
return;
}
_nextAuditMaintenance = DateTimeOffset.UtcNow.AddSeconds( 60 );
_auditMaintenance = Audit.MaintainAutomaticAsync( CancellationToken.None );
}
private IReadOnlyList<CommandPlayer> OnlinePlayers()
{
var players = new List<CommandPlayer>();
foreach ( var connection in Connection.All )
{
if ( !TryResolvePlayer( connection, out var actor ) )
{
continue;
}
var name = connection.DisplayName;
if ( string.IsNullOrWhiteSpace( name ) || name.Length > 256 || name.Any( char.IsControl ) )
{
name = actor.SubjectId;
}
players.Add( new CommandPlayer( actor.SubjectId, name ) );
if ( players.Count > 1024 )
{
return null;
}
}
return players.AsReadOnly();
}
internal bool TryResolvePlayer( Connection connection, out ActorIdentity identity )
{
identity = null;
if ( !IsUsable || connection is null || !connection.IsActive
|| !Connection.All.Any( current => ReferenceEquals( current, connection ) ) )
{
return false;
}
try
{
identity = new ActorIdentity( ActorKind.Player, connection.SteamId.ToString() );
return true;
}
catch ( ArgumentException )
{
// SteamId 0 (including the dedicated host connection) is not a player or console credential.
return false;
}
}
internal bool IsOwner( Connection connection )
{
return TryResolvePlayer( connection, out var identity )
&& Policy.IsOwner( identity.SubjectId );
}
internal CommandResult CheckOrdinaryChat( Connection sender )
{
if ( !TryResolvePlayer( sender, out var actor ) )
{
return new CommandResult( CommandStatus.Denied, "An authenticated player connection is required." );
}
if ( !Policy.IsUsable )
{
return new CommandResult( CommandStatus.StorageUnavailable, "Authority state is unavailable." );
}
return Moderation.CheckRestriction( actor.SubjectId, Core.Moderation.ModerationKind.TextMute );
}
// Called only by the separate non-forwarded dedicated-console entry, never the player route.
internal bool TryResolveDedicatedConsole( out ActorIdentity identity )
{
identity = null;
if ( !IsUsable || !Application.IsDedicatedServer )
{
return false;
}
identity = new ActorIdentity( ActorKind.DedicatedConsole, "dedicated-console" );
return true;
}
/// <summary>Revokes this runtime so retained references cannot resolve identities.</summary>
public void Dispose()
{
if ( _disposed ) return;
_disposed = true;
Chat?.Dispose();
RecentChat.Clear();
Modules?.Dispose();
Administration?.Dispose();
PlayerCommands?.Dispose();
PropCommands?.Dispose();
Dispatcher?.Dispose();
Store.StorageChanged -= Policy.OnStorageChanged;
Store.StorageChanged -= Audit.OnStorageChanged;
Store.StorageChanged -= Moderation.OnStorageChanged;
Store.StorageChanged -= Messages.OnStorageChanged;
Store.StorageChanged -= Content.OnStorageChanged;
Store.StorageChanged -= Alts.OnStorageChanged;
}
}