Clone() public method

public Clone ( ) : object
return object
Esempio n. 1
0
        public static Tile[,] LoadTileData(BinaryReader r, int maxX, int maxY)
        {
            var tiles = new Tile[maxX, maxY];

            int rle;

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

                for (int y = 0; y < maxY; y++)
                {
                    Tile tile = DeserializeTileData(r, out rle);

                    tiles[x, y] = tile;
                    while (rle > 0)
                    {
                        y++;

                        if (y > maxY)
                        {
                            throw new FileFormatException(string.Format("Invalid Tile Data: RLE Compression outside of bounds [{0},{1}]", x, y));
                        }

                        tiles[x, y] = (Tile)tile.Clone();
                        rle--;
                    }
                }
            }

            return(tiles);
        }
        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;
                }
            }
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
        private static void LoadV1(BinaryReader reader, string filename, World w)
        {
            uint version = w.Version;

            w.Title   = reader.ReadString();
            w.WorldId = reader.ReadInt32();

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

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

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


            if (version >= 63)
            {
                w.MoonType = reader.ReadByte();
            }
            else
            {
                w.MoonType = w.Rand.Next(MaxMoons);
            }


            if (version >= 44)
            {
                w.TreeX[0]     = reader.ReadInt32();
                w.TreeX[1]     = reader.ReadInt32();
                w.TreeX[2]     = reader.ReadInt32();
                w.TreeStyle[0] = reader.ReadInt32();
                w.TreeStyle[1] = reader.ReadInt32();
                w.TreeStyle[2] = reader.ReadInt32();
                w.TreeStyle[3] = reader.ReadInt32();
            }
            if (version >= 60)
            {
                w.CaveBackX[0]     = reader.ReadInt32();
                w.CaveBackX[1]     = reader.ReadInt32();
                w.CaveBackX[2]     = reader.ReadInt32();
                w.CaveBackStyle[0] = reader.ReadInt32();
                w.CaveBackStyle[1] = reader.ReadInt32();
                w.CaveBackStyle[2] = reader.ReadInt32();
                w.CaveBackStyle[3] = reader.ReadInt32();
                w.IceBackStyle     = reader.ReadInt32();
                if (version >= 61)
                {
                    w.JungleBackStyle = reader.ReadInt32();
                    w.HellBackStyle   = reader.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      = reader.ReadInt32();
            w.SpawnY      = reader.ReadInt32();
            w.GroundLevel = (int)reader.ReadDouble();
            w.RockLevel   = (int)reader.ReadDouble();

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

            if (version >= 70)
            {
                w.IsEclipse = reader.ReadBoolean();
            }

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

            if (version >= 56)
            {
                w.IsCrimson = reader.ReadBoolean();
            }
            else
            {
                w.IsCrimson = false;
            }

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

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


            w.ShadowOrbSmashed = reader.ReadBoolean();
            w.SpawnMeteor      = reader.ReadBoolean();
            w.ShadowOrbCount   = reader.ReadByte();

            if (version >= 23)
            {
                w.AltarCount = reader.ReadInt32();
                w.HardMode   = reader.ReadBoolean();
            }

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

            if (version >= 53)
            {
                w.TempRaining  = reader.ReadBoolean();
                w.TempRainTime = reader.ReadInt32();
                w.TempMaxRain  = reader.ReadSingle();
            }
            if (version >= 54)
            {
                w.OreTier1 = reader.ReadInt32();
                w.OreTier2 = reader.ReadInt32();
                w.OreTier3 = reader.ReadInt32();
            }
            else if (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 (version >= 55)
            {
                w.BgTree       = reader.ReadByte();
                w.BgCorruption = reader.ReadByte();
                w.BgJungle     = reader.ReadByte();
            }
            if (version >= 60)
            {
                w.BgSnow       = reader.ReadByte();
                w.BgHallow     = reader.ReadByte();
                w.BgCorruption = reader.ReadByte();
                w.BgDesert     = reader.ReadByte();
                w.BgOcean      = reader.ReadByte();
            }

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

            if (version >= 62)
            {
                w.NumClouds    = reader.ReadInt16();
                w.WindSpeedSet = reader.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 UndoTiles..."));

                for (int y = 0; y < w.TilesHigh; y++)
                {
                    Tile tile = ReadTileDataFromStreamV1(reader, version);

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

                    if (version >= 25)
                    {
                        int rle = reader.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();
                                w.Tiles[x, k] = tcopy;
                            }
                            y = y + rle;
                        }
                    }
                }
            }

            if (version < 67)
            {
                w.FixSunflowers();
            }
            if (version < 72)
            {
                w.FixChand();
            }

            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests..."));
            w.Chests.Clear();
            ((ObservableCollection <Chest>)w.Chests).AddRange(ReadChestDataFromStreamV1(reader, version));

            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Signs..."));
            w.Signs.Clear();

            foreach (Sign sign in ReadSignDataFromStreamV1(reader))
            {
                if (w.Tiles[sign.X, sign.Y].IsActive &&
                    (w.Tiles[sign.X, sign.Y].Type == 55 || w.Tiles[sign.X, sign.Y].Type == 85))
                {
                    w.Signs.Add(sign);
                }
            }


            w.NPCs.Clear();
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Data..."));
            while (reader.ReadBoolean())
            {
                var npc = new NPC();
                npc.Name       = reader.ReadString();
                npc.Position   = new Vector2(reader.ReadSingle(), reader.ReadSingle());
                npc.IsHomeless = reader.ReadBoolean();
                npc.Home       = new Vector2Int32(reader.ReadInt32(), reader.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 (version >= 31)
            {
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPC Names..."));
                w.CharacterNames.Add(new NpcName(17, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(18, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(19, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(20, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(22, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(54, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(38, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(107, reader.ReadString()));
                w.CharacterNames.Add(new NpcName(108, reader.ReadString()));
                if (version >= 35)
                {
                    w.CharacterNames.Add(new NpcName(124, reader.ReadString()));
                }
                else
                {
                    w.CharacterNames.Add(new NpcName(124, "Nancy"));
                }

                if (version >= 65)
                {
                    w.CharacterNames.Add(new NpcName(160, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(178, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(207, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(208, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(209, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(227, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(228, reader.ReadString()));
                    w.CharacterNames.Add(new NpcName(229, reader.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 (version >= 7)
            {
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Validating File..."));
                bool   validation   = reader.ReadBoolean();
                string checkTitle   = reader.ReadString();
                int    checkVersion = reader.ReadInt32();
                if (validation && checkTitle == w.Title && checkVersion == w.WorldId)
                {
                    //w.loadSuccess = true;
                }
                else
                {
                    reader.Close();
                    throw new FileLoadException(
                              string.Format("Error reading world file validation parameters! {0}", filename));
                }
            }
            OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Load Complete."));
        }
Esempio 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.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);
        }
        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;
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public static bool loadWorldTiles(World w, int num, MemoryStream fileStream)
        {
            //Tile.activeArr = new byte[1000];
            //string savePath = Path.Combine(Main.worldTempPath, "world.tiles");
            //using (FileStream fileStream = new FileStream(savePath, FileMode.Open))
            //{
            // Main.tile.Init();
            using (BinaryReader b = new BinaryReader(fileStream))
            {
                try
                {
                    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();

                            //if (num >= 100) tile.Type = (ushort)b.ReadInt16();
                            //else
                            tile.Type = b.ReadByte();

                            if (tile.Type == 0) tile.IsActive = false;
                            else
                            {
                                tile.IsActive = true;

                                tile.Type = (byte)(tile.Type - 1);
                                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));
                                }
                                //tConfig code for handling custom tiles
                                /*
                                //newcode
                                if (Main.tile[j, k].type >= Main.maxTileSets)
                                {
                                    string name = Config.tileDefs.load[Main.tile[j, k].type];
                                    int ID = 0;
                                    if (!String.IsNullOrEmpty(name) && Config.tileDefs.ID.TryGetValue(name, out ID))
                                    {
                                        //Debug.WriteLine("Updating tile type:" + Main.tile[j, num5].type + " to " + Main.tileID[Main.tileLoad[Main.tile[j, num5].type]]);
                                        Main.tile[j, k].type = (ushort)ID;
                                    }
                                    else if (Main.tile[j, k].type >= Main.maxTileSets)// + Config.customTileAmt)// || String.IsNullOrWhiteSpace(Main.tileName[Main.tile[j, k].type]))
                                    {
                                        Debug.WriteLine("World Load Error: Tile " + Main.tile[j, k].type + " does not exist");
                                        //loadSuccess = false;
                                        //loadFailed = true;
                                        binaryReader.Close();
                                        fileStream.Close();
                                        string tilename = Main.tile[j, k].type.ToString();
                                        Config.tileDefs.load.TryGetValue(Main.tile[j, k].type, out tilename);
                                        string mess = "Tile '" + tilename + "' does not exist";
                                        string modname = "";
                                        if (Config.tileDefs.loadModname.TryGetValue(Main.tile[j, k].type, out modname))
                                            mess += "\nMod '" + modname + "' needs to be loaded";
                                        throw new Exception(mess);
                                        return false;
                                    }
                                }*/

                               // 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;
                                }
                            }

                            tile.Wall = b.ReadByte();

                            //tConfig code for handling custom walls
                            /*
                            if (Main.tile[j, k].wall >= Main.maxWallTypes)
                            {
                                string name = Config.wallDefs.load[Main.tile[j, k].wall];
                                int ID = 0;
                                if (!String.IsNullOrEmpty(name) && Config.wallDefs.ID.TryGetValue(name, out ID))
                                {
                                    Main.tile[j, k].wall = (byte)ID;
                                }
                                else if (Main.tile[j, k].wall >= Main.maxWallTypes + Config.customWallAmt || String.IsNullOrWhiteSpace(Config.wallDefs.name[Main.tile[j, k].wall]))
                                {
                                    Debug.WriteLine("World Load Error: Wall " + Main.tile[j, k].wall + " does not exist");
                                    //loadSuccess = false;
                                    //loadFailed = true;
                                    binaryReader.Close();
                                    fileStream.Close();

                                    string tilename = Main.tile[j, k].wall.ToString();
                                    Config.wallDefs.load.TryGetValue(Main.tile[j, k].wall, out tilename);
                                    string mess = "Wall " + tilename + " does not exist";
                                    string modname = "";
                                    if (Config.wallDefs.loadModname.TryGetValue(Main.tile[j, k].wall, out modname))
                                        mess += "\nMod " + modname + " needs to be loaded";
                                    throw new Exception(mess);
                                    return false;
                                }
                            }*/

                            tile.Liquid = b.ReadByte();
                            if (tile.Liquid > 0) tile.IsLava = b.ReadBoolean();

                            tile.HasWire = b.ReadBoolean();

                            w.Tiles[x, y] = tile;

                            var ptype = (Tile)prevtype.Clone();
                            prevtype = (Tile)tile.Clone();

                            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;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    b.Close();
                    fileStream.Close();
                    throw new Exception("Error loading tiles\n" + e.Message);
                }
                b.Close();
                fileStream.Close();
            }
            return true;
        }
Esempio n. 9
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;
        }