source: binary-improvements/MapRendering/API/GetAllowedCommands.cs@ 464

Last change on this file since 464 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 1.9 KB
Line 
1using JetBrains.Annotations;
2using Utf8Json;
3using Webserver;
4using Webserver.WebAPI;
5
6namespace 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}
Note: See TracBrowser for help on using the repository browser.