Example #1
0
        static void SaveTileData(BinBuffer bb)
        {
            for (int y = 0; y < Main.maxTilesY; y++)
            {
                for (int x = 0; x < Main.maxTilesX; x++)
                {
                    var p = new Point16(x, y);
                    var t = Main.tile[x, y].type;

                    var fl = TileDataEntryFlags.Finished;

                    if (TileHooks.TileSpecificHandlers.ContainsKey(p))
                    {
                        fl |= TileDataEntryFlags.Tile;
                    }
                    if (TileHooks.TypeSpecificHandlers.ContainsKey(t))
                    {
                        fl |= TileDataEntryFlags.Type;
                    }

                    if (fl == TileDataEntryFlags.Finished)
                    {
                        continue;
                    }

                    bb.Write((byte)fl);
                    bb.Write(p.X);
                    bb.Write(p.Y);

                    if ((fl & TileDataEntryFlags.Tile) != 0)
                    {
                        var bh = TileHooks.TileSpecificHandlers[p];

                        bh.Save(bb);
                    }
                    if ((fl & TileDataEntryFlags.Type) != 0)
                    {
                        var bh = TileHooks.TypeSpecificHandlers[t];

                        foreach (var b in bh.behaviours)
                        {
                            b.Position = p;
                        }

                        bh.Save(bb);
                    }
                }
            }

            bb.Write(0);
        }
Example #2
0
        static void SaveNpcData(BinBuffer bb)
        {
            for (int i = 0; i < Main.npc.Length; i++)
            {
                var n = Main.npc[i];
                if (n == null || !n.active || !n.townNPC || n.type == NPCID.TravellingMerchant)
                {
                    continue;
                }

                bb.Write(true);
                if (n.P_BHandler == null)
                {
                    bb.Write(0); // as length for the IOBHandler
                }
                else
                {
                    var bh = n.P_BHandler as NpcBHandler;

                    bh.Save(bb);
                }
            }
            //bb.Write(false); // don't write, loops can be unified in the Load method (only mod data is written)

            // blame red for the double loop. see Terraria.IO.WorldFile.SaveNPCs/LoadNPCs
            for (int i = 0; i < Main.npc.Length; i++)
            {
                var n = Main.npc[i];
                if (n == null || !n.active || !NPCID.Sets.SavesAndLoads[n.type] || (n.townNPC && n.type != NPCID.TravellingMerchant))
                {
                    continue;
                }

                bb.Write(true);
                if (n.P_BHandler == null)
                {
                    bb.Write(0); // as length for the IOBHandler
                }
                else
                {
                    var bh = n.P_BHandler as NpcBHandler;

                    bh.Save(bb);
                }
            }
            bb.Write(false);
        }
Example #3
0
        static void SaveChestItems(BinBuffer bb)
        {
            var chests = Main.chest.Where(c => c != null);

            bb.Write((short)chests.Count());
            bb.WriteByte(Chest.maxItems);

            foreach (var c in chests)
            {
                SaveItemSlots(bb, c.item, Chest.maxItems, true, false);
            }
        }
Example #4
0
        public void WriteDictionary(BinBuffer bb)
        {
            unchecked
            {
                bb.WriteByte((ModID)Mods.Count);
            }

            foreach (var kvp in Mods)
            {
                bb.Write(kvp.Key);
                bb.Write(kvp.Value);
            }

            foreach (var kvp in ModObjects)
            {
                bb.Write(kvp.Key);

                var mod = kvp.Value;

                bb.Write(unchecked ((ObjID)mod.Count));

                foreach (var kvp_ in mod)
                {
                    bb.Write(kvp_.Key);
                    bb.Write(kvp_.Value);
                }
            }
        }
