Exemplo n.º 1
0
        public void Add(ChunkPosition chunk, BlockPosition pos, ScarifBlock block)
        {
            var entry = new BlockDiffEntry(pos, block);

            if (!BlockDiffMap.ContainsKey(chunk))
            {
                BlockDiffMap.Add(chunk, new List <BlockDiffEntry>());
            }
            BlockDiffMap[chunk].Add(entry);
        }
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));
                    }
        }