protected override void ExpandCore()
        {
            try {
                if (_region == null)
                {
                    _region = new RegionFile(_path);
                }

                for (int x = 0; x < 32; x++)
                {
                    for (int z = 0; z < 32; z++)
                    {
                        if (_region.HasChunk(x, z))
                        {
                            Nodes.Add(new RegionChunkDataNode(_region, x, z));
                        }
                    }
                }
            }
            catch {
                if (FormRegistry.MessageBox != null)
                {
                    FormRegistry.MessageBox("Not a valid region file.");
                }
            }
        }
Example #2
0
        /// <summary>
        /// Deletes a chunk from the underlying data store at the given local coordinates relative to this region.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
        /// <returns>True if there is a chunk was deleted; false otherwise.</returns>
        /// <remarks>If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region
        /// transparently.</remarks>
        public bool DeleteChunk(int lcx, int lcz)
        {
            if (!LocalBoundsCheck(lcx, lcz))
            {
                Region alt = GetForeignRegion(lcx, lcz);
                return((alt == null) ? false : alt.DeleteChunk(ForeignX(lcx), ForeignZ(lcz)));
            }

            RegionFile rf = GetRegionFile();

            if (!rf.HasChunk(lcx, lcz))
            {
                return(false);
            }

            rf.DeleteChunk(lcx, lcz);

            ChunkKey k = new ChunkKey(lcx, lcz);

            _cache.Remove(k);

            if (ChunkCount() == 0)
            {
                _regionMan.DeleteRegion(X, Z);
            }

            return(true);
        }
 public RegionChunkDataNode(RegionFile regionFile, int x, int z)
 {
     _regionFile = regionFile;
     _x          = x;
     _z          = z;
     _container  = new CompoundTagContainer(new TagNodeCompound());
 }
 protected override void ReleaseCore()
 {
     if (_region != null)
     {
         _region.Close();
     }
     _region = null;
     Nodes.Clear();
 }
Example #5
0
        internal void SaveChunk(AnvilChunk chunk)
        {
            RegionFile  f        = FetchRegion(chunk.Coord.RegionCoord);
            TagCompound chunkTag = chunk.BuildTag();

            using (Stream stream = f.WriteChunk(new ChunkCoord(chunk.Coord.X & 31, chunk.Coord.Z & 31)))
            {
                NBTFile.ToStream(stream, chunkTag);
            }
        }
Example #6
0
        public bool Contains(ChunkCoord coord)
        {
            RegionFile rf = FetchRegion(coord.RegionCoord);

            if (rf == null)
            {
                return(false);
            }
            return(rf.ContainsChunk(new ChunkCoord(coord.X & 31, coord.Z & 31)));
        }
Example #7
0
        private RegionFile GetRegionFile ()
        {
            RegionFile rf = _regionFile.Target as RegionFile;
            if (rf == null) {
                rf = new RegionFile(GetFilePath());
                _regionFile.Target = rf;
            }

            return rf;
        }
Example #8
0
        /// <summary>
        /// Gets an output stream for replacing chunk data at the given coordinates within the region.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of the chunk to replace within the region.</param>
        /// <param name="lcz">The local Z-coordinate of the chunk to replace within the region.</param>
        /// <returns>An output stream that can be written to on demand.</returns>
        /// <remarks>There is no guarantee that any data will be saved until the stream is closed.</remarks>
        public Stream GetChunkOutStream (int lcx, int lcz)
        {
            if (!LocalBoundsCheck(lcx, lcz)) {
                Region alt = GetForeignRegion(lcx, lcz);
                return (alt == null) ? null : alt.GetChunkOutStream(ForeignX(lcx), ForeignZ(lcz));
            }

            RegionFile rf = GetRegionFile();
            return rf.GetChunkDataOutputStream(lcx, lcz);
        }
Example #9
0
        /// <summary>
        /// Checks if a chunk exists at the given local coordinates relative to this region.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
        /// <returns>True if there is a chunk at the given coordinates; false otherwise.</returns>
        /// <remarks>If the local coordinates are out of bounds for this region, the action will be forwarded to the correct region
        /// transparently.</remarks>
        public bool ChunkExists (int lcx, int lcz)
        {
            if (!LocalBoundsCheck(lcx, lcz)) {
                Region alt = GetForeignRegion(lcx, lcz);
                return (alt == null) ? false : alt.ChunkExists(ForeignX(lcx), ForeignZ(lcz));
            }

            RegionFile rf = GetRegionFile();
            return rf.HasChunk(lcx, lcz);
        }
