source: binary-improvements2/WebServer/src/WebAPI/APIs/Command.cs@ 415

Last change on this file since 415 was 408, checked in by alloc, 21 months ago

Few pieces of code cleanup

File size: 4.6 KB
RevLine 
[402]1using System.Collections.Generic;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5
6namespace 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)) {
[404]27 IList<IConsoleCommand> ccs = SdtdConsole.Instance.GetCommands ();
28 for (int i = 0; i < ccs.Count; i++) {
29 IConsoleCommand cc = ccs [i];
30
31 if (i > 0) {
[402]32 writer.WriteValueSeparator ();
33 }
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
[408]58 string[] ses = _command.GetCommands ();
59 for (int i = 0; i < ses.Length; i++) {
60 string s = ses [i];
61
62 if (i > 0) {
[402]63 _writer.WriteValueSeparator ();
64 }
[408]65
[402]66 _writer.WriteString (s);
[408]67
[402]68 if (s.Length > cmd.Length) {
69 cmd = s;
70 }
71 }
[408]72
[402]73 _writer.WriteEndArray ();
74
75 _writer.WriteRaw (jsonCommandKey);
76 _writer.WriteString (cmd);
77
78 _writer.WriteRaw (jsonDescriptionKey);
79 _writer.WriteString (_command.GetDescription ());
80
81 _writer.WriteRaw (jsonHelpKey);
82 _writer.WriteString (_command.GetHelp ());
83
[404]84 int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (_command.GetCommands ());
[402]85 _writer.WriteRaw (jsonAllowedKey);
86 _writer.WriteBoolean (_userPermissionLevel <= commandPermissionLevel);
87
88 _writer.WriteEndObject ();
89 }
90
91 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
92 if (!TryGetJsonField (_jsonInput, "command", out string commandString)) {
93 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND");
94 return;
95 }
96
97 WebCommandResult.ResultType responseType = WebCommandResult.ResultType.Full;
98
99 if (TryGetJsonField (_jsonInput, "format", out string formatString)) {
100 if (formatString.EqualsCaseInsensitive ("raw")) {
101 responseType = WebCommandResult.ResultType.Raw;
102 } else if (formatString.EqualsCaseInsensitive ("simple")) {
103 responseType = WebCommandResult.ResultType.ResultOnly;
104 }
105 }
106
107 int commandSepIndex = commandString.IndexOf (' ');
108 string commandPart = commandSepIndex > 0 ? commandString.Substring (0, commandSepIndex) : commandString;
109 string argumentsPart = commandSepIndex > 0
110 ? commandString.Substring (commandPart.Length + 1)
111 : "";
112
113 IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandPart, true);
114
115 if (command == null) {
116 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND");
117 return;
118 }
119
[404]120 int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (command.GetCommands ());
[402]121
122 if (_context.PermissionLevel > commandPermissionLevel) {
123 SendErrorResult (_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION");
124 return;
125 }
126
127 _context.Response.SendChunked = true;
128 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _context);
129 SdtdConsole.Instance.ExecuteAsync (commandString, wcr);
130 }
131
[408]132 public override int DefaultPermissionLevel () => 2000;
[402]133 }
134}
Note: See TracBrowser for help on using the repository browser.