/// <summary> /// Converts the live data back into the raw binary data format /// </summary> /// <returns>byte array holding the raw data</returns> public byte[] Encode(Stash sta) { int dataLength; byte[] data; // Encode the item data into a memory stream using (MemoryStream writeStream = new MemoryStream(2048)) { using (BinaryWriter writer = new BinaryWriter(writeStream)) { // Write zero into the checksum value writer.Write(Convert.ToInt32(0, CultureInfo.InvariantCulture)); TQData.WriteCString(writer, "begin_block"); writer.Write(sta.beginBlockCrap); TQData.WriteCString(writer, "stashVersion"); writer.Write(sta.stashVersion); TQData.WriteCString(writer, "fName"); // Changed to raw data to support extended characters writer.Write(sta.name.Length); writer.Write(sta.name); TQData.WriteCString(writer, "sackWidth"); writer.Write(sta.Width); TQData.WriteCString(writer, "sackHeight"); writer.Write(sta.Height); // SackType should already be set at sta point SackCollectionProvider.Encode(sta.sack, writer); dataLength = (int)writeStream.Length; } // now just return the buffer we wrote to. data = writeStream.GetBuffer(); } // The problem is that data[] may be bigger than the amount of data in it. // We need to resize the array if (dataLength == data.Length) { return(data); } byte[] realData = new byte[dataLength]; Array.Copy(data, realData, dataLength); return(realData); }
/// <summary> /// Encodes the live item data into raw binary format /// </summary> /// <returns>byte array holding the converted binary data</returns> private byte[] EncodeItemData(PlayerCollection pc) { int dataLength; byte[] data; // Encode the item data into a memory stream using (MemoryStream writeStream = new MemoryStream(2048)) { using (BinaryWriter writer = new BinaryWriter(writeStream)) { TQData.WriteCString(writer, "numberOfSacks"); writer.Write(pc.numberOfSacks); TQData.WriteCString(writer, "currentlyFocusedSackNumber"); writer.Write(pc.currentlyFocusedSackNumber); TQData.WriteCString(writer, "currentlySelectedSackNumber"); writer.Write(pc.currentlySelectedSackNumber); for (int i = 0; i < pc.numberOfSacks; ++i) { // SackType should already be set at pc point SackCollectionProvider.Encode(pc.sacks[i], writer); } dataLength = (int)writeStream.Length; } // now just return the buffer we wrote to. data = writeStream.GetBuffer(); } // The problem is that ans() may be bigger than the amount of data in it. // We need to resize the array if (dataLength == data.Length) { return(data); } byte[] realData = new byte[dataLength]; Array.Copy(data, realData, dataLength); return(realData); }
/// <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); }