Example #10
0
        public bool ContainsChunk(ChunkCoord coord)
        {
            if (File.Exists(GetRegionFileName(coord.RegionCoord)))
            {
                return(false);
            }
            RegionFile rf = GetRegionFile(coord.RegionCoord);

            return(rf.ContainsChunk(coord));
        }
Example #11
0
        public void SetDirtyFlags(RegionFile region)
        {
            if (lastBiomeAction == null || lastPopulateAction == null)
            {
                throw new Exception("No record of last region save state.");
            }

            region.Dirty = false;
            foreach (ChunkState state in lastBiomeAction.Chunks)
            {
                Chunk c = region.Chunks[state.Coords.X, state.Coords.Z];
                if (c == null)
                {
                    continue;
                }
                else if (c.Root == null)
                {
                    c.Dirty = false;
                    continue;
                }

                if (ByteArraysEqual((byte[])c.Root["Level"]["Biomes"], state.Biomes))
                {
                    c.Dirty = false;
                }
                else
                {
                    c.Dirty      = true;
                    region.Dirty = true;
                }
            }

            for (int chunkX = 0; chunkX < 32; chunkX++)
            {
                for (int chunkZ = 0; chunkZ < 32; chunkZ++)
                {
                    Chunk c = region.Chunks[chunkX, chunkZ];
                    if (c == null)
                    {
                        continue;
                    }
                    else if (c.Root == null)
                    {
                        c.Dirty = false;
                        continue;
                    }

                    if (((byte)c.Root["Level"]["TerrainPopulated"]) != lastPopulateAction.PopulatedFlags[chunkX, chunkZ])
                    {
                        c.Dirty      = true;
                        region.Dirty = true;
                    }
                }
            }
        }
Example #12
0
        public RegionFile GetRegionFile(RegionCoord coord)
        {
            if (_regionFiles.ContainsKey(coord))
            {
                return(_regionFiles[coord]);
            }
            RegionFile region = new RegionFile(GetRegionFileName(coord), coord);

            _regionFiles.Add(coord, region);
            return(region);
        }
 internal TileCommandBlock(TagNodeCompound tileEntitie, RegionFile region, int chunkX, int chunkZ)
 {
     this.x        = int.Parse(tileEntitie["x"].ToString());
     this.y        = int.Parse(tileEntitie["y"].ToString());
     this.z        = int.Parse(tileEntitie["z"].ToString());
     this._command = tileEntitie["Command"].ToString();
     this.edit     = false;
     this.region   = region;
     this.chunkX   = chunkX;
     this.chunkZ   = chunkZ;
 }
Example #14
0
        /// <summary>
        /// Gets the timestamp of a chunk from the underlying region file.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
        /// <returns>The timestamp of the chunk slot in the region.</returns>
        /// <remarks>The value returned may differ from any timestamp stored in the chunk data itself.</remarks>
        public int GetChunkTimestamp(int lcx, int lcz)
        {
            if (!LocalBoundsCheck(lcx, lcz))
            {
                Region alt = GetForeignRegion(lcx, lcz);
                return((alt == null) ? 0 : alt.GetChunkTimestamp(ForeignX(lcx), ForeignZ(lcz)));
            }

            RegionFile rf = GetRegionFile();

            return(rf.GetTimestamp(lcx, lcz));
        }