Example #5
0
        static void SaveTileTypes(BinBuffer bb)
        {
            // don't write anything for now, custom tiles aren't implemented
            return;

#pragma warning disable 162
            var map = new ModIdMap(TileID.Count, or => new TileRef(or).Resolve().Type, id => new TileRef(id));
#pragma warning restore 162

            int  ot   = 0;
            int  amt  = 0;
            bool once = true;

            int op = bb.Position;
            bb.Write(0);

            for (int y = 0; y < Main.maxTilesY; y++)
            {
                for (int x = 0; x < Main.maxTilesX; x++)
                {
                    int t = Main.tile[x, y] == null || !Main.tile[x, y].active() ? 0 : Main.tile[x, y].type;

                    if (once)
                    {
                        ot = t;
                        bb.Write(map.Register(new TileRef(t)));

                        once = false;
                        continue;
                    }

                    if (t == ot && amt < UInt16.MaxValue)
                    {
                        amt++;
                    }
                    else
                    {
                        bb.Write((ushort)amt); // write the amount of successing tiles of the same type,
                                               // instead of the type over and over again, to save some space
                        amt = 0;

                        ot = t;
                        bb.Write(map.Register(new TileRef(t)));
                    }
                }
            }
            bb.Write((ushort)amt);

            var p = bb.Position;
            bb.Position = op;
            bb.Write(p - op); // dictionary offset
            bb.Position = p;

            map.WriteDictionary(bb);
        }
Example #6
0
        /// <summary>
        /// Save <paramref name="slots" /> items from <paramref name="inventory" /> to the <paramref name="bb" />.
        /// </summary>
        /// <param name="bb">The writer for storing data</param>
        /// <param name="inventory">The array of items</param>
        /// <param name="slots">The amount of items in the inventory to save</param>
        /// <param name="stack">Whether or not the stack size should be saved</param>
        /// <param name="favourited">Whether or not the favourited state should be saved</param>
        static void SaveItemSlots(BinBuffer bb, Item[] inventory, int slots, bool stack, bool favourited)
        {
            for (int i = 0; i < slots; i++)
            {
                if (inventory[i].type < ItemID.Count)
                {
                    bb.Write(String.Empty); // write an empty string instead of 'Vanilla'
                }
                else
                {
                    // Save basic item data
                    ItemDef item = Handler.ItemDef.DefsByType[inventory[i].type];

                    bb.Write(item.Mod.InternalName);
                    bb.Write(item.InternalName);

                    // why, vanilla writes these already?
                    // only type + mod data is needed imo (and prefix type (+ data?) later on)
                    if (stack)
                    {
                        bb.Write(inventory[i].stack);
                    }
                    bb.WriteByte(inventory[i].prefix);
                    if (favourited)
                    {
                        bb.Write(inventory[i].favorited);
                    }
                }

                // Save Mod Data
                if (inventory[i].P_BHandler != null)
                {
                    ItemBHandler handler = (ItemBHandler)inventory[i].P_BHandler;

                    handler.Save(bb);
                }
                else
                {
                    bb.Write(0);
                }
            }
        }
