Example #1
0
        /// <summary>
        /// Encodes the sack into binary form
        /// </summary>
        /// <param name="writer">BinaryWriter instance</param>
        public void Encode(SackCollection sc, BinaryWriter writer)
        {
            if (sc.sackType == SackType.Stash)
            {
                // Item stacks are stored as single items in the stash
                TQData.WriteCString(writer, "numItems");
                writer.Write(sc.Count);
            }
            else if (sc.sackType == SackType.Equipment)
            {
                // Nothing special except to skip all of the other header crap
                // since the number of items is always fixed.
            }
            else
            {
                TQData.WriteCString(writer, "begin_block");
                writer.Write(sc.beginBlockCrap);

                TQData.WriteCString(writer, "tempBool");
                writer.Write(sc.tempBool);

                TQData.WriteCString(writer, "size");
                writer.Write(sc.CountTQItems());
            }

            int slotNumber = -1;

            foreach (Item item in sc)
            {
                ++slotNumber;
                item.ContainerType = sc.sackType;
                int itemAttached = 0;
                int alternate    = 0;

                // Additional logic to encode the weapon slots in the equipment section
                if (sc.sackType == SackType.Equipment && (slotNumber == 7 || slotNumber == 9))
                {
                    TQData.WriteCString(writer, "begin_block");
                    writer.Write(sc.beginBlockCrap);

                    TQData.WriteCString(writer, "alternate");
                    if (slotNumber == 9)
                    {
                        // Only set the flag for the second set of weapons
                        alternate = 1;
                    }
                    else
                    {
                        // Otherwise set the flag to false.
                        alternate = 0;
                    }

                    writer.Write(alternate);
                }

                ItemProvider.Encode(item, writer);

                if (sc.sackType == SackType.Equipment)
                {
                    TQData.WriteCString(writer, "itemAttached");
                    if (!string.IsNullOrEmpty(item.BaseItemId) && slotNumber != 9 && slotNumber != 10)
                    {
                        // If there is an item in sc slot, set the flag.
                        // Unless it's in the secondary weapon slot.
                        itemAttached = 1;
                    }
                    else
                    {
                        // sc is only a dummy item so we do not set the flag.
                        itemAttached = 0;
                    }

                    writer.Write(itemAttached);
                }

                // Additional logic to encode the weapon slots in the equipment section
                if (sc.sackType == SackType.Equipment && (slotNumber == 8 || slotNumber == 10))
                {
                    TQData.WriteCString(writer, "end_block");
                    writer.Write(sc.endBlockCrap);
                }
            }

            TQData.WriteCString(writer, "end_block");
            writer.Write(sc.endBlockCrap);
        }