示例#1
0
        public void NBTIOTests_ReadRawFileTest()
        {
            CompoundTag tag = NBTIO.ReadRawFile(Environment.CurrentDirectory + "\\test.nbt");

            tag.GetBool("bool");
            tag.GetByte("byte");
            tag.GetByteArray("byteArray");
            tag.GetShort("short");
            tag.GetInt("int");
            tag.GetIntArray("intArray");
            tag.GetLong("long");
            tag.GetLongArray("longArray");
            tag.GetFloat("float");
            tag.GetDouble("double");
            tag.GetList("list");
            tag.GetCompound("com");
            Console.WriteLine(tag);
        }
        private CompoundTag ConvertSection(Tag sectionTag)
        {
            byte[]      blockData = new byte[4096];
            NibbleArray metaData  = new NibbleArray(4096);

            CompoundTag section = (CompoundTag)sectionTag;

            long[]  states  = section.GetLongArray("BlockStates");
            ListTag palette = section.GetList("Palette");
            List <RuntimeTable.Table> indexs = new List <RuntimeTable.Table>();

            foreach (Tag paletteTag in palette.Tags)
            {
                if (paletteTag is CompoundTag)
                {
                    CompoundTag pt   = (CompoundTag)paletteTag;
                    string      name = pt.GetString("Name");
                    indexs.Add(RuntimeTable.GetNameToTable(name, pt.GetCompound("Properties").Tags));
                }
            }

            int         bits      = CheckMostBit(indexs.Count - 1);
            List <byte> fixStates = new List <byte>();

            foreach (long state in states)
            {
                fixStates.AddRange(BitConverter.GetBytes((ulong)state));
            }

            BitArray stateBits = new BitArray(fixStates.ToArray());

            for (int i = 0; i < 4096; i++)
            {
                int  bitOffset = i * bits;
                uint index     = stateBits.Get(bitOffset + bits - 1) ? 1u : 0u;
                for (int j = bits - 2; j >= 0; j--)
                {
                    index <<= 1;
                    index  |= stateBits.Get(bitOffset + j) ? 1u : 0u;
                }

                try
                {
                    RuntimeTable.Table table = indexs[(int)index];
                    blockData[i] = (byte)(table.Id & 0xff);
                    metaData[i]  = (byte)(table.Data & 0xf);
                }
                catch (Exception e)
                {
                    Logger.Info(indexs.Count + " = " + states[0] + " >>> " + states[1]);
                    throw e;
                }
            }

            var newSection = (CompoundTag)section.Clone();

            newSection.Remove("BlockStates");
            newSection.Remove("Palette");

            newSection.PutByteArray("Blocks", blockData);
            newSection.PutByteArray("Data", metaData.ArrayData);

            return(newSection);
        }