Example #15
0
        private void lstRegions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (world == null || lstRegions.SelectedIndex == lastSelectedRegionIndex)
            {
                return;
            }

            if (!SaveIfNecessary())
            {
                lstRegions.SelectedIndex = lastSelectedRegionIndex;
                return;
            }

            history.FilterOutType(typeof(BiomeAction));
            history.FilterOutType(typeof(PopulateAction));

            Match  m          = Regex.Match(lstRegions.SelectedItem.ToString(), @"Region (-?\d+), (-?\d+)");
            int    x          = int.Parse(m.Groups[1].Value);
            int    z          = int.Parse(m.Groups[2].Value);
            String pathFormat = String.Format("{0}{1}r.{{0}}.{{1}}.mca", world.GetRegionDirectory(dim), Path.DirectorySeparatorChar);

            UpdateStatus("Reading region file");
            region = new RegionFile(String.Format(pathFormat, x, z));
            history.RecordBiomeState(region, "Initial Biomes");
            history.RecordPopulateState(region, "Initial Populate Flags");
            history.SetLastSaveActions();
            imgRegion.Reset();
            UpdateStatus("Generating terrain map");
            RegionUtil.RenderRegionTerrain(region, imgRegion.Layers[MAPLAYER].Image, false);
            UpdateStatus("Generating biome map");
            RegionUtil.RenderRegionBiomes(region, imgRegion.Layers[BIOMELAYER].Image, imgRegion.ToolTips, false);
            UpdateStatus("");
            RegionUtil.RenderRegionChunkstobePopulated(region, imgRegion.Layers[POPULATELAYER].Image, false);
            imgRegion.Redraw();

            RegionFile[,] surrounding = new RegionFile[3, 3];
            UpdateStatus("Reading surrounding chunks");
            surrounding[0, 0] = new RegionFile(String.Format(pathFormat, x - 1, z - 1), 30, 31, 30, 31);
            surrounding[1, 0] = new RegionFile(String.Format(pathFormat, x, z - 1), 0, 31, 30, 31);
            surrounding[2, 0] = new RegionFile(String.Format(pathFormat, x + 1, z - 1), 0, 1, 30, 31);
            surrounding[0, 1] = new RegionFile(String.Format(pathFormat, x - 1, z), 30, 31, 0, 31);
            surrounding[1, 1] = null;
            surrounding[2, 1] = new RegionFile(String.Format(pathFormat, x + 1, z, 0, 1, 0, 31));
            surrounding[0, 2] = new RegionFile(String.Format(pathFormat, x - 1, z + 1), 30, 31, 0, 1);
            surrounding[1, 2] = new RegionFile(String.Format(pathFormat, x, z + 1), 0, 31, 0, 1);
            surrounding[2, 2] = new RegionFile(String.Format(pathFormat, x + 1, z + 1), 0, 1, 0, 1);
            UpdateStatus("Generating map for surrounding chunks");
            RegionUtil.RenderSurroundingRegions(surrounding, imgRegion.Layers[MAPLAYER].Image, imgRegion.Layers[BIOMELAYER].Image, imgRegion.ToolTips, imgRegion.Layers[POPULATELAYER].Image);
            UpdateStatus("");
            imgRegion.Redraw();

            lastSelectedRegionIndex = lstRegions.SelectedIndex;
        }
Example #16
0
        public static void RenderRegionTerrain(RegionFile region, Bitmap b, bool clip = true)
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                if (clip)
                {
                    g.SetClip(CLIP);
                }
                g.Clear(Color.Black);
            }

            RenderRegionTerrain(region, 0, 31, 0, 31, OFFSETX, OFFSETY, b);
        }
Example #17
0
        public static void RenderRegionBiomes(RegionFile region, Bitmap b, String[,] toolTips, bool clip = true)
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                if (clip)
                {
                    g.SetClip(CLIP);
                }
                g.Clear(Color.Black);
            }

            RenderRegionBiomes(region, 0, 31, 0, 31, OFFSETX, OFFSETY, b, toolTips);
        }
Example #18
0
        public static void RenderRegionChunkstobePopulated(RegionFile region, Bitmap b, bool clip = true)
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                if (clip)
                {
                    g.SetClip(CLIP);
                }
                g.Clear(Color.Transparent);
            }

            RenderRegionChunkstobePopulated(region, 0, 31, 0, 31, OFFSETX, OFFSETY, b);
        }
Example #19
0
        /// <summary>
        /// Sets the timestamp of a chunk in the underlying region file.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk relative to this region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk relative to this region.</param>
        /// <param name="timestamp">The new timestamp value.</param>
        /// <remarks>This function will only update the timestamp of the chunk slot in the underlying region file.  It will not update
        /// any timestamp information in the chunk data itself.</remarks>
        public void SetChunkTimestamp(int lcx, int lcz, int timestamp)
        {
            if (!LocalBoundsCheck(lcx, lcz))
            {
                Region alt = GetForeignRegion(lcx, lcz);
                if (alt != null)
                {
                    alt.SetChunkTimestamp(ForeignX(lcx), ForeignZ(lcz), timestamp);
                }
            }

            RegionFile rf = GetRegionFile();

            rf.SetTimestamp(lcx, lcz, timestamp);
        }
Example #20
0
        public static void Fill(RegionFile region, Bitmap selection, Color selectionColor, Object biome, long worldSeed)
        {
            byte?     biomeId;
            BiomeUtil util;

            GetBiome(biome, worldSeed, out biomeId, out util);
            if (biomeId != null)
            {
                Fill(region, selection, selectionColor, (byte)biomeId);
            }
            else
            {
                Fill(region, selection, selectionColor, util);
            }
        }
Example #21
0
        public static void Replace(RegionFile region, Bitmap selection, Color selectionColor, byte biome1, Object biome2, long worldSeed)
        {
            byte?     biomeId;
            BiomeUtil util;

            GetBiome(biome2, worldSeed, out biomeId, out util);
            if (biomeId != null)
            {
                Replace(region, selection, selectionColor, biome1, (byte)biomeId);
            }
            else
            {
                Replace(region, selection, selectionColor, biome1, util);
            }
        }
