public static int SaveTiles(Tile[,] tiles, int maxX, int maxY, BinaryWriter bw) { for (int x = 0; x < maxX; x++) { OnProgressChanged(null, new ProgressChangedEventArgs(x.ProgressPercentage(maxX), "Saving Tiles...")); for (int y = 0; y < maxY; y++) { Tile tile = tiles[x, y]; int dataIndex; int headerIndex; byte[] tileData = SerializeTileData(tile, out dataIndex, out headerIndex); // rle compression byte header1 = tileData[headerIndex]; short rle = 0; int nextY = y + 1; int remainingY = maxY - y - 1; while (remainingY > 0 && tile.Equals(tiles[x, nextY])) { rle = (short)(rle + 1); remainingY--; nextY++; } y = y + rle; if (rle > 0) { tileData[dataIndex++] = (byte)(rle & 255); if (rle <= 255) { // set bit[6] of header1 for byte size rle header1 = (byte)(header1 | 64); } else { // set bit[7] of header1 for int16 size rle header1 = (byte)(header1 | 128); // grab the upper half of the int16 and stick it in tiledata tileData[dataIndex++] = (byte)((rle & 65280) >> 8); } } tileData[headerIndex] = header1; // end rle compression bw.Write(tileData, headerIndex, dataIndex - headerIndex); } } return((int)bw.BaseStream.Position); }
private static void SaveV1(World world, BinaryWriter bw) { bw.Write(CompatibleVersion); bw.Write(world.Title); bw.Write(world.WorldId); bw.Write((int)world.LeftWorld); bw.Write((int)world.RightWorld); bw.Write((int)world.TopWorld); bw.Write((int)world.BottomWorld); bw.Write(world.TilesHigh); bw.Write(world.TilesWide); bw.Write((byte)world.MoonType); bw.Write(world.TreeX[0]); bw.Write(world.TreeX[1]); bw.Write(world.TreeX[2]); bw.Write(world.TreeStyle[0]); bw.Write(world.TreeStyle[1]); bw.Write(world.TreeStyle[2]); bw.Write(world.TreeStyle[3]); bw.Write(world.CaveBackX[0]); bw.Write(world.CaveBackX[1]); bw.Write(world.CaveBackX[2]); bw.Write(world.CaveBackStyle[0]); bw.Write(world.CaveBackStyle[1]); bw.Write(world.CaveBackStyle[2]); bw.Write(world.CaveBackStyle[3]); bw.Write(world.IceBackStyle); bw.Write(world.JungleBackStyle); bw.Write(world.HellBackStyle); bw.Write(world.SpawnX); bw.Write(world.SpawnY); bw.Write(world.GroundLevel); bw.Write(world.RockLevel); bw.Write(world.Time); bw.Write(world.DayTime); bw.Write(world.MoonPhase); bw.Write(world.BloodMoon); bw.Write(world.IsEclipse); bw.Write(world.DungeonX); bw.Write(world.DungeonY); bw.Write(world.IsCrimson); bw.Write(world.DownedBoss1); bw.Write(world.DownedBoss2); bw.Write(world.DownedBoss3); bw.Write(world.DownedQueenBee); bw.Write(world.DownedMechBoss1); bw.Write(world.DownedMechBoss2); bw.Write(world.DownedMechBoss3); bw.Write(world.DownedMechBossAny); bw.Write(world.DownedPlantBoss); bw.Write(world.DownedGolemBoss); bw.Write(world.SavedGoblin); bw.Write(world.SavedWizard); bw.Write(world.SavedMech); bw.Write(world.DownedGoblins); bw.Write(world.DownedClown); bw.Write(world.DownedFrost); bw.Write(world.DownedPirates); bw.Write(world.ShadowOrbSmashed); bw.Write(world.SpawnMeteor); bw.Write((byte)world.ShadowOrbCount); bw.Write(world.AltarCount); bw.Write(world.HardMode); bw.Write(world.InvasionDelay); bw.Write(world.InvasionSize); bw.Write(world.InvasionType); bw.Write(world.InvasionX); bw.Write(world.TempRaining); bw.Write(world.TempRainTime); bw.Write(world.TempMaxRain); bw.Write(world.OreTier1); bw.Write(world.OreTier2); bw.Write(world.OreTier3); bw.Write(world.BgTree); bw.Write(world.BgCorruption); bw.Write(world.BgJungle); bw.Write(world.BgSnow); bw.Write(world.BgHallow); bw.Write(world.BgCrimson); bw.Write(world.BgDesert); bw.Write(world.BgOcean); bw.Write((int)world.CloudBgActive); bw.Write(world.NumClouds); bw.Write(world.WindSpeedSet); for (int x = 0; x < world.TilesWide; ++x) { OnProgressChanged(world, new ProgressChangedEventArgs(x.ProgressPercentage(world.TilesWide), "Saving UndoTiles...")); int rle = 0; for (int y = 0; y < world.TilesHigh; y = y + rle + 1) { Tile curTile = world.Tiles[x, y]; WriteTileDataToStreamV1(curTile, bw); int rleTemp = 1; while (y + rleTemp < world.TilesHigh && curTile.Equals(world.Tiles[x, (y + rleTemp)])) { ++rleTemp; } rle = rleTemp - 1; bw.Write((short)rle); } } OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving Chests...")); WriteChestDataToStreamV1(world.Chests, bw); OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving Signs...")); WriteSignDataToStreamV1(world.Signs, bw); OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving NPC Data...")); foreach (NPC curNpc in world.NPCs) { bw.Write(true); bw.Write(curNpc.Name); bw.Write(curNpc.Position.X); bw.Write(curNpc.Position.Y); bw.Write(curNpc.IsHomeless); bw.Write(curNpc.Home.X); bw.Write(curNpc.Home.Y); } bw.Write(false); OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving NPC Names...")); world.FixNpcs(); bw.Write(world.GetNpc(17).Name); bw.Write(world.GetNpc(18).Name); bw.Write(world.GetNpc(19).Name); bw.Write(world.GetNpc(20).Name); bw.Write(world.GetNpc(22).Name); bw.Write(world.GetNpc(54).Name); bw.Write(world.GetNpc(38).Name); bw.Write(world.GetNpc(107).Name); bw.Write(world.GetNpc(108).Name); bw.Write(world.GetNpc(124).Name); bw.Write(world.GetNpc(160).Name); bw.Write(world.GetNpc(178).Name); bw.Write(world.GetNpc(207).Name); bw.Write(world.GetNpc(208).Name); bw.Write(world.GetNpc(209).Name); bw.Write(world.GetNpc(227).Name); bw.Write(world.GetNpc(228).Name); bw.Write(world.GetNpc(229).Name); OnProgressChanged(null, new ProgressChangedEventArgs(100, "Saving Validation Data...")); bw.Write(true); bw.Write(world.Title); bw.Write(world.WorldId); }