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

Last change on this file since 455 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
RevLine 
[455]1using JetBrains.Annotations;
2using Utf8Json;
[454]3using Webserver;
4using Webserver.WebAPI;
[279]5
[454]6namespace AllocsFixes.WebAPIs {
[455]7 [UsedImplicitly]
[454]8 public class GetAllowedCommands : AbsWebAPI {
[455]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
[454]15 public override void HandleRequest (RequestContext _context) {
[455]16 JsonWriter writer = new JsonWriter ();
17
18 writer.WriteRaw (jsonKeyCommands);
19 writer.WriteBeginArray ();
20
21 bool first = true;
22
[279]23 foreach (IConsoleCommand cc in SdtdConsole.Instance.GetCommands ()) {
[420]24 int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (cc.GetCommands ());
[455]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;
[279]33 }
[455]34 }
[325]35
[455]36 if (!first) {
37 writer.WriteValueSeparator ();
[279]38 }
[455]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 ();
[279]56 }
57
[455]58 writer.WriteEndArray ();
59 writer.WriteEndObject ();
60
61 WebUtils.WriteJsonData (_context.Response, ref writer);
[279]62 }
63
64 public override int DefaultPermissionLevel () {
65 return 2000;
66 }
67 }
[325]68}
Note: See TracBrowser for help on using the repository browser.