예제 #1
0
        /// <summary>
        /// Saves a <see cref="Level"/> object to disk as a standard compressed NBT stream.
        /// </summary>
        /// <returns>True if the level was saved; false otherwise.</returns>
        /// <exception cref="LevelIOException">Thrown when an error is encountered writing out the level.</exception>
        public bool Save()
        {
            if (_world == null)
            {
                return(false);
            }

            try {
                NBTFile nf = new NBTFile(Path.Combine(_world.Path, "level.dat"));
                using (Stream zipstr = nf.GetDataOutputStream())
                {
                    if (zipstr == null)
                    {
                        NbtIOException nex = new NbtIOException("Failed to initialize compressed NBT stream for output");
                        nex.Data["Level"] = this;
                        throw nex;
                    }

                    new NbtTree(BuildTree() as TagNodeCompound).WriteTo(zipstr);
                }

                return(true);
            }
            catch (Exception ex) {
                LevelIOException lex = new LevelIOException("Could not save level file.", ex);
                lex.Data["Level"] = this;
                throw lex;
            }
        }
예제 #2
0
        public override bool Save()
        {
            if (_world == null)
            {
                return(false);
            }

            try {
                string  path = Path.Combine(_world.Path, _world.DataDirectory);
                NBTFile nf   = new NBTFile(Path.Combine(path, "idcounts.dat"));

                using (Stream zipstr = nf.GetDataOutputStream(CompressionType.None))
                {
                    if (zipstr == null)
                    {
                        NbtIOException nex = new NbtIOException("Failed to initialize uncompressed NBT stream for output");
                        nex.Data["DataManager"] = this;
                        throw nex;
                    }

                    new NbtTree(BuildTree() as TagNodeCompound).WriteTo(zipstr);
                }

                return(true);
            }
            catch (Exception ex) {
                Exception lex = new Exception("Could not save idcounts.dat file.", ex);
                lex.Data["DataManager"] = this;
                throw lex;
            }
        }
예제 #3
0
파일: Map.cs 프로젝트: whatupdave/Substrate
        /// <summary>
        /// Saves a <see cref="Map"/> object to disk as a standard compressed NBT stream.
        /// </summary>
        /// <returns>True if the map was saved; false otherwise.</returns>
        /// <exception cref="Exception">Thrown when an error is encountered writing out the level.</exception>
        public bool Save()
        {
            if (_world == null)
            {
                return(false);
            }

            try {
                string  path = Path.Combine(_world.Path, _world.DataDirectory);
                NBTFile nf   = new NBTFile(Path.Combine(path, "map_" + _id + ".dat"));

                Stream zipstr = nf.GetDataOutputStream();
                if (zipstr == null)
                {
                    NbtIOException nex = new NbtIOException("Failed to initialize compressed NBT stream for output");
                    nex.Data["Map"] = this;
                    throw nex;
                }

                new NbtTree(BuildTree() as TagNodeCompound).WriteTo(zipstr);
                zipstr.Close();

                return(true);
            }
            catch (Exception ex) {
                Exception mex = new Exception("Could not save map file.", ex); // TODO: Exception Type
                mex.Data["Map"] = this;
                throw mex;
            }
        }
예제 #4
0
 protected override void SaveCore()
 {
     var file = new NBTFile(_path);
     using (var str = file.GetDataOutputStream(_compressionType))
     {
         _tree.WriteTo(str);
     }
 }
