public static NbtMap Load(string filename)
        {
            var map = new NbtMap();

            var nf = new NBTFile(filename);

            using (var nbtstr = nf.GetDataInputStream())
            {
                var tree = new NbtTree(nbtstr);

                var root = tree.Root["map"];
                var list = root.ToTagList();

                foreach (var tag in list)
                {
                    var k = tag.ToTagCompound()["k"].ToTagString();
                    var v = (short)tag.ToTagCompound()["v"].ToTagInt();
                    if (!map.ContainsKey(v))
                    {
                        map.Add(v, k);
                    }
                }

                return(map);
            }
        }
Exemplo n.º 2
0
        public static ScarifStructure Load(string filename)
        {
            using (var fs = File.OpenRead(filename))
                using (var bs = new BrotliStream(fs, CompressionMode.Decompress))
                    using (var s = new BinaryReader(bs))
                    {
                        var idMap   = new NbtMap();
                        var diffMap = new DiffMap();

                        var identBytes = new byte[Magic.Length];
                        var read       = s.Read(identBytes, 0, identBytes.Length);
                        var ident      = Encoding.UTF8.GetString(identBytes);
                        if (ident != Magic || read != identBytes.Length)
                        {
                            throw new IOException("Input file not SCARIF structure");
                        }

                        var version         = s.ReadInt32();
                        var numChunks       = s.ReadInt32();
                        var numIdMapEntries = s.ReadInt32();

                        for (var entryIdx = 0; entryIdx < numIdMapEntries; entryIdx++)
                        {
                            var id   = s.ReadInt16();
                            var name = ReadNullTerminatedString(s);
                            idMap.Add(id, name);
                        }

                        for (var chunkIdx = 0; chunkIdx < numChunks; chunkIdx++)
                        {
                            var chunkX    = s.ReadInt32();
                            var chunkZ    = s.ReadInt32();
                            var numBlocks = s.ReadInt32();

                            var blocks = new BlockDiffEntry[numBlocks];

                            for (var blockIdx = 0; blockIdx < numBlocks; blockIdx++)
                            {
                                // Format:
                                // 0x 0000 1111
                                //    xxxx zzzz
                                var xz = s.ReadByte();

                                var x = (byte)((xz & 0xF0) >> 4);
                                var z = (byte)(xz & 0x0F);
                                var y = s.ReadByte();

                                var id    = s.ReadInt16();
                                var flags = (ScarifBlock.BlockFlags)s.ReadByte();

                                byte    metadata = 0;
                                NbtTree tileTag  = null;

                                if (flags.Has(ScarifBlock.BlockFlags.Metadata))
                                {
                                    metadata = s.ReadByte();
                                }
                                if (flags.Has(ScarifBlock.BlockFlags.Nbt))
                                {
                                    var len = s.ReadInt32();
                                    if (len <= 0)
                                    {
                                        throw new IOException("Zero-length NBT present");
                                    }
                                    var bytes = s.ReadBytes(len);
                                    using (var ms = new MemoryStream(bytes))
                                        tileTag = new NbtTree(ms);
                                }

                                if (idMap.ContainsKey(id))
                                {
                                    blocks[blockIdx] = new BlockDiffEntry(new BlockPosition(x, y, z), new ScarifBlock(id, metadata, tileTag));
                                }
                                else
                                {
                                    throw new IOException($"Unknown block ID found: {id}");
                                }
                            }

                            diffMap.Add(new ChunkPosition(chunkX, chunkZ), blocks);
                        }

                        return(new ScarifStructure(version, idMap, diffMap));
                    }
        }