Example #1
0
 public static void LoadInventory(Item[] inv, IList <TagCompound> list)
 {
     foreach (var tag in list)
     {
         inv[tag.GetShort("slot")] = ItemIO.Load(tag);
     }
 }
Example #2
0
        internal static bool WriteInventory(Item[] inv, BinaryWriter writer, bool writeStack = false, bool writeFavorite = false)
        {
            ushort count = 0;

            byte[] data;
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter invWriter = new BinaryWriter(stream))
                {
                    for (int k = 0; k < inv.Length; k++)
                    {
                        if (ItemIO.WriteModItemSlot(inv, k, invWriter, writeStack, writeFavorite))
                        {
                            count++;
                        }
                    }
                    invWriter.Flush();
                    data = stream.ToArray();
                }
            }
            if (count > 0)
            {
                writer.Write(count);
                writer.Write(data);
                return(true);
            }
            return(false);
        }
Example #3
0
        public static string ToBase64(Item item)
        {
            MemoryStream ms = new MemoryStream();

            TagIO.ToStream(ItemIO.Save(item), ms, true);
            return(Convert.ToBase64String(ms.ToArray()));
        }
Example #4
0
        internal static bool WriteInventory(Item[] inv, BinaryWriter writer, bool writeStack = false, bool writeFavorite = false)
        {
            ushort count = 0;

            byte[] data;
            using (MemoryStream stream = new MemoryStream())
            {
                using (BinaryWriter invWriter = new BinaryWriter(stream))
                {
                    for (int k = 0; k < inv.Length; k++)
                    {
                        if (ItemLoader.NeedsModSaving(inv[k]))
                        {
                            invWriter.Write((ushort)k);
                            ItemIO.WriteItem(inv[k], invWriter, writeStack, writeFavorite);
                            count++;
                        }
                    }
                }
                data = stream.ToArray();
            }
            if (count > 0)
            {
                writer.Write(count);
                writer.Write(data);
                return(true);
            }
            return(false);
        }
Example #5
0
        internal static void ReadInventory(Item[] inv, BinaryReader reader, bool readStack = false, bool readFavorite = false)
        {
            int count = reader.ReadUInt16();

            for (int k = 0; k < count; k++)
            {
                ItemIO.ReadModItemSlot(inv, reader, readStack, readFavorite);
            }
        }
Example #6
0
        internal static void LoadContainers(TagCompound tag)
        {
            if (tag.ContainsKey("data"))
            {
                ReadContainers(new BinaryReader(new MemoryStream(tag.GetByteArray("data"))));
            }

            foreach (var frameTag in tag.GetList <TagCompound>("itemFrames"))
            {
                TEItemFrame itemFrame = TileEntity.ByID[tag.GetInt("id")] as TEItemFrame;
                ItemIO.Load(itemFrame.item, frameTag.GetCompound("item"));
            }
        }
Example #7
0
        public static List <TagCompound> SaveInventory(Item[] inv)
        {
            var list = new List <TagCompound>();

            for (int k = 0; k < inv.Length; k++)
            {
                if (ItemLoader.NeedsModSaving(inv[k]))
                {
                    var tag = ItemIO.Save(inv[k]);
                    tag.Set("slot", (short)k);
                    list.Add(tag);
                }
            }
            return(list.Count > 0 ? list : null);
        }
Example #8
0
 internal static void ReadContainers(BinaryReader reader)
 {
     byte[] flags = new byte[1];
     reader.Read(flags, 0, reader.ReadByte());
     if ((flags[0] & 1) == 1)
     {
         ContainerTables tables = ContainerTables.Create();
         int             count  = reader.ReadUInt16();
         for (int k = 0; k < count; k++)
         {
             int    slot    = reader.ReadUInt16();
             string modName = reader.ReadString();
             string name    = reader.ReadString();
             Mod    mod     = ModLoader.GetMod(modName);
             tables.headSlots[slot] = mod?.GetItem(name).item.headSlot ?? 0;
         }
         count = reader.ReadUInt16();
         for (int k = 0; k < count; k++)
         {
             int    slot    = reader.ReadUInt16();
             string modName = reader.ReadString();
             string name    = reader.ReadString();
             Mod    mod     = ModLoader.GetMod(modName);
             tables.bodySlots[slot] = mod?.GetItem(name).item.bodySlot ?? 0;
         }
         count = reader.ReadUInt16();
         for (int k = 0; k < count; k++)
         {
             int    slot    = reader.ReadUInt16();
             string modName = reader.ReadString();
             string name    = reader.ReadString();
             Mod    mod     = ModLoader.GetMod(modName);
             tables.legSlots[slot] = mod?.GetItem(name).item.legSlot ?? 0;
         }
         ReadContainerData(reader, tables);
     }
     //legacy load
     if ((flags[0] & 2) == 2)
     {
         int count = reader.ReadInt32();
         for (int k = 0; k < count; k++)
         {
             int         id        = reader.ReadInt32();
             TEItemFrame itemFrame = TileEntity.ByID[id] as TEItemFrame;
             ItemIO.LoadLegacy(itemFrame.item, reader, true);
         }
     }
 }
