Exemplo n.º 1
0
        public Chest Copy()
        {
            var chest = new Chest(_x, _y);
            //chest.Items.Clear();
            for (int i = 0; i < Chest.MaxItems; i++)
            {
                chest.Items[i] = Items[i].Copy();
            }

            return chest;
        }
Exemplo n.º 2
0
        public Chest Copy()
        {
            var chest = new Chest(_x, _y);
            //chest.Items.Clear();
            for (int i = 0; i < Chest.MaxItems; i++)
            {
                if (Items.Count > i)
                    chest.Items[i] = Items[i].Copy();
                else
                {
                    chest.Items[i] = new Item();
                }
            }

            return chest;
        }
        public static IEnumerable<Chest> ReadChestDataFromStreamV1(BinaryReader b, uint version)
        {
            int chestSize = Chest.MaxItems;
            if (version < 58)
                chestSize = 20;

            for (int i = 0; i < 1000; i++)
            {
                if (b.ReadBoolean())
                {
                    var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                    for (int slot = 0; slot < Chest.MaxItems; slot++)
                    {
                        if (slot < chestSize)
                        {
                            int stackSize = version < 59 ? b.ReadByte() : b.ReadInt16();
                            chest.Items[slot].StackSize = stackSize;

                            if (chest.Items[slot].StackSize > 0)
                            {
                                if (version >= 38)
                                    chest.Items[slot].NetId = b.ReadInt32();
                                else
                                    chest.Items[slot].SetFromName(b.ReadString());

                                chest.Items[slot].StackSize = stackSize;
                                // Read prefix
                                if (version >= 36)
                                    chest.Items[slot].Prefix = b.ReadByte();
                            }
                        }
                    }
                    yield return chest;
                }
            }
        }
        public static ClipboardBuffer Load2(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    string name = reader.ReadString();
                    int version = reader.ReadInt32();
                    int maxx = reader.ReadInt32();
                    int maxy = reader.ReadInt32();

                    var buffer = new ClipboardBuffer(new Vector2Int32(maxx, maxy));

                    buffer.Name = string.IsNullOrWhiteSpace(name) ? Path.GetFileNameWithoutExtension(filename) : name;

                    try
                    {
                        for (int x = 0; x < maxx; x++)
                        {
                            for (int y = 0; y < maxy; y++)
                            {
                                var curTile = new Tile();
                                curTile.IsActive = reader.ReadBoolean();

                                if (curTile.IsActive)
                                {
                                    curTile.Type = reader.ReadByte();
                                    if (curTile.Type == 19) // fix for platforms
                                    {
                                        curTile.U = 0;
                                        curTile.V = 0;
                                    }
                                    else if (World.TileProperties[curTile.Type].IsFramed)
                                    {
                                        curTile.U = reader.ReadInt16();
                                        curTile.V = reader.ReadInt16();

                                        if (curTile.Type == 144) //timer
                                            curTile.V = 0;
                                    }
                                    else
                                    {
                                        curTile.U = -1;
                                        curTile.V = -1;
                                    }
                                }

                                if (reader.ReadBoolean())
                                    curTile.Wall = reader.ReadByte();

                                if (reader.ReadBoolean())
                                {
                                    curTile.Liquid = reader.ReadByte();
                                    curTile.IsLava = reader.ReadBoolean();
                                }

                                curTile.HasWire = reader.ReadBoolean();
                                buffer.Tiles[x, y] = curTile;
                            }

                        }

                    }
                    catch (Exception)
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                if (buffer.Tiles[x, y] == null)
                                    buffer.Tiles[x, y] = new Tile();
                            }
                        }
                        return buffer;
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            var chest = new Chest();
                            chest.X = reader.ReadInt32();
                            chest.Y = reader.ReadInt32();

                            for (int slot = 0; slot < 20; slot++)
                            {
                                byte stackSize = reader.ReadByte();
                                if (stackSize > 0)
                                {
                                    string itemName = reader.ReadString();
                                    chest.Items[slot].SetFromName(itemName);
                                    chest.Items[slot].StackSize = stackSize;
                                }
                            }

                            //Chests[chestIndex] = chest;
                            buffer.Chests.Add(chest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            string signText = reader.ReadString();
                            int x = reader.ReadInt32();
                            int y = reader.ReadInt32();
                            if (buffer.Tiles[x, y].IsActive && (buffer.Tiles[x, y].Type == 55 || buffer.Tiles[x, y].Type == 85))
                            // validate tile location
                            {
                                var sign = new Sign(x, y, signText);
                                //Signs[signIndex] = sign;
                                buffer.Signs.Add(sign);
                            }
                        }
                    }
                    string checkName = reader.ReadString();
                    int checkversion = reader.ReadInt32();
                    int checkx = reader.ReadInt32();
                    int checky = reader.ReadInt32();

                    if (checkName != buffer.Name || checkversion != version || checkx != maxx || checky != maxy)
                        System.Windows.MessageBox.Show("Verification failed. Some schematic data may be missing.", "Legacy Schematic Version");

                    return buffer;

                }
            }
            #pragma warning disable 162
            return null;
            #pragma warning restore
        }
