Exemplo n.º 1
0
        public void Generate()
        {
            byte[] blocks = new byte[32768], data = new byte[16384];
            byte[] skylight = new byte[16384], light = new byte[16384];
            byte[] height = new byte[256];
            BinaryTag[] entities = new BinaryTag[0], tileEntities = new BinaryTag[0];

            for (int i = 0; i < 16348; ++i) {
                blocks[i*2] = (byte) Block.Rock;
                blocks[i*2 + 1] = (byte) Block.Rock;
                skylight[i] = 0xFF;
                light[i] = 0xFF;
            }

            BinaryTag[] structure = new BinaryTag[] {
                new BinaryTag(TagType.ByteArray, blocks, "Blocks"),
                new BinaryTag(TagType.ByteArray, data, "Data"),
                new BinaryTag(TagType.ByteArray, skylight, "SkyLight"),
                new BinaryTag(TagType.ByteArray, light, "BlockLight"),
                new BinaryTag(TagType.ByteArray, height, "HeightMap"),
                new BinaryTag(TagType.List, entities, "Entities"),
                new BinaryTag(TagType.List, tileEntities, "TileEntities"),
                new BinaryTag(TagType.Long, (long) 0, "LastUpdate"),
                new BinaryTag(TagType.Int, (int) ChunkX, "xPos"),
                new BinaryTag(TagType.Int, (int) ChunkZ, "zPos"),
                new BinaryTag(TagType.Byte, (byte) 0, "TerrainPopulated")
            };

            _Structure = new BinaryTag(TagType.Compound, new BinaryTag[] {
                new BinaryTag(TagType.Compound, structure, "Level")
            });
            Save();
        }
Exemplo n.º 2
0
        public Map(string Name)
        {
            WorldName = Name;
            _DespawnTimer = 0;
            _Chunks = new Dictionary<long, Chunk>();

            StreamReader rawReader = new StreamReader(Name + "/level.dat");
            GZipStream reader = new GZipStream(rawReader.BaseStream, CompressionMode.Decompress);
            _Structure = NbtParser.ParseTagStream(reader);
            reader.Close();
            rawReader.Close();
        }
Exemplo n.º 3
0
        static void WriteTag(BinaryTag Tag, Stream ByteStream)
        {
            byte[] buffer;

            switch(Tag.Type) {
                case TagType.End:
                    break;

                case TagType.Byte:
                    ByteStream.WriteByte((byte) Tag.Payload);
                    break;

                case TagType.Short:
                    buffer = BitConverter.GetBytes(hton((short) Tag.Payload));
                    ByteStream.Write(buffer, 0, 2);
                    break;

                case TagType.Int:
                    buffer = BitConverter.GetBytes(hton((int) Tag.Payload));
                    ByteStream.Write(buffer, 0, 4);
                    break;

                case TagType.Long:
                    buffer = BitConverter.GetBytes(hton((long) Tag.Payload));
                    ByteStream.Write(buffer, 0, 8);
                    break;

                case TagType.Float:
                    buffer = BitConverter.GetBytes((float) Tag.Payload);
                    buffer = BitConverter.GetBytes(hton(BitConverter.ToInt32(buffer, 0)));
                    ByteStream.Write(buffer, 0, 4);
                    break;

                case TagType.Double:
                    buffer = BitConverter.GetBytes((double) Tag.Payload);
                    buffer = BitConverter.GetBytes(hton(BitConverter.ToInt64(buffer, 0)));
                    ByteStream.Write(buffer, 0, 8);
                    break;

                case TagType.ByteArray: {
                    byte[] array = (byte[]) Tag.Payload;
                    WriteTag(new BinaryTag(TagType.Int, array.Length), ByteStream);
                    ByteStream.Write(array, 0, array.Length);
                    break;
                }

                case TagType.String: {
                    byte[] data = UTF8Encoding.UTF8.GetBytes((string) Tag.Payload);
                    WriteTag(new BinaryTag(TagType.Short, (short) data.Length), ByteStream);
                    ByteStream.Write(data, 0, data.Length);
                    break;
                }

                case TagType.List: {
                    BinaryTag[] list = (BinaryTag[]) Tag.Payload;
                    WriteTag(new BinaryTag(TagType.Byte, (byte) list[0].Type), ByteStream);
                    WriteTag(new BinaryTag(TagType.Int, (int) list.Length), ByteStream);
                    for (int i = 0; i < list.Length; ++i) {
                        WriteTag(list[i], ByteStream);
                    }
                    break;
                }

                case TagType.Compound: {
                    BinaryTag[] list = (BinaryTag[]) Tag.Payload;
                    for (int i = 0; i < list.Length; ++i) {
                        WriteNamedTag(list[i], ByteStream);
                    }
                    WriteNamedTag(new BinaryTag(TagType.End), ByteStream);
                    break;
                }

                default:
                    throw new IOException("Parser exploded.");
            }
        }
