| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.IO;
|
|---|
| 4 |
|
|---|
| 5 | namespace AllocsFixes.PersistentData {
|
|---|
| 6 | [Serializable]
|
|---|
| 7 | public class Inventory {
|
|---|
| 8 | public static readonly EnumDictionary<EquipmentSlots, string> CustomEquipmentSlotNames =
|
|---|
| 9 | new() {
|
|---|
| 10 | { EquipmentSlots.Chest, "armor" },
|
|---|
| 11 | { EquipmentSlots.Hands, "gloves" },
|
|---|
| 12 | { EquipmentSlots.Feet, "boots" },
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | public List<InvItem> bag;
|
|---|
| 16 | public List<InvItem> belt;
|
|---|
| 17 | public InvItem[] equipment;
|
|---|
| 18 |
|
|---|
| 19 | public Inventory () {
|
|---|
| 20 | bag = new List<InvItem> ();
|
|---|
| 21 | belt = new List<InvItem> ();
|
|---|
| 22 | equipment = null;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | public void Update (PlayerDataFile _pdf) {
|
|---|
| 26 | lock (this) {
|
|---|
| 27 | ItemStackGrid toolbeltRead = readToolbelt (_pdf.inventoryData);
|
|---|
| 28 |
|
|---|
| 29 | Bag bagRead = null;
|
|---|
| 30 | StreamUtils.FromBlob (_pdf.bagData, pbr => bagRead = Bag.Read (pbr, XUiC_ItemStack.StackLocationTypes.Backpack, StreamModeRead.FromClient, null));
|
|---|
| 31 |
|
|---|
| 32 | Equipment equipRead = null;
|
|---|
| 33 | StreamUtils.FromBlob (_pdf.equipmentData, pbr => equipRead = Equipment.Read (pbr, StreamModeRead.FromClient));
|
|---|
| 34 |
|
|---|
| 35 | ProcessInv (bag, bagRead.ItemGrid.items, _pdf.id);
|
|---|
| 36 | ProcessInv (belt, toolbeltRead.items, _pdf.id);
|
|---|
| 37 | ProcessEqu (equipRead, _pdf.id);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | private ItemStackGrid readToolbelt (MemoryStream _ms) {
|
|---|
| 42 | ItemStackGrid grid = ItemStackGrid.Create (new Vector2i (1, 1), XUiC_ItemStack.StackLocationTypes.ToolBelt, false, false, null);
|
|---|
| 43 | StreamUtils.FromBlob (_ms, pbr => {
|
|---|
| 44 | byte version = pbr.ReadByte ();
|
|---|
| 45 | grid.ReadInto (pbr, StreamModeRead.FromClient);
|
|---|
| 46 | });
|
|---|
| 47 | return grid;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | private void ProcessInv (ICollection<InvItem> _target, IReadOnlyList<ItemStack> _sourceFields, int _playerId) {
|
|---|
| 51 | _target.Clear ();
|
|---|
| 52 | for (int i = 0; i < _sourceFields.Count; i++) {
|
|---|
| 53 | ItemValue curItemValue = _sourceFields [i].itemValue;
|
|---|
| 54 | InvItem item = CreateInvItem (curItemValue, _sourceFields [i].count, _playerId);
|
|---|
| 55 | if (item != null) {
|
|---|
| 56 | ProcessParts (curItemValue, item, _playerId);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | _target.Add (item);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | private void ProcessEqu (Equipment _sourceEquipment, int _playerId) {
|
|---|
| 64 | equipment = new InvItem[_sourceEquipment.GetSlotCount ()];
|
|---|
| 65 | for (int i = 0; i < _sourceEquipment.GetSlotCount (); i++) {
|
|---|
| 66 | ItemValue curItemValue = _sourceEquipment.GetSlotItem (i);
|
|---|
| 67 | equipment [i] = CreateInvItem (curItemValue, 1, _playerId);
|
|---|
| 68 | if (equipment [i] != null) {
|
|---|
| 69 | ProcessParts (curItemValue, equipment [i], _playerId);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | private void ProcessParts (ItemValue _parent, InvItem _item, int _playerId) {
|
|---|
| 75 | if (_parent.ModificationCount < 1) {
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | InvItem[] itemParts = new InvItem[_parent.ModificationCount];
|
|---|
| 80 | for (int i = 0; i < _parent.ModificationCount; i++) {
|
|---|
| 81 | ItemValue part = _parent.GetModification (i);
|
|---|
| 82 | InvItem partItem = CreateInvItem (part, 1, _playerId);
|
|---|
| 83 | if (partItem != null) {
|
|---|
| 84 | ProcessParts (part, partItem, _playerId);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | itemParts [i] = partItem;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | _item.parts = itemParts;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | private InvItem CreateInvItem (ItemValue _itemValue, int _count, int _playerId) {
|
|---|
| 94 | if (_count <= 0 || _itemValue == null || _itemValue.Equals (ItemValue.None)) {
|
|---|
| 95 | return null;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | ItemClass itemClass = ItemClass.list [_itemValue.type];
|
|---|
| 99 | int maxAllowed = itemClass.MaxCount;
|
|---|
| 100 | string name = itemClass.GetItemName ();
|
|---|
| 101 |
|
|---|
| 102 | if (_count > maxAllowed) {
|
|---|
| 103 | Log.Out ($"Player with ID {_playerId} has stack for \"{name}\" greater than allowed ({_count} > {maxAllowed})");
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | InvItem item;
|
|---|
| 107 | if (_itemValue.HasQuality) {
|
|---|
| 108 | item = new InvItem (name, _count, _itemValue.Quality, _itemValue.MaxUseTimes, _itemValue.UseTimes);
|
|---|
| 109 | } else {
|
|---|
| 110 | item = new InvItem (name, _count, -1, _itemValue.MaxUseTimes, _itemValue.UseTimes);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | item.icon = itemClass.GetIconName ();
|
|---|
| 114 |
|
|---|
| 115 | item.iconcolor = AllocsUtils.ColorToHex (itemClass.GetIconTint ());
|
|---|
| 116 |
|
|---|
| 117 | return item;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|