Validate() public méthode

public Validate ( ) : void
Résultat void
        public static void Save(World world, string filename, bool resetTime = false)
        {
            try
            {
                OnProgressChanged(world, new ProgressChangedEventArgs(0, "Validating World..."));
                world.Validate();
            }
            catch (ArgumentOutOfRangeException err)
            {
                string msg = string.Format("There is a problem in your world.\r\n" + 
                                           "{0}\r\nThis world will not open in Terraria\r\n" + 
                                           "Would you like to save anyways??\r\n"
                                           , err.ParamName);
                if (MessageBox.Show(msg, "World Error", MessageBoxButton.YesNo, MessageBoxImage.Error) !=
                    MessageBoxResult.Yes)
                    return;
            }
            lock (_fileLock)
            {
                

                if (resetTime)
                {
                    OnProgressChanged(world, new ProgressChangedEventArgs(0, "Resetting Time..."));
                    world.ResetTime();
                }

                if (filename == null)
                    return;

                string temp = filename + ".tmp";
                using (var fs = new FileStream(temp, FileMode.Create))
                {
                    using (var bw = new BinaryWriter(fs))
                    {
                        if (world.Version > 87)
                            SaveV2(world, bw);
                        else
                            SaveV1(world, bw);

                        bw.Close();
                        fs.Close();

                        // make a backup of current file if it exists
                        if (File.Exists(filename))
                        {
                            string backup = filename + ".TEdit";
                            File.Copy(filename, backup, true);
                        }
                        // replace actual file with temp save file
                        File.Copy(temp, filename, true);
                        // delete temp save file
                        File.Delete(temp);
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Save Complete."));
                    }
                }

                world._lastSave = File.GetLastWriteTimeUtc(filename);
            }
        }
Exemple #2
0
        public static void Save(World world, string filename, bool resetTime = false)
        {
            lock (_fileLock)
            {
                OnProgressChanged(world, new ProgressChangedEventArgs(0, "Validating World..."));
                world.Validate();

                if (resetTime)
                {
                    OnProgressChanged(world, new ProgressChangedEventArgs(0, "Resetting Time..."));
                    world.ResetTime();
                }

                if (filename == null)
                {
                    return;
                }

                string temp = filename + ".tmp";
                using (var fs = new FileStream(temp, FileMode.Create))
                {
                    using (var bw = new BinaryWriter(fs))
                    {
                        if (world.Version > 87)
                        {
                            SaveV2(world, bw);
                        }
                        else
                        {
                            SaveV1(world, bw);
                        }

                        bw.Close();
                        fs.Close();

                        // make a backup of current file if it exists
                        if (File.Exists(filename))
                        {
                            string backup = filename + ".TEdit";
                            File.Copy(filename, backup, true);
                        }
                        // replace actual file with temp save file
                        File.Copy(temp, filename, true);
                        // delete temp save file
                        File.Delete(temp);
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Save Complete."));
                    }
                }

                world._lastSave = File.GetLastWriteTimeUtc(filename);
            }
        }
