1 | using System;
|
---|
2 | using JetBrains.Annotations;
|
---|
3 | using Utf8Json;
|
---|
4 |
|
---|
5 | namespace Webserver.WebAPI.APIs.ServerState {
|
---|
6 | [UsedImplicitly]
|
---|
7 | public class GamePrefs : AbsRestApi {
|
---|
8 | private static readonly byte[] keyType = JsonWriter.GetEncodedPropertyNameWithBeginObject ("type");
|
---|
9 | private static readonly byte[] keyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value");
|
---|
10 | private static readonly byte[] keyDefault = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("default");
|
---|
11 |
|
---|
12 | private int largestBuffer;
|
---|
13 |
|
---|
14 | protected override void HandleRestGet (RequestContext _context) {
|
---|
15 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
16 |
|
---|
17 | writer.EnsureCapacity (largestBuffer);
|
---|
18 | writer.WriteBeginObject ();
|
---|
19 |
|
---|
20 | bool first = true;
|
---|
21 | foreach (EnumGamePrefs stat in EnumUtils.Values<EnumGamePrefs> ()) {
|
---|
22 | string name = stat.ToStringCached ();
|
---|
23 | global::GamePrefs.EnumType? type = global::GamePrefs.GetPrefType (stat);
|
---|
24 |
|
---|
25 | if (name.Contains ("Password", StringComparison.Ordinal)) {
|
---|
26 | continue;
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (!first) {
|
---|
30 | writer.WriteValueSeparator ();
|
---|
31 | }
|
---|
32 |
|
---|
33 | first = false;
|
---|
34 |
|
---|
35 | writer.WritePropertyName (name);
|
---|
36 |
|
---|
37 | writer.WriteRaw (keyType);
|
---|
38 | writer.WriteString (type.HasValue ? type.Value.ToStringCached () : "noType");
|
---|
39 |
|
---|
40 | writer.WriteRaw (keyValue);
|
---|
41 | switch (type) {
|
---|
42 | case global::GamePrefs.EnumType.Int:
|
---|
43 | writer.WriteInt32 (global::GamePrefs.GetInt (stat));
|
---|
44 | break;
|
---|
45 | case global::GamePrefs.EnumType.Float:
|
---|
46 | writer.WriteSingle (global::GamePrefs.GetFloat (stat));
|
---|
47 | break;
|
---|
48 | case global::GamePrefs.EnumType.String:
|
---|
49 | writer.WriteString (global::GamePrefs.GetString (stat));
|
---|
50 | break;
|
---|
51 | case global::GamePrefs.EnumType.Bool:
|
---|
52 | writer.WriteBoolean (global::GamePrefs.GetBool (stat));
|
---|
53 | break;
|
---|
54 | default:
|
---|
55 | writer.WriteNull ();
|
---|
56 | break;
|
---|
57 | }
|
---|
58 |
|
---|
59 | writer.WriteRaw (keyDefault);
|
---|
60 | object defaultValue = global::GamePrefs.GetDefault (stat);
|
---|
61 | switch (type) {
|
---|
62 | case global::GamePrefs.EnumType.Int:
|
---|
63 | if (defaultValue is int defaultInt) {
|
---|
64 | writer.WriteInt32 (defaultInt);
|
---|
65 | } else {
|
---|
66 | writer.WriteNull ();
|
---|
67 | }
|
---|
68 |
|
---|
69 | break;
|
---|
70 | case global::GamePrefs.EnumType.Float:
|
---|
71 | if (defaultValue is float defaultFloat) {
|
---|
72 | writer.WriteSingle (defaultFloat);
|
---|
73 | } else {
|
---|
74 | writer.WriteNull ();
|
---|
75 | }
|
---|
76 |
|
---|
77 | break;
|
---|
78 | case global::GamePrefs.EnumType.String:
|
---|
79 | if (defaultValue is string defaultString) {
|
---|
80 | writer.WriteString (defaultString);
|
---|
81 | } else {
|
---|
82 | writer.WriteNull ();
|
---|
83 | }
|
---|
84 |
|
---|
85 | break;
|
---|
86 | case global::GamePrefs.EnumType.Bool:
|
---|
87 | if (defaultValue is bool defaultBool) {
|
---|
88 | writer.WriteBoolean (defaultBool);
|
---|
89 | } else {
|
---|
90 | writer.WriteNull ();
|
---|
91 | }
|
---|
92 |
|
---|
93 | break;
|
---|
94 | default:
|
---|
95 | writer.WriteNull ();
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | writer.WriteEndObject ();
|
---|
100 | }
|
---|
101 |
|
---|
102 | writer.WriteEndObject ();
|
---|
103 |
|
---|
104 | int bufferContentSize = writer.CurrentOffset + 128;
|
---|
105 | if (bufferContentSize > largestBuffer) {
|
---|
106 | largestBuffer = bufferContentSize;
|
---|
107 | }
|
---|
108 |
|
---|
109 | SendEnvelopedResult (_context, ref writer);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|