예제 #5
0
        /// <summary>
        /// Exports the <see cref="Schematic"/> object to a schematic file.
        /// </summary>
        /// <param name="path">The path to write out the schematic file to.</param>
        public void Export(string path)
        {
            int xdim = _blockset.XDim;
            int ydim = _blockset.YDim;
            int zdim = _blockset.ZDim;

            byte[] blockData = new byte[xdim * ydim * zdim];
            byte[] dataData  = new byte[xdim * ydim * zdim];

            YZXByteArray schemaBlocks = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, blockData);
            YZXByteArray schemaData   = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, dataData);

            TagNodeList entities     = new TagNodeList(TagType.TAG_COMPOUND);
            TagNodeList tileEntities = new TagNodeList(TagType.TAG_COMPOUND);

            for (int x = 0; x < xdim; x++)
            {
                for (int z = 0; z < zdim; z++)
                {
                    for (int y = 0; y < ydim; y++)
                    {
                        AlphaBlock block = _blockset.GetBlock(x, y, z);
                        schemaBlocks[x, y, z] = (byte)block.ID;
                        schemaData[x, y, z]   = (byte)block.Data;

                        TileEntity te = block.GetTileEntity();
                        if (te != null)
                        {
                            te.X = x;
                            te.Y = y;
                            te.Z = z;

                            tileEntities.Add(te.BuildTree());
                        }
                    }
                }
            }

            foreach (EntityTyped e in _entityset)
            {
                entities.Add(e.BuildTree());
            }

            TagNodeCompound schematic = new TagNodeCompound();

            schematic["Width"]  = new TagNodeShort((short)xdim);
            schematic["Length"] = new TagNodeShort((short)zdim);
            schematic["Height"] = new TagNodeShort((short)ydim);

            schematic["Entities"]     = entities;
            schematic["TileEntities"] = tileEntities;

            schematic["Materials"] = new TagNodeString("Alpha");

            schematic["Blocks"] = new TagNodeByteArray(blockData);
            schematic["Data"]   = new TagNodeByteArray(dataData);

            NBTFile schematicFile = new NBTFile(path);

            Stream nbtStream = schematicFile.GetDataOutputStream();

            if (nbtStream == null)
            {
                return;
            }

            NbtTree tree = new NbtTree(schematic, "Schematic");

            tree.WriteTo(nbtStream);

            nbtStream.Close();
        }
        private static void RunOptionsAndReturnExitCode(CliOptions opts)
        {
            Console.Clear();
            var cgui = new ConsoleGui();

            var pbBlocks = new ConsoleGuiProgressBar(0, 0, Console.WindowWidth, 0, 1)
            {
                ForegroundColor = ConsoleColor.Green,
                BackgroundColor = ConsoleColor.DarkGray
            };

            var lBlocksTotal     = new ConsoleGuiLabel(0, 1, "Total Blocks    : {0}");
            var lBlocksRemaining = new ConsoleGuiLabel(0, 2, "Remaining Blocks: {0}");
            var lBlocksFailed    = new ConsoleGuiLabel(0, 3, "Failed Blocks   : {0}");
            var lStatus          = new ConsoleGuiLabel(0, 4, "Status          : {0}");

            cgui.Add(pbBlocks);
            cgui.Add(lBlocksTotal);
            cgui.Add(lBlocksRemaining);
            cgui.Add(lBlocksFailed);
            cgui.Add(lStatus);

            var failed = new List <int>();

            var inputMap  = NbtMap.Load(opts.InputMap);
            var outputMap = NbtMap.Load(opts.OutputMap);

            var inputSchematic = new NBTFile(opts.InputSchematic);
            var tag            = new NbtTree(inputSchematic.GetDataInputStream()).Root;

            var bLower    = tag["Blocks"].ToTagByteArray().Data;
            var addBlocks = new byte[(bLower.Length >> 1) + 1];

            if (tag.ContainsKey("AddBlocks"))
            {
                addBlocks = tag["AddBlocks"].ToTagByteArray().Data;
            }

            lStatus.Value = "Processing...";

            for (var index = 0; index < bLower.Length; index++)
            {
                short oldId;
                if ((index & 1) == 1)
                {
                    oldId = (short)(((addBlocks[index >> 1] & 0x0F) << 8) + (bLower[index] & 0xFF));
                }
                else
                {
                    oldId = (short)(((addBlocks[index >> 1] & 0xF0) << 4) + (bLower[index] & 0xFF));
                }

                if (!TranslateId(oldId, inputMap, outputMap, out var newId))
                {
                    failed.Add(oldId);
                }

                bLower[index]         = (byte)(newId & 0xFF);
                addBlocks[index >> 1] = (byte)(((index & 1) == 1) ?
                                               addBlocks[index >> 1] & 0xF0 | (newId >> 8) & 0xF
                    : addBlocks[index >> 1] & 0xF | ((newId >> 8) & 0xF) << 4);

                // Gui
                lBlocksTotal.Value     = index + 1;
                lBlocksRemaining.Value = bLower.Length - index - 1;
                lBlocksFailed.Value    = failed.Count;
                pbBlocks.Value         = (float)index / bLower.Length;

                if (index % 10000 == 0)
                {
                    cgui.Render();
                }
            }

            cgui.Render();

            tag["Blocks"] = new TagNodeByteArray(bLower);

            if (!tag.ContainsKey("AddBlocks"))
            {
                tag.Add("AddBlocks", new TagNodeByteArray(addBlocks));
            }
            else
            {
                tag["AddBlocks"] = new TagNodeByteArray(addBlocks);
            }

            lStatus.Value = "Saving...";
            cgui.Render();

            var schematicFile = new NBTFile(opts.OutputSchematic);

            using (var nbtStream = schematicFile.GetDataOutputStream())
            {
                if (nbtStream == null)
                {
                    return;
                }

                var tree = new NbtTree(tag, "Schematic");
                tree.WriteTo(nbtStream);
            }

            lStatus.Value = "Done. Press Enter.";
            cgui.Render();

            Console.WriteLine();
            foreach (var i in failed)
            {
                Console.WriteLine($"Failed ID: {i}");
            }

            Console.ReadKey();
        }