Esempio n. 1
0
 public NBTViewer(string path)
 {
     filename = Path.GetFileName(path);
     if (path.EndsWith(".mca"))
     {
         content = new NBTContent();
         Region r = RegionLoader.LoadRegion(path);
         for (int z = 0; z < 32; z++)
         {
             for (int x = 0; x < 32; x++)
             {
                 if (r.chunks[x, z] != null)
                 {
                     content.contents.Add($"Chunk [{x},{z}]", r.chunks[x, z].sourceNBT.contents);
                 }
             }
         }
     }
     else
     {
         content = new NBTContent(RegionLoader.CreateZLibDecompressionStream(File.ReadAllBytes(path)));
     }
     //filename = "root";
     //var root = new CompoundContainer();
     //foreach(var k in content.contents.GetContentKeys("")) {
     //	root.Add(k, content.contents.Get(k));
     //}
     //content.contents.cont.Clear();
     //content.contents.Add(filename, root);
 }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            Console.ResetColor();
            Console.Clear();
            WriteLine("--------------");
            WriteLine("MinecraftUtils");
            WriteLine("--------------");
            string fname = null;

            if (args.Length > 0)
            {
                if (File.Exists(args[0]))
                {
                    var v = new NBTViewer(args[0]);
                    v.Run();
                }
            }
            else
            {
                Console.WriteLine("File '" + fname + "' does not exist!");
            }
            string input = "";

            while (input != "exit")
            {
                WriteLine("Chose an operation to perform:");
                WriteLine("- mergeregions       Merges region files based on an input map");
                WriteLine("- mergeworlds        Merges worlds based on an input map");
                WriteLine("- randomblocks       Makes a region out of random blocks");
                WriteLine("- view               Shows the contents of an NBT file or region");
                WriteLine("- readchunk          Loads chunk data at a specific byte offset in a region file");
                input = GetInput();
                if (input.StartsWith("mergeregions"))
                {
                    var m = new RegionMergerConsoleTool();
                    m.Run();
                }
                if (input.StartsWith("mergeworlds"))
                {
                    var m = new WorldMerger();
                    m.Run();
                }
                if (input.StartsWith("randomblocks"))
                {
                    var g = new RandomBlockRegionGen();
                    g.Run();
                }
                if (input.StartsWith("view "))
                {
                    var v = new NBTViewer(input.Substring(5).Replace("\"", ""));
                    v.Run();
                }
                if (input.StartsWith("readchunk "))
                {
                    WriteLine("enter index: ");
                    int index = int.Parse(Console.ReadLine());
                    var v     = new NBTViewer(RegionLoader.LoadChunkDataAtIndex(input.Substring(10).Replace("\"", ""), index));
                    v.Run();
                }
            }
        }
Esempio n. 3
0
        public void Run()
        {
            MCUtilsConsole.WriteLine("Enter path to world 1's region files:");
            world1Path = GetFilePath(true);
            if (world1Path == null)
            {
                return;
            }
            MCUtilsConsole.WriteLine("Enter path to world 2's region files:");
            world2Path = GetFilePath(true);
            if (world2Path == null)
            {
                return;
            }

            MCUtilsConsole.WriteLine("Enter the lower coordinate for the merge (inclusive):");
            lowerBound = ParseCoordinates(MCUtilsConsole.GetInput());
            MCUtilsConsole.WriteLine("Enter the upper coordinate for the merge (inclusive):");
            upperBound = ParseCoordinates(MCUtilsConsole.GetInput());

            int sizeX = upperBound.x - lowerBound.x + 1;
            int sizeZ = upperBound.z - lowerBound.z + 1;

            MCUtilsConsole.WriteLine($"Enter path to map image (Must be {sizeX * 512}x{sizeZ * 512} or {sizeX * 32}x{sizeZ * 32}:");
            string map = GetFilePath(false);

            if (map == null)
            {
                return;
            }
            Bitmap worldMergeMap = new Bitmap(map);
            byte   mergeMode     = 0;

            if (worldMergeMap.Width == sizeX * 512 && worldMergeMap.Height == sizeZ * 512)
            {
                mergeMode = 1;
            }
            if (worldMergeMap.Width == sizeX * 32 && worldMergeMap.Height == sizeZ * 32)
            {
                mergeMode = 2;
            }
            if (mergeMode == 0)
            {
                MCUtilsConsole.WriteError("Map was not the correct size!");
                return;
            }

            MCUtilsConsole.WriteLine("Enter path to output file:");
            outputPath = GetFilePath(true);

            List <RegionRecord> records = new List <RegionRecord>();

            for (int rz = lowerBound.z; rz <= upperBound.z; rz++)
            {
                for (int rx = lowerBound.x; rx <= upperBound.x; rx++)
                {
                    records.Add(new RegionRecord(this, rx, rz));
                }
            }

            int w1only    = 0;
            int w2only    = 0;
            int mergeable = 0;

            foreach (var r in records)
            {
                if (r.CanMerge)
                {
                    mergeable++;
                }
                else
                {
                    if (r.existsInWorld1)
                    {
                        w1only++;
                    }
                    else if (r.existsInWorld2)
                    {
                        w2only++;
                    }
                }
            }

            if (w1only > 0)
            {
                MCUtilsConsole.WriteWarning($"There are {w1only} regions that only exist in world 1.");
            }
            if (w2only > 0)
            {
                MCUtilsConsole.WriteWarning($"There are {w2only} regions that only exist in world 2.");
            }
            MCUtilsConsole.WriteLine($"{mergeable} out of {records.Count} can be merged. Press any key to start.");
            Console.ReadKey();

            MCUtilsConsole.WriteLine("Starting world merge...");
            foreach (var r in records)
            {
                int    localX  = r.loc.x - lowerBound.x;
                int    localZ  = r.loc.z - lowerBound.z;
                int    scale   = mergeMode == 1 ? 512 : 32;
                Bitmap section = worldMergeMap.Clone(new Rectangle(localX * scale, localZ * scale, scale, scale), worldMergeMap.PixelFormat);

                MCUtilsConsole.WriteLine($"Merging {r.loc.x}.{r.loc.z}.mca ...");
                var region1      = RegionLoader.LoadRegion(Path.Combine(world1Path, r.filename));
                var region2      = RegionLoader.LoadRegion(Path.Combine(world2Path, r.filename));
                var merger       = new RegionMerger(region1, region2, section);
                var mergedRegion = merger.Merge();

                MCUtilsConsole.WriteLine("Writing file...");
                FileStream stream = new FileStream(Path.Combine(outputPath, r.filename), FileMode.Create);
                RegionSerializer.WriteRegionToStream(mergedRegion, stream, Version.DefaultVersion);
                stream.Close();
            }
        }