Example #7
0
        //TODO: make these faster (especially write)
        static void Write2DArray(BinBuffer bb, ModIdMap map, int xLen, int yLen, Func <int, int, bool> isEmpty, Func <int, int, int> getElemV, Func <int, int, ObjectRef> getElemM)
        {
            var  ov   = 0;
            var  ot   = ObjectRef.Null;
            bool isOV = true;

            int  amt  = 0;
            bool once = true;

            int dictOffsetPosition = bb.Position;

            bb.Write(0); // dictionary position

            for (int y = 0; y < yLen; y++)
            {
                for (int x = 0; x < xLen; x++)
                {
                    var e   = isEmpty(x, y);
                    var v   = e ? 0 : getElemV(x, y);
                    var t   = ObjectRef.Null;
                    var isV = e || v > 0;

                    if (!e && v == 0)
                    {
                        t   = getElemM(x, y);
                        isV = false;
                    }

                    if (once)
                    {
                        if (isV)
                        {
                            ov = v;
                        }
                        else
                        {
                            ot = t;
                        }
                        isOV = isV;

                        bb.Write((uint)(isV ? map.Register(v) : map.Register(t)));

                        once = false;
                    }
                    else if (isV == isOV && (isV ? v == ov : t == ot) && amt < UInt16.MaxValue)
                    {
                        amt++;
                    }
                    else
                    {
                        bb.Write((ushort)amt); // write the amount of successing elements of the same type,
                                               // instead of the type over and over again, to save some space
                        amt = 0;               // amt == 0 -> one element

                        if (isV)
                        {
                            ov = v;
                        }
                        else
                        {
                            ot = t;
                        }
                        isOV = isV;

                        bb.Write((uint)(isV ? map.Register(v) : map.Register(t)));
                    }
                }
            }

            bb.Write((ushort)amt); // write final amt

            var afterData = bb.Position;

            bb.Position = dictOffsetPosition;
            bb.Write(afterData); // dictionary position
            bb.Position = afterData;

            map.WriteDictionary(bb);
        }
Example #8
0
        /// <summary>
        /// Save mod data to a .plr.prism file
        /// </summary>
        /// <param name="playerFile">The player being saved</param>
        internal static void SavePlayer(PlayerFileData playerFile)
        {
            string path   = playerFile.Path;
            Player player = playerFile.Player;

            if (Main.ServerSideCharacter || String.IsNullOrEmpty(path))
            {
                return;
            }

            path += ".prism";

            if (File.Exists(path))
            {
                File.Copy(path, playerFile.Path + ".bak.prism", true);
            }

            using (FileStream fileStream = new FileStream(path, FileMode.Create))
            {
                fileStream.WriteByte(PLAYER_VERSION); // write this before doing the crypto stuff, so we can change it between versions

                // can't we just get rid of this?
                using (CryptoStream cryptoStream = new CryptoStream(fileStream, new RijndaelManaged() /*{ Padding = PaddingMode.None }*/.CreateEncryptor(GenerateKey(player.name), ENCRYPTION_KEY), CryptoStreamMode.Write))
                    using (BinBuffer bb = new BinBuffer(cryptoStream))
                    {
                        #region Player Data
                        if (player.P_BHandler != null)
                        {
                            var bh = (PlayerBHandler)player.P_BHandler;

                            bh.Save(bb);
                        }
                        else
                        {
                            bb.Write(0);
                        }
                        #endregion Player Data

                        #region Item Data
                        SaveItemSlots(bb, player.armor, player.armor.Length, false, false);
                        SaveItemSlots(bb, player.dye, player.dye.Length, false, false);
                        SaveItemSlots(bb, player.inventory, Main.maxInventory, true, true);
                        SaveItemSlots(bb, player.miscEquips, player.miscEquips.Length, false, false);
                        SaveItemSlots(bb, player.bank.item, Chest.maxItems, true, false);
                        SaveItemSlots(bb, player.bank2.item, Chest.maxItems, true, false);
                        #endregion Item Data

                        #region Buff Data
                        for (int i = 0; i < Player.maxBuffs; i++)
                        {
                            if (Main.buffNoSave[player.buffType[i]] || player.buffType[i] < BuffID.Count || player.buffTime[i] <= 0)
                            {
                                bb.Write(String.Empty);
                            }
                            else
                            {
                                var buff = Handler.BuffDef.DefsByType[player.buffType[i]];

                                bb.Write(buff.Mod.InternalName);
                                bb.Write(buff.InternalName);
                                bb.Write(player.buffTime[i]);
                            }

                            if (player.P_BuffBHandler[i] != null)
                            {
                                var bh = (BuffBHandler)player.P_BuffBHandler[i];

                                bh.Save(bb);
                            }
                            else
                            {
                                bb.Write(0);
                            }
                        }
                        #endregion Buff Data
                    }
            }
        }