Example #22
0
        /// <summary>
        /// Returns the count of valid chunks stored in this region.
        /// </summary>
        /// <returns>The count of currently stored chunks.</returns>
        public int ChunkCount ()
        {
            RegionFile rf = GetRegionFile();

            int count = 0;
            for (int x = 0; x < XDIM; x++) {
                for (int z = 0; z < ZDIM; z++) {
                    if (rf.HasChunk(x, z)) {
                        count++;
                    }
                }
            }

            return count;
        }
Example #23
0
        /// <summary>
        /// Conditionally dispose managed or unmanaged resources.
        /// </summary>
        /// <param name="disposing">True if the call to Dispose was explicit.</param>
        protected virtual void Dispose (bool disposing)
        {
            if (!_disposed) {
                if (disposing) {
                    // Cleanup managed resources
                    RegionFile rf = _regionFile.Target as RegionFile;
                    if (rf != null) {
                        rf.Dispose();
                        rf = null;
                    }
                }

                // Cleanup unmanaged resources
            }
            _disposed = true;
        }
Example #24
0
        private void RegionLoadSave(object sender, EventArgs e)
        {
            OpenFileDialog load = new OpenFileDialog();

            load.Filter = "Region (region.clt)|*.clt|All Files (*.*)|*.*";
            if (load.ShowDialog() == System.Windows.Forms.DialogResult.OK && File.Exists(load.FileName))
            {
                autoOpenPath = load.FileName;
                isAutoOpen   = true;

                Read = null;
                RegionDataGrid.RowCount = 0;
                ZonesDataGrid.RowCount  = 0;
                DoLoading();
            }
        }
Example #25
0
        private Bitmap RenderRegionBiomes(RegionFile region, Bitmap b, int offsetX, int offsetY)
        {
            foreach (Chunk c in region.Chunks)
            {
                if (c == null || c.Root == null)
                {
                    continue;
                }
                Coord chunkOffset = new Coord(c.Coords);
                chunkOffset.ChunktoRegionRelative();
                chunkOffset.ChunktoAbsolute();
                RenderChunkBiomes(c, b, offsetX + chunkOffset.X, offsetY + chunkOffset.Z);
            }

            return(b);
        }
Example #26
0
    public static void SaveToDisk()
    {
        TagNodeList Pos = playerData.Root["Pos"] as TagNodeList;

        Pos[0] = (TagNodeDouble)PlayerController.instance.transform.position.x;
        Pos[1] = (TagNodeDouble)PlayerController.instance.transform.position.y;
        Pos[2] = (TagNodeDouble)PlayerController.instance.transform.position.z;
        TagNodeList Rotation = playerData.Root["Rotation"] as TagNodeList;

        Rotation[0] = (TagNodeFloat)(-PlayerController.instance.transform.localEulerAngles.y);
        Rotation[1] = (TagNodeFloat)PlayerController.instance.camera.localEulerAngles.x;

        InventorySystem.SaveData(playerData.Root["Inventory"] as TagNodeList);

        using (Stream stream = playerFile.GetDataOutputStream())
        {
            playerData.WriteTo(stream);
        }

        TagNodeCompound player    = levelDat["Player"] as TagNodeCompound;
        TagNodeCompound abilities = player["abilities"] as TagNodeCompound;

        abilities["flying"] = (TagNodeByte)(PlayerController.instance.isFlying ? 1 : 0);
        levelDat["DayTime"] = (TagNodeLong)TimeOfDay.instance.tick;
        int mode = (int)GameModeManager.mode;

        levelDat["GameType"] = (TagNodeInt)mode;
        using (Stream stream = levelFile.GetDataOutputStream())
        {
            levelTree.WriteTo(stream);
        }

        foreach (KeyValuePair <Vector2Int, NbtTree> kvPair in chunkDictNBT)
        {
            int        chunkX  = kvPair.Key.x;
            int        chunkZ  = kvPair.Key.y;
            int        regionX = GetRegionCoordinate(chunkX);
            int        regionZ = GetRegionCoordinate(chunkZ);
            RegionFile region  = GetRegion(regionX, regionZ);
            int        _x      = chunkX - regionX * 32;
            int        _z      = chunkZ - regionZ * 32;
            using (Stream stream = region.GetChunkDataOutputStream(_x, _z))
            {
                kvPair.Value.WriteTo(stream);
            }
        }
    }
