- Timestamp:
- Jul 31, 2023, 4:06:13 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/API/GetPlayerInventory.cs
r454 r455 1 1 using System.Collections.Generic; 2 2 using System.Net; 3 using AllocsFixes.JSON;4 3 using AllocsFixes.PersistentData; 4 using JetBrains.Annotations; 5 using Utf8Json; 5 6 using Webserver; 6 7 using Webserver.WebAPI; 7 8 8 9 namespace AllocsFixes.WebAPIs { 10 [UsedImplicitly] 9 11 public class GetPlayerInventory : AbsWebAPI { 10 12 public override void HandleRequest (RequestContext _context) { … … 28 30 GetInventoryArguments (_context, out bool showIconColor, out bool showIconName); 29 31 30 J SONObject result = DoPlayer (p, showIconColor, showIconName);31 32 LegacyApiHelper.WriteJSON (_context.Response, result);32 JsonWriter writer = new JsonWriter (); 33 DoPlayer (ref writer, p, showIconColor, showIconName); 34 WebUtils.WriteJsonData (_context.Response, ref writer); 33 35 } 34 36 … … 43 45 } 44 46 45 internal static JSONObject DoPlayer (Player _player, bool _showIconColor, bool _showIconName) { 47 private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("userid"); 48 private static readonly byte[] jsonKeyCrossPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("crossplatformid"); 49 private static readonly byte[] jsonKeyEntityId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("entityid"); 50 private static readonly byte[] jsonKeyPlayerName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("playername"); 51 private static readonly byte[] jsonKeyBag = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("bag"); 52 private static readonly byte[] jsonKeyBelt = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("belt"); 53 private static readonly byte[] jsonKeyEquipment = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("equipment"); 54 55 internal static void DoPlayer (ref JsonWriter _writer, Player _player, bool _showIconColor, bool _showIconName) { 46 56 PersistentData.Inventory inv = _player.Inventory; 47 48 JSONObject result = new JSONObject (); 49 50 JSONArray bag = new JSONArray (); 51 JSONArray belt = new JSONArray (); 52 JSONObject equipment = new JSONObject (); 53 result.Add ("userid", new JSONString (_player.PlatformId.CombinedString)); 54 result.Add ("crossplatformid", new JSONString (_player.CrossPlatformId?.CombinedString ?? "")); 55 result.Add ("entityid", new JSONNumber (_player.EntityID)); 56 result.Add ("playername", new JSONString (_player.Name)); 57 result.Add ("bag", bag); 58 result.Add ("belt", belt); 59 result.Add ("equipment", equipment); 60 61 DoInventory (belt, inv.belt, _showIconColor, _showIconName); 62 DoInventory (bag, inv.bag, _showIconColor, _showIconName); 63 64 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName); 65 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName); 66 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName); 67 68 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName); 69 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName); 70 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName); 71 72 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName); 73 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName); 74 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName); 75 76 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName); 77 78 return result; 57 58 _writer.WriteRaw (jsonKeyUserId); 59 _writer.WriteString (_player.PlatformId.CombinedString); 60 61 _writer.WriteRaw (jsonKeyCrossPlatformId); 62 _writer.WriteString (_player.CrossPlatformId?.CombinedString ?? ""); 63 64 _writer.WriteRaw (jsonKeyEntityId); 65 _writer.WriteInt32 (_player.EntityID); 66 67 _writer.WriteRaw (jsonKeyPlayerName); 68 _writer.WriteString (_player.Name); 69 70 _writer.WriteRaw (jsonKeyBag); 71 DoInventory (ref _writer, inv.bag, _showIconColor, _showIconName); 72 73 _writer.WriteRaw (jsonKeyBelt); 74 DoInventory (ref _writer, inv.belt, _showIconColor, _showIconName); 75 76 _writer.WriteRaw (jsonKeyEquipment); 77 DoEquipment (ref _writer, inv.equipment, _showIconColor, _showIconName); 78 79 _writer.WriteEndObject (); 79 80 } 80 81 81 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) { 82 private static void DoInventory (ref JsonWriter _writer, IReadOnlyList<InvItem> _inv, bool _showIconColor, bool _showIconName) { 83 _writer.WriteBeginArray (); 82 84 for (int i = 0; i < _inv.Count; i++) { 83 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName)); 85 if (i > 0) { 86 _writer.WriteValueSeparator (); 87 } 88 GetJsonForItem (ref _writer, _inv [i], _showIconColor, _showIconName); 84 89 } 90 _writer.WriteEndArray (); 85 91 } 86 92 87 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) { 93 private static void DoEquipment (ref JsonWriter _writer, IReadOnlyList<InvItem> _equ, bool _showIconColor, bool _showIconName) { 94 _writer.WriteBeginObject (); 95 96 AddEquipment (ref _writer, "head", _equ, EquipmentSlots.Headgear, _showIconColor, _showIconName); 97 _writer.WriteValueSeparator (); 98 AddEquipment (ref _writer, "eyes", _equ, EquipmentSlots.Eyewear, _showIconColor, _showIconName); 99 _writer.WriteValueSeparator (); 100 AddEquipment (ref _writer, "face", _equ, EquipmentSlots.Face, _showIconColor, _showIconName); 101 _writer.WriteValueSeparator (); 102 103 AddEquipment (ref _writer, "armor", _equ, EquipmentSlots.ChestArmor, _showIconColor, _showIconName); 104 _writer.WriteValueSeparator (); 105 AddEquipment (ref _writer, "jacket", _equ, EquipmentSlots.Jacket, _showIconColor, _showIconName); 106 _writer.WriteValueSeparator (); 107 AddEquipment (ref _writer, "shirt", _equ, EquipmentSlots.Shirt, _showIconColor, _showIconName); 108 _writer.WriteValueSeparator (); 109 110 AddEquipment (ref _writer, "legarmor", _equ, EquipmentSlots.LegArmor, _showIconColor, _showIconName); 111 _writer.WriteValueSeparator (); 112 AddEquipment (ref _writer, "pants", _equ, EquipmentSlots.Legs, _showIconColor, _showIconName); 113 _writer.WriteValueSeparator (); 114 AddEquipment (ref _writer, "boots", _equ, EquipmentSlots.Feet, _showIconColor, _showIconName); 115 _writer.WriteValueSeparator (); 116 117 AddEquipment (ref _writer, "gloves", _equ, EquipmentSlots.Hands, _showIconColor, _showIconName); 118 119 _writer.WriteEndObject (); 120 } 121 122 private static void AddEquipment (ref JsonWriter _writer, string _slotname, IReadOnlyList<InvItem> _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) { 123 _writer.WritePropertyName (_slotname); 124 88 125 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot); 89 126 … … 91 128 if (_items? [slotindices [i]] != null) { 92 129 InvItem item = _items [slotindices [i]]; 93 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName)); 130 131 GetJsonForItem (ref _writer, item, _showIconColor, _showIconName); 94 132 return; 95 133 } 96 134 } 97 135 98 _eq.Add (_slotname, new JSONNull ()); 136 // Slot not found / empty 137 _writer.WriteNull (); 99 138 } 100 139 101 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) { 140 private static readonly byte[] jsonKeyCount = JsonWriter.GetEncodedPropertyNameWithBeginObject ("count"); 141 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name"); 142 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon"); 143 private static readonly byte[] jsonKeyIconColor = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("iconcolor"); 144 private static readonly byte[] jsonKeyQuality = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("quality"); 145 private static readonly byte[] jsonKeyQualityColor = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("qualitycolor"); 146 147 private static void GetJsonForItem (ref JsonWriter _writer, InvItem _item, bool _showIconColor, bool _showIconName) { 102 148 if (_item == null) { 103 return new JSONNull (); 149 _writer.WriteNull (); 150 return; 104 151 } 105 106 JSONObject jsonItem = new JSONObject (); 107 jsonItem.Add ("count", new JSONNumber (_item.count)); 108 jsonItem.Add ("name", new JSONString (_item.itemName)); 152 153 _writer.WriteRaw (jsonKeyCount); 154 _writer.WriteInt32 (_item.count); 155 156 _writer.WriteRaw (jsonKeyName); 157 _writer.WriteString (_item.itemName); 109 158 110 159 if (_showIconName) { 111 jsonItem.Add ("icon", new JSONString (_item.icon)); 160 _writer.WriteRaw (jsonKeyIcon); 161 _writer.WriteString (_item.icon); 112 162 } 113 163 114 164 if (_showIconColor) { 115 jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor)); 165 _writer.WriteRaw (jsonKeyIconColor); 166 _writer.WriteString (_item.iconcolor); 116 167 } 117 168 118 jsonItem.Add ("quality", new JSONNumber (_item.quality)); 169 _writer.WriteRaw (jsonKeyQuality); 170 _writer.WriteInt32 (_item.quality); 171 119 172 if (_item.quality >= 0) { 120 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality))); 173 _writer.WriteRaw (jsonKeyQualityColor); 174 _writer.WriteString (QualityInfo.GetQualityColorHex (_item.quality)); 121 175 } 122 123 return jsonItem; 124 176 177 _writer.WriteEndObject (); 125 178 } 126 179 }
Note:
See TracChangeset
for help on using the changeset viewer.