Toolshed Additions: Field & DoExt commands (#6329)
* Toolshed Additions * Removed MarkingCommand, added description to FieldCommand * Added DoExt command
This commit is contained in:
parent
ae4a864ad0
commit
f20d8d9f7f
|
|
@ -0,0 +1,163 @@
|
|||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Numerics;
|
||||
using System.Text.RegularExpressions;
|
||||
using Content.Server.Administration;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Toolshed;
|
||||
using Robust.Shared.Toolshed.Errors;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server._DV.Toolshed;
|
||||
|
||||
/// <summary>
|
||||
/// An extended version of the engine's <c>do</c> command. Runs a console command once per piped value,
|
||||
/// substituting <c>$NAME</c> tokens in the command string first.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// On top of the built-in tokens that <c>do</c> supports (<c>$ID</c>, <c>$PID</c>, <c>$WX</c>, <c>$WY</c>,
|
||||
/// <c>$LX</c>, <c>$LY</c>, <c>$SELF</c>), any other <c>$name</c> is resolved as a toolshed variable and
|
||||
/// converted to a string. Tokens that don't resolve to anything are left untouched.
|
||||
///
|
||||
/// Where the command actually runs is explicit:
|
||||
/// <list type="bullet">
|
||||
/// <item><c>doext:client</c> sends it back down to the calling user's own client to run locally. This is what
|
||||
/// you want for client-side commands like <c>exec</c>, which resolve paths against the client's user data.</item>
|
||||
/// <item><c>doext:server</c> runs it server-side as the calling session, like <c>do</c> does.</item>
|
||||
/// </list>
|
||||
/// </remarks>
|
||||
/// <example><code>
|
||||
/// self doext:client "exec /Script_$var.txt"
|
||||
/// i 5 => $count; self doext:server "somecommand $count"
|
||||
/// </code></example>
|
||||
[ToolshedCommand(Name = "doext"), AdminCommand(AdminFlags.Debug)]
|
||||
public sealed class DoExtCommand : ToolshedCommand
|
||||
{
|
||||
/// <summary>
|
||||
/// Matches a <c>$name</c> token. Names use the same character set as toolshed variable names, so this
|
||||
/// matches whole identifiers - <c>$IDLE</c> resolves as "IDLE" rather than being mangled into <c>$ID</c>.
|
||||
/// </summary>
|
||||
private static readonly Regex TokenRegex = new(@"\$(\w+)", RegexOptions.Compiled);
|
||||
|
||||
[Dependency] private readonly IConsoleHost _console = default!;
|
||||
|
||||
private SharedTransformSystem? _xform;
|
||||
|
||||
/// <summary>
|
||||
/// Sends the command back down to the calling user's client, which runs it locally.
|
||||
/// </summary>
|
||||
[CommandImplementation("client"), TakesPipedTypeAsGeneric]
|
||||
public IEnumerable<T> Client<T>(IInvocationContext ctx, [PipedArgument] IEnumerable<T> input, string command)
|
||||
{
|
||||
// No session means there's no client to hand this back to - e.g. the server console.
|
||||
if (ctx.Session is not { } session)
|
||||
{
|
||||
ctx.ReportError(new NoClientSessionError());
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var value in input)
|
||||
{
|
||||
_console.RemoteExecuteCommand(session, Substitute(command, value, ctx));
|
||||
yield return value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs the command on the server as the calling session.
|
||||
/// </summary>
|
||||
[CommandImplementation("server"), TakesPipedTypeAsGeneric]
|
||||
public IEnumerable<T> Server<T>(IInvocationContext ctx, [PipedArgument] IEnumerable<T> input, string command)
|
||||
{
|
||||
foreach (var value in input)
|
||||
{
|
||||
_console.ExecuteCommand(ctx.Session, Substitute(command, value, ctx));
|
||||
yield return value;
|
||||
}
|
||||
}
|
||||
|
||||
private string Substitute<T>(string command, T value, IInvocationContext ctx)
|
||||
{
|
||||
return TokenRegex.Replace(command, match =>
|
||||
{
|
||||
var name = match.Groups[1].Value;
|
||||
|
||||
if (TryGetBuiltin(name, value, ctx, out var builtin))
|
||||
return builtin;
|
||||
|
||||
// Anything else is looked up as a toolshed variable and implicitly stringified.
|
||||
if (ctx.ReadVar(name) is { } variable)
|
||||
return Stringify(variable);
|
||||
|
||||
// Unresolved, so leave the token alone rather than silently blanking it.
|
||||
return match.Value;
|
||||
});
|
||||
}
|
||||
|
||||
private bool TryGetBuiltin<T>(string name, T value, IInvocationContext ctx, [NotNullWhen(true)] out string? result)
|
||||
{
|
||||
switch (name)
|
||||
{
|
||||
case "SELF":
|
||||
result = Stringify(value);
|
||||
return true;
|
||||
case "PID":
|
||||
result = (ctx.Session?.AttachedEntity ?? EntityUid.Invalid).ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
// The remaining tokens are all positional, so they only apply when piping entities.
|
||||
if (value is not EntityUid uid)
|
||||
{
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (name)
|
||||
{
|
||||
case "ID":
|
||||
result = uid.ToString();
|
||||
return true;
|
||||
case "WX":
|
||||
result = Number(WorldPosition(uid).X);
|
||||
return true;
|
||||
case "WY":
|
||||
result = Number(WorldPosition(uid).Y);
|
||||
return true;
|
||||
case "LX":
|
||||
result = Number(Transform(uid).Coordinates.X);
|
||||
return true;
|
||||
case "LY":
|
||||
result = Number(Transform(uid).Coordinates.Y);
|
||||
return true;
|
||||
}
|
||||
|
||||
result = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private Vector2 WorldPosition(EntityUid uid)
|
||||
{
|
||||
_xform ??= GetSys<SharedTransformSystem>();
|
||||
return _xform.GetWorldPosition(uid);
|
||||
}
|
||||
|
||||
private static string Number(float value) => value.ToString(CultureInfo.InvariantCulture);
|
||||
|
||||
private static string Stringify(object? value) => value?.ToString() ?? string.Empty;
|
||||
}
|
||||
|
||||
public sealed class NoClientSessionError : IConError
|
||||
{
|
||||
public FormattedMessage DescribeInner()
|
||||
{
|
||||
return FormattedMessage.FromUnformatted("There is no client to run this on. doext:client must be run by a player, not the server console.");
|
||||
}
|
||||
|
||||
public string? Expression { get; set; }
|
||||
public Vector2i? IssueSpan { get; set; }
|
||||
public StackTrace? Trace { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,136 @@
|
|||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Content.Server.Administration;
|
||||
using Content.Shared.Administration;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Toolshed;
|
||||
using Robust.Shared.Toolshed.Errors;
|
||||
using Robust.Shared.Toolshed.Syntax;
|
||||
using Robust.Shared.Toolshed.TypeParsers;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server._DV.Toolshed;
|
||||
|
||||
/// <summary>
|
||||
/// Reads the value of a single field or property off of the piped input, by name.
|
||||
/// Member names are resolved the same way <c>vvread</c> does: the member must be exposed
|
||||
/// via <c>[ViewVariables]</c>, <c>[DataField]</c>, or <c>[IncludeDataField]</c>.
|
||||
/// </summary>
|
||||
/// <example><code>ent 12345 . Name</code></example>
|
||||
[ToolshedCommand(Name = "."), AdminCommand(AdminFlags.Debug)]
|
||||
public sealed class FieldCommand : ToolshedCommand
|
||||
{
|
||||
[CommandImplementation]
|
||||
public object? Field([PipedArgument] object? value, [CommandArgument(typeof(FieldNameParser))] string field, IInvocationContext ctx)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
ctx.ReportError(new NullInputFieldError());
|
||||
return null;
|
||||
}
|
||||
|
||||
var type = value.GetType();
|
||||
var member = GetSingleMember(type, field);
|
||||
|
||||
// Restrict to members that vvread would let you read, so this can't be used to peek at arbitrary internals.
|
||||
if (member == null || !ViewVariablesUtility.TryGetViewVariablesAccess(member, out _))
|
||||
{
|
||||
ctx.ReportError(new NoSuchFieldError(type, field));
|
||||
return null;
|
||||
}
|
||||
|
||||
return member switch
|
||||
{
|
||||
FieldInfo f => f.GetValue(value),
|
||||
PropertyInfo p => p.GetValue(value),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the field or property with the given name, mirroring the resolution used by <c>vvread</c>.
|
||||
/// </summary>
|
||||
private static MemberInfo? GetSingleMember(Type type, string member)
|
||||
{
|
||||
var members = type
|
||||
.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Where(m => m.Name == member && m is FieldInfo or PropertyInfo)
|
||||
.ToArray();
|
||||
|
||||
if (members.Length == 0)
|
||||
return null;
|
||||
|
||||
// In case there's member hiding going on, grab the one declared by the type of the object by default.
|
||||
return members.Length > 1
|
||||
? members.FirstOrDefault(m => m.DeclaringType == type) ?? members[0]
|
||||
: members[0];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parses a field name as a bare identifier (letters, digits, underscore) so that the <c>.</c> command doesn't
|
||||
/// require the name to be wrapped in quotes like the default <see cref="string"/> parser does.
|
||||
/// </summary>
|
||||
public sealed class FieldNameParser : CustomTypeParser<string>
|
||||
{
|
||||
public override bool TryParse(ParserContext ctx, [NotNullWhen(true)] out string? result)
|
||||
{
|
||||
ctx.ConsumeWhitespace();
|
||||
result = ctx.GetWord(ParserContext.IsToken);
|
||||
if (result != null)
|
||||
return true;
|
||||
|
||||
if (ctx.PeekRune() is null)
|
||||
ctx.Error = new OutOfInputError();
|
||||
else
|
||||
ctx.Error = new InvalidFieldNameError();
|
||||
|
||||
ctx.Error.Contextualize(ctx.Input, (ctx.Index, ctx.Index + 1));
|
||||
return false;
|
||||
}
|
||||
|
||||
public override CompletionResult? TryAutocomplete(ParserContext ctx, CommandArgument? arg)
|
||||
{
|
||||
return CompletionResult.FromHint(GetArgHint(arg));
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class InvalidFieldNameError : IConError
|
||||
{
|
||||
public FormattedMessage DescribeInner()
|
||||
{
|
||||
return FormattedMessage.FromUnformatted("Expected a field name (letters, digits, or underscores).");
|
||||
}
|
||||
|
||||
public string? Expression { get; set; }
|
||||
public Vector2i? IssueSpan { get; set; }
|
||||
public StackTrace? Trace { get; set; }
|
||||
}
|
||||
|
||||
public sealed class NullInputFieldError : IConError
|
||||
{
|
||||
public FormattedMessage DescribeInner()
|
||||
{
|
||||
return FormattedMessage.FromUnformatted("Cannot read a field off of a null input.");
|
||||
}
|
||||
|
||||
public string? Expression { get; set; }
|
||||
public Vector2i? IssueSpan { get; set; }
|
||||
public StackTrace? Trace { get; set; }
|
||||
}
|
||||
|
||||
public sealed class NoSuchFieldError(Type type, string field) : IConError
|
||||
{
|
||||
public FormattedMessage DescribeInner()
|
||||
{
|
||||
return FormattedMessage.FromUnformatted($"Type {type.Name} has no readable field or property named '{field}'. It must be exposed via [ViewVariables], [DataField], or [IncludeDataField].");
|
||||
}
|
||||
|
||||
public string? Expression { get; set; }
|
||||
public Vector2i? IssueSpan { get; set; }
|
||||
public StackTrace? Trace { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
command-description-doext-client = Sends a console command back to the calling user's own client to run locally, once per piped value, substituting $ID/$PID/$WX/$WY/$LX/$LY/$SELF as well as any toolshed variable by name.
|
||||
command-description-doext-server = Runs a console command on the server as the calling session, once per piped value, substituting $ID/$PID/$WX/$WY/$LX/$LY/$SELF as well as any toolshed variable by name.
|
||||
|
|
@ -0,0 +1 @@
|
|||
command-description-FieldCommand = Reads the value of a field or property (by name) off of the piped input, resolved the same way vvread does.
|
||||
Loading…
Reference in New Issue