Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Robust.Shared/Toolshed/Errors/DetachedSessionError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Diagnostics;
using Robust.Shared.Maths;
using Robust.Shared.Utility;

namespace Robust.Shared.Toolshed.Errors;

/// <summary>
/// A player-owned invocation context had no live session. A null session means it's ran from the server console,
/// so for obvious reasons we don't want this to be true for a client context.
/// </summary>
public record DetachedSessionError : IConError
{
public FormattedMessage DescribeInner()
{
return FormattedMessage.FromUnformatted("Your session is not ready to run commands.");
}

public string? Expression { get; set; }
public Vector2i? IssueSpan { get; set; }
public StackTrace? Trace { get; set; }
}
7 changes: 7 additions & 0 deletions Robust.Shared/Toolshed/IInvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public interface IInvocationContext
/// </remarks>
public bool CheckInvokable(CommandSpec command, out IConError? error)
{
// A null session means "server console". A player context that lost its session must never get that.
if (User != null && Session == null)
{
error = new DetachedSessionError();
return false;
}

return Toolshed.CheckInvokable(command, Session, out error);
}

Expand Down
25 changes: 23 additions & 2 deletions Robust.Shared/Toolshed/ToolshedManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ private void OnStatusChanged(object? sender, SessionStatusEventArgs e)

if (e.NewStatus == SessionStatus.InGame)
{
DebugTools.AssertNull(ctx.Session);
DebugTools.AssertNull(ctx.Shell);
// Shell may already have been re-attached by a command run before InGame.
ctx.Shell = new ConsoleShell(_conHost, e.Session, false);
}
}
Expand Down Expand Up @@ -94,6 +93,11 @@ public bool InvokeCommand(ICommonSession session, string command, object? input,
var shell = new ConsoleShell(_conHost, session, false);
_contexts[session.UserId] = ctx = new(shell);
}
else if (ctx.Shell?.Player != session)
{
// Cached context lost its shell on disconnect, re-attach to the current session.
ctx.Shell = new ConsoleShell(_conHost, session, false);
}

ctx.ClearErrors();

Expand Down Expand Up @@ -125,6 +129,11 @@ public bool InvokeCommand(IConsoleShell session, string command, object? input,
ourCtx = new OldShellInvocationContext(session);
_contexts[idx] = ourCtx;
}
else if (session.Player != null && ourCtx.Shell?.Player != session.Player)
{
// Cached context lost its shell on disconnect, use the live one.
ourCtx.Shell = session;
}

ourCtx.ClearErrors();
ctx = ourCtx;
Expand All @@ -151,6 +160,13 @@ public bool InvokeCommand(IInvocationContext ctx, string command, object? input,
ctx.ClearErrors();
result = null;

// Null session == server console, NEVER run a player-owned context without its session.
if (ctx.User != null && ctx.Session == null)
{
ctx.ReportError(new DetachedSessionError());
return false;
}

var parser = new ParserContext(command, this, ctx);
if (!CommandRun.TryParse(parser, input?.GetType(), null, out var expr))
{
Expand All @@ -167,13 +183,18 @@ public bool InvokeCommand(IInvocationContext ctx, string command, object? input,
var idx = shell.Player?.UserId ?? new NetUserId();
if (!_contexts.TryGetValue(idx, out var ourCtx))
ourCtx = _contexts[idx] = new OldShellInvocationContext(shell);
else if (shell.Player != null && ourCtx.Shell?.Player != shell.Player)
ourCtx.Shell = shell;

return GetCompletions(ourCtx, command);
}

public CompletionResult? GetCompletions(IInvocationContext ctx, string command)
{
ctx.ClearErrors();
if (ctx.User != null && ctx.Session == null)
return null;

var parser = new ParserContext(command, this, ctx);
parser.GenerateCompletions = true;
CommandRun.TryParse(parser, null, null, out _);
Expand Down
Loading