| 1 | using System.Collections.Generic;
 | 
|---|
| 2 | using AllocsFixes.PersistentData;
 | 
|---|
| 3 | using JetBrains.Annotations;
 | 
|---|
| 4 | 
 | 
|---|
| 5 | namespace AllocsFixes.CustomCommands {
 | 
|---|
| 6 |         [UsedImplicitly]
 | 
|---|
| 7 |         public class ShowInventory : ConsoleCmdAbstract {
 | 
|---|
| 8 |                 public override string getDescription () {
 | 
|---|
| 9 |                         return "list inventory of a given player";
 | 
|---|
| 10 |                 }
 | 
|---|
| 11 | 
 | 
|---|
| 12 |                 public override string getHelp () {
 | 
|---|
| 13 |                         return "Usage:\n" +
 | 
|---|
| 14 |                                "   showinventory <user id / player name / entity id> [tag]\n" +
 | 
|---|
| 15 |                                "Show the inventory of the player given by his UserID, player name or\n" +
 | 
|---|
| 16 |                                "entity id (as given by e.g. \"lpi\").\n" +
 | 
|---|
| 17 |                                "Optionally specify a tag that is included in each line of the output. In\n" +
 | 
|---|
| 18 |                                "this case output is designed to be easily parseable by tools.\n" +
 | 
|---|
| 19 |                                "Note: This only shows the player's inventory after it was first sent to\n" +
 | 
|---|
| 20 |                                "the server which happens at least every 30 seconds.";
 | 
|---|
| 21 |                 }
 | 
|---|
| 22 | 
 | 
|---|
| 23 |                 public override string[] getCommands () {
 | 
|---|
| 24 |                         return new[] {"showinventory", "si"};
 | 
|---|
| 25 |                 }
 | 
|---|
| 26 | 
 | 
|---|
| 27 |                 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
 | 
|---|
| 28 |                         if (_params.Count < 1) {
 | 
|---|
| 29 |                                 SdtdConsole.Instance.Output ("Usage: showinventory <steamid|playername|entityid> [tag]");
 | 
|---|
| 30 |                                 return;
 | 
|---|
| 31 |                         }
 | 
|---|
| 32 | 
 | 
|---|
| 33 |                         Player p = PersistentContainer.Instance.Players.GetByString (_params [0], true);
 | 
|---|
| 34 |                         if (p == null) {
 | 
|---|
| 35 |                                 SdtdConsole.Instance.Output (
 | 
|---|
| 36 |                                         "Playername or entity/steamid id not found or no inventory saved (first saved after a player has been online for 30s).");
 | 
|---|
| 37 |                                 return;
 | 
|---|
| 38 |                         }
 | 
|---|
| 39 | 
 | 
|---|
| 40 |                         string tag = null;
 | 
|---|
| 41 |                         if (_params.Count > 1 && _params [1].Length > 0) {
 | 
|---|
| 42 |                                 tag = _params [1];
 | 
|---|
| 43 |                         }
 | 
|---|
| 44 | 
 | 
|---|
| 45 |                         PersistentData.Inventory inv = p.Inventory;
 | 
|---|
| 46 | 
 | 
|---|
| 47 |                         if (tag == null) {
 | 
|---|
| 48 |                                 SdtdConsole.Instance.Output ($"Belt of player {p.Name}:");
 | 
|---|
| 49 |                         }
 | 
|---|
| 50 | 
 | 
|---|
| 51 |                         PrintInv (inv.belt, p.EntityID, "belt", tag);
 | 
|---|
| 52 |                         if (tag == null) {
 | 
|---|
| 53 |                                 SdtdConsole.Instance.Output (string.Empty);
 | 
|---|
| 54 |                         }
 | 
|---|
| 55 | 
 | 
|---|
| 56 |                         if (tag == null) {
 | 
|---|
| 57 |                                 SdtdConsole.Instance.Output ($"Bagpack of player {p.Name}:");
 | 
|---|
| 58 |                         }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |                         PrintInv (inv.bag, p.EntityID, "backpack", tag);
 | 
|---|
| 61 |                         if (tag == null) {
 | 
|---|
| 62 |                                 SdtdConsole.Instance.Output (string.Empty);
 | 
|---|
| 63 |                         }
 | 
|---|
| 64 | 
 | 
|---|
| 65 |                         if (tag == null) {
 | 
|---|
| 66 |                                 SdtdConsole.Instance.Output ($"Equipment of player {p.Name}:");
 | 
|---|
| 67 |                         }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |                         PrintEquipment (inv.equipment, p.EntityID, "equipment", tag);
 | 
|---|
| 70 | 
 | 
|---|
| 71 |                         if (tag != null) {
 | 
|---|
| 72 |                                 SdtdConsole.Instance.Output ($"tracker_item id={p.EntityID}, tag={tag}, SHOWINVENTORY DONE");
 | 
|---|
| 73 |                         }
 | 
|---|
| 74 |                 }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |                 private static void PrintInv (IReadOnlyList<InvItem> _inv, int _entityId, string _location, string _tag) {
 | 
|---|
| 77 |                         for (int i = 0; i < _inv.Count; i++) {
 | 
|---|
| 78 |                                 if (_inv[i] == null) {
 | 
|---|
| 79 |                                         continue;
 | 
|---|
| 80 |                                 }
 | 
|---|
| 81 | 
 | 
|---|
| 82 |                                 if (_tag == null) {
 | 
|---|
| 83 |                                         // no Tag defined -> readable output
 | 
|---|
| 84 |                                         if (_inv [i].quality < 0) {
 | 
|---|
| 85 |                                                 SdtdConsole.Instance.Output (string.Format ("    Slot {0}: {1:000} * {2}", i,
 | 
|---|
| 86 |                                                         _inv [i].count, _inv [i].itemName));
 | 
|---|
| 87 |                                         } else {
 | 
|---|
| 88 |                                                 SdtdConsole.Instance.Output (string.Format ("    Slot {0}: {1:000} * {2} - quality: {3}", i,
 | 
|---|
| 89 |                                                         _inv [i].count, _inv [i].itemName, _inv [i].quality));
 | 
|---|
| 90 |                                         }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |                                         DoParts (_inv [i].parts, 1, null);
 | 
|---|
| 93 |                                 } else {
 | 
|---|
| 94 |                                         // Tag defined -> parseable output
 | 
|---|
| 95 |                                         string partsMsg = DoParts (_inv [i].parts, 1, "");
 | 
|---|
| 96 |                                         string msg = "tracker_item id=" + _entityId + ", tag=" + _tag + ", location=" + _location +
 | 
|---|
| 97 |                                                      ", slot=" + i + ", item=" + _inv [i].itemName + ", qnty=" + _inv [i].count +
 | 
|---|
| 98 |                                                      ", quality=" + _inv [i].quality + ", parts=(" + partsMsg + ")";
 | 
|---|
| 99 |                                         SdtdConsole.Instance.Output (msg);
 | 
|---|
| 100 |                                 }
 | 
|---|
| 101 |                         }
 | 
|---|
| 102 |                 }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |                 private static void PrintEquipment (IReadOnlyList<InvItem> _equipment, int _entityId, string _location, string _tag) {
 | 
|---|
| 105 |                         AddEquipment ("head", _equipment, EquipmentSlots.Head, _entityId, _location, _tag);
 | 
|---|
| 106 |                         // AddEquipment ("eyes", _equipment, EquipmentSlots.Eyewear, _entityId, _location, _tag);
 | 
|---|
| 107 |                         // AddEquipment ("face", _equipment, EquipmentSlots.Face, _entityId, _location, _tag);
 | 
|---|
| 108 | 
 | 
|---|
| 109 |                         AddEquipment ("armor", _equipment, EquipmentSlots.Chest, _entityId, _location, _tag);
 | 
|---|
| 110 |                         // AddEquipment ("jacket", _equipment, EquipmentSlots.Jacket, _entityId, _location, _tag);
 | 
|---|
| 111 |                         // AddEquipment ("shirt", _equipment, EquipmentSlots.Shirt, _entityId, _location, _tag);
 | 
|---|
| 112 | 
 | 
|---|
| 113 |                         // AddEquipment ("legarmor", _equipment, EquipmentSlots.LegArmor, _entityId, _location, _tag);
 | 
|---|
| 114 |                         // AddEquipment ("pants", _equipment, EquipmentSlots.Legs, _entityId, _location, _tag);
 | 
|---|
| 115 |                         AddEquipment ("boots", _equipment, EquipmentSlots.Feet, _entityId, _location, _tag);
 | 
|---|
| 116 | 
 | 
|---|
| 117 |                         AddEquipment ("gloves", _equipment, EquipmentSlots.Hands, _entityId, _location, _tag);
 | 
|---|
| 118 |                 }
 | 
|---|
| 119 | 
 | 
|---|
| 120 |                 private static void AddEquipment (string _slotname, IReadOnlyList<InvItem> _items, EquipmentSlots _slot, int _entityId,
 | 
|---|
| 121 |                         string _location, string _tag) {
 | 
|---|
| 122 |                         int slotindex = (int)_slot;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |                         if (_items == null || _items[slotindex] == null) {
 | 
|---|
| 125 |                                 return;
 | 
|---|
| 126 |                         }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |                         InvItem item = _items [slotindex];
 | 
|---|
| 129 |                         if (_tag == null) {
 | 
|---|
| 130 |                                 // no Tag defined -> readable output
 | 
|---|
| 131 |                                 if (item.quality < 0) {
 | 
|---|
| 132 |                                         SdtdConsole.Instance.Output (string.Format ("    Slot {0:8}: {1:000}", _slotname,
 | 
|---|
| 133 |                                                 item.itemName));
 | 
|---|
| 134 |                                 } else {
 | 
|---|
| 135 |                                         SdtdConsole.Instance.Output (string.Format ("    Slot {0:8}: {1:000} - quality: {2}",
 | 
|---|
| 136 |                                                 _slotname, item.itemName, item.quality));
 | 
|---|
| 137 |                                 }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |                                 DoParts (_items [slotindex].parts, 1, null);
 | 
|---|
| 140 |                         } else {
 | 
|---|
| 141 |                                 // Tag defined -> parseable output
 | 
|---|
| 142 |                                 string partsMsg = DoParts (_items [slotindex].parts, 1, "");
 | 
|---|
| 143 |                                 string msg = $"tracker_item id={_entityId}, tag={_tag}, location={_location}, slot={_slotname}, item={item.itemName}, qnty=1, quality={item.quality}, parts=({partsMsg})";
 | 
|---|
| 144 |                                 SdtdConsole.Instance.Output (msg);
 | 
|---|
| 145 |                         }
 | 
|---|
| 146 |                 }
 | 
|---|
| 147 | 
 | 
|---|
| 148 |                 private static string DoParts (IReadOnlyList<InvItem> _parts, int _indent, string _currentMessage) {
 | 
|---|
| 149 |                         if (_parts == null || _parts.Count <= 0) {
 | 
|---|
| 150 |                                 return _currentMessage;
 | 
|---|
| 151 |                         }
 | 
|---|
| 152 | 
 | 
|---|
| 153 |                         string indenter = new string (' ', _indent * 4);
 | 
|---|
| 154 |                         for (int i = 0; i < _parts.Count; i++) {
 | 
|---|
| 155 |                                 if (_parts[i] == null) {
 | 
|---|
| 156 |                                         continue;
 | 
|---|
| 157 |                                 }
 | 
|---|
| 158 | 
 | 
|---|
| 159 |                                 if (_currentMessage == null) {
 | 
|---|
| 160 |                                         // no currentMessage given -> readable output
 | 
|---|
| 161 |                                         if (_parts [i].quality < 0) {
 | 
|---|
| 162 |                                                 SdtdConsole.Instance.Output (string.Format ("{0}         - {1}", indenter,
 | 
|---|
| 163 |                                                         _parts [i].itemName));
 | 
|---|
| 164 |                                         } else {
 | 
|---|
| 165 |                                                 SdtdConsole.Instance.Output (string.Format ("{0}         - {1} - quality: {2}",
 | 
|---|
| 166 |                                                         indenter, _parts [i].itemName, _parts [i].quality));
 | 
|---|
| 167 |                                         }
 | 
|---|
| 168 | 
 | 
|---|
| 169 |                                         DoParts (_parts [i].parts, _indent + 1, null);
 | 
|---|
| 170 |                                 } else {
 | 
|---|
| 171 |                                         // currentMessage given -> parseable output
 | 
|---|
| 172 |                                         if (_currentMessage.Length > 0) {
 | 
|---|
| 173 |                                                 _currentMessage += ",";
 | 
|---|
| 174 |                                         }
 | 
|---|
| 175 | 
 | 
|---|
| 176 |                                         _currentMessage += $"{_parts[i].itemName}@{_parts[i].quality}";
 | 
|---|
| 177 |                                         _currentMessage = DoParts (_parts [i].parts, _indent + 1, _currentMessage);
 | 
|---|
| 178 |                                 }
 | 
|---|
| 179 |                         }
 | 
|---|
| 180 | 
 | 
|---|
| 181 |                         return _currentMessage;
 | 
|---|
| 182 |                 }
 | 
|---|
| 183 |         }
 | 
|---|
| 184 | }
 | 
|---|