1 | using JetBrains.Annotations;
|
---|
2 | using Utf8Json;
|
---|
3 | using Webserver;
|
---|
4 | using Webserver.WebAPI;
|
---|
5 |
|
---|
6 | namespace AllocsFixes.WebAPIs {
|
---|
7 | [UsedImplicitly]
|
---|
8 | public class GetAllowedCommands : AbsWebAPI {
|
---|
9 | private static readonly byte[] jsonKeyCommands = JsonWriter.GetEncodedPropertyNameWithBeginObject ("commands");
|
---|
10 |
|
---|
11 | private static readonly byte[] jsonKeyCommand = JsonWriter.GetEncodedPropertyNameWithBeginObject ("command");
|
---|
12 | private static readonly byte[] jsonKeyDescription = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("description");
|
---|
13 | private static readonly byte[] jsonKeyHelp = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("help");
|
---|
14 |
|
---|
15 | public override void HandleRequest (RequestContext _context) {
|
---|
16 | JsonWriter writer = new JsonWriter ();
|
---|
17 |
|
---|
18 | writer.WriteRaw (jsonKeyCommands);
|
---|
19 | writer.WriteBeginArray ();
|
---|
20 |
|
---|
21 | bool first = true;
|
---|
22 |
|
---|
23 | foreach (IConsoleCommand cc in SdtdConsole.Instance.GetCommands ()) {
|
---|
24 | int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (cc.GetCommands ());
|
---|
25 | if (_context.PermissionLevel > commandPermissionLevel) {
|
---|
26 | continue;
|
---|
27 | }
|
---|
28 |
|
---|
29 | string cmd = string.Empty;
|
---|
30 | foreach (string s in cc.GetCommands ()) {
|
---|
31 | if (s.Length > cmd.Length) {
|
---|
32 | cmd = s;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | if (!first) {
|
---|
37 | writer.WriteValueSeparator ();
|
---|
38 | }
|
---|
39 |
|
---|
40 | first = false;
|
---|
41 |
|
---|
42 | writer.WriteRaw (jsonKeyCommand);
|
---|
43 | writer.WriteString (cmd);
|
---|
44 |
|
---|
45 | writer.WriteRaw (jsonKeyDescription);
|
---|
46 | writer.WriteString (cc.GetDescription ());
|
---|
47 |
|
---|
48 | writer.WriteRaw (jsonKeyHelp);
|
---|
49 | if (cc.GetHelp () == null) {
|
---|
50 | writer.WriteString ("");
|
---|
51 | } else {
|
---|
52 | writer.WriteString (cc.GetHelp ());
|
---|
53 | }
|
---|
54 |
|
---|
55 | writer.WriteEndObject ();
|
---|
56 | }
|
---|
57 |
|
---|
58 | writer.WriteEndArray ();
|
---|
59 | writer.WriteEndObject ();
|
---|
60 |
|
---|
61 | WebUtils.WriteJsonData (_context.Response, ref writer);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override int DefaultPermissionLevel () {
|
---|
65 | return 2000;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|