source: binary-improvements/MapRendering/API/ExecuteConsoleCommand.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.8 KB
Line 
1using System;
2using System.Net;
3using JetBrains.Annotations;
4using Webserver;
5using Webserver.WebAPI;
6using WebCommandResult = AllocsFixes.Web.WebCommandResult;
7
8namespace AllocsFixes.WebAPIs {
9 [UsedImplicitly]
10 public class ExecuteConsoleCommand : AbsWebAPI {
11 public override void HandleRequest (RequestContext _context) {
12 if (string.IsNullOrEmpty (_context.Request.QueryString ["command"])) {
13 WebUtils.WriteText (_context.Response, "No command given", HttpStatusCode.BadRequest);
14 return;
15 }
16
17 WebCommandResult.ResultType responseType =
18 _context.Request.QueryString ["raw"] != null
19 ? WebCommandResult.ResultType.Raw
20 : _context.Request.QueryString ["simple"] != null
21 ? WebCommandResult.ResultType.ResultOnly
22 : WebCommandResult.ResultType.Full;
23
24 string commandline = _context.Request.QueryString ["command"];
25 string commandPart = commandline.Split (' ') [0];
26 string argumentsPart = commandline.Substring (Math.Min (commandline.Length, commandPart.Length + 1));
27
28 IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandline);
29
30 if (command == null) {
31 WebUtils.WriteText (_context.Response, "Unknown command", HttpStatusCode.NotFound);
32 return;
33 }
34
35 int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (command.GetCommands ());
36
37 if (_context.PermissionLevel > commandPermissionLevel) {
38 WebUtils.WriteText (_context.Response, "You are not allowed to execute this command", HttpStatusCode.Forbidden);
39 return;
40 }
41
42 _context.Response.SendChunked = true;
43 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _context);
44 SdtdConsole.Instance.ExecuteAsync (commandline, wcr);
45 }
46
47 public override int DefaultPermissionLevel () {
48 return 2000;
49 }
50 }
51}
Note: See TracBrowser for help on using the repository browser.