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