Exemple #3
0
        private static void SaveV2(World world, BinaryWriter bw)
        {
            world.Validate();

            // initialize tileframeimportance array if empty
            if (TileFrameImportant == null || TileFrameImportant.Length <= 0)
            {
                TileFrameImportant = new bool[TileCount];
                for (int i = 0; i < TileCount; i++)
                {
                    if (World.TileProperties.Count > i)
                    {
                        TileFrameImportant[i] = World.TileProperties[i].IsFramed;
                    }
                }
            }

            int[] sectionPointers = new int[SectionCount];

            OnProgressChanged(null, new ProgressChangedEventArgs(0, "Save headers..."));
            sectionPointers[0] = SaveSectionHeader(world, bw);
            sectionPointers[1] = SaveHeaderFlags(world, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(0, "Save UndoTiles..."));
            sectionPointers[2] = SaveTiles(world.Tiles, world.TilesWide, world.TilesHigh, bw);

            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Chests..."));
            sectionPointers[3] = SaveChests(world.Chests, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Signs..."));
            sectionPointers[4] = SaveSigns(world.Signs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save NPCs..."));
            sectionPointers[5] = SaveNPCs(world.NPCs, bw);
            if (world.Version > 140)
            {
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Mobs..."));
                sectionPointers[5] = SaveMobs(world.Mobs, bw);
                OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Tile Entities Section..."));
                sectionPointers[6] = SaveTileEntities(world, bw);
            }
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Footers..."));
            SaveFooter(world, bw);
            if (world.Version > 140)
            {
                UpdateSectionPointersVer140(sectionPointers, bw);
            }
            else
            {
                UpdateSectionPointers(sectionPointers, bw);
            }
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Complete."));
        }
        private static void SaveV2(World world, BinaryWriter bw)
        {
            world.Validate();

            // initialize tileframeimportance array if empty
            if (TileFrameImportant == null || TileFrameImportant.Length < TileCount)
            {
                TileFrameImportant = new bool[TileCount];
                for (int i = 0; i < TileCount; i++)
                {
                    if (World.TileProperties.Count > i)
                    {
                        TileFrameImportant[i] = World.TileProperties[i].IsFramed;
                    }
                }
            }

            int[] sectionPointers = new int[SectionCount];

            OnProgressChanged(null, new ProgressChangedEventArgs(0, "保存标头...")); // todo: head
            sectionPointers[0] = SaveSectionHeader(world, bw);
            sectionPointers[1] = SaveHeaderFlags(world, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(0, "保存物块..."));
            sectionPointers[2] = SaveTiles(world.Tiles, world.TilesWide, world.TilesHigh, bw);

            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存宝箱..."));
            sectionPointers[3] = SaveChests(world.Chests, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存标牌..."));
            sectionPointers[4] = SaveSigns(world.Signs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存NPC..."));
            sectionPointers[5] = SaveNPCs(world.NPCs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存怪物..."));
            sectionPointers[5] = SaveMobs(world.Mobs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存物块单位..."));
            sectionPointers[6] = SaveTileEntities(world, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存压力板..."));
            sectionPointers[7] = SavePressurePlate(world.PressurePlates, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存末尾信息..."));
            SaveFooter(world, bw);
            UpdateSectionPointers(sectionPointers, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "保存完毕."));
        }
        private static void SaveV2(World world, BinaryWriter bw)
        {
            world.Validate();

            // initialize tileframeimportance array if empty
            if (TileFrameImportant == null || TileFrameImportant.Length < TileCount)
            {
                TileFrameImportant = new bool[TileCount];
                for (int i = 0; i < TileCount; i++)
                {
                    if (World.TileProperties.Count > i)
                    {
                        TileFrameImportant[i] = World.TileProperties[i].IsFramed;
                    }
                }
            }

            int[] sectionPointers = new int[SectionCount];

            OnProgressChanged(null, new ProgressChangedEventArgs(0, "Save headers..."));
            sectionPointers[0] = SaveSectionHeader(world, bw);
            sectionPointers[1] = SaveHeaderFlags(world, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(0, "Save UndoTiles..."));
            sectionPointers[2] = SaveTiles(world.Tiles, world.TilesWide, world.TilesHigh, bw);

            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Chests..."));
            sectionPointers[3] = SaveChests(world.Chests, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Signs..."));
            sectionPointers[4] = SaveSigns(world.Signs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save NPCs..."));
            sectionPointers[5] = SaveNPCs(world.NPCs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Mobs..."));
            sectionPointers[5] = SaveMobs(world.Mobs, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Tile Entities Section..."));
            sectionPointers[6] = SaveTileEntities(world, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Footers..."));
            SaveFooter(world, bw);
            UpdateSectionPointers(sectionPointers, bw);
            OnProgressChanged(null, new ProgressChangedEventArgs(100, "Save Complete."));
        }
        public static void Save(World world, string filename, bool resetTime = false)
        {
            lock (_fileLock)
            {
                OnProgressChanged(world, new ProgressChangedEventArgs(0, "Validating World..."));
                world.Validate();

                if (resetTime)
                {
                    OnProgressChanged(world, new ProgressChangedEventArgs(0, "Resetting Time..."));
                    world.ResetTime();
                }

                if (filename == null)
                    return;

                string temp = filename + ".tmp";
                using (var fs = new FileStream(temp, FileMode.Create))
                {
                    using (var bw = new BinaryWriter(fs))
                    {
                        if (CompatibleVersion > 87)
                            SaveV2(world, bw);
                        else
                            SaveV1(world, bw);

                        bw.Close();
                        fs.Close();

                        // make a backup of current file if it exists
                        if (File.Exists(filename))
                        {
                            string backup = filename + ".TEdit";
                            File.Copy(filename, backup, true);
                        }
                        // replace actual file with temp save file
                        File.Copy(temp, filename, true);
                        // delete temp save file
                        File.Delete(temp);
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Save Complete."));
                    }
                }

                world._lastSave = File.GetLastWriteTimeUtc(filename);
            }
        }
Exemple #7
0
        public static void Save(World world, string filename, bool resetTime = false)
        {
            try
            {
                OnProgressChanged(world, new ProgressChangedEventArgs(0, "Validating World..."));
                world.Validate();
            }
            catch (ArgumentOutOfRangeException err)
            {
                string msg = string.Format("There is a problem in your world.\r\n" +
                                           "{0}\r\nThis world will not open in Terraria\r\n" +
                                           "Would you like to save anyways??\r\n"
                                           , err.ParamName);
                if (MessageBox.Show(msg, "World Error", MessageBoxButton.YesNo, MessageBoxImage.Error) !=
                    MessageBoxResult.Yes)
                {
                    return;
                }
            }
            lock (_fileLock)
            {
                if (resetTime)
                {
                    OnProgressChanged(world, new ProgressChangedEventArgs(0, "Resetting Time..."));
                    world.ResetTime();
                }

                if (filename == null)
                {
                    return;
                }

                string temp = filename + ".tmp";
                using (var fs = new FileStream(temp, FileMode.Create))
                {
                    using (var bw = new BinaryWriter(fs))
                    {
                        if (world.Version > 87)
                        {
                            SaveV2(world, bw);
                        }
                        else
                        {
                            SaveV1(world, bw);
                        }

                        bw.Close();
                        fs.Close();

                        // make a backup of current file if it exists
                        if (File.Exists(filename))
                        {
                            string backup = filename + ".TEdit";
                            File.Copy(filename, backup, true);
                        }
                        // replace actual file with temp save file
                        File.Copy(temp, filename, true);
                        // delete temp save file
                        File.Delete(temp);
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "World Save Complete."));
                    }
                }

                world._lastSave = File.GetLastWriteTimeUtc(filename);
            }
        }
        private static void WriteAnalyzeWorld(StreamWriter sb, World world, bool fullAnalysis = false)
        {
            world.Validate();
            WriteHeader(sb, world);
            WriteFlags(sb, world);

            if (!fullAnalysis) return;

            sb.WriteLine("===SECTION: Tiles===");

            var tileCounts = new Dictionary<int, int>();

            int activeTiles = 0;
            for (int x = 0; x < world.TilesWide; x++)
            {
                for (int y = 0; y < world.TilesHigh; y++)
                {

                    var tile = world.Tiles[x, y];

                    if (tile.IsActive)
                    {
                        if (tileCounts.ContainsKey(tile.Type))
                        {
                            tileCounts[tile.Type] += 1;
                        }
                        else
                        {
                            tileCounts.Add(tile.Type, 1);
                        }
                        activeTiles++;
                    }

                }
            }

            float totalTiles = world.TilesWide * world.TilesHigh;
            int airTiles = (int)(totalTiles - activeTiles);
            sb.WriteLine("Air: {0} ({1:P2})", airTiles, airTiles / totalTiles);

            var tiles = tileCounts.OrderByDescending(kvp => kvp.Value);
            foreach (var tilePair in tiles)
            {
                string name = tilePair.Key.ToString();
                if (World.TileProperties.Count >= tilePair.Key)
                {
                    var prop = World.TileProperties[tilePair.Key];
                    if (prop != null)
                    {
                        name = prop.Name;
                    }
                }

                sb.WriteLine("{0}: {1} ({2:P2})", name, tilePair.Value, tilePair.Value / totalTiles);
            }

            sb.WriteLine("===SECTION: Chests===");
            sb.WriteProperty("Chest Count", world.Chests.Count);
            sb.WriteProperty("Chest Max Items", Chest.MaxItems);

            foreach (var chest in world.Chests)
            {
                sb.Write("[{0}, {1}] {2} - Contents: ", chest.X, chest.Y, chest.Name);

                for (int i = 0; i < Chest.MaxItems; i++)
                {
                    Item item = chest.Items[i];
                    if (item == null)
                        sb.Write("[{0}]: Empty,", i.ToString());
                    else
                    {
                        sb.Write("[{0}]: {1} - {2}{3},", i.ToString(), item.StackSize, item.PrefixName, item.Name);
                    }
                }

                sb.WriteLine();
            }

            sb.WriteLine("===SECTION: Signs===");
            sb.WriteProperty("Sign Count", world.Signs.Count);

            foreach (var sign in world.Signs)
            {
                sb.Write("[{0}, {1}] {2}\r\n", sign.X, sign.Y, sign.Text);
            }

            sb.WriteLine("===SECTION: NPCs===");
            sb.WriteProperty("NPC Count", world.NPCs.Count);
            foreach (var npc in world.NPCs)
            {
                sb.Write("ID: {0}, Name: {1}, Position: [{2:0.00}, {3:0.00}], {4}: [{5}, {6}]\r\n", npc.Name, npc.DisplayName, npc.Position.X, npc.Position.Y, npc.IsHomeless ? "Homeless" : "Home", npc.Home.X, npc.Home.Y);
            }

            sb.WriteLine("===SECTION: Tile Entities===");
            sb.WriteProperty("Tile Entities Count", world.TileEntities.Count);
            foreach (var entity in world.TileEntities)
            {
                switch (entity.Type)
                {
                    case 0:
                        sb.Write("Dummy - ID: {0}, Position: [{1}, {2}], NPC: {3}\r\n", entity.Id, entity.PosX, entity.PosY, entity.Npc);
                        break;
                    case 1:
                        sb.Write("ItemFrame - ID: {0}, Position: [{1}, {2}], ItemID: {3}, Prefix: {4}, Stack: {5}\r\n", entity.Id, entity.PosX, entity.PosY, entity.NetId, entity.Prefix, entity.StackSize);
                        break;
                    case 2:
                        sb.Write("Logic Sensor - ID: {0}, Position: [{1}, {2}], LogicCheck: {3}, On: {4}\r\n", entity.Id, entity.PosX, entity.PosY, entity.LogicCheck, entity.On);
                        break;
                }
            }
        }
        private static void WriteAnalyzeWorld(StreamWriter sb, World world, bool fullAnalysis = false)
        {
            world.Validate();
            WriteHeader(sb, world);
            WriteFlags(sb, world);

            if (!fullAnalysis)
            {
                return;
            }

            sb.WriteLine("===SECTION: Tiles===");

            var tileCounts = new Dictionary <int, int>();

            int activeTiles = 0;

            for (int x = 0; x < world.TilesWide; x++)
            {
                for (int y = 0; y < world.TilesHigh; y++)
                {
                    var tile = world.Tiles[x, y];

                    if (tile.IsActive)
                    {
                        if (tileCounts.ContainsKey(tile.Type))
                        {
                            tileCounts[tile.Type] += 1;
                        }
                        else
                        {
                            tileCounts.Add(tile.Type, 1);
                        }
                        activeTiles++;
                    }
                }
            }

            float totalTiles = world.TilesWide * world.TilesHigh;
            int   airTiles   = (int)(totalTiles - activeTiles);

            sb.WriteLine("Air: {0} ({1:P2})", airTiles, airTiles / totalTiles);


            var tiles = tileCounts.OrderByDescending(kvp => kvp.Value);

            foreach (var tilePair in tiles)
            {
                string name = tilePair.Key.ToString();
                if (World.TileProperties.Count >= tilePair.Key)
                {
                    var prop = World.TileProperties[tilePair.Key];
                    if (prop != null)
                    {
                        name = prop.Name;
                    }
                }

                sb.WriteLine("{0}: {1} ({2:P2})", name, tilePair.Value, tilePair.Value / totalTiles);
            }


            sb.WriteLine("===SECTION: Chests===");
            sb.WriteProperty("Chest Count", world.Chests.Count);
            sb.WriteProperty("Chest Max Items", Chest.MaxItems);

            foreach (var chest in world.Chests)
            {
                sb.Write("[{0}, {1}] {2} - Contents: ", chest.X, chest.Y, chest.Name);

                for (int i = 0; i < Chest.MaxItems; i++)
                {
                    Item item = chest.Items[i];
                    if (item == null)
                    {
                        sb.Write("[{0}]: Empty,", i.ToString());
                    }
                    else
                    {
                        sb.Write("[{0}]: {1} - {2}{3},", i.ToString(), item.StackSize, item.PrefixName, item.Name);
                    }
                }

                sb.WriteLine();
            }



            sb.WriteLine("===SECTION: Signs===");
            sb.WriteProperty("Sign Count", world.Signs.Count);

            foreach (var sign in world.Signs)
            {
                sb.Write("[{0}, {1}] {2}\r\n", sign.X, sign.Y, sign.Text);
            }

            sb.WriteLine("===SECTION: NPCs===");
            sb.WriteProperty("NPC Count", world.NPCs.Count);
            foreach (var npc in world.NPCs)
            {
                sb.Write("ID: {0}, Name: {1}, Position: [{2:0.00}, {3:0.00}], {4}: [{5}, {6}]\r\n", npc.Name, npc.DisplayName, npc.Position.X, npc.Position.Y, npc.IsHomeless ? "Homeless" : "Home", npc.Home.X, npc.Home.Y);
            }

            sb.WriteLine("===SECTION: Tile Entities===");
            sb.WriteProperty("Tile Entities Count", world.TileEntities.Count);
            foreach (var entity in world.TileEntities)
            {
                switch (entity.Type)
                {
                case 0:
                    sb.Write("Dummy - ID: {0}, Position: [{1}, {2}], NPC: {3}\r\n", entity.Id, entity.PosX, entity.PosY, entity.Npc);
                    break;

                case 1:
                    sb.Write("ItemFrame - ID: {0}, Position: [{1}, {2}], ItemID: {3}, Prefix: {4}, Stack: {5}\r\n", entity.Id, entity.PosX, entity.PosY, entity.NetId, entity.Prefix, entity.StackSize);
                    break;

                case 2:
                    sb.Write("Logic Sensor - ID: {0}, Position: [{1}, {2}], LogicCheck: {3}, On: {4}\r\n", entity.Id, entity.PosX, entity.PosY, entity.LogicCheck, entity.On);
                    break;
                }
            }
        }
        public static void Save(World world, string filename, bool resetTime = false)
        {
            try
            {
                OnProgressChanged(world, new ProgressChangedEventArgs(0, "验证世界有效性..."));
                world.Validate();
            }
            catch (ArgumentOutOfRangeException err)
            {
                string msg = string.Format("地图文件有问题.\r\n" +
                                           "{0}\r\n它不能在游戏内打开\r\n" +
                                           "无论如何, 你还要保存它么??\r\n"
                                           , err.ParamName);
                if (MessageBox.Show(msg, "地图文件错误", MessageBoxButton.YesNo, MessageBoxImage.Error) !=
                    MessageBoxResult.Yes)
                {
                    return;
                }
            }
            lock (_fileLock)
            {
                if (resetTime)
                {
                    OnProgressChanged(world, new ProgressChangedEventArgs(0, "重置时间..."));
                    world.ResetTime();
                }

                if (filename == null)
                {
                    return;
                }

                string temp = filename + ".tmp";
                using (var fs = new FileStream(temp, FileMode.Create))
                {
                    using (var bw = new BinaryWriter(fs))
                    {
                        if (world.Version > 87)
                        {
                            SaveV2(world, bw);
                        }
                        else
                        {
                            SaveV1(world, bw);
                        }

                        bw.Close();
                        fs.Close();

                        // make a backup of current file if it exists
                        if (File.Exists(filename))
                        {
                            string backup = filename + "." + DateTime.Now.ToString("yyyyMMddHHmmss") + ".TEdit";
                            File.Copy(filename, backup, true);
                        }
                        // replace actual file with temp save file
                        File.Copy(temp, filename, true);
                        // delete temp save file
                        File.Delete(temp);
                        OnProgressChanged(null, new ProgressChangedEventArgs(0, "世界保存完毕."));
                    }
                }

                world._lastSave = File.GetLastWriteTimeUtc(filename);
            }
        }