Exemplo n.º 5
0
        public static World LoadWorld(string filename)
        {
            var w = new World();
            try
            {
                lock (_fileLock)
                {
                    using (var b = new BinaryReader(File.OpenRead(filename)))
                    {
                        w.Version = b.ReadUInt32(); //now we care about the version
                        w.Title = b.ReadString();
                        w.WorldId = b.ReadInt32();

                        w.Rand = new Random(w.WorldId);

                        w.LeftWorld = (float)b.ReadInt32();
                        w.RightWorld = (float)b.ReadInt32();
                        w.TopWorld = (float)b.ReadInt32();
                        w.BottomWorld = (float)b.ReadInt32();
                        w.TilesHigh = b.ReadInt32();
                        w.TilesWide = b.ReadInt32();

                        //if (w.TilesHigh > 10000 || w.TilesWide > 10000 || w.TilesHigh <= 0 || w.TilesWide <= 0)
                        //    throw new FileLoadException(string.Format("Invalid File: {0}", filename));

                        if (w.Version >= 63)
                            w.MoonType = (int)b.ReadByte();
                        else
                            w.MoonType = w.Rand.Next(MaxMoons);

                        if (w.Version >= 44)
                        {
                            w.TreeX[0] = b.ReadInt32();
                            w.TreeX[1] = b.ReadInt32();
                            w.TreeX[2] = b.ReadInt32();
                            w.TreeStyle[0] = b.ReadInt32();
                            w.TreeStyle[1] = b.ReadInt32();
                            w.TreeStyle[2] = b.ReadInt32();
                            w.TreeStyle[3] = b.ReadInt32();
                        }
                        if (w.Version >= 60)
                        {
                            w.CaveBackX[0] = b.ReadInt32();
                            w.CaveBackX[1] = b.ReadInt32();
                            w.CaveBackX[2] = b.ReadInt32();
                            w.CaveBackStyle[0] = b.ReadInt32();
                            w.CaveBackStyle[1] = b.ReadInt32();
                            w.CaveBackStyle[2] = b.ReadInt32();
                            w.CaveBackStyle[3] = b.ReadInt32();
                            w.IceBackStyle = b.ReadInt32();
                            if (w.Version >= 61)
                            {
                                w.JungleBackStyle = b.ReadInt32();
                                w.HellBackStyle = b.ReadInt32();
                            }
                        }
                        else
                        {
                            w.CaveBackX[0] = w.TilesWide / 2;
                            w.CaveBackX[1] = w.TilesWide;
                            w.CaveBackX[2] = w.TilesWide;
                            w.CaveBackStyle[0] = 0;
                            w.CaveBackStyle[1] = 1;
                            w.CaveBackStyle[2] = 2;
                            w.CaveBackStyle[3] = 3;
                            w.IceBackStyle = 0;
                            w.JungleBackStyle = 0;
                            w.HellBackStyle = 0;
                        }

                        w.SpawnX = b.ReadInt32();
                        w.SpawnY = b.ReadInt32();
                        w.GroundLevel = (int)b.ReadDouble();
                        w.RockLevel = (int)b.ReadDouble();

                        // read world flags
                        w.Time = b.ReadDouble();
                        w.DayTime = b.ReadBoolean();
                        w.MoonPhase = b.ReadInt32();
                        w.BloodMoon = b.ReadBoolean();

                        if (w.Version >= 70)
                        {
                            w.IsEclipse = b.ReadBoolean();
                        }

                        w.DungeonX = b.ReadInt32();
                        w.DungeonY = b.ReadInt32();

                        if (w.Version >= 56)
                        {
                            w.IsCrimson = b.ReadBoolean();
                        }
                        else
                        {
                            w.IsCrimson = false;
                        }

                        w.DownedBoss1 = b.ReadBoolean();
                        w.DownedBoss2 = b.ReadBoolean();
                        w.DownedBoss3 = b.ReadBoolean();

                        if (w.Version >= 66)
                        {
                            w.DownedQueenBee = b.ReadBoolean();
                        }
                        if (w.Version >= 44)
                        {
                            w.DownedMechBoss1 = b.ReadBoolean();
                            w.DownedMechBoss2 = b.ReadBoolean();
                            w.DownedMechBoss3 = b.ReadBoolean();
                            w.DownedMechBossAny = b.ReadBoolean();
                        }
                        if (w.Version >= 64)
                        {
                            w.DownedPlantBoss = b.ReadBoolean();
                            w.DownedGolemBoss = b.ReadBoolean();
                        }
                        if (w.Version >= 29)
                        {
                            w.SavedGoblin = b.ReadBoolean();
                            w.SavedWizard = b.ReadBoolean();
                            if (w.Version >= 34)
                            {
                                w.SavedMech = b.ReadBoolean();
                            }
                            w.DownedGoblins = b.ReadBoolean();
                        }
                        if (w.Version >= 32)
                            w.DownedClown = b.ReadBoolean();
                        if (w.Version >= 37)
                            w.DownedFrost = b.ReadBoolean();
                        if (w.Version >= 56)
                            w.DownedPirates = b.ReadBoolean();

                        w.ShadowOrbSmashed = b.ReadBoolean();
                        w.SpawnMeteor = b.ReadBoolean();
                        w.ShadowOrbCount = (int)b.ReadByte();

                        if (w.Version >= 23)
                        {
                            w.AltarCount = b.ReadInt32();
                            w.HardMode = b.ReadBoolean();
                        }

                        w.InvasionDelay = b.ReadInt32();
                        w.InvasionSize = b.ReadInt32();
                        w.InvasionType = b.ReadInt32();
                        w.InvasionX = b.ReadDouble();

                        if (w.Version >= 53)
                        {
                            w.TempRaining = b.ReadBoolean();
                            w.TempRainTime = b.ReadInt32();
                            w.TempMaxRain = b.ReadSingle();
                        }
                        if (w.Version >= 54)
                        {
                            w.OreTier1 = b.ReadInt32();
                            w.OreTier2 = b.ReadInt32();
                            w.OreTier3 = b.ReadInt32();
                        }
                        else if (w.Version < 23 || w.AltarCount != 0)
                        {
                            w.OreTier1 = 107;
                            w.OreTier2 = 108;
                            w.OreTier3 = 111;
                        }
                        else
                        {
                            w.OreTier1 = -1;
                            w.OreTier2 = -1;
                            w.OreTier3 = -1;
                        }

                        if (w.Version >= 55)
                        {
                            w.BgTree = b.ReadByte();
                            w.BgCorruption = b.ReadByte();
                            w.BgJungle = b.ReadByte();
                        }
                        if (w.Version >= 60)
                        {
                            w.BgSnow = b.ReadByte();
                            w.BgHallow = b.ReadByte();
                            w.BgCorruption = b.ReadByte();
                            w.BgDesert = b.ReadByte();
                            w.BgOcean = b.ReadByte();
                        }

                        if (w.Version >= 60)
                        {
                            w.CloudBgActive = (float)b.ReadInt32();
                        }
                        else
                        {
                            w.CloudBgActive = -w.Rand.Next(8640, 86400);
                        }

                        if (w.Version >= 62)
                        {
                            w.NumClouds = b.ReadInt16();
                            w.WindSpeedSet = b.ReadSingle();
                        }

                        w.Tiles = new Tile[w.TilesWide, w.TilesHigh];
                        for (int i = 0; i < w.TilesWide; i++)
                        {
                            for (int j = 0; j < w.TilesHigh; j++)
                            {
                                w.Tiles[i, j] = new Tile();
                            }
                        }

                        for (int x = 0; x < w.TilesWide; ++x)
                        {
                            OnProgressChanged(null, new ProgressChangedEventArgs(x.ProgressPercentage(w.TilesWide), "Loading Tiles..."));

                            for (int y = 0; y < w.TilesHigh; y++)
                            {
                                var tile = new Tile();

                                tile.IsActive = b.ReadBoolean();

                                if (!tile.IsActive)
                                    DebugLog(string.Format("Reading Empty Tile [{0},{1}]", x, y));

                                TileProperty tileProperty = null;
                                if (tile.IsActive)
                                {
                                    tile.Type = b.ReadByte();
                                    tileProperty = TileProperties[tile.Type];

                                    DebugLog(string.Format("Reading Tile {2} [{0},{1}] {3}", x, y, tile.Type, tileProperty.IsFramed ? "Framed" : ""));

                                    if (tile.Type == 127)
                                        tile.IsActive = false;

                                    if (tileProperty.IsFramed)
                                    {
                                        if (w.Version < 28 && tile.Type == 4)
                                        {
                                            // torches didn't have extra in older versions.
                                            tile.U = 0;
                                            tile.V = 0;
                                        }
                                        else if (w.Version < 40 && tile.Type == 19)
                                        {
                                            tile.U = 0;
                                            tile.V = 0;
                                        }
                                        else
                                        {
                                            tile.U = b.ReadInt16();
                                            tile.V = b.ReadInt16();

                                            if (tile.Type == 144) //timer
                                                tile.V = 0;
                                        }
                                    }
                                    else
                                    {
                                        tile.U = -1;
                                        tile.V = -1;
                                    }

                                    if (w.Version >= 48 && b.ReadBoolean())
                                    {
                                        tile.Color = b.ReadByte();
                                    }
                                }

                                //skip obsolete hasLight
                                if (w.Version <= 25)
                                    b.ReadBoolean();

                                if (b.ReadBoolean())
                                {
                                    tile.Wall = b.ReadByte();
                                    if (w.Version >= 48 && b.ReadBoolean())
                                        tile.WallColor = b.ReadByte();
                                }

                                if (b.ReadBoolean())
                                {
                                    tile.Liquid = b.ReadByte();
                                    tile.IsLava = b.ReadBoolean();
                                    if (w.Version >= 51)
                                    {
                                        tile.IsHoney = b.ReadBoolean();
                                    }
                                }

                                if (w.Version >= 33)
                                {
                                    tile.HasWire = b.ReadBoolean();
                                }
                                if (w.Version >= 43)
                                {
                                    tile.HasWire2 = b.ReadBoolean();
                                    tile.HasWire3 = b.ReadBoolean();
                                }

                                if (w.Version >= 41)
                                {
                                    tile.HalfBrick = b.ReadBoolean();

                                    if (tileProperty == null || !tileProperty.IsSolid)
                                        tile.HalfBrick = false;

                                    if (w.Version >= 49)
                                    {
                                        tile.Slope = b.ReadByte();

                                        if (tileProperty == null || !tileProperty.IsSolid)
                                            tile.Slope = 0;
                                    }
                                }
                                if (w.Version >= 42)
                                {
                                    tile.Actuator = b.ReadBoolean();
                                    tile.InActive = b.ReadBoolean();
                                }

                                // read complete, start compression
                                w.Tiles[x, y] = tile;

                                if (w.Version >= 25)
                                {
                                    int rle = b.ReadInt16();

                                    if (rle < 0)
                                        throw new ApplicationException("Invalid Tile Data!");

                                    DebugLog(string.Format("RLE {0}", rle));
                                    if (rle > 0)
                                    {
                                        for (int k = y + 1; k < y + rle + 1; k++)
                                        {
                                            var tcopy = (Tile)tile.Clone();
                                            w.Tiles[x, k] = tcopy;
                                        }
                                        y = y + rle;
                                    }
                                }
                            }
                        }

                        if (w.Version < 67)
                            w.FixSunflowers();
                        int chestSize = Chest.MaxItems;
                        if (w.Version < 58)
                            chestSize = 20;
                        w.Chests.Clear();
                        OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests..."));
                        for (int i = 0; i < 1000; i++)
                        {
                            if (b.ReadBoolean())
                            {
                                var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                                for (int slot = 0; slot < Chest.MaxItems; slot++)
                                {
                                    if (slot < chestSize)
                                    {

                                        int stackSize = w.Version < 59 ? b.ReadByte() : (int)b.ReadInt16();
                                        chest.Items[slot].StackSize = stackSize;

                                        if (chest.Items[slot].StackSize > 0)
                                        {
                                            if (w.Version >= 38)
                                                chest.Items[slot].NetId = b.ReadInt32();
                                            else
                                                chest.Items[slot].SetFromName(b.ReadString());

                                            chest.Items[slot].StackSize = stackSize;
                                            // Read prefix
                                            if (w.Version >= 36)
                                                chest.Items[slot].Prefix = b.ReadByte();
                                        }
                                    }
                                }
                                w.Chests.Add(chest);
                            }
                        }
                        w.Signs.Clear();
                        OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Signs..."));
                        for (int i = 0; i < 1000; i++)
                        {
                            if (b.ReadBoolean())
                            {
                                Sign sign = new Sign();
                                sign.Text = b.ReadString();
                                sign.X = b.ReadInt32();
                                sign.Y = b.ReadInt32();

                                if (w.Tiles[sign.X, sign.Y].IsActive && (int)w.Tiles[sign.X, sign.Y].Type == 55 && (int)w.Tiles[sign.X, sign.Y].Type == 85)
                                    w.Signs.Add(sign);
                            }
                        }
                        w.NPCs.Clear();
                        OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Data..."));
                        while (b.ReadBoolean())
                        {
                            var npc = new NPC();
                            npc.Name = b.ReadString();
                            npc.Position = new Vector2(b.ReadSingle(), b.ReadSingle());
                            npc.IsHomeless = b.ReadBoolean();
                            npc.Home = new Vector2Int32(b.ReadInt32(), b.ReadInt32());
                            npc.SpriteId = 0;
                            if (NpcIds.ContainsKey(npc.Name))
                                npc.SpriteId = NpcIds[npc.Name];

                            w.NPCs.Add(npc);
                        }
                        // if (version>=0x1f) read the names of the following npcs:
                        // merchant, nurse, arms dealer, dryad, guide, clothier, demolitionist,
                        // tinkerer and wizard
                        // if (version>=0x23) read the name of the mechanic

                        if (w.Version >= 31)
                        {
                            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Names..."));
                            w.CharacterNames.Add(new NpcName(17, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(18, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(19, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(20, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(22, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(54, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(38, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(107, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(108, b.ReadString()));
                            if (w.Version >= 35)
                                w.CharacterNames.Add(new NpcName(124, b.ReadString()));
                            else
                                w.CharacterNames.Add(new NpcName(124, "Nancy"));

                            if (w.Version >= 65)
                            {
                                w.CharacterNames.Add(new NpcName(160, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(178, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(207, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(208, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(209, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(227, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(228, b.ReadString()));
                                w.CharacterNames.Add(new NpcName(229, b.ReadString()));
                            }
                            else
                            {
                                w.CharacterNames.Add(GetNewNpc(160));
                                w.CharacterNames.Add(GetNewNpc(178));
                                w.CharacterNames.Add(GetNewNpc(207));
                                w.CharacterNames.Add(GetNewNpc(208));
                                w.CharacterNames.Add(GetNewNpc(209));
                                w.CharacterNames.Add(GetNewNpc(227));
                                w.CharacterNames.Add(GetNewNpc(228));
                                w.CharacterNames.Add(GetNewNpc(229));
                            }
                        }
                        else
                        {
                            w.CharacterNames.Add(GetNewNpc(17));
                            w.CharacterNames.Add(GetNewNpc(18));
                            w.CharacterNames.Add(GetNewNpc(19));
                            w.CharacterNames.Add(GetNewNpc(20));
                            w.CharacterNames.Add(GetNewNpc(22));
                            w.CharacterNames.Add(GetNewNpc(54));
                            w.CharacterNames.Add(GetNewNpc(38));
                            w.CharacterNames.Add(GetNewNpc(107));
                            w.CharacterNames.Add(GetNewNpc(108));
                            w.CharacterNames.Add(GetNewNpc(124));
                            w.CharacterNames.Add(GetNewNpc(160));
                            w.CharacterNames.Add(GetNewNpc(178));
                            w.CharacterNames.Add(GetNewNpc(207));
                            w.CharacterNames.Add(GetNewNpc(208));
                            w.CharacterNames.Add(GetNewNpc(209));
                            w.CharacterNames.Add(GetNewNpc(227));
                            w.CharacterNames.Add(GetNewNpc(228));
                            w.CharacterNames.Add(GetNewNpc(229));

                        }
                        if (w.Version >= 7)
                        {
                            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Validating File..."));
                            bool validation = b.ReadBoolean();
                            string checkTitle = b.ReadString();
                            int checkVersion = b.ReadInt32();
                            if (validation && checkTitle == w.Title && checkVersion == w.WorldId)
                            {
                                //w.loadSuccess = true;
                            }
                            else
                            {
                                b.Close();
                                throw new FileLoadException(string.Format("Error reading world file validation parameters! {0}", filename));
                            }
                        }
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Load Complete."));

                    }
                    w.LastSave = File.GetLastWriteTimeUtc(filename);
                }
            }
            catch (Exception err)
            {
                string msg = "There was an error reading the world file, do you wish to force it to load anyway?\r\n\r\n" +
                             "WARNING: This may have unexpected results including corrupt world files and program crashes.\r\n\r\n" +
                             "The error is :\r\n";
                if (MessageBox.Show(msg + err, "World File Error", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                    return null;
            }
            return w;
        }
        public static IEnumerable<Chest> LoadChestData(BinaryReader r)
        {
            int totalChests = r.ReadInt16();
            int maxItems = r.ReadInt16();

            // overflow item check?
            int itemsPerChest;
            int overflowItems;
            if (maxItems >= Chest.MaxItems)
            {
                itemsPerChest = Chest.MaxItems;
                overflowItems = maxItems - Chest.MaxItems;
            }
            else
            {
                itemsPerChest = maxItems;
                overflowItems = 0;
            }


            // read chests
            for (int i = 0; i < totalChests; i++)
            {
                var chest = new Chest
                {
                    X = r.ReadInt32(),
                    Y = r.ReadInt32(),
                    Name = r.ReadString()
                };

                // read items in chest
                for (int slot = 0; slot < itemsPerChest; slot++)
                {
                    var stackSize = r.ReadInt16();
                    chest.Items[slot].StackSize = stackSize;

                    if (stackSize > 0)
                    {
                        int id = r.ReadInt32();
                        byte prefix = r.ReadByte();

                        chest.Items[slot].NetId = id;
                        chest.Items[slot].StackSize = stackSize;
                        chest.Items[slot].Prefix = prefix;

                    }
                }

                // dump overflow items
                for (int overflow = 0; overflow < overflowItems; overflow++)
                {
                    var stackSize = r.ReadInt16();
                    if (stackSize > 0)
                    {
                        r.ReadInt32();
                        r.ReadByte();
                    }
                }

                yield return chest;
            }

        }
Exemplo n.º 7
0
        public static bool loadWorldChests(World w, int num, MemoryStream fileStream, MemoryStream modStream)
        {
            //string savePath = Path.Combine(Main.worldTempPath, "world.chests");
            //string modSavePath = Path.Combine(Main.worldTempPath, "world.chests.moddata");
            //using (FileStream fileStream = new FileStream(savePath, FileMode.Open))
            //{
            using (BinaryReader b = new BinaryReader(fileStream))
            {
                //BinaryReader modReader = new BinaryReader(modStream); //This stream contains mod data
                try
                {
                    w.Chests.Clear();
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests..."));
                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                            for (int slot = 0; slot < Chest.MaxItems; slot++)
                            {
                                var stackSize = b.ReadByte();
                                chest.Items[slot].StackSize = stackSize;
                                if (chest.Items[slot].StackSize > 0)
                                {
                                    if (w.Version >= 38)
                                        chest.Items[slot].NetId = b.ReadInt32();
                                    else
                                        chest.Items[slot].SetFromName(b.ReadString());

                                    chest.Items[slot].StackSize = stackSize;
                                    // Read prefix
                                    if (w.Version >= 36)
                                        chest.Items[slot].Prefix = b.ReadByte();

                                    //Unique ID value associates this item with mod data
                                    chest.Items[slot].ModId = b.ReadUInt16();
                                }
                            }
                            w.Chests.Add(chest);
                        }
                    }
                }
                catch (Exception e)
                {
                   // modReader.Close();
                    modStream.Close();

                    b.Close();
                    fileStream.Close();
                    throw new Exception("Error loading chests\n" + e);
                }
                //modReader.Close();
                modStream.Close();
                b.Close();
                fileStream.Close();
            }
            return true;
        }
        public static ClipboardBuffer LoadOld(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var reader = new BinaryReader(stream))
                {
                    string name = reader.ReadString();
                    int version = reader.ReadInt32();
                    int maxx = reader.ReadInt32();
                    int maxy = reader.ReadInt32();

                    var buffer = new ClipboardBuffer(new Vector2Int32(maxx, maxy));

                    buffer.Name = string.IsNullOrWhiteSpace(name) ? Path.GetFileNameWithoutExtension(filename) : name;

                    try
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                var tile = new Tile();

                                tile.IsActive = reader.ReadBoolean();

                                if (tile.IsActive)
                                {
                                    tile.Type = reader.ReadByte();

                                    if (tile.Type == 19)
                                    {
                                        tile.U = 0;
                                        tile.V = 0;
                                    }
                                    else if (World.TileProperties[tile.Type].IsFramed)
                                    {
                                        tile.U = reader.ReadInt16();
                                        tile.V = reader.ReadInt16();
                                    }
                                    else
                                    {
                                        tile.U = -1;
                                        tile.V = -1;
                                    }
                                }

                                // trash old lighted value
                                reader.ReadBoolean();

                                if (reader.ReadBoolean())
                                {
                                    tile.Wall = reader.ReadByte();
                                }

                                if (reader.ReadBoolean())
                                {
                                    tile.Liquid = reader.ReadByte();
                                    tile.IsLava = reader.ReadBoolean();
                                }

                                buffer.Tiles[x, y] = tile;
                            }
                        }

                    }
                    catch (Exception)
                    {
                        for (int x = 0; x < buffer.Size.X; x++)
                        {
                            for (int y = 0; y < buffer.Size.Y; y++)
                            {
                                if (buffer.Tiles[x, y] == null)
                                    buffer.Tiles[x, y] = new Tile();
                            }
                        }
                        return buffer;
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            var chest = new Chest();
                            chest.X = reader.ReadInt32();
                            chest.Y = reader.ReadInt32();

                            for (int slot = 0; slot < 20; slot++)
                            {
                                byte stackSize = reader.ReadByte();
                                if (stackSize > 0)
                                {
                                    string itemName = reader.ReadString();
                                    chest.Items[slot].SetFromName(itemName);
                                    chest.Items[slot].StackSize = stackSize;
                                }
                            }

                            //Chests[chestIndex] = chest;
                            buffer.Chests.Add(chest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (reader.ReadBoolean())
                        {
                            string signText = reader.ReadString();
                            int x = reader.ReadInt32();
                            int y = reader.ReadInt32();
                            if (buffer.Tiles[x, y].IsActive && (buffer.Tiles[x, y].Type == 55 || buffer.Tiles[x, y].Type == 85))
                            // validate tile location
                            {
                                var sign = new Sign(x, y, signText);
                                //Signs[signIndex] = sign;
                                buffer.Signs.Add(sign);
                            }
                        }
                    }

                    int checkx = reader.ReadInt32();
                    int checky = reader.ReadInt32();

                    if (checkx == maxx && checky == maxy)
                        return buffer;

                }
            }

            return null;
        }
 public ChestPopup(Chest chest)
 {
     InitializeComponent();
     DataContext = chest.Items;
 }
Exemplo n.º 10
0
        public static World LoadWorld(string filename)
        {
            var w = new World();
            try
            {
                lock (_fileLock)
                {
                    using (var b = new BinaryReader(File.OpenRead(filename)))
                    {
                        w.Version = b.ReadUInt32(); //now we care about the version
                        w.Title = b.ReadString();

                        w.WorldId = b.ReadInt32();
                        w.LeftWorld = (float)b.ReadInt32();
                        w.RightWorld = (float)b.ReadInt32();
                        w.TopWorld = (float)b.ReadInt32();
                        w.BottomWorld = (float)b.ReadInt32();

                        w.TilesHigh = b.ReadInt32();
                        w.TilesWide = b.ReadInt32();

                        if (w.TilesHigh > 10000 || w.TilesWide > 10000 || w.TilesHigh <= 0 || w.TilesWide <= 0)
                            throw new FileLoadException(string.Format("Invalid File: {0}", filename));

                        w.SpawnX = b.ReadInt32();
                        w.SpawnY = b.ReadInt32();
                        w.GroundLevel = (int)b.ReadDouble();
                        w.RockLevel = (int)b.ReadDouble();

                        // read world flags
                        w.Time = b.ReadDouble();
                        w.DayTime = b.ReadBoolean();
                        w.MoonPhase = b.ReadInt32();
                        w.BloodMoon = b.ReadBoolean();
                        w.DungeonX = b.ReadInt32();
                        w.DungeonY = b.ReadInt32();
                        w.DownedBoss1 = b.ReadBoolean();
                        w.DownedBoss2 = b.ReadBoolean();
                        w.DownedBoss3 = b.ReadBoolean();
                        if (w.Version >= 29)
                        {
                            w.SavedGoblin = b.ReadBoolean();
                            w.SavedWizard = b.ReadBoolean();
                            if (w.Version >= 34)
                                w.SavedMech = b.ReadBoolean();
                            w.DownedGoblins = b.ReadBoolean();
                        }
                        if (w.Version >= 32)
                            w.DownedClown = b.ReadBoolean();
                        if (w.Version >= 37)
                            w.DownedFrost = b.ReadBoolean();
                        w.ShadowOrbSmashed = b.ReadBoolean();
                        w.SpawnMeteor = b.ReadBoolean();
                        w.ShadowOrbCount = (int)b.ReadByte();
                        if (w.Version >= 23)
                        {
                            w.AltarCount = b.ReadInt32();
                            w.HardMode = b.ReadBoolean();
                        }
                        w.InvasionDelay = b.ReadInt32();
                        w.InvasionSize = b.ReadInt32();
                        w.InvasionType = b.ReadInt32();
                        w.InvasionX = b.ReadDouble();

                        w.Tiles = new Tile[w.TilesWide, w.TilesHigh];
                        for (int x = 0; x < w.TilesWide; ++x)
                        {
                            OnProgressChanged(null, new ProgressChangedEventArgs(x.ProgressPercentage(w.TilesWide), "Loading Tiles..."));

                            Tile prevtype = new Tile();
                            for (int y = 0; y < w.TilesHigh; y++)
                            {

                                var tile = new Tile();

                                tile.IsActive = b.ReadBoolean();

                                if (tile.IsActive)
                                {

                                    tile.Type = b.ReadByte();
                                    var tileProperty = TileProperties[tile.Type];
                                    if (string.Equals(tileProperty.Name, "UNKNOWN", StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        throw new ArgumentOutOfRangeException(string.Format("Unknown tile tile: {0}, please add tile id {0} too your settings.xml.\r\nBE SURE TO INCLUDE THE isFramed PROPERTY (sprites=true, blocks=false).\r\nYou are seeing this message due to an update or mod.", tile.Type));
                                    }

                                    if (tile.Type == (int)sbyte.MaxValue)
                                        tile.IsActive = false;

                                    if (tileProperty.IsFramed)
                                    {
                                        // torches didn't have extra in older versions.
                                        if (w.Version < 28 && tile.Type == 4)
                                        {
                                            tile.U = 0;
                                            tile.V = 0;
                                        }
                                        else
                                        {
                                            tile.U = b.ReadInt16();
                                            tile.V = b.ReadInt16();
                                            //if (tile.Type == 128) //armor stand
                                            //    tile.Frame = new PointShort((short)(tile.Frame.X % 100), tile.Frame.Y);

                                            if ((int)tile.Type == 144) //timer
                                                tile.V = 0;
                                        }
                                    }
                                    else
                                    {
                                        tile.U = -1;
                                        tile.V = -1;
                                    }
                                }
                                if (w.Version <= 25)
                                    b.ReadBoolean(); //skip obsolete hasLight
                                if (b.ReadBoolean())
                                {
                                    tile.Wall = b.ReadByte();
                                }
                                //else
                                //    tile.Wall = 0;
                                if (b.ReadBoolean())
                                {
                                    tile.Liquid = b.ReadByte();
                                    tile.IsLava = b.ReadBoolean();
                                }

                                if (w.Version >= 33)
                                    tile.HasWire = b.ReadBoolean();
                                //else
                                //    tile.HasWire = false;
                                w.Tiles[x, y] = tile;

                                var ptype = (Tile)prevtype.Clone();
                                prevtype = (Tile)tile.Clone();
                                if (w.Version >= 25) //compression ftw :)
                                {
                                    int rle = b.ReadInt16();
                                    if (rle > 0)
                                    {
                                        for (int r = y + 1; r < y + rle + 1; r++)
                                        {
                                            var tcopy = (Tile)tile.Clone();
                                            w.Tiles[x, r] = tcopy;
                                        }
                                        y += rle;
                                    }
                                }
                            }
                        }
                        w.Chests.Clear();
                        OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests..."));
                        for (int i = 0; i < 1000; i++)
                        {
                            if (b.ReadBoolean())
                            {
                                var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                                for (int slot = 0; slot < Chest.MaxItems; slot++)
                                {
                                    var stackSize = b.ReadByte();
                                    chest.Items[slot].StackSize = stackSize;
                                    if (chest.Items[slot].StackSize > 0)
                                    {
                                        if (w.Version >= 38)
                                            chest.Items[slot].NetId = b.ReadInt32();
                                        else
                                            chest.Items[slot].SetFromName(b.ReadString());

                                        chest.Items[slot].StackSize = stackSize;
                                        // Read prefix
                                        if (w.Version >= 36)
                                            chest.Items[slot].Prefix = b.ReadByte();
                                    }
                                }
                                w.Chests.Add(chest);
                            }
                        }
                        w.Signs.Clear();
                        OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Signs..."));
                        for (int i = 0; i < 1000; i++)
                        {
                            if (b.ReadBoolean())
                            {
                                Sign sign = new Sign();
                                sign.Text = b.ReadString();
                                sign.X = b.ReadInt32();
                                sign.Y = b.ReadInt32();
                                w.Signs.Add(sign);
                            }
                        }
                        w.NPCs.Clear();
                        OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Data..."));
                        while (b.ReadBoolean())
                        {
                            var npc = new NPC();
                            npc.Name = b.ReadString();
                            npc.Position = new Vector2(b.ReadSingle(), b.ReadSingle());
                            npc.IsHomeless = b.ReadBoolean();
                            npc.Home = new Vector2Int32(b.ReadInt32(), b.ReadInt32());
                            npc.SpriteId = 0;
                            if (NpcIds.ContainsKey(npc.Name))
                                npc.SpriteId = NpcIds[npc.Name];

                            w.NPCs.Add(npc);
                        }
                        // if (version>=0x1f) read the names of the following npcs:
                        // merchant, nurse, arms dealer, dryad, guide, clothier, demolitionist,
                        // tinkerer and wizard
                        // if (version>=0x23) read the name of the mechanic

                        if (w.Version >= 31)
                        {
                            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Names..."));
                            w.CharacterNames.Add(new NpcName(17, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(18, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(19, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(20, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(22, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(54, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(38, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(107, b.ReadString()));
                            w.CharacterNames.Add(new NpcName(108, b.ReadString()));
                            if (w.Version >= 35)
                                w.CharacterNames.Add(new NpcName(124, b.ReadString()));
                            else
                            {
                                w.CharacterNames.Add(new NpcName(124, "Nancy"));
                            }
                        }
                        else
                        {
                            w.CharacterNames.Add(new NpcName(17, "Harold"));
                            w.CharacterNames.Add(new NpcName(18, "Molly"));
                            w.CharacterNames.Add(new NpcName(19, "Dominique"));
                            w.CharacterNames.Add(new NpcName(20, "Felicitae"));
                            w.CharacterNames.Add(new NpcName(22, "Steve"));
                            w.CharacterNames.Add(new NpcName(54, "Fitz"));
                            w.CharacterNames.Add(new NpcName(38, "Gimut"));
                            w.CharacterNames.Add(new NpcName(107, "Knogs"));
                            w.CharacterNames.Add(new NpcName(108, "Fizban"));
                            w.CharacterNames.Add(new NpcName(124, "Nancy"));
                        }
                        if (w.Version >= 7)
                        {
                            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Validating File..."));
                            bool validation = b.ReadBoolean();
                            string checkTitle = b.ReadString();
                            int checkVersion = b.ReadInt32();
                            if (validation && checkTitle == w.Title && checkVersion == w.WorldId)
                            {
                                //w.loadSuccess = true;
                            }
                            else
                            {
                                b.Close();
                                throw new FileLoadException(string.Format("Error reading world file validation parameters! {0}", filename));
                            }
                        }
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Load Complete."));

                    }
                    w.LastSave = File.GetLastWriteTimeUtc(filename);
                }
            }
            catch (Exception err)
            {
                string msg = "There was an error reading the world file, do you wish to force it to load anyway?\r\n\r\n" +
                             "WARNING: This may have unexpected results including corrupt world files and program crashes.\r\n\r\n" +
                             "The error is :\r\n";
                if (MessageBox.Show(msg + err, "World File Error", MessageBoxButton.YesNo, MessageBoxImage.Error) != MessageBoxResult.Yes)
                    return null;
            }
            return w;
        }
Exemplo n.º 11
0
        public static IEnumerable <Chest> LoadChestData(BinaryReader r)
        {
            int totalChests = r.ReadInt16();
            int maxItems    = r.ReadInt16();

            // overflow item check?
            int itemsPerChest;
            int overflowItems;

            if (maxItems > Chest.MaxItems)
            {
                itemsPerChest = Chest.MaxItems;
                overflowItems = maxItems - Chest.MaxItems;
            }
            else
            {
                itemsPerChest = maxItems;
                overflowItems = 0;
            }


            // read chests
            for (int i = 0; i < totalChests; i++)
            {
                var chest = new Chest
                {
                    X    = r.ReadInt32(),
                    Y    = r.ReadInt32(),
                    Name = r.ReadString()
                };

                // read items in chest
                for (int slot = 0; slot < itemsPerChest; slot++)
                {
                    var stackSize = r.ReadInt16();
                    chest.Items[slot].StackSize = stackSize;

                    if (stackSize > 0)
                    {
                        int  id     = r.ReadInt32();
                        byte prefix = r.ReadByte();

                        chest.Items[slot].NetId     = id;
                        chest.Items[slot].StackSize = stackSize;
                        chest.Items[slot].Prefix    = prefix;
                    }
                }

                // dump overflow items
                for (int overflow = 0; overflow < overflowItems; overflow++)
                {
                    var stackSize = r.ReadInt16();
                    if (stackSize > 0)
                    {
                        r.ReadInt32();
                        r.ReadByte();
                    }
                }

                yield return(chest);
            }
        }
Exemplo n.º 12
0
        public static World LoadWorld(string filename)
        {
            var w = new World();

            using (var b = new BinaryReader(File.OpenRead(filename)))
            {
                w.Version = b.ReadUInt32(); //now we care about the version
                w.Title   = b.ReadString();

                w.WorldId     = b.ReadInt32();
                w.LeftWorld   = (float)b.ReadInt32();
                w.RightWorld  = (float)b.ReadInt32();
                w.TopWorld    = (float)b.ReadInt32();
                w.BottomWorld = (float)b.ReadInt32();

                w.TilesHigh   = b.ReadInt32();
                w.TilesWide   = b.ReadInt32();
                w.SpawnX      = b.ReadInt32();
                w.SpawnY      = b.ReadInt32();
                w.GroundLevel = (int)b.ReadDouble();
                w.RockLevel   = (int)b.ReadDouble();

                // read world flags
                w.Time        = b.ReadDouble();
                w.DayTime     = b.ReadBoolean();
                w.MoonPhase   = b.ReadInt32();
                w.BloodMoon   = b.ReadBoolean();
                w.DungeonX    = b.ReadInt32();
                w.DungeonY    = b.ReadInt32();
                w.DownedBoss1 = b.ReadBoolean();
                w.DownedBoss2 = b.ReadBoolean();
                w.DownedBoss3 = b.ReadBoolean();
                if (w.Version >= 29)
                {
                    w.SavedGoblin = b.ReadBoolean();
                    w.SavedWizard = b.ReadBoolean();
                    if (w.Version >= 34)
                    {
                        w.SavedMech = b.ReadBoolean();
                    }
                    w.DownedGoblins = b.ReadBoolean();
                }
                if (w.Version >= 32)
                {
                    w.DownedClown = b.ReadBoolean();
                }
                if (w.Version >= 37)
                {
                    w.DownedFrost = b.ReadBoolean();
                }
                w.ShadowOrbSmashed = b.ReadBoolean();
                w.SpawnMeteor      = b.ReadBoolean();
                w.ShadowOrbCount   = (int)b.ReadByte();
                if (w.Version >= 23)
                {
                    w.AltarCount = b.ReadInt32();
                    w.HardMode   = b.ReadBoolean();
                }
                w.InvasionDelay = b.ReadInt32();
                w.InvasionSize  = b.ReadInt32();
                w.InvasionType  = b.ReadInt32();
                w.InvasionX     = b.ReadDouble();



                w.Tiles = new Tile[w.TilesWide, w.TilesHigh];
                for (int x = 0; x < w.TilesWide; ++x)
                {
                    OnProgressChanged(null, new ProgressChangedEventArgs(x.ProgressPercentage(w.TilesWide), "Loading Tiles..."));

                    Tile prevtype = new Tile();
                    for (int y = 0; y < w.TilesHigh; y++)
                    {
                        var tile = new Tile();

                        tile.IsActive = b.ReadBoolean();

                        if (tile.IsActive)
                        {
                            tile.Type = b.ReadByte();
                            if (tile.Type == (int)sbyte.MaxValue)
                            {
                                tile.IsActive = false;
                            }
                            if (TileProperties[tile.Type].IsFramed)
                            {
                                // torches didn't have extra in older versions.
                                if (w.Version < 28 && tile.Type == 4)
                                {
                                    tile.U = 0;
                                    tile.V = 0;
                                }
                                else
                                {
                                    tile.U = b.ReadInt16();
                                    tile.V = b.ReadInt16();
                                    //if (tile.Type == 128) //armor stand
                                    //    tile.Frame = new PointShort((short)(tile.Frame.X % 100), tile.Frame.Y);

                                    if (tile.Type == 144) //timer
                                    {
                                        tile.V = 0;
                                    }
                                }
                            }
                            else
                            {
                                tile.U = -1;
                                tile.V = -1;
                            }
                        }
                        if (w.Version <= 25)
                        {
                            b.ReadBoolean(); //skip obsolete hasLight
                        }
                        if (b.ReadBoolean())
                        {
                            tile.Wall = b.ReadByte();
                        }
                        //else
                        //    tile.Wall = 0;
                        if (b.ReadBoolean())
                        {
                            tile.Liquid = b.ReadByte();
                            tile.IsLava = b.ReadBoolean();
                        }

                        if (w.Version >= 33)
                        {
                            tile.HasWire = b.ReadBoolean();
                        }
                        //else
                        //    tile.HasWire = false;
                        w.Tiles[x, y] = tile;

                        var ptype = (Tile)prevtype.Clone();
                        prevtype = (Tile)tile.Clone();
                        if (w.Version >= 25) //compression ftw :)
                        {
                            int rle = b.ReadInt16();
                            if (rle > 0)
                            {
                                for (int r = y + 1; r < y + rle + 1; r++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    w.Tiles[x, r] = tcopy;
                                }
                                y += rle;
                            }
                        }
                    }
                }
                w.Chests.Clear();
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests..."));
                for (int i = 0; i < 1000; i++)
                {
                    if (b.ReadBoolean())
                    {
                        var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                        for (int slot = 0; slot < Chest.MaxItems; slot++)
                        {
                            chest.Items[slot].StackSize = b.ReadByte();
                            if (chest.Items[slot].StackSize > 0)
                            {
                                chest.Items[slot].ItemName = b.ReadString();

                                // Read prefix
                                if (w.Version >= 36)
                                {
                                    chest.Items[slot].Prefix = b.ReadByte();
                                }
                            }
                        }
                        w.Chests.Add(chest);
                    }
                }
                w.Signs.Clear();
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Signs..."));
                for (int i = 0; i < 1000; i++)
                {
                    if (b.ReadBoolean())
                    {
                        Sign sign = new Sign();
                        sign.Text = b.ReadString();
                        sign.X    = b.ReadInt32();
                        sign.Y    = b.ReadInt32();
                        w.Signs.Add(sign);
                    }
                }
                w.NPCs.Clear();
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Data..."));
                while (b.ReadBoolean())
                {
                    var npc = new NPC();
                    npc.Name       = b.ReadString();
                    npc.Position   = new Vector2(b.ReadSingle(), b.ReadSingle());
                    npc.IsHomeless = b.ReadBoolean();
                    npc.Home       = new Vector2Int32(b.ReadInt32(), b.ReadInt32());
                    npc.SpriteId   = 0;
                    if (NpcIds.ContainsKey(npc.Name))
                    {
                        npc.SpriteId = NpcIds[npc.Name];
                    }

                    w.NPCs.Add(npc);
                }
                // if (version>=0x1f) read the names of the following npcs:
                // merchant, nurse, arms dealer, dryad, guide, clothier, demolitionist,
                // tinkerer and wizard
                // if (version>=0x23) read the name of the mechanic


                if (w.Version >= 31)
                {
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Names..."));
                    w.CharacterNames.Add(new NpcName(17, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(18, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(19, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(20, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(22, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(54, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(38, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(107, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(108, b.ReadString()));
                    if (w.Version >= 35)
                    {
                        w.CharacterNames.Add(new NpcName(124, b.ReadString()));
                    }
                    else
                    {
                        w.CharacterNames.Add(new NpcName(124, "Nancy"));
                    }
                }
                else
                {
                    w.CharacterNames.Add(new NpcName(17, "Harold"));
                    w.CharacterNames.Add(new NpcName(18, "Molly"));
                    w.CharacterNames.Add(new NpcName(19, "Dominique"));
                    w.CharacterNames.Add(new NpcName(20, "Felicitae"));
                    w.CharacterNames.Add(new NpcName(22, "Steve"));
                    w.CharacterNames.Add(new NpcName(54, "Fitz"));
                    w.CharacterNames.Add(new NpcName(38, "Gimut"));
                    w.CharacterNames.Add(new NpcName(107, "Knogs"));
                    w.CharacterNames.Add(new NpcName(108, "Fizban"));
                    w.CharacterNames.Add(new NpcName(124, "Nancy"));
                }
                if (w.Version >= 7)
                {
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Validating File..."));
                    bool   validation   = b.ReadBoolean();
                    string checkTitle   = b.ReadString();
                    int    checkVersion = b.ReadInt32();
                    if (validation && checkTitle == w.Title && checkVersion == w.WorldId)
                    {
                        //w.loadSuccess = true;
                    }
                    else
                    {
                        b.Close();
                        throw new FileLoadException("Error reading world file validation parameters!");
                    }
                }
                OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Load Complete."));
            }
            return(w);
        }
Exemplo n.º 13
0
        public void Save(string filename, bool resetTime = false)
        {
            if (resetTime)
            {
                OnProgressChanged(this, new ProgressChangedEventArgs(0, "Resetting Time..."));
                ResetTime();
            }

            if (filename == null)
            {
                return;
            }

            string temp = filename + ".tmp";

            using (var fs = new FileStream(temp, FileMode.Create))
            {
                using (var bw = new BinaryWriter(fs))
                {
                    bw.Write(World.CompatibleVersion);
                    bw.Write(Title);
                    bw.Write(WorldId);
                    bw.Write((int)LeftWorld);
                    bw.Write((int)RightWorld);
                    bw.Write((int)TopWorld);
                    bw.Write((int)BottomWorld);
                    bw.Write(TilesHigh);
                    bw.Write(TilesWide);
                    bw.Write(SpawnX);
                    bw.Write(SpawnY);
                    bw.Write(GroundLevel);
                    bw.Write(RockLevel);
                    bw.Write(Time);
                    bw.Write(DayTime);
                    bw.Write(MoonPhase);
                    bw.Write(BloodMoon);
                    bw.Write(DungeonX);
                    bw.Write(DungeonY);
                    bw.Write(DownedBoss1);
                    bw.Write(DownedBoss2);
                    bw.Write(DownedBoss3);
                    bw.Write(SavedGoblin);
                    bw.Write(SavedWizard);
                    bw.Write(SavedMech);
                    bw.Write(DownedGoblins);
                    bw.Write(DownedClown);
                    bw.Write(DownedFrost);
                    bw.Write(ShadowOrbSmashed);
                    bw.Write(SpawnMeteor);
                    bw.Write((byte)ShadowOrbCount);
                    bw.Write(AltarCount);
                    bw.Write(HardMode);
                    bw.Write(InvasionDelay);
                    bw.Write(InvasionSize);
                    bw.Write(InvasionType);
                    bw.Write(InvasionX);


                    for (int x = 0; x < TilesWide; ++x)
                    {
                        OnProgressChanged(this, new ProgressChangedEventArgs(x.ProgressPercentage(TilesWide), "Saving Tiles..."));

                        int rle = 0;
                        for (int y = 0; y < TilesHigh; y = y + rle + 1)
                        {
                            var curTile = Tiles[x, y];
                            bw.Write(curTile.IsActive);
                            if (curTile.IsActive)
                            {
                                bw.Write(curTile.Type);
                                if (TileProperties[curTile.Type].IsFramed)
                                {
                                    bw.Write(curTile.U);
                                    bw.Write(curTile.V);

                                    // TODO: Let Validate handle these
                                    //validate chest entry exists
                                    if (curTile.Type == 21)
                                    {
                                        if (GetChestAtTile(x, y) == null)
                                        {
                                            Chests.Add(new Chest(x, y));
                                        }
                                    }
                                    //validate sign entry exists
                                    else if (curTile.Type == 55 || curTile.Type == 85)
                                    {
                                        if (GetSignAtTile(x, y) == null)
                                        {
                                            Signs.Add(new Sign(x, y, string.Empty));
                                        }
                                    }
                                }
                            }
                            if ((int)curTile.Wall > 0)
                            {
                                bw.Write(true);
                                bw.Write(curTile.Wall);
                            }
                            else
                            {
                                bw.Write(false);
                            }

                            if ((int)curTile.Liquid > 0)
                            {
                                bw.Write(true);
                                bw.Write(curTile.Liquid);
                                bw.Write(curTile.IsLava);
                            }
                            else
                            {
                                bw.Write(false);
                            }

                            bw.Write(curTile.HasWire);

                            int rleTemp = 1;
                            while (y + rleTemp < TilesHigh && curTile.Equals(Tiles[x, (y + rleTemp)]))
                            {
                                ++rleTemp;
                            }
                            rle = rleTemp - 1;
                            bw.Write((short)rle);
                        }
                    }
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving Chests..."));
                    for (int i = 0; i < 1000; ++i)
                    {
                        if (i >= Chests.Count)
                        {
                            bw.Write(false);
                        }
                        else
                        {
                            Chest curChest = Chests[i];
                            bw.Write(true);
                            bw.Write(curChest.X);
                            bw.Write(curChest.Y);
                            for (int j = 0; j < Chest.MaxItems; ++j)
                            {
                                if (curChest.Items.Count > j)
                                {
                                    bw.Write((byte)curChest.Items[j].StackSize);
                                    if (curChest.Items[j].StackSize > 0)
                                    {
                                        bw.Write(curChest.Items[j].ItemName);
                                        bw.Write(curChest.Items[j].Prefix);
                                    }
                                }
                                else
                                {
                                    bw.Write((byte)0);
                                }
                            }
                        }
                    }
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving Signs..."));
                    for (int i = 0; i < 1000; ++i)
                    {
                        if (i >= Signs.Count || string.IsNullOrWhiteSpace(Signs[i].Text))
                        {
                            bw.Write(false);
                        }
                        else
                        {
                            var curSign = Signs[i];
                            bw.Write(true);
                            bw.Write(curSign.Text);
                            bw.Write(curSign.X);
                            bw.Write(curSign.Y);
                        }
                    }
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving NPC Data..."));
                    foreach (NPC curNpc in NPCs)
                    {
                        bw.Write(true);
                        bw.Write(curNpc.Name);
                        bw.Write(curNpc.Position.X);
                        bw.Write(curNpc.Position.Y);
                        bw.Write(curNpc.IsHomeless);
                        bw.Write(curNpc.Home.X);
                        bw.Write(curNpc.Home.Y);
                    }
                    bw.Write(false);

                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving NPC Names..."));
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 17).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 18).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 19).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 20).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 22).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 54).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 38).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 107).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 108).Name);
                    bw.Write(CharacterNames.FirstOrDefault(c => c.Id == 124).Name);

                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving Validation Data..."));
                    bw.Write(true);
                    bw.Write(Title);
                    bw.Write(WorldId);
                    bw.Close();
                    fs.Close();

                    // make a backup of current file if it exists
                    if (File.Exists(filename))
                    {
                        string backup = filename + ".TEdit";
                        File.Copy(filename, backup, true);
                    }
                    // replace actual file with temp save file
                    File.Copy(temp, filename, true);
                    // delete temp save file
                    File.Delete(temp);
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "World Save Complete."));
                }
            }
        }
        public static ClipboardBuffer Load3(string filename, bool frame19 = false)
        {
            bool failed = false;
            try
            {
                using (var stream = new FileStream(filename, FileMode.Open))
                {
                    using (var br = new BinaryReader(stream))
                    {
                        string name = br.ReadString();
                        int version = br.ReadInt32();

                        int sizeX = br.ReadInt32();
                        int sizeY = br.ReadInt32();
                        var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                        buffer.Name = name;

                        for (int x = 0; x < sizeX; x++)
                        {
                            for (int y = 0; y < sizeY; y++)
                            {
                                var curTile = new Tile();
                                curTile.IsActive = br.ReadBoolean();

                                if (curTile.IsActive)
                                {
                                    curTile.Type = br.ReadByte();
                                    if (curTile.Type == 19) // fix for platforms
                                    {

                                        curTile.U = 0;
                                        curTile.V = 0;
                                        if (frame19)
                                        {
                                            curTile.U = br.ReadInt16();
                                            curTile.V = br.ReadInt16();
                                        }
                                    }
                                    else if (World.TileProperties[curTile.Type].IsFramed)
                                    {
                                        curTile.U = br.ReadInt16();
                                        curTile.V = br.ReadInt16();

                                        if (curTile.Type == 144) //timer
                                            curTile.V = 0;
                                    }
                                    else
                                    {
                                        curTile.U = -1;
                                        curTile.V = -1;
                                    }
                                }

                                if (br.ReadBoolean())
                                    curTile.Wall = br.ReadByte();

                                if (br.ReadBoolean())
                                {
                                    curTile.Liquid = br.ReadByte();
                                    curTile.IsLava = br.ReadBoolean();
                                }

                                curTile.HasWire = br.ReadBoolean();
                                buffer.Tiles[x, y] = curTile;
                            }
                        }
                        for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                        {
                            if (br.ReadBoolean())
                            {
                                var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                                for (int j = 0; j < 20; ++j)
                                {
                                    curChest.Items[j].StackSize = br.ReadByte();

                                    if (curChest.Items[j].StackSize > 0)
                                    {
                                        if (version >= 3)
                                            curChest.Items[j].NetId = br.ReadInt32();
                                        else
                                            curChest.Items[j].SetFromName(br.ReadString());
                                        curChest.Items[j].Prefix = br.ReadByte();
                                    }
                                    else
                                    {
                                        curChest.Items[j].SetFromName("[empty]");
                                    }

                                }
                                buffer.Chests.Add(curChest);
                            }
                        }
                        for (int signIndex = 0; signIndex < 1000; signIndex++)
                        {
                            if (br.ReadBoolean())
                            {
                                string text = br.ReadString();
                                int x = br.ReadInt32();
                                int y = br.ReadInt32();
                                buffer.Signs.Add(new Sign(x, y, text));
                            }
                        }

                        if (buffer.Name != br.ReadString() || version != br.ReadInt32() || buffer.Size.X != br.ReadInt32() || buffer.Size.Y != br.ReadInt32())
                        {
                            if (!frame19)
                            {
                                br.Close();
                                return Load3(filename, true);
                            }
                            else
                                System.Windows.MessageBox.Show("Verification failed. Some schematic data may be missing.", "Legacy Schematic Version");
                        }

                        br.Close();
                        return buffer;
                    }
                }
            }
            catch (Exception)
            {
                failed = true;
            }

            if (failed && !frame19)
            {
                return Load3(filename, true);
            }

            return null;
        }
 public void OpenChest(Chest chest)
 {
     DataContext = chest.Items;
 }
        public static ClipboardBuffer Load4(string filename)
        {
            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var b = new BinaryReader(stream))
                {

                    string name = b.ReadString();
                    int version = b.ReadInt32();

                    int sizeX = b.ReadInt32();
                    int sizeY = b.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; ++x)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var tile = new Tile();

                            tile.IsActive = b.ReadBoolean();

                            TileProperty tileProperty = null;
                            if (tile.IsActive)
                            {
                                tile.Type = b.ReadByte();
                                tileProperty = World.TileProperties[tile.Type];

                                if (tile.Type == 127)
                                    tile.IsActive = false;

                                if (tileProperty.IsFramed)
                                {
                                    tile.U = b.ReadInt16();
                                    tile.V = b.ReadInt16();

                                    if (tile.Type == 144) //timer
                                        tile.V = 0;
                                }
                                else
                                {
                                    tile.U = -1;
                                    tile.V = -1;
                                }

                                if (b.ReadBoolean())
                                {
                                    tile.Color = b.ReadByte();
                                }
                            }

                            if (b.ReadBoolean())
                            {
                                tile.Wall = b.ReadByte();
                                if (b.ReadBoolean())
                                    tile.WallColor = b.ReadByte();
                            }

                            if (b.ReadBoolean())
                            {
                                tile.Liquid = b.ReadByte();
                                tile.IsLava = b.ReadBoolean();
                                tile.IsHoney = b.ReadBoolean();
                            }

                            tile.HasWire = b.ReadBoolean();
                            tile.HasWire2 = b.ReadBoolean();
                            tile.HasWire3 = b.ReadBoolean();
                            tile.HalfBrick = b.ReadBoolean();

                            if (tileProperty == null || !tileProperty.IsSolid)
                                tile.HalfBrick = false;

                            tile.Slope = b.ReadByte();
                            if (tileProperty == null || !tileProperty.IsSolid)
                                tile.Slope = 0;

                            tile.Actuator = b.ReadBoolean();
                            tile.InActive = b.ReadBoolean();

                            // read complete, start compression
                            buffer.Tiles[x, y] = tile;

                            int rle = b.ReadInt16();
                            if (rle < 0)
                                throw new ApplicationException("Invalid Tile Data!");

                            if (rle > 0)
                            {
                                for (int k = y + 1; k < y + rle + 1; k++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    buffer.Tiles[x, k] = tcopy;
                                }
                                y = y + rle;
                            }
                        }
                    }
                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                            for (int slot = 0; slot < Chest.MaxItems; slot++)
                            {
                                if (slot < Chest.MaxItems)
                                {
                                    int stackSize = (int)b.ReadInt16();
                                    chest.Items[slot].StackSize = stackSize;

                                    if (chest.Items[slot].StackSize > 0)
                                    {
                                        chest.Items[slot].NetId = b.ReadInt32();
                                        chest.Items[slot].StackSize = stackSize;
                                        chest.Items[slot].Prefix = b.ReadByte();
                                    }
                                }
                            }
                            buffer.Chests.Add(chest);
                        }
                    }

                    for (int i = 0; i < 1000; i++)
                    {
                        if (b.ReadBoolean())
                        {
                            Sign sign = new Sign();
                            sign.Text = b.ReadString();
                            sign.X = b.ReadInt32();
                            sign.Y = b.ReadInt32();

                            if (buffer.Tiles[sign.X, sign.Y].IsActive && (int)buffer.Tiles[sign.X, sign.Y].Type == 55 && (int)buffer.Tiles[sign.X, sign.Y].Type == 85)
                                buffer.Signs.Add(sign);
                        }
                    }

                    string verifyName = b.ReadString();
                    int verifyVersion = b.ReadInt32();
                    int verifyX = b.ReadInt32();
                    int verifyY = b.ReadInt32();
                    if (buffer.Name == verifyName &&
                        version == verifyVersion &&
                        buffer.Size.X == verifyX &&
                        buffer.Size.Y == verifyY)
                    {
                        // valid;
                        return buffer;
                    }
                    b.Close();
                    return null;
                }
            }
        }
        public static ClipboardBuffer Load(string filename)
        {
            string ext = Path.GetExtension(filename);
            if (string.Equals(ext, ".jpg", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".png", StringComparison.InvariantCultureIgnoreCase) || string.Equals(ext, ".bmp", StringComparison.InvariantCultureIgnoreCase))
                return LoadFromImage(filename);

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    string name = br.ReadString();
                    int version = br.ReadInt32();

                    if (name != Path.GetFileNameWithoutExtension(filename))
                    {
                        br.Close();
                        stream.Close();
                        return LoadOld(filename);
                    }

                    int sizeX = br.ReadInt32();
                    int sizeY = br.ReadInt32();
                    var buffer = new ClipboardBuffer(new Vector2Int32(sizeX, sizeY));
                    buffer.Name = name;

                    for (int x = 0; x < sizeX; x++)
                    {
                        for (int y = 0; y < sizeY; y++)
                        {
                            var curTile = new Tile();
                            curTile.IsActive = br.ReadBoolean();

                            if (curTile.IsActive)
                            {
                                curTile.Type = br.ReadByte();
                                if (World.TileProperties[curTile.Type].IsFramed)
                                {
                                    curTile.U = br.ReadInt16();
                                    curTile.V = br.ReadInt16();
                                }
                            }

                            if (br.ReadBoolean())
                                curTile.Wall = br.ReadByte();

                            if (br.ReadBoolean())
                            {
                                curTile.Liquid = br.ReadByte();
                                curTile.IsLava = br.ReadBoolean();
                            }

                            curTile.HasWire = br.ReadBoolean();
                            buffer.Tiles[x, y] = curTile;
                        }
                    }
                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                            for (int j = 0; j < Chest.MaxItems; ++j)
                            {
                                curChest.Items[j].StackSize = br.ReadByte();

                                if (curChest.Items[j].StackSize > 0)
                                {
                                    if (version >= 3)
                                        curChest.Items[j].NetId = br.ReadInt32();
                                    else
                                        curChest.Items[j].SetFromName(br.ReadString());
                                    curChest.Items[j].Prefix = br.ReadByte();
                                }
                                else
                                {
                                    curChest.Items[j].SetFromName("[empty]");
                                }

                            }
                            buffer.Chests.Add(curChest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            string text = br.ReadString();
                            int x = br.ReadInt32();
                            int y = br.ReadInt32();
                            buffer.Signs.Add(new Sign(x, y, text));
                        }
                    }

                    if (buffer.Name == br.ReadString() &&
                        version == br.ReadInt32() &&
                        buffer.Size.X == br.ReadInt32() &&
                        buffer.Size.Y == br.ReadInt32())
                    {
                        // valid;
                        return buffer;
                    }
                    br.Close();
                    return null;
                }
            }
        }
        public static UndoBuffer Read(string filename)
        {
            var buffer = new UndoBuffer();

            using (var stream = new FileStream(filename, FileMode.Open))
            {
                using (var br = new BinaryReader(stream))
                {
                    var tilecount = br.ReadInt32();
                    for (int i = 0; i < tilecount; i++)
                    {
                        int x = br.ReadInt32();
                        int y = br.ReadInt32();
                        var curTile = new Tile();
                        curTile.IsActive = br.ReadBoolean();

                        if (curTile.IsActive)
                        {
                            curTile.Type = br.ReadByte();
                            if (World.TileProperties[curTile.Type].IsFramed)
                            {
                                curTile.U = br.ReadInt16();
                                curTile.V = br.ReadInt16();
                            }
                        }

                        if (br.ReadBoolean())
                            curTile.Wall = br.ReadByte();

                        if (br.ReadBoolean())
                        {
                            curTile.Liquid = br.ReadByte();
                            curTile.IsLava = br.ReadBoolean();
                        }

                        curTile.HasWire = br.ReadBoolean();
                        buffer.Tiles.Add(new UndoTile(new Vector2Int32(x,y), curTile));
                    }

                    for (int chestIndex = 0; chestIndex < 1000; chestIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            var curChest = new Chest(br.ReadInt32(), br.ReadInt32());
                            for (int j = 0; j < Chest.MaxItems; ++j)
                            {
                                curChest.Items[j].StackSize = br.ReadByte();

                                if (curChest.Items[j].StackSize > 0)
                                {
                                    curChest.Items[j].NetId = br.ReadInt32();
                                    curChest.Items[j].Prefix = br.ReadByte();
                                }
                                else
                                {
                                    curChest.Items[j].NetId = 0;
                                }
                            }

                            buffer.Chests.Add(curChest);
                        }
                    }
                    for (int signIndex = 0; signIndex < 1000; signIndex++)
                    {
                        if (br.ReadBoolean())
                        {
                            string text = br.ReadString();
                            int x = br.ReadInt32();
                            int y = br.ReadInt32();
                            buffer.Signs.Add(new Sign(x, y, text));
                        }
                    }
                }
            }
            return buffer;
        }
Exemplo n.º 19
0
        public static World LoadWorld(string filename)
        {
            var w = new World();
            using (var b = new BinaryReader(File.OpenRead(filename)))
            {
                w.Version = b.ReadUInt32(); //now we care about the version
                w.Title = b.ReadString();

                w.WorldId = b.ReadInt32();
                w.LeftWorld = (float)b.ReadInt32();
                w.RightWorld = (float)b.ReadInt32();
                w.TopWorld = (float)b.ReadInt32();
                w.BottomWorld = (float)b.ReadInt32();

                w.TilesHigh = b.ReadInt32();
                w.TilesWide = b.ReadInt32();
                w.SpawnX = b.ReadInt32();
                w.SpawnY = b.ReadInt32();
                w.GroundLevel = (int)b.ReadDouble();
                w.RockLevel = (int)b.ReadDouble();

                // read world flags
                w.Time = b.ReadDouble();
                w.DayTime = b.ReadBoolean();
                w.MoonPhase = b.ReadInt32();
                w.BloodMoon = b.ReadBoolean();
                w.DungeonX = b.ReadInt32();
                w.DungeonY = b.ReadInt32();
                w.DownedBoss1 = b.ReadBoolean();
                w.DownedBoss2 = b.ReadBoolean();
                w.DownedBoss3 = b.ReadBoolean();
                if (w.Version >= 29)
                {
                    w.SavedGoblin = b.ReadBoolean();
                    w.SavedWizard = b.ReadBoolean();
                    if (w.Version >= 34)
                        w.SavedMech = b.ReadBoolean();
                    w.DownedGoblins = b.ReadBoolean();
                }
                if (w.Version >= 32)
                    w.DownedClown = b.ReadBoolean();
                if (w.Version >= 37)
                    w.DownedFrost = b.ReadBoolean();
                w.ShadowOrbSmashed = b.ReadBoolean();
                w.SpawnMeteor = b.ReadBoolean();
                w.ShadowOrbCount = (int)b.ReadByte();
                if (w.Version >= 23)
                {
                    w.AltarCount = b.ReadInt32();
                    w.HardMode = b.ReadBoolean();
                }
                w.InvasionDelay = b.ReadInt32();
                w.InvasionSize = b.ReadInt32();
                w.InvasionType = b.ReadInt32();
                w.InvasionX = b.ReadDouble();

                w.Tiles = new Tile[w.TilesWide, w.TilesHigh];
                for (int x = 0; x < w.TilesWide; ++x)
                {
                    OnProgressChanged(null, new ProgressChangedEventArgs(x.ProgressPercentage(w.TilesWide), "Loading Tiles..."));

                    Tile prevtype = new Tile();
                    for (int y = 0; y < w.TilesHigh; y++)
                    {

                        var tile = new Tile();

                        tile.IsActive = b.ReadBoolean();

                        if (tile.IsActive)
                        {
                            tile.Type = b.ReadByte();
                            if (tile.Type == (int)sbyte.MaxValue)
                                tile.IsActive = false;
                            if (TileProperties[tile.Type].IsFramed)
                            {
                                // torches didn't have extra in older versions.
                                if (w.Version < 28 && tile.Type == 4)
                                {
                                    tile.U = 0;
                                    tile.V = 0;
                                }
                                else
                                {
                                    tile.U = b.ReadInt16();
                                    tile.V = b.ReadInt16();
                                    //if (tile.Type == 128) //armor stand
                                    //    tile.Frame = new PointShort((short)(tile.Frame.X % 100), tile.Frame.Y);

                                    if (tile.Type == 144) //timer
                                        tile.V = 0;
                                }
                            }
                            else
                            {
                                tile.U = -1;
                                tile.V = -1;
                            }
                        }
                        if (w.Version <= 25)
                            b.ReadBoolean(); //skip obsolete hasLight
                        if (b.ReadBoolean())
                        {
                            tile.Wall = b.ReadByte();
                        }
                        //else
                        //    tile.Wall = 0;
                        if (b.ReadBoolean())
                        {
                            tile.Liquid = b.ReadByte();
                            tile.IsLava = b.ReadBoolean();
                        }

                        if (w.Version >= 33)
                            tile.HasWire = b.ReadBoolean();
                        //else
                        //    tile.HasWire = false;
                        w.Tiles[x, y] = tile;

                        var ptype = (Tile)prevtype.Clone();
                        prevtype = (Tile)tile.Clone();
                        if (w.Version >= 25) //compression ftw :)
                        {
                            int rle = b.ReadInt16();
                            if (rle > 0)
                            {
                                for (int r = y + 1; r < y + rle + 1; r++)
                                {
                                    var tcopy = (Tile)tile.Clone();
                                    w.Tiles[x, r] = tcopy;
                                }
                                y += rle;
                            }
                        }
                    }
                }
                w.Chests.Clear();
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests..."));
                for (int i = 0; i < 1000; i++)
                {
                    if (b.ReadBoolean())
                    {
                        var chest = new Chest(b.ReadInt32(), b.ReadInt32());
                        for (int slot = 0; slot < Chest.MaxItems; slot++)
                        {
                            chest.Items[slot].StackSize = b.ReadByte();
                            if (chest.Items[slot].StackSize > 0)
                            {
                                chest.Items[slot].ItemName = b.ReadString();

                                // Read prefix
                                if (w.Version >= 36)
                                    chest.Items[slot].Prefix = b.ReadByte();
                            }
                        }
                        w.Chests.Add(chest);
                    }
                }
                w.Signs.Clear();
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Signs..."));
                for (int i = 0; i < 1000; i++)
                {
                    if (b.ReadBoolean())
                    {
                        Sign sign = new Sign();
                        sign.Text = b.ReadString();
                        sign.X = b.ReadInt32();
                        sign.Y = b.ReadInt32();
                        w.Signs.Add(sign);
                    }
                }
                w.NPCs.Clear();
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Data..."));
                while (b.ReadBoolean())
                {
                    var npc = new NPC();
                    npc.Name = b.ReadString();
                    npc.Position = new Vector2(b.ReadSingle(), b.ReadSingle());
                    npc.IsHomeless = b.ReadBoolean();
                    npc.Home = new Vector2Int32(b.ReadInt32(), b.ReadInt32());
                    npc.SpriteId = 0;
                    if (NpcIds.ContainsKey(npc.Name))
                        npc.SpriteId = NpcIds[npc.Name];

                    w.NPCs.Add(npc);
                }
                // if (version>=0x1f) read the names of the following npcs:
                // merchant, nurse, arms dealer, dryad, guide, clothier, demolitionist,
                // tinkerer and wizard
                // if (version>=0x23) read the name of the mechanic

                if (w.Version >= 31)
                {
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Names..."));
                    w.CharacterNames.Add(new NpcName(17, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(18, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(19, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(20, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(22, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(54, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(38, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(107, b.ReadString()));
                    w.CharacterNames.Add(new NpcName(108, b.ReadString()));
                    if (w.Version >= 35)
                        w.CharacterNames.Add(new NpcName(124, b.ReadString()));
                    else
                    {
                        w.CharacterNames.Add(new NpcName(124, "Nancy"));
                    }
                }
                else
                {
                    w.CharacterNames.Add(new NpcName(17, "Harold"));
                    w.CharacterNames.Add(new NpcName(18, "Molly"));
                    w.CharacterNames.Add(new NpcName(19, "Dominique"));
                    w.CharacterNames.Add(new NpcName(20, "Felicitae"));
                    w.CharacterNames.Add(new NpcName(22, "Steve"));
                    w.CharacterNames.Add(new NpcName(54, "Fitz"));
                    w.CharacterNames.Add(new NpcName(38, "Gimut"));
                    w.CharacterNames.Add(new NpcName(107, "Knogs"));
                    w.CharacterNames.Add(new NpcName(108, "Fizban"));
                    w.CharacterNames.Add(new NpcName(124, "Nancy"));
                }
                if (w.Version >= 7)
                {
                    OnProgressChanged(null, new ProgressChangedEventArgs(100, "Validating File..."));
                    bool validation = b.ReadBoolean();
                    string checkTitle = b.ReadString();
                    int checkVersion = b.ReadInt32();
                    if (validation && checkTitle == w.Title && checkVersion == w.WorldId)
                    {
                        //w.loadSuccess = true;
                    }
                    else
                    {
                        b.Close();
                        throw new FileLoadException("Error reading world file validation parameters!");
                    }
                }
                OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Load Complete."));

            }
            return w;
        }