public static void LoadV2(BinaryReader b, string filename, World w) { //throw new NotImplementedException("World Version > 87"); bool[] tileFrameImportant; int[] sectionPointers; // reset the stream b.BaseStream.Position = (long)0; OnProgressChanged(null, new ProgressChangedEventArgs(0, "Loading File Header...")); // read section pointers and tile frame data if (!LoadSectionHeader(b, out tileFrameImportant, out sectionPointers, w)) { throw new FileFormatException("Invalid File Format Section"); } TileFrameImportant = tileFrameImportant; // we should be at the end of the first section if (b.BaseStream.Position != sectionPointers[0]) { throw new FileFormatException("Unexpected Position: Invalid File Format Section"); } // Load the flags LoadHeaderFlags(b, w, sectionPointers[1]); if (b.BaseStream.Position != sectionPointers[1]) { throw new FileFormatException("Unexpected Position: Invalid Header Flags"); } OnProgressChanged(null, new ProgressChangedEventArgs(0, "Loading UndoTiles...")); w.Tiles = LoadTileData(b, w.TilesWide, w.TilesHigh); if (b.BaseStream.Position != sectionPointers[2]) { throw new FileFormatException("Unexpected Position: Invalid Tile Data"); } OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Chests...")); foreach (Chest chest in LoadChestData(b)) { //Tile tile = w.Tiles[chest.X, chest.Y]; //if (tile.IsActive && (tile.Type == 55 || tile.Type == 85)) { w.Chests.Add(chest); } } if (b.BaseStream.Position != sectionPointers[3]) { throw new FileFormatException("Unexpected Position: Invalid Chest Data"); } OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Signs...")); foreach (Sign sign in LoadSignData(b)) { Tile tile = w.Tiles[sign.X, sign.Y]; if (tile.IsActive && Tile.IsSign(tile.Type)) { w.Signs.Add(sign); } } if (b.BaseStream.Position != sectionPointers[4]) { throw new FileFormatException("Unexpected Position: Invalid Sign Data"); } OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading NPCs...")); LoadNPCsData(b, w); if (w.Version >= 140) { OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Mobs...")); LoadMobsData(b, w); if (b.BaseStream.Position != sectionPointers[5]) { throw new FileFormatException("Unexpected Position: Invalid Mob and NPC Data"); } OnProgressChanged(null, new ProgressChangedEventArgs(100, "Loading Tile Entities Section...")); LoadTileEntities(b, w); if (b.BaseStream.Position != sectionPointers[6]) { throw new FileFormatException("Unexpected Position: Invalid Tile Entities Section"); } } else { if (b.BaseStream.Position != sectionPointers[5]) { throw new FileFormatException("Unexpected Position: Invalid NPC Data"); } } OnProgressChanged(null, new ProgressChangedEventArgs(100, "Verifying File...")); LoadFooter(b, w); OnProgressChanged(null, new ProgressChangedEventArgs(100, "Load Complete.")); }
public void Validate() { for (int x = 0; x < TilesWide; x++) { OnProgressChanged(this, new ProgressChangedEventArgs((int)(x / (float)TilesWide * 100.0), "Validating World...")); for (int y = 0; y < TilesHigh; y++) { Tile curTile = Tiles[x, y]; if (curTile.Type == (int)TileType.IceByRod) { curTile.IsActive = false; } ValSpecial(x, y); } } foreach (Chest chest in Chests.ToArray()) { bool removed = false; for (int x = chest.X; x < chest.X + 1; x++) { for (int y = chest.Y; y < chest.Y + 1; y++) { if (!Tiles[x, y].IsActive || !Tile.IsChest(Tiles[x, y].Type)) { Chests.Remove(chest); removed = true; break; } } if (removed) { break; } } } foreach (Sign sign in Signs.ToArray()) { if (sign.Text == null) { Signs.Remove(sign); continue; } bool removed = false; for (int x = sign.X; x < sign.X + 1; x++) { for (int y = sign.Y; y < sign.Y + 1; y++) { if (!Tiles[x, y].IsActive || !Tile.IsSign(Tiles[x, y].Type)) { Signs.Remove(sign); removed = true; break; } } if (removed) { break; } } } foreach (TileEntity tileEntity in TileEntities.ToArray()) { int x = tileEntity.PosX; int y = tileEntity.PosY; if (!Tiles[x, y].IsActive || !Tile.IsTileEntity(Tiles[x, y].Type)) { TileEntities.Remove(tileEntity); } } OnProgressChanged(this, new ProgressChangedEventArgs(0, "Validating Complete...")); if (Chests.Count > 1000) { throw new ArgumentOutOfRangeException($"Chest Count is {Chests.Count} which is greater than 1000"); } if (Signs.Count > 1000) { throw new ArgumentOutOfRangeException($"Sign Count is {Signs.Count} which is greater than 1000"); } }
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 = (byte)w.Rand.Next(MaxMoons); } if (version >= 44) { w.TreeX0 = reader.ReadInt32(); w.TreeX1 = reader.ReadInt32(); w.TreeX2 = reader.ReadInt32(); w.TreeStyle0 = reader.ReadInt32(); w.TreeStyle1 = reader.ReadInt32(); w.TreeStyle2 = reader.ReadInt32(); w.TreeStyle3 = reader.ReadInt32(); } if (version >= 60) { w.CaveBackX0 = reader.ReadInt32(); w.CaveBackX1 = reader.ReadInt32(); w.CaveBackX2 = reader.ReadInt32(); w.CaveBackStyle0 = reader.ReadInt32(); w.CaveBackStyle1 = reader.ReadInt32(); w.CaveBackStyle2 = reader.ReadInt32(); w.CaveBackStyle3 = 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.CaveBackStyle0 = 0; w.CaveBackStyle1 = 1; w.CaveBackStyle2 = 2; w.CaveBackStyle3 = 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 && Tile.IsSign(w.Tiles[sign.X, sign.Y].Type)) { 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( $"Error reading world file validation parameters! {filename}"); } } OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Load Complete.")); }
public void Validate() { for (int x = 0; x < TilesWide; x++) { OnProgressChanged(this, new ProgressChangedEventArgs((int)(x / (float)TilesWide * 100.0), "Validating World...")); for (int y = 0; y < TilesHigh; y++) { Tile curTile = Tiles[x, y]; if (curTile.Type == (int)TileType.IceByRod) { curTile.IsActive = false; } // TODO: Let Validate handle these //validate chest entry exists if (Tile.IsChest(curTile.Type)) { if (GetChestAtTile(x, y) == null) { Chests.Add(new Chest(x, y)); } } //validate sign entry exists else if (Tile.IsSign(curTile.Type)) { if (GetSignAtTile(x, y) == null) { Signs.Add(new Sign(x, y, string.Empty)); } } //validate logic sensors else if (curTile.Type == 423) { if (GetTileEntityAtTile(x, y) == null) { TileEntity TE = new TileEntity(); TE.Type = 2; TE.PosX = (short)x; TE.PosY = (short)y; TE.On = false; TE.LogicCheck = (byte)(curTile.V / 18 + 1); TE.Id = TileEntities.Count; TileEntities.Add(TE); } } } } foreach (Chest chest in Chests.ToArray()) { bool removed = false; for (int x = chest.X; x < chest.X + 1; x++) { for (int y = chest.Y; y < chest.Y + 1; y++) { if (!Tiles[x, y].IsActive || !Tile.IsChest(Tiles[x, y].Type)) { Chests.Remove(chest); removed = true; break; } } if (removed) { break; } } } foreach (Sign sign in Signs.ToArray()) { if (sign.Text == null) { Signs.Remove(sign); continue; } bool removed = false; for (int x = sign.X; x < sign.X + 1; x++) { for (int y = sign.Y; y < sign.Y + 1; y++) { if (!Tiles[x, y].IsActive || !Tile.IsSign(Tiles[x, y].Type)) { Signs.Remove(sign); removed = true; break; } } if (removed) { break; } } } OnProgressChanged(this, new ProgressChangedEventArgs(0, "Validating Complete...")); if (Chests.Count > 1000) { throw new ArgumentOutOfRangeException(string.Format("Chest Count is {0} which is greater than 1000", Chests.Count)); } if (Signs.Count > 1000) { throw new ArgumentOutOfRangeException(string.Format("Sign Count is {0} which is greater than 1000", Signs.Count)); } }