Example #27
0
        public static void Copy(RegionFile region, Bitmap selection, Color selectionColor)
        {
            BiomeCopy biomeData = new BiomeCopy();

            for (int chunkX = 0; chunkX < 32; chunkX++)
            {
                for (int chunkZ = 0; chunkZ < 32; chunkZ++)
                {
                    Chunk c = region.Chunks[chunkX, chunkZ];
                    if (c == null || c.Root == null)
                    {
                        continue;
                    }

                    byte[] biomes = (byte[])c.Root["Level"]["Biomes"];
                    for (int x = 0; x < 16; x++)
                    {
                        for (int z = 0; z < 16; z++)
                        {
                            if (selection.GetPixel(RegionUtil.OFFSETX + chunkX * 16 + x, RegionUtil.OFFSETY + chunkZ * 16 + z).ToArgb() == selectionColor.ToArgb())
                            {
                                biomeData.Biomes[chunkX * 16 + x, chunkZ * 16 + z] = biomes[x + z * 16];
                                biomeData.Empty = false;
                                if (biomeData.Left > chunkX * 16 + x)
                                {
                                    biomeData.Left = chunkX * 16 + x;
                                }
                                if (biomeData.Right < chunkX * 16 + x)
                                {
                                    biomeData.Right = chunkX * 16 + x;
                                }
                                if (biomeData.Top > chunkZ * 16 + z)
                                {
                                    biomeData.Top = chunkZ * 16 + z;
                                }
                                if (biomeData.Bottom < chunkZ * 16 + z)
                                {
                                    biomeData.Bottom = chunkZ * 16 + z;
                                }
                            }
                        }
                    }
                }
            }
            biomeData.Crop();
            System.Windows.Forms.Clipboard.SetData("BiomeCopy", biomeData);
        }
Example #28
0
        private void ApplyPopulateState(PopulateAction action, RegionFile region, Bitmap populateOverlay)
        {
            for (int chunkX = 0; chunkX < 32; chunkX++)
            {
                for (int chunkZ = 0; chunkZ < 32; chunkZ++)
                {
                    Chunk c = region.Chunks[chunkX, chunkZ];
                    if (c == null || c.Root == null)
                    {
                        continue;
                    }

                    ((TAG_Byte)c.Root["Level"]["TerrainPopulated"]).Payload = action.PopulatedFlags[chunkX, chunkZ];
                }
            }
            RegionUtil.RenderRegionChunkstobePopulated(region, populateOverlay);
        }
Example #29
0
        /// <summary>
        /// Gets the <see cref="NbtTree"/> for a chunk given local coordinates into the region.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of a chunk within the region.</param>
        /// <param name="lcz">The local Z-coordinate of a chunk within the region.</param>
        /// <returns>An <see cref="NbtTree"/> for a local chunk, or null if there is no chunk at the given coordinates.</returns>
        public NbtTree GetChunkTree (int lcx, int lcz)
        {
            if (!LocalBoundsCheck(lcx, lcz)) {
                Region alt = GetForeignRegion(lcx, lcz);
                return (alt == null) ? null : alt.GetChunkTree(ForeignX(lcx), ForeignZ(lcz));
            }

            RegionFile rf = GetRegionFile();
            Stream nbtstr = rf.GetChunkDataInputStream(lcx, lcz);
            if (nbtstr == null) {
                return null;
            }

            NbtTree tree = new NbtTree(nbtstr);

            nbtstr.Close();
            return tree;
        }
Example #30
0
        // XXX: Exceptions
        /// <summary>
        /// Saves an <see cref="NbtTree"/> for a chunk back to the region's data store at the given local coordinates.
        /// </summary>
        /// <param name="lcx">The local X-coordinate of the chunk within the region.</param>
        /// <param name="lcz">The local Z-coordinate of the chunk within the region.</param>
        /// <param name="tree">The <see cref="NbtTree"/> of a chunk to write back to the region.</param>
        /// <returns>True if the save succeeded.</returns>
        /// <remarks>It is up to the programmer to ensure that the global coordinates defined within the chunk's tree
        /// are consistent with the local coordinates of the region being written into.</remarks>
        public bool SaveChunkTree (int lcx, int lcz, NbtTree tree)
        {
            if (!LocalBoundsCheck(lcx, lcz)) {
                Region alt = GetForeignRegion(lcx, lcz);
                return (alt == null) ? false : alt.SaveChunkTree(ForeignX(lcx), ForeignZ(lcz), tree);
            }

            RegionFile rf = GetRegionFile();
            Stream zipstr = rf.GetChunkDataOutputStream(lcx, lcz);
            if (zipstr == null) {
                return false;
            }

            tree.WriteTo(zipstr);
            zipstr.Close();

            return true;
        }