using System.Collections.Generic; using System.Linq; using Robust.Shared.Console; using Robust.Shared.IoC; using Robust.Shared.Network; using Robust.Shared.Player; using Robust.Shared.Toolshed.Errors; using Robust.Shared.Utility; namespace Robust.Shared.Toolshed.Invocation; /// internal sealed partial class OldShellInvocationContext : IInvocationContext { [Dependency] private ToolshedManager _toolshed = null!; public ToolshedManager Toolshed => _toolshed; public ToolshedEnvironment Environment => Toolshed.DefaultEnvironment; private readonly List _errors = new(); /// /// Old system's shell associated with this context. May be null if the player is currently disconnected. /// public IConsoleShell? Shell; /// public NetUserId? User { get; } /// public ICommonSession? Session => Shell?.Player; public OldShellInvocationContext(IConsoleShell shell) { IoCManager.InjectDependencies(this); Shell = shell; User = Session?.UserId; } /// public void WriteLine(string line) { Shell?.WriteLine(line); } /// public void WriteLine(FormattedMessage line) { Shell?.WriteLine(line); } /// public void ReportError(IConError err) { _errors.Add(err); } /// public IEnumerable GetErrors() { return _errors; } public bool HasErrors => _errors.Count > 0; /// public void ClearErrors() { _errors.Clear(); } /// public object? ReadVar(string name) { if (name == "self" && Session?.AttachedEntity is { } ent) return ent; return Variables.GetValueOrDefault(name); } /// public void WriteVar(string name, object? value) { if (name == "self") ReportError(new ReadonlyVariableError("self")); else Variables[name] = value; } /// public bool IsReadonlyVar(string name) => name == "self"; /// public IEnumerable GetVars() { return Session?.AttachedEntity != null ? Variables.Keys.Append("self") : Variables.Keys; } public Dictionary Variables { get; } = new(); }