[402] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Net;
|
---|
| 3 | using JetBrains.Annotations;
|
---|
| 4 | using Utf8Json;
|
---|
| 5 |
|
---|
| 6 | namespace Webserver.WebAPI.APIs {
|
---|
| 7 | [UsedImplicitly]
|
---|
| 8 | public class Command : AbsRestApi {
|
---|
| 9 | private static readonly byte[] jsonCommandsKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("commands");
|
---|
| 10 |
|
---|
| 11 | private static readonly byte[] jsonOverloadsKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("overloads");
|
---|
| 12 | private static readonly byte[] jsonCommandKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("command");
|
---|
| 13 | private static readonly byte[] jsonDescriptionKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("description");
|
---|
| 14 | private static readonly byte[] jsonHelpKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("help");
|
---|
| 15 | private static readonly byte[] jsonAllowedKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("allowed");
|
---|
| 16 |
|
---|
| 17 | protected override void HandleRestGet (RequestContext _context) {
|
---|
| 18 | string id = _context.RequestPath;
|
---|
| 19 | int permissionLevel = _context.PermissionLevel;
|
---|
| 20 |
|
---|
| 21 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 22 |
|
---|
| 23 | writer.WriteRaw (jsonCommandsKey);
|
---|
| 24 | writer.WriteBeginArray ();
|
---|
| 25 |
|
---|
| 26 | if (string.IsNullOrEmpty (id)) {
|
---|
| 27 | bool first = true;
|
---|
| 28 | foreach (IConsoleCommand cc in SdtdConsole.Instance.GetCommands ()) {
|
---|
| 29 | if (!first) {
|
---|
| 30 | writer.WriteValueSeparator ();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | first = false;
|
---|
| 34 |
|
---|
| 35 | writeCommandJson (ref writer, cc, permissionLevel);
|
---|
| 36 | }
|
---|
| 37 | } else if (SdtdConsole.Instance.GetCommand (id) is { } command) {
|
---|
| 38 | writeCommandJson (ref writer, command, permissionLevel);
|
---|
| 39 | } else {
|
---|
| 40 | writer.WriteEndArray ();
|
---|
| 41 | writer.WriteEndObject ();
|
---|
| 42 | SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
|
---|
| 43 | return;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | writer.WriteEndArray ();
|
---|
| 47 | writer.WriteEndObject ();
|
---|
| 48 |
|
---|
| 49 | SendEnvelopedResult (_context, ref writer);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | private void writeCommandJson (ref JsonWriter _writer, IConsoleCommand _command, int _userPermissionLevel) {
|
---|
| 53 | _writer.WriteRaw (jsonOverloadsKey);
|
---|
| 54 | _writer.WriteBeginArray ();
|
---|
| 55 |
|
---|
| 56 | string cmd = string.Empty;
|
---|
| 57 |
|
---|
| 58 | bool firstOverload = true;
|
---|
| 59 | foreach (string s in _command.GetCommands ()) {
|
---|
| 60 | if (!firstOverload) {
|
---|
| 61 | _writer.WriteValueSeparator ();
|
---|
| 62 | }
|
---|
| 63 | firstOverload = false;
|
---|
| 64 |
|
---|
| 65 | _writer.WriteString (s);
|
---|
| 66 |
|
---|
| 67 | if (s.Length > cmd.Length) {
|
---|
| 68 | cmd = s;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | _writer.WriteEndArray ();
|
---|
| 73 |
|
---|
| 74 | _writer.WriteRaw (jsonCommandKey);
|
---|
| 75 | _writer.WriteString (cmd);
|
---|
| 76 |
|
---|
| 77 | _writer.WriteRaw (jsonDescriptionKey);
|
---|
| 78 | _writer.WriteString (_command.GetDescription ());
|
---|
| 79 |
|
---|
| 80 | _writer.WriteRaw (jsonHelpKey);
|
---|
| 81 | _writer.WriteString (_command.GetHelp ());
|
---|
| 82 |
|
---|
| 83 | int commandPermissionLevel = GameManager.Instance.adminTools.GetCommandPermissionLevel (_command.GetCommands ());
|
---|
| 84 | _writer.WriteRaw (jsonAllowedKey);
|
---|
| 85 | _writer.WriteBoolean (_userPermissionLevel <= commandPermissionLevel);
|
---|
| 86 |
|
---|
| 87 | _writer.WriteEndObject ();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
|
---|
| 91 | if (!TryGetJsonField (_jsonInput, "command", out string commandString)) {
|
---|
| 92 | SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND");
|
---|
| 93 | return;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | WebCommandResult.ResultType responseType = WebCommandResult.ResultType.Full;
|
---|
| 97 |
|
---|
| 98 | if (TryGetJsonField (_jsonInput, "format", out string formatString)) {
|
---|
| 99 | if (formatString.EqualsCaseInsensitive ("raw")) {
|
---|
| 100 | responseType = WebCommandResult.ResultType.Raw;
|
---|
| 101 | } else if (formatString.EqualsCaseInsensitive ("simple")) {
|
---|
| 102 | responseType = WebCommandResult.ResultType.ResultOnly;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | int commandSepIndex = commandString.IndexOf (' ');
|
---|
| 107 | string commandPart = commandSepIndex > 0 ? commandString.Substring (0, commandSepIndex) : commandString;
|
---|
| 108 | string argumentsPart = commandSepIndex > 0
|
---|
| 109 | ? commandString.Substring (commandPart.Length + 1)
|
---|
| 110 | : "";
|
---|
| 111 |
|
---|
| 112 | IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandPart, true);
|
---|
| 113 |
|
---|
| 114 | if (command == null) {
|
---|
| 115 | SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND");
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | int commandPermissionLevel = GameManager.Instance.adminTools.GetCommandPermissionLevel (command.GetCommands ());
|
---|
| 120 |
|
---|
| 121 | if (_context.PermissionLevel > commandPermissionLevel) {
|
---|
| 122 | SendErrorResult (_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION");
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | _context.Response.SendChunked = true;
|
---|
| 127 | WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _context);
|
---|
| 128 | SdtdConsole.Instance.ExecuteAsync (commandString, wcr);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public override int DefaultPermissionLevel () {
|
---|
| 132 | return 2000;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|