source: TFP-WebServer/WebServer/src/WebAPI/APIs/ServerState/ServerInfo.cs@ 453

Last change on this file since 453 was 437, checked in by alloc, 18 months ago

Added GameStats and GamePrefs APIs
Fixed ServerInfo API JSON output

File size: 2.7 KB
Line 
1using System.Collections.Generic;
2using JetBrains.Annotations;
3using Utf8Json;
4using Webserver.Permissions;
5
6namespace Webserver.WebAPI.APIs.ServerState {
7 [UsedImplicitly]
8 public class ServerInfo : AbsRestApi {
9 private static readonly UnityEngine.Profiling.CustomSampler buildSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_ServerInfo_BuildSampler");
10
11 private static readonly byte[] keyType = JsonWriter.GetEncodedPropertyNameWithBeginObject ("type");
12 private static readonly byte[] keyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value");
13
14 private int largestBuffer;
15
16 protected override void HandleRestGet (RequestContext _context) {
17 buildSampler.Begin ();
18
19 PrepareEnvelopedResult (out JsonWriter writer);
20
21 writer.EnsureCapacity (largestBuffer);
22 writer.WriteBeginObject ();
23
24 GameServerInfo gsi = ConnectionManager.Instance.LocalServerInfo;
25
26 bool first = true;
27
28 IList<GameInfoString> list = EnumUtils.Values<GameInfoString> ();
29 for (int i = 0; i < list.Count; i++) {
30 GameInfoString stringGamePref = list [i];
31
32 if (!first) {
33 writer.WriteValueSeparator ();
34 }
35
36 first = false;
37
38 writer.WritePropertyName (stringGamePref.ToStringCached ());
39
40 writer.WriteRaw (keyType);
41 writer.WriteString ("string");
42
43 writer.WriteRaw (keyValue);
44 writer.WriteString (gsi.GetValue (stringGamePref));
45
46 writer.WriteEndObject ();
47 }
48
49 IList<GameInfoInt> ints = EnumUtils.Values<GameInfoInt> ();
50 for (int i = 0; i < ints.Count; i++) {
51 GameInfoInt intGamePref = ints [i];
52
53 if (!first) {
54 writer.WriteValueSeparator ();
55 }
56
57 first = false;
58
59 writer.WritePropertyName (intGamePref.ToStringCached ());
60
61 writer.WriteRaw (keyType);
62 writer.WriteString ("int");
63
64 writer.WriteRaw (keyValue);
65 writer.WriteInt32 (gsi.GetValue (intGamePref));
66
67 writer.WriteEndObject ();
68 }
69
70 IList<GameInfoBool> prefs = EnumUtils.Values<GameInfoBool> ();
71 for (int i = 0; i < prefs.Count; i++) {
72 GameInfoBool boolGamePref = prefs [i];
73
74 if (!first) {
75 writer.WriteValueSeparator ();
76 }
77
78 first = false;
79
80 writer.WritePropertyName (boolGamePref.ToStringCached ());
81
82 writer.WriteRaw (keyType);
83 writer.WriteString ("bool");
84
85 writer.WriteRaw (keyValue);
86 writer.WriteBoolean (gsi.GetValue (boolGamePref));
87
88 writer.WriteEndObject ();
89 }
90
91 writer.WriteEndObject ();
92
93 buildSampler.End ();
94
95 int bufferContentSize = writer.CurrentOffset + 128;
96 if (bufferContentSize > largestBuffer) {
97 largestBuffer = bufferContentSize;
98 }
99
100 SendEnvelopedResult (_context, ref writer);
101 }
102
103 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest;
104 }
105}
Note: See TracBrowser for help on using the repository browser.