- Timestamp:
- Aug 7, 2023, 12:26:05 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TFP-WebServer/WebServer/src/WebAPI/APIs/ServerState/GamePrefs.cs
r438 r457 5 5 namespace Webserver.WebAPI.APIs.ServerState { 6 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"); 7 public class GamePrefs : KeyValueListAbs { 8 public GamePrefs () : base ("GamePrefs") { 9 } 11 10 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; 11 protected override void iterateList (ref JsonWriter _writer, ref bool _first) { 21 12 foreach (EnumGamePrefs stat in EnumUtils.Values<EnumGamePrefs> ()) { 22 13 string name = stat.ToStringCached (); 23 global::GamePrefs.EnumType? type = global::GamePrefs.GetPrefType (stat);24 14 25 15 if (name.Contains ("Password", StringComparison.Ordinal)) { … … 27 17 } 28 18 29 if (!first) { 30 writer.WriteValueSeparator (); 31 } 19 global::GamePrefs.EnumType? type = global::GamePrefs.GetPrefType (stat); 20 object defaultValue = global::GamePrefs.GetDefault (stat); 32 21 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 22 switch (type) { 42 23 case global::GamePrefs.EnumType.Int: 43 writer.WriteInt32 (global::GamePrefs.GetInt (stat)); 24 int? defaultInt = defaultValue as int?; 25 addItem (ref _writer, ref _first, name, global::GamePrefs.GetInt (stat), defaultInt); 44 26 break; 45 27 case global::GamePrefs.EnumType.Float: 46 writer.WriteSingle (global::GamePrefs.GetFloat (stat)); 28 float? defaultFloat = defaultValue as float?; 29 addItem (ref _writer, ref _first, name, global::GamePrefs.GetFloat (stat), defaultFloat); 47 30 break; 48 31 case global::GamePrefs.EnumType.String: 49 writer.WriteString (global::GamePrefs.GetString (stat)); 32 string defaultString = defaultValue as string; 33 addItem (ref _writer, ref _first, name, global::GamePrefs.GetString (stat), defaultString); 50 34 break; 51 35 case global::GamePrefs.EnumType.Bool: 52 writer.WriteBoolean (global::GamePrefs.GetBool (stat)); 53 break; 54 default: 55 writer.WriteNull (); 36 bool? defaultBool = defaultValue as bool?; 37 addItem (ref _writer, ref _first, name, global::GamePrefs.GetBool (stat), defaultBool); 56 38 break; 57 39 } 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 } 40 } 41 } 68 42 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 43 } 112 44 }
Note:
See TracChangeset
for help on using the changeset viewer.