Exemplo n.º 4
0
 static void WriteNamedTag(BinaryTag Tag, Stream ByteStream)
 {
     ByteStream.WriteByte((byte) Tag.Type);
     if (Tag.Type != TagType.End) {
         WriteTag(new BinaryTag(TagType.String, Tag.Name), ByteStream);
     }
     WriteTag(Tag, ByteStream);
 }
Exemplo n.º 5
0
 public static void WriteTagStream(BinaryTag tag, Stream ByteStream)
 {
     WriteNamedTag(tag, ByteStream);
 }
Exemplo n.º 6
0
        static BinaryTag ReadTag(Stream ByteStream, TagType Type)
        {
            BinaryTag Tag = new BinaryTag(Type);
            byte[] buffer;

            switch(Type) {
                case TagType.End:
                    Tag.Payload = null;
                    return Tag;

                case TagType.Byte:
                    Tag.Payload = (byte) ByteStream.ReadByte();
                    return Tag;

                case TagType.Short:
                    buffer = new byte[2];
                    ByteStream.Read(buffer, 0, 2);
                    Tag.Payload = ntoh(BitConverter.ToInt16(buffer, 0));
                    return Tag;

                case TagType.Int:
                    buffer = new byte[4];
                    ByteStream.Read(buffer, 0, 4);
                    Tag.Payload = ntoh(BitConverter.ToInt32(buffer, 0));
                    return Tag;

                case TagType.Long:
                    buffer = new byte[8];
                    ByteStream.Read(buffer, 0, 8);
                    Tag.Payload = ntoh(BitConverter.ToInt64(buffer, 0));
                    return Tag;

                case TagType.Float:
                    buffer = new byte[4];
                    ByteStream.Read(buffer, 0, 4);
                    buffer = BitConverter.GetBytes(ntoh(BitConverter.ToInt32(buffer, 0)));
                    Tag.Payload = BitConverter.ToSingle(buffer, 0);
                    return Tag;

                case TagType.Double:
                    buffer = new byte[8];
                    ByteStream.Read(buffer, 0, 8);
                    buffer = BitConverter.GetBytes(ntoh(BitConverter.ToInt64(buffer, 0)));
                    Tag.Payload = BitConverter.ToDouble(buffer, 0);
                    return Tag;

                case TagType.ByteArray: {
                    int length = (int) ReadTag(ByteStream, TagType.Int).Payload;
                    buffer = new byte[length];
                    ByteStream.Read(buffer, 0, length);
                    Tag.Payload = buffer;
                    return Tag;
                }

                case TagType.String: {
                    short length = (short) ReadTag(ByteStream, TagType.Short).Payload;
                    buffer = new byte[length];
                    ByteStream.Read(buffer, 0, length);
                    Tag.Payload = UTF8Encoding.UTF8.GetString(buffer);
                    return Tag;
                }

                case TagType.List: {
                    TagType type = (TagType) (byte) ReadTag(ByteStream, TagType.Byte).Payload;
                    int length = (int) ReadTag(ByteStream, TagType.Int).Payload;
                    BinaryTag[] list = new BinaryTag[length];
                    for (int i = 0; i < length; ++i) {
                        list[i] = ReadTag(ByteStream, type);
                    }
                    Tag.Payload = list;
                    return Tag;
                }

                case TagType.Compound:
                    List<BinaryTag> tags = new List<BinaryTag>();
                    while (true) {
                        BinaryTag tag = ReadNamedTag(ByteStream);
                        if (tag.Type == TagType.End) {
                            break;
                        }
                        tags.Add(tag);
                    }
                    Tag.Payload = tags.ToArray();
                    return Tag;

                default:
                    throw new IOException("Parser exploded.");
            }
        }
