[425] | 1 | using System;
|
---|
[434] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Globalization;
|
---|
[425] | 4 | using UnityEngine;
|
---|
[402] | 5 | using Utf8Json;
|
---|
| 6 |
|
---|
| 7 | namespace Webserver.WebAPI {
|
---|
| 8 | public static class JsonCommons {
|
---|
| 9 | private static readonly byte[] jsonKeyPositionX = JsonWriter.GetEncodedPropertyNameWithBeginObject ("x");
|
---|
| 10 | private static readonly byte[] jsonKeyPositionY = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("y");
|
---|
| 11 | private static readonly byte[] jsonKeyPositionZ = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("z");
|
---|
| 12 |
|
---|
[408] | 13 | public static void WritePositionObject (ref JsonWriter _writer, Vector3i _position) {
|
---|
[402] | 14 | _writer.WriteRaw (jsonKeyPositionX);
|
---|
| 15 | _writer.WriteInt32 (_position.x);
|
---|
| 16 |
|
---|
| 17 | _writer.WriteRaw (jsonKeyPositionY);
|
---|
| 18 | _writer.WriteInt32 (_position.y);
|
---|
| 19 |
|
---|
| 20 | _writer.WriteRaw (jsonKeyPositionZ);
|
---|
| 21 | _writer.WriteInt32 (_position.z);
|
---|
| 22 |
|
---|
| 23 | _writer.WriteEndObject ();
|
---|
| 24 | }
|
---|
[425] | 25 |
|
---|
| 26 | public static void WritePositionObject (ref JsonWriter _writer, Vector3 _position) {
|
---|
| 27 | _writer.WriteRaw (jsonKeyPositionX);
|
---|
| 28 | _writer.WriteSingle (_position.x);
|
---|
| 29 |
|
---|
| 30 | _writer.WriteRaw (jsonKeyPositionY);
|
---|
| 31 | _writer.WriteSingle (_position.y);
|
---|
| 32 |
|
---|
| 33 | _writer.WriteRaw (jsonKeyPositionZ);
|
---|
| 34 | _writer.WriteSingle (_position.z);
|
---|
| 35 |
|
---|
| 36 | _writer.WriteEndObject ();
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[436] | 39 | private static readonly byte[] jsonKeyDays = JsonWriter.GetEncodedPropertyNameWithBeginObject ("days");
|
---|
| 40 | private static readonly byte[] jsonKeyHours = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("hours");
|
---|
| 41 | private static readonly byte[] jsonKeyMinutes = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("minutes");
|
---|
| 42 |
|
---|
| 43 | public static void WriteGameTimeObject (ref JsonWriter _writer, int _days, int _hours, int _minutes) {
|
---|
| 44 | _writer.WriteRaw (jsonKeyDays);
|
---|
| 45 | _writer.WriteInt32 (_days);
|
---|
| 46 |
|
---|
| 47 | _writer.WriteRaw (jsonKeyHours);
|
---|
| 48 | _writer.WriteInt32 (_hours);
|
---|
| 49 |
|
---|
| 50 | _writer.WriteRaw (jsonKeyMinutes);
|
---|
| 51 | _writer.WriteInt32 (_minutes);
|
---|
| 52 |
|
---|
| 53 | _writer.WriteEndObject ();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[425] | 56 | private static readonly byte[] jsonKeyCombinedString = JsonWriter.GetEncodedPropertyNameWithBeginObject ("combinedString");
|
---|
| 57 | private static readonly byte[] jsonKeyPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("platformId");
|
---|
| 58 | private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("userId");
|
---|
| 59 |
|
---|
| 60 | public static void WritePlatformUserIdentifier (ref JsonWriter _writer, PlatformUserIdentifierAbs _userIdentifier) {
|
---|
| 61 | if (_userIdentifier == null) {
|
---|
| 62 | _writer.WriteNull ();
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | _writer.WriteRaw (jsonKeyCombinedString);
|
---|
| 67 | _writer.WriteString (_userIdentifier.CombinedString);
|
---|
| 68 |
|
---|
| 69 | _writer.WriteRaw (jsonKeyPlatformId);
|
---|
| 70 | _writer.WriteString (_userIdentifier.PlatformIdentifierString);
|
---|
| 71 |
|
---|
| 72 | _writer.WriteRaw (jsonKeyUserId);
|
---|
| 73 | _writer.WriteString (_userIdentifier.ReadablePlatformUserIdentifier);
|
---|
| 74 |
|
---|
| 75 | _writer.WriteEndObject ();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[434] | 78 | public static bool TryReadPlatformUserIdentifier (IDictionary<string, object> _jsonInput, out PlatformUserIdentifierAbs _userIdentifier) {
|
---|
| 79 | if (TryGetJsonField (_jsonInput, "combinedString", out string combinedString)) {
|
---|
| 80 | _userIdentifier = PlatformUserIdentifierAbs.FromCombinedString (combinedString, false);
|
---|
| 81 | if (_userIdentifier != null) {
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if (!TryGetJsonField (_jsonInput, "platformId", out string platformId)) {
|
---|
| 87 | _userIdentifier = default;
|
---|
| 88 | return false;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | if (!TryGetJsonField (_jsonInput, "userId", out string userId)) {
|
---|
| 92 | _userIdentifier = default;
|
---|
| 93 | return false;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | _userIdentifier = PlatformUserIdentifierAbs.FromPlatformAndId (platformId, userId, false);
|
---|
| 97 | return _userIdentifier != null;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[425] | 100 | public static void WriteDateTime (ref JsonWriter _writer, DateTime _dateTime) {
|
---|
| 101 | _writer.WriteString (_dateTime.ToString ("o"));
|
---|
| 102 | }
|
---|
[426] | 103 |
|
---|
[434] | 104 | public static bool TryReadDateTime (IDictionary<string, object> _jsonInput, string _fieldName, out DateTime _result) {
|
---|
| 105 | _result = default;
|
---|
| 106 |
|
---|
| 107 | if (!TryGetJsonField (_jsonInput, _fieldName, out string dateTimeString)) {
|
---|
| 108 | return false;
|
---|
[426] | 109 | }
|
---|
[434] | 110 |
|
---|
| 111 | return DateTime.TryParse (dateTimeString, null, DateTimeStyles.RoundtripKind, out _result);
|
---|
[426] | 112 | }
|
---|
[434] | 113 |
|
---|
| 114 |
|
---|
| 115 | public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out int _value) {
|
---|
| 116 | _value = default;
|
---|
| 117 |
|
---|
| 118 | if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
|
---|
| 119 | return false;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | if (fieldNode is not double value) {
|
---|
| 123 | return false;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | try {
|
---|
| 127 | _value = (int)value;
|
---|
| 128 | return true;
|
---|
| 129 | } catch (Exception) {
|
---|
| 130 | return false;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out double _value) {
|
---|
| 135 | _value = default;
|
---|
| 136 |
|
---|
| 137 | if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | if (fieldNode is not double value) {
|
---|
| 142 | return false;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | try {
|
---|
| 146 | _value = value;
|
---|
| 147 | return true;
|
---|
| 148 | } catch (Exception) {
|
---|
| 149 | return false;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName, out string _value) {
|
---|
| 154 | _value = default;
|
---|
| 155 |
|
---|
| 156 | if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
|
---|
| 157 | return false;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if (fieldNode is not string value) {
|
---|
| 161 | return false;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | try {
|
---|
| 165 | _value = value;
|
---|
| 166 | return true;
|
---|
| 167 | } catch (Exception) {
|
---|
| 168 | return false;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public static bool TryGetJsonField (IDictionary<string, object> _jsonObject, string _fieldName,
|
---|
| 173 | out IDictionary<string, object> _value) {
|
---|
| 174 | _value = default;
|
---|
| 175 |
|
---|
| 176 | if (!_jsonObject.TryGetValue (_fieldName, out object fieldNode)) {
|
---|
| 177 | return false;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if (fieldNode is not IDictionary<string, object> value) {
|
---|
| 181 | return false;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | try {
|
---|
| 185 | _value = value;
|
---|
| 186 | return true;
|
---|
| 187 | } catch (Exception) {
|
---|
| 188 | return false;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
[402] | 191 | }
|
---|
| 192 | }
|
---|