[253] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Runtime.Serialization;
|
---|
| 4 | using System.Threading;
|
---|
| 5 |
|
---|
| 6 | namespace AllocsFixes.PersistentData
|
---|
| 7 | {
|
---|
| 8 | [Serializable]
|
---|
| 9 | public class Inventory {
|
---|
| 10 | public List<InvItem> bag;
|
---|
| 11 | public List<InvItem> belt;
|
---|
| 12 | public InvItem[] equipment;
|
---|
| 13 |
|
---|
| 14 | public Inventory () {
|
---|
| 15 | bag = new List<InvItem> ();
|
---|
| 16 | belt = new List<InvItem> ();
|
---|
| 17 | equipment = null;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public void Update (PlayerDataFile pdf) {
|
---|
| 21 | lock (this) {
|
---|
| 22 | //Log.Out ("Updating player inventory - player id: " + pdf.id);
|
---|
| 23 | ProcessInv (bag, pdf.bag, pdf.id);
|
---|
| 24 | ProcessInv (belt, pdf.inventory, pdf.id);
|
---|
| 25 | ProcessEqu (pdf.equipment, pdf.id);
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | private void ProcessInv (List<InvItem> target, ItemStack[] sourceFields, int id) {
|
---|
| 30 | target.Clear ();
|
---|
| 31 | for (int i = 0; i < sourceFields.Length; i++) {
|
---|
| 32 | InvItem item = CreateInvItem (sourceFields [i].itemValue, sourceFields [i].count, id);
|
---|
| 33 | if (item != null && sourceFields [i].itemValue.Parts != null) {
|
---|
| 34 | ProcessParts (sourceFields [i].itemValue.Parts, item, id);
|
---|
| 35 | }
|
---|
| 36 | target.Add (item);
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | private void ProcessEqu (Equipment sourceEquipment, int _playerId) {
|
---|
| 41 | equipment = new InvItem[sourceEquipment.GetSlotCount ()];
|
---|
| 42 | for (int i = 0; i < sourceEquipment.GetSlotCount (); i++) {
|
---|
| 43 | equipment [i] = CreateInvItem (sourceEquipment.GetSlotItem (i), 1, _playerId);
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private void ProcessParts (ItemValue[] _parts, InvItem _item, int _playerId) {
|
---|
| 48 | InvItem[] itemParts = new InvItem[_parts.Length];
|
---|
| 49 | for (int i = 0; i < _parts.Length; i++) {
|
---|
| 50 | InvItem partItem = CreateInvItem (_parts [i], 1, _playerId);
|
---|
| 51 | if (partItem != null && _parts [i].Parts != null) {
|
---|
| 52 | ProcessParts (_parts [i].Parts, partItem, _playerId);
|
---|
| 53 | }
|
---|
| 54 | itemParts [i] = partItem;
|
---|
| 55 | }
|
---|
| 56 | _item.parts = itemParts;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private InvItem CreateInvItem (ItemValue _itemValue, int _count, int _playerId) {
|
---|
| 60 | if (_count > 0 && _itemValue != null && !_itemValue.Equals (ItemValue.None)) {
|
---|
| 61 | ItemClass itemClass = ItemClass.list [_itemValue.type];
|
---|
| 62 | int maxAllowed = itemClass.Stacknumber.Value;
|
---|
| 63 | string name = itemClass.GetItemName ();
|
---|
| 64 |
|
---|
| 65 | if (_count > maxAllowed) {
|
---|
| 66 | Log.Out ("Player with ID " + _playerId + " has stack for \"" + name + "\" greater than allowed (" + _count + " > " + maxAllowed + ")");
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | InvItem item = null;
|
---|
| 70 | if (_itemValue.HasQuality) {
|
---|
[287] | 71 | item = new InvItem (name, _count, _itemValue.Quality, _itemValue.MaxUseTimes, _itemValue.UseTimes);
|
---|
[253] | 72 | } else {
|
---|
[287] | 73 | item = new InvItem (name, _count, -1, _itemValue.MaxUseTimes, _itemValue.UseTimes);
|
---|
[253] | 74 | }
|
---|
| 75 |
|
---|
| 76 | item.icon = itemClass.GetIconName ();
|
---|
| 77 |
|
---|
| 78 | item.iconcolor = AllocsUtils.ColorToHex (itemClass.GetIconTint ());
|
---|
| 79 |
|
---|
| 80 | return item;
|
---|
| 81 | } else {
|
---|
| 82 | return null;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|