Example #9
0
        internal static void LoadContainers(TagCompound tag)
        {
            if (tag.ContainsKey("data"))
            {
                ReadContainers(new BinaryReader(new MemoryStream(tag.GetByteArray("data"))));
            }

            foreach (var frameTag in tag.GetList <TagCompound>("itemFrames"))
            {
                if (TileEntity.ByID.TryGetValue(frameTag.GetInt("id"), out TileEntity tileEntity) && tileEntity is TEItemFrame itemFrame)
                {
                    ItemIO.Load(itemFrame.item, frameTag.GetCompound("item"));
                }
                else
                {
                    Logging.tML.Warn($"Due to a bug in previous versions of tModLoader, the following ItemFrame data has been lost: {frameTag.ToString()}");
                }
            }
        }
Example #10
0
        private static void LoadLegacyChest(BinaryReader reader)
        {
            int x     = reader.ReadInt32();
            int y     = reader.ReadInt32();
            int chest = Chest.FindChest(x, y);

            if (chest < 0)
            {
                chest = Chest.CreateChest(x, y);
            }
            if (chest >= 0)
            {
                ItemIO.LoadLegacyInventory(Main.chest[chest].item, reader, true);
            }
            else
            {
                ItemIO.LoadLegacyInventory(new Item[40], reader, true);
            }
        }
Example #11
0
 private static void LoadLegacyModPlayer(Player player, byte[] flags, BinaryReader reader)
 {
     if ((flags[0] & 1) == 1)
     {
         ItemIO.LoadLegacyInventory(player.armor, reader);
     }
     if ((flags[0] & 2) == 2)
     {
         ItemIO.LoadLegacyInventory(player.dye, reader);
     }
     if ((flags[0] & 4) == 4)
     {
         ItemIO.LoadLegacyInventory(player.inventory, reader, true, true);
     }
     if ((flags[0] & 8) == 8)
     {
         ItemIO.LoadLegacyInventory(player.miscEquips, reader);
     }
     if ((flags[0] & 16) == 16)
     {
         ItemIO.LoadLegacyInventory(player.miscDyes, reader);
     }
     if ((flags[0] & 32) == 32)
     {
         ItemIO.LoadLegacyInventory(player.bank.item, reader, true);
     }
     if ((flags[0] & 64) == 64)
     {
         ItemIO.LoadLegacyInventory(player.bank2.item, reader, true);
     }
     if ((flags[0] & 128) == 128)
     {
         LoadLegacyModData(player, reader);
     }
     if ((flags[1] & 1) == 1)
     {
         LoadLegacyModBuffs(player, reader);
     }
 }
Example #12
0
        public static Item FromBase64(string base64)
        {
            MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64));

            return(ItemIO.Load(TagIO.FromStream(ms, true)));
        }
Example #13
0
 public static void WriteItem(this BinaryWriter writer, Item item, bool readStack = false, bool readFavorite = false) =>
 ItemIO.Send(item, writer, readStack, readFavorite);
Example #14
0
 public static Item ReadItem(this BinaryReader reader, bool readStack = false, bool readFavorite = false) =>
 ItemIO.Receive(reader, readStack, readFavorite);
Example #15
0
        //in Terraria.GameContent.Tile_Entities.TEItemFrame.WriteExtraData
        //  if item is a mod item write 0 as the ID
        internal static TagCompound SaveContainers()
        {
            var ms     = new MemoryStream();
            var writer = new BinaryWriter(ms);

            byte[]                 flags      = new byte[1];
            byte                   numFlags   = 0;
            ISet <int>             headSlots  = new HashSet <int>();
            ISet <int>             bodySlots  = new HashSet <int>();
            ISet <int>             legSlots   = new HashSet <int>();
            IDictionary <int, int> itemFrames = new Dictionary <int, int>();

            for (int i = 0; i < Main.maxTilesX; i++)
            {
                for (int j = 0; j < Main.maxTilesY; j++)
                {
                    Tile tile = Main.tile[i, j];
                    if (tile.active() && (tile.type == TileID.Mannequin || tile.type == TileID.Womannequin))
                    {
                        int slot     = tile.frameX / 100;
                        int position = tile.frameY / 18;
                        if (HasModArmor(slot, position))
                        {
                            if (position == 0)
                            {
                                headSlots.Add(slot);
                            }
                            else if (position == 1)
                            {
                                bodySlots.Add(slot);
                            }
                            else if (position == 2)
                            {
                                legSlots.Add(slot);
                            }
                            flags[0] |= 1;
                            numFlags  = 1;
                        }
                    }
                }
            }
            int tileEntity = 0;

            foreach (KeyValuePair <int, TileEntity> entity in TileEntity.ByID)
            {
                TEItemFrame itemFrame = entity.Value as TEItemFrame;
                if (itemFrame != null && ItemLoader.NeedsModSaving(itemFrame.item))
                {
                    itemFrames.Add(itemFrame.ID, tileEntity);
                    //flags[0] |= 2; legacy
                    numFlags = 1;
                }
                if (!(entity.Value is ModTileEntity))
                {
                    tileEntity++;
                }
            }
            if (numFlags == 0)
            {
                return(null);
            }
            writer.Write(numFlags);
            writer.Write(flags, 0, numFlags);
            if ((flags[0] & 1) == 1)
            {
                writer.Write((ushort)headSlots.Count);
                foreach (int slot in headSlots)
                {
                    writer.Write((ushort)slot);
                    ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Head][slot]);
                    writer.Write(item.mod.Name);
                    writer.Write(item.Name);
                }
                writer.Write((ushort)bodySlots.Count);
                foreach (int slot in bodySlots)
                {
                    writer.Write((ushort)slot);
                    ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Body][slot]);
                    writer.Write(item.mod.Name);
                    writer.Write(item.Name);
                }
                writer.Write((ushort)legSlots.Count);
                foreach (int slot in legSlots)
                {
                    writer.Write((ushort)slot);
                    ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Legs][slot]);
                    writer.Write(item.mod.Name);
                    writer.Write(item.Name);
                }
                WriteContainerData(writer);
            }
            var tag = new TagCompound();

            tag.Set("data", ms.ToArray());

            if (itemFrames.Count > 0)
            {
                tag.Set("itemFrames", itemFrames.Select(entry =>
                                                        new TagCompound {
                    ["id"]   = entry.Value,
                    ["item"] = ItemIO.Save(((TEItemFrame)TileEntity.ByID[entry.Key]).item)
                }
                                                        ).ToList());
            }
            return(tag);
        }
