using System.Collections.Generic; using JetBrains.Annotations; using Utf8Json; namespace Webserver.WebAPI.APIs { [UsedImplicitly] public class ServerInfo : AbsRestApi { private static readonly UnityEngine.Profiling.CustomSampler buildSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_ServerInfo_BuildSampler"); private static readonly byte[] keyType = JsonWriter.GetEncodedPropertyNameWithBeginObject ("type"); private static readonly byte[] keyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value"); private int largestBuffer; protected override void HandleRestGet (RequestContext _context) { buildSampler.Begin (); PrepareEnvelopedResult (out JsonWriter writer); writer.EnsureCapacity (largestBuffer); writer.WriteBeginObject (); GameServerInfo gsi = ConnectionManager.Instance.LocalServerInfo; IList list = EnumUtils.Values (); for (int i = 0; i < list.Count; i++) { GameInfoString stringGamePref = list [i]; if (i > 0) { writer.WriteValueSeparator (); } writer.WriteString (stringGamePref.ToStringCached ()); writer.WriteNameSeparator (); writer.WriteRaw (keyType); writer.WriteString ("string"); writer.WriteRaw (keyValue); writer.WriteString (gsi.GetValue (stringGamePref)); writer.WriteEndObject (); } IList ints = EnumUtils.Values (); for (int i = 0; i < ints.Count; i++) { GameInfoInt intGamePref = ints [i]; if (i > 0) { writer.WriteValueSeparator (); } writer.WriteString (intGamePref.ToStringCached ()); writer.WriteNameSeparator (); writer.WriteRaw (keyType); writer.WriteString ("int"); writer.WriteRaw (keyValue); writer.WriteInt32 (gsi.GetValue (intGamePref)); writer.WriteEndObject (); } IList prefs = EnumUtils.Values (); for (int i = 0; i < prefs.Count; i++) { GameInfoBool boolGamePref = prefs [i]; if (i > 0) { writer.WriteValueSeparator (); } writer.WriteString (boolGamePref.ToStringCached ()); writer.WriteNameSeparator (); writer.WriteRaw (keyType); writer.WriteString ("bool"); writer.WriteRaw (keyValue); writer.WriteBoolean (gsi.GetValue (boolGamePref)); writer.WriteEndObject (); } writer.WriteEndObject (); buildSampler.End (); int bufferContentSize = writer.CurrentOffset + 128; if (bufferContentSize > largestBuffer) { largestBuffer = bufferContentSize; } SendEnvelopedResult (_context, ref writer); } } }