Exemplo n.º 7
0
 public void Load()
 {
     try {
         StreamReader rawReader = new StreamReader(CalculateFilename());
         GZipStream reader = new GZipStream(rawReader.BaseStream, CompressionMode.Decompress);
         _Structure = NbtParser.ParseTagStream(reader);
         reader.Close();
         rawReader.Close();
     }
     catch (FileNotFoundException) {
         Generate();
     }
     catch (DirectoryNotFoundException) {
         Generate();
     }
 }
Exemplo n.º 8
0
        public static Map Load(string filename)
        {
            StreamReader RawReader = new StreamReader(filename);
            GZipStream Reader = new GZipStream(RawReader.BaseStream, CompressionMode.Decompress);

            BinaryTag Tree = NbtParser.ParseTagStream(Reader);

            Reader.Close();
            RawReader.Close();

            BinaryTag MapTag  = new BinaryTag(){ Payload = null, };

            // Find the MapTag data.
            foreach (var Item in (BinaryTag[])Tree.Payload)
            {
                if (Item.Name == "Map")
                {
                    MapTag = Item;
                    break;
                }
            }

            if (!(MapTag.Payload is BinaryTag[]))
                throw new IOException("Map tree did not or contained invalid Map tag!");

            BinaryTag[] Items = MapTag.Payload as BinaryTag[];

            Map MapInProgress = new Map();

            foreach (var Item in Items)
            {
                switch (Item.Name)
                {
                    case "Width":
                        MapInProgress.xdim = (short)Item.Payload;
                        break;
                    case "Height":
                        MapInProgress.ydim = (short) Item.Payload;
                        break;
                    case "Length":
                        MapInProgress.zdim = (short)Item.Payload;
                        break;
                    case "Blocks":
                        MapInProgress.CopyBlocks((byte[])Item.Payload, 0);
                        break;
                    case "Spawn":
                        BinaryTag[] List = (BinaryTag[]) Item.Payload;
                        short x = (short) List[0].Payload;
                        short y = (short)List[1].Payload;
                        short z = (short)List[2].Payload;

                        x *= 32;
                        y *= 32;
                        z *= 32;

                        MapInProgress.SetSpawn(new Position(x, y, z), 0);

                        break;
                    default:
                        break;
                }
            }

            // We do our own optimizations.
            MapInProgress.ReplaceAll(Block.StillWater, Block.Water, -1);
            MapInProgress.ReplaceAll(Block.StillLava, Block.Lava, -1);

            return MapInProgress;
        }
Exemplo n.º 9
0
        public bool LoadLevel()
        {
            string path = Path.Combine(WorldName, "level.dat");
            if(File.Exists(path))
            {
                using(FileStream rawReader = File.OpenRead(path)) {
                    using(GZipStream reader = new GZipStream(rawReader, CompressionMode.Decompress)) {
                    _Structure = NbtParser.ParseTagStream(reader);
                    }
                }
            }
            else
            {
                CXMineServer.Log("Missing level.dat file, the world cannot start");
                return false;
            }

            return true;
        }
Exemplo n.º 10
0
 public string CompoundToString(string type, string indent)
 {
     BinaryTag[] List = (BinaryTag[]) Payload;
     if (Payload == null) {
         List = new BinaryTag[0];
     }
     StringBuilder Builder = new StringBuilder();
     Builder.Append("TAG_").Append(type).Append(NameString(Name)).Append(": ");
     Builder.Append(List.Length).Append(" entries\n");
     Builder.Append(indent).Append("{\n");
     foreach(BinaryTag Tag in List) {
         Builder.Append(indent).Append("  ");
         if(Tag.Type == TagType.Compound) {
             Builder.Append(Tag.CompoundToString("Compound", indent + "  "));
         } else if(Tag.Type == TagType.List) {
             Builder.Append(Tag.CompoundToString("List", indent + "  "));
         } else {
             Builder.Append(Tag);
         }
         Builder.Append("\n");
     }
     Builder.Append(indent).Append("}");
     return Builder.ToString();
 }
Exemplo n.º 11
0
        public void Load()
        {
            try
            {
                using (FileStream rawReader = File.OpenRead(CalculateFilename()))
                {
                    using (GZipStream reader = new GZipStream(rawReader, CompressionMode.Decompress))
                    {
                        _Structure = NbtParser.ParseTagStream(reader);
                    }
                }
                //CXMineServer.Log(_Structure.CompoundToString("structure", ""));
            }
            catch (FileNotFoundException)
            {
                Generate();
            }
            catch (DirectoryNotFoundException)
            {
                Generate();
            }

            //CheckVisibility();
        }