Example #1
0
        private static void Main(string[] args)
        {
            NbtFile nbtFile;

            RegionFile regionFile = null;
            RegionFile anvilFile = null;

            int choice = 3;

            foo:
            Console.WriteLine("Hiya! This is a test application for my NBT reader, MC NBT reader.");
            Console.WriteLine("Be sure to have a .nbt and/or .mcr on your desktop or else this won't work...");

            Console.WriteLine("\n");
            Console.WriteLine("(1) open a NBT file");
            Console.WriteLine("(2) open a MCR file");
            Console.WriteLine("(2) open a MCA file");
            Console.WriteLine("(3) say bye bye to this application");

            Console.Write("\nEnter choice: ");

            string parse = Console.ReadLine();

            if (!int.TryParse(parse, out choice) || (choice > 3 || choice < 0))
            {
                Console.Beep();
                Console.Clear();

                goto foo;
            }
            else
            {
                Console.Clear();

                try
                {
                    switch (choice)
                    {
                    case 1:
                        nbtFile = NbtFile.OpenTag(File.OpenRead(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*.nbt")[0]), 1);
                        break;
                    case 2:
                        regionFile = RegionFile.OpenRegion(File.OpenRead(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*.mcr")[0]));
                        break;
                    case 3:
                        anvilFile = RegionFile.OpenRegion(File.OpenRead(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "*.mca")[0]));
                        break;
                    case 4:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            Console.WriteLine("parse complete!");

            foreach (NbtFile file in anvilFile.Content)
            {
                if (file != null)
                {
                    var anvil = new Anvil(file);

                    Console.WriteLine(anvil.XPos + " " + anvil.ZPos);
                }
            }

            Console.WriteLine(anvilFile.Content.Length);

            Console.ReadLine();
        }
Example #2
0
        public void RenderSegment(Anvil anvil, RenderTarget renderTarget, out ChunkEntry output)
        {
            // create char array to hold rendered blocks
            int[] drawnChunk = new int[16 * 16]; // x * z * 4 where 4 is colors

            // render code here
            foreach (AnvilSection section in anvil.Sections)
            {
                // render blocks
                for (int y = 0; y < 16; y++)
                {
                    for (int z = 0; z < 16; z++)
                    {
                        for (int x = 0; x < 16; x++)
                        {
                            int     index       = y * 256 + z * 16 + x;
                            int     dindex      = z *  16 + x;

                            byte    blockId     = section.Blocks[index];

                            if (blockId == 0 || !theBlocks.BlockList.ContainsKey(blockId))
                                continue;

                            var     blockCol    = theBlocks.BlockList[blockId];
                            var     finalCol    = new byte[4];
                            var     prevCol     = BitConverter.GetBytes(drawnChunk[dindex]);

                            finalCol[0] = processAlpha(blockCol[0], prevCol[0], blockCol[3] / 255.0f);
                            finalCol[1] = processAlpha(blockCol[1], prevCol[1], blockCol[3] / 255.0f);
                            finalCol[2] = processAlpha(blockCol[2], prevCol[2], blockCol[3] / 255.0f);
                            finalCol[3] = 255;

                            drawnChunk[dindex] = BitConverter.ToInt32(finalCol, 0);
                        }
                    }
                }
            }

            // render heightmap
            for (int z = 0; z < 16; z++)
            {
                for (int x = 0; x < 16; x++)
                {
                    int dindex      = z * 16 + x;

                    var value       = (byte)MathUtil.Clamp((anvil.HeightMap[z * 16 + x] - 64) * 2, 0, 255);
                    var finalCol    = new byte[4];
                    var prevCol     = BitConverter.GetBytes(drawnChunk[dindex]);

                    finalCol[0] = processAlpha(255, prevCol[0], value / 255.0f);
                    finalCol[1] = processAlpha(255, prevCol[1], value / 255.0f);
                    finalCol[2] = processAlpha(255, prevCol[2], value / 255.0f);
                    finalCol[3] = 255;

                    drawnChunk[dindex] = BitConverter.ToInt32(finalCol, 0);
                }
            }

            Bitmap newBitmap = new Bitmap(renderTarget, new DrawingSize(16, 16), new BitmapProperties()
                {
                    PixelFormat = new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)
                });
                   newBitmap.CopyFromMemory(drawnChunk, 16 * 4);

            output = new ChunkEntry
            {
                XPos            = anvil.XPos,
                ZPos            = anvil.ZPos,
                RenderedChunk   = newBitmap
            };
        }