Example #16
0
        //in Terraria.GameContent.Tile_Entities.TEItemFrame.WriteExtraData
        //  if item is a mod item write 0 as the ID
        internal static bool WriteContainers(BinaryWriter writer)
        {
            byte[]                 flags      = new byte[1];
            byte                   numFlags   = 0;
            ISet <int>             headSlots  = new HashSet <int>();
            ISet <int>             bodySlots  = new HashSet <int>();
            ISet <int>             legSlots   = new HashSet <int>();
            IDictionary <int, int> itemFrames = new Dictionary <int, int>();

            for (int i = 0; i < Main.maxTilesX; i++)
            {
                for (int j = 0; j < Main.maxTilesY; j++)
                {
                    Tile tile = Main.tile[i, j];
                    if (tile.active() && (tile.type == TileID.Mannequin || tile.type == TileID.Womannequin))
                    {
                        int slot     = tile.frameX / 100;
                        int position = tile.frameY / 18;
                        if (HasModArmor(slot, position))
                        {
                            if (position == 0)
                            {
                                headSlots.Add(slot);
                            }
                            else if (position == 1)
                            {
                                bodySlots.Add(slot);
                            }
                            else if (position == 2)
                            {
                                legSlots.Add(slot);
                            }
                            flags[0] |= 1;
                            numFlags  = 1;
                        }
                    }
                }
            }
            int tileEntity = 0;

            foreach (KeyValuePair <int, TileEntity> entity in TileEntity.ByID)
            {
                TEItemFrame itemFrame = entity.Value as TEItemFrame;
                if (itemFrame != null && ItemLoader.NeedsModSaving(itemFrame.item))
                {
                    itemFrames.Add(itemFrame.ID, tileEntity);
                    flags[0] |= 2;
                    numFlags  = 1;
                }
                tileEntity++;
            }
            if (numFlags == 0)
            {
                return(false);
            }
            writer.Write(numFlags);
            writer.Write(flags, 0, numFlags);
            if ((flags[0] & 1) == 1)
            {
                writer.Write((ushort)headSlots.Count);
                foreach (int slot in headSlots)
                {
                    writer.Write((ushort)slot);
                    ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Head][slot]);
                    writer.Write(item.mod.Name);
                    writer.Write(Main.itemName[item.item.type]);
                }
                writer.Write((ushort)bodySlots.Count);
                foreach (int slot in bodySlots)
                {
                    writer.Write((ushort)slot);
                    ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Body][slot]);
                    writer.Write(item.mod.Name);
                    writer.Write(Main.itemName[item.item.type]);
                }
                writer.Write((ushort)legSlots.Count);
                foreach (int slot in legSlots)
                {
                    writer.Write((ushort)slot);
                    ModItem item = ItemLoader.GetItem(EquipLoader.slotToId[EquipType.Legs][slot]);
                    writer.Write(item.mod.Name);
                    writer.Write(Main.itemName[item.item.type]);
                }
                WriteContainerData(writer);
            }
            if ((flags[0] & 2) == 2)
            {
                writer.Write(itemFrames.Count);
                foreach (int oldID in itemFrames.Keys)
                {
                    TEItemFrame itemFrame = TileEntity.ByID[oldID] as TEItemFrame;
                    writer.Write(itemFrames[oldID]);
                    ItemIO.WriteItem(itemFrame.item, writer, true);
                }
            }
            return(true);
        }