Esempio n. 1
0
        /// <inheritdoc />
        public Structure Load(string filename)
        {
            var input = new NBTFile(filename);
            var nbt   = new NbtTree(input.GetDataInputStream()).Root;

            var dataVersion = nbt["DataVersion"].ToTagInt().Data;
            var author      = nbt.ContainsKey("author") ? nbt["author"].ToTagString().Data : null;

            var size   = nbt["size"].ToTagList().Select(node => node.ToTagInt().Data).ToArray();
            var width  = size[0];
            var height = size[1];
            var length = size[2];

            var palette  = LoadPalette(nbt);
            var blocks   = LoadBlocks(nbt, palette);
            var entities = LoadEntities(nbt);

            return(new StructureBlockStructure(author, width, height, length, blocks, entities));
        }
Esempio n. 2
0
        private void Scan_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                label.Content       = "开始扫描";
                Previous1.IsEnabled = false;
                scan.IsEnabled      = false;
                tab1.IsEnabled      = false;
                tab2.IsEnabled      = false;
                string FilePath = path.Text, OutputPath = path.Text + "\\.translation", Version = "Before 1.11";
                if (!Directory.Exists(OutputPath))
                {
                    Directory.CreateDirectory(OutputPath);
                }

                progress.IsIndeterminate      = false;
                totalProgress.IsIndeterminate = false;
                int len = Directory.GetFiles(FilePath + "\\region").Length;
                totalProgress.Maximum = len;

                using (Stream stream = new NBTFile(FilePath + "\\level.dat").GetDataInputStream())
                {
                    TagNodeCompound root = new NbtTree(stream).Root["Data"].ToTagCompound();
                    bool            flag = false;
                    if (!root.ContainsKey("Version"))
                    {
                        flag = true;
                    }
                    else
                    {
                        TagNodeCompound version = root["Version"].ToTagCompound();
                        Version = version["Name"].ToTagString().Data;
                        if (Version.Contains("w"))
                        {
                            string[] ver = Version.Split('w');
                            if (int.Parse(ver[0]) < 16)
                            {
                                flag = true;
                            }
                            else
                            {
                                if (int.Parse(ver[0]) == 16)
                                {
                                    if (int.Parse(ver[1].Substring(0, ver[1].Length - 1)) < 32)
                                    {
                                        flag = true;
                                    }
                                }
                            }
                        }
                        else if (!Version.Contains("Pre"))
                        {
                            string[] ver = Version.Split('.');
                            if (int.Parse(ver[0]) < 2)
                            {
                                if (int.Parse(ver[1]) < 11)
                                {
                                    flag = true;
                                }
                            }
                        }
                    }
                    if (flag)
                    {
                        CMDBLOCK = "Control";
                        SIGN     = "Sign";
                    }
                    string[] split    = OutputPath.Split('\\');
                    string   fileName = split[split.Length - 2];
                    while (File.Exists(OutputPath + "\\" + fileName + ".mtbl"))
                    {
                        fileName += "-";
                    }
                    bool[] settings = new bool[] { (bool)cb.IsChecked, (bool)sign.IsChecked, (bool)container.IsChecked, (bool)entity.IsChecked, (bool)spawner.IsChecked, (bool)saveinfo.IsChecked };
                    Task.Run(new Action(delegate { Start(Directory.GetFiles(FilePath + "\\region"), OutputPath + "\\" + fileName + ".mtbl", settings, Version, root); }));
                }
            }
            catch
            {
                (System.Windows.Application.Current.MainWindow as MetroWindow).ShowMessageAsync("无法打开存档", "存档不存在或已经损坏", MessageDialogStyle.Affirmative, new MetroDialogSettings()
                {
                    AffirmativeButtonText = "确定"
                });
            }
        }
        private static void RunOptionsAndReturnExitCode(CliOptions opts)
        {
            Console.Clear();
            var cgui = new ConsoleGui();

            var pbBlocks = new ConsoleGuiProgressBar(0, 0, Console.WindowWidth, 0, 1)
            {
                ForegroundColor = ConsoleColor.Green,
                BackgroundColor = ConsoleColor.DarkGray
            };

            var lBlocksTotal     = new ConsoleGuiLabel(0, 1, "Total Blocks    : {0}");
            var lBlocksRemaining = new ConsoleGuiLabel(0, 2, "Remaining Blocks: {0}");
            var lBlocksFailed    = new ConsoleGuiLabel(0, 3, "Failed Blocks   : {0}");
            var lStatus          = new ConsoleGuiLabel(0, 4, "Status          : {0}");

            cgui.Add(pbBlocks);
            cgui.Add(lBlocksTotal);
            cgui.Add(lBlocksRemaining);
            cgui.Add(lBlocksFailed);
            cgui.Add(lStatus);

            var failed = new List <int>();

            var inputMap  = NbtMap.Load(opts.InputMap);
            var outputMap = NbtMap.Load(opts.OutputMap);

            var inputSchematic = new NBTFile(opts.InputSchematic);
            var tag            = new NbtTree(inputSchematic.GetDataInputStream()).Root;

            var bLower    = tag["Blocks"].ToTagByteArray().Data;
            var addBlocks = new byte[(bLower.Length >> 1) + 1];

            if (tag.ContainsKey("AddBlocks"))
            {
                addBlocks = tag["AddBlocks"].ToTagByteArray().Data;
            }

            lStatus.Value = "Processing...";

            for (var index = 0; index < bLower.Length; index++)
            {
                short oldId;
                if ((index & 1) == 1)
                {
                    oldId = (short)(((addBlocks[index >> 1] & 0x0F) << 8) + (bLower[index] & 0xFF));
                }
                else
                {
                    oldId = (short)(((addBlocks[index >> 1] & 0xF0) << 4) + (bLower[index] & 0xFF));
                }

                if (!TranslateId(oldId, inputMap, outputMap, out var newId))
                {
                    failed.Add(oldId);
                }

                bLower[index]         = (byte)(newId & 0xFF);
                addBlocks[index >> 1] = (byte)(((index & 1) == 1) ?
                                               addBlocks[index >> 1] & 0xF0 | (newId >> 8) & 0xF
                    : addBlocks[index >> 1] & 0xF | ((newId >> 8) & 0xF) << 4);

                // Gui
                lBlocksTotal.Value     = index + 1;
                lBlocksRemaining.Value = bLower.Length - index - 1;
                lBlocksFailed.Value    = failed.Count;
                pbBlocks.Value         = (float)index / bLower.Length;

                if (index % 10000 == 0)
                {
                    cgui.Render();
                }
            }

            cgui.Render();

            tag["Blocks"] = new TagNodeByteArray(bLower);

            if (!tag.ContainsKey("AddBlocks"))
            {
                tag.Add("AddBlocks", new TagNodeByteArray(addBlocks));
            }
            else
            {
                tag["AddBlocks"] = new TagNodeByteArray(addBlocks);
            }

            lStatus.Value = "Saving...";
            cgui.Render();

            var schematicFile = new NBTFile(opts.OutputSchematic);

            using (var nbtStream = schematicFile.GetDataOutputStream())
            {
                if (nbtStream == null)
                {
                    return;
                }

                var tree = new NbtTree(tag, "Schematic");
                tree.WriteTo(nbtStream);
            }

            lStatus.Value = "Done. Press Enter.";
            cgui.Render();

            Console.WriteLine();
            foreach (var i in failed)
            {
                Console.WriteLine($"Failed ID: {i}");
            }

            Console.ReadKey();
        }
Esempio n. 4
0
        private void Scan_Click(object sender, RoutedEventArgs e)
        {
            //try
            //{
            using (Stream stream = new NBTFile(path.Text + "\\level.dat").GetDataInputStream())
            {
                TagNodeCompound root = new NbtTree(stream).Root["Data"].ToTagCompound();
                bool            flag = false;
                if (!root.ContainsKey("Version"))
                {
                    flag = true;
                }
                else
                {
                    TagNodeCompound version = root["Version"].ToTagCompound();
                    string          verName = version["Name"].ToTagString().Data;
                    if (verName.Contains("w"))
                    {
                        string[] ver = verName.Split('w');
                        if (int.Parse(ver[0]) < 16)
                        {
                            flag = true;
                        }
                        else
                        {
                            if (int.Parse(ver[0]) == 16)
                            {
                                if (int.Parse(ver[1].Substring(0, ver[1].Length - 1)) < 32)
                                {
                                    flag = true;
                                }
                            }
                        }
                    }
                    else if (!verName.Contains("Pre"))
                    {
                        string[] ver = verName.Split('.');
                        if (int.Parse(ver[0]) < 2)
                        {
                            if (int.Parse(ver[1]) < 11)
                            {
                                flag = true;
                            }
                        }
                    }
                }
                if (flag)
                {
                    CMDBLOCK = "Control";
                    SIGN     = "Sign";
                }
            }

            List <LanguageItem> ItemList       = new List <LanguageItem>();
            List <LanguageItem> SignList       = new List <LanguageItem>();
            List <LanguageItem> ContainerList  = new List <LanguageItem>();
            List <LanguageItem> EntityList     = new List <LanguageItem>();
            List <LanguageItem> MobSpawnerList = new List <LanguageItem>();

            foreach (CheckBox cb in languageFiles.Items)
            {
                if (cb.IsChecked == false)
                {
                    continue;
                }
                XmlDocument doc = new XmlDocument();
                doc.Load((languageFiles.Items[0] as CheckBox).ToolTip.ToString());
                foreach (XmlNode node in doc.ChildNodes[1].ChildNodes[1].ChildNodes)
                {
                    ItemList.Add(new LanguageItem {
                        Original = node.Attributes["Original"].Value, Translated = node.Attributes.Count == 2 ? node.Attributes[1].Value : "", Type = LangItemType.CommandBlock
                    });
                }
                foreach (XmlNode node in doc.ChildNodes[1].ChildNodes[2].ChildNodes)
                {
                    Sign o = new Sign(), t = new Sign();
                    o.Line1 = node.ChildNodes[0].Attributes[0].Value;
                    o.Line2 = node.ChildNodes[1].Attributes[0].Value;
                    o.Line3 = node.ChildNodes[2].Attributes[0].Value;
                    o.Line4 = node.ChildNodes[3].Attributes[0].Value;
                    t.Line1 = node.ChildNodes[0].Attributes.Count == 2 ? node.ChildNodes[0].Attributes[1].Value : "";
                    t.Line2 = node.ChildNodes[1].Attributes.Count == 2 ? node.ChildNodes[1].Attributes[1].Value : "";
                    t.Line3 = node.ChildNodes[2].Attributes.Count == 2 ? node.ChildNodes[2].Attributes[1].Value : "";
                    t.Line4 = node.ChildNodes[3].Attributes.Count == 2 ? node.ChildNodes[3].Attributes[1].Value : "";
                    SignList.Add(new LanguageItem {
                        Original = o, Translated = t, Type = LangItemType.Sign
                    });
                }
                foreach (XmlNode node in doc.ChildNodes[1].ChildNodes[3].ChildNodes)
                {
                    Container o = new Container(), t = new Container();
                    foreach (XmlNode item in node.ChildNodes)
                    {
                        if (item.Name == "CustomName")
                        {
                            o.CustomName = item.Attributes[0].Value;
                            t.CustomName = item.Attributes.Count == 2 ? item.Attributes[1].Value : "";
                        }
                        else if (item.Name == "Lock")
                        {
                            o.Lock = item.Attributes[0].Value;
                            t.Lock = item.Attributes.Count == 2 ? item.Attributes[1].Value : "";
                        }
                        else if (item.Name == "Book")
                        {
                            Book io = new Book(), it = new Book();
                            foreach (XmlNode data in item.ChildNodes)
                            {
                                if (data.Name == "Title")
                                {
                                    io.Title = data.Attributes[0].Value; it.Title = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                                else
                                {
                                    io.Pages = data.Attributes[0].Value; it.Pages = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                            }
                            o.Items.Add(io);
                            t.Items.Add(it);
                        }
                        else
                        {
                            Item io = new Item(), it = new Item();
                            foreach (XmlNode data in item.ChildNodes)
                            {
                                if (data.Name == "Name")
                                {
                                    io.Name = data.Attributes[0].Value; it.Name = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                                else
                                {
                                    io.Lore = data.Attributes[0].Value; it.Lore = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                            }
                            o.Items.Add(io);
                            t.Items.Add(it);
                        }
                    }
                    ContainerList.Add(new LanguageItem {
                        Original = o, Translated = t, Type = LangItemType.Container, Name = node.Attributes["Id"].Value, Info = node.Attributes["Pos"].Value
                    });
                }
                foreach (XmlNode node in doc.ChildNodes[1].ChildNodes[4].ChildNodes)
                {
                    Entity o = new Entity(), t = new Entity();
                    foreach (XmlNode item in node.ChildNodes)
                    {
                        if (item.Name == "CustomName")
                        {
                            o.CustomName = item.Attributes[0].Value; t.CustomName = item.Attributes.Count == 2 ? item.Attributes[1].Value : "";
                        }
                        else if (item.Name == "Book")
                        {
                            Book io = new Book(), it = new Book();
                            int  slot = -1;
                            switch (item.Attributes["Slot"].Value)
                            {
                            case "主手": slot = 0; break;

                            case "副手": slot = 1; break;

                            case "头盔": slot = 2; break;

                            case "胸甲": slot = 3; break;

                            case "护腿": slot = 4; break;

                            case "靴子": slot = 5; break;
                            }
                            foreach (XmlNode data in item.ChildNodes)
                            {
                                if (data.Name == "Title")
                                {
                                    io.Title = data.Attributes[0].Value; it.Title = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                                else
                                {
                                    io.Pages = data.Attributes[0].Value; it.Pages = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                            }
                            o.Equipments[slot] = io;
                            t.Equipments[slot] = it;
                        }
                        else
                        {
                            Item io = new Item(), it = new Item();
                            int  slot = -1;
                            switch (item.Attributes["Slot"].Value)
                            {
                            case "主手": slot = 0; break;

                            case "副手": slot = 1; break;

                            case "头盔": slot = 2; break;

                            case "胸甲": slot = 3; break;

                            case "护腿": slot = 4; break;

                            case "靴子": slot = 5; break;
                            }
                            if (slot == -1)
                            {
                                continue;
                            }
                            foreach (XmlNode data in item.ChildNodes)
                            {
                                if (data.Name == "Name")
                                {
                                    io.Name = data.Attributes[0].Value; it.Name = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                                else
                                {
                                    io.Lore = data.Attributes[0].Value; it.Lore = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                            }
                            o.Equipments[slot] = io;
                            t.Equipments[slot] = it;
                        }
                    }
                    EntityList.Add(new LanguageItem {
                        Original = o, Translated = t, Type = LangItemType.Entity, Name = node.Attributes["Id"].Value
                    });
                }
                foreach (XmlNode node in doc.ChildNodes[1].ChildNodes[5].ChildNodes)
                {
                    Entity o = new Entity(), t = new Entity();
                    foreach (XmlNode item in node.ChildNodes)
                    {
                        if (item.Name == "CustomName")
                        {
                            o.CustomName = item.Attributes[0].Value; t.CustomName = item.Attributes.Count == 2 ? item.Attributes[1].Value : "";
                        }
                        else if (item.Name == "Book")
                        {
                            Book io = new Book(), it = new Book();
                            int  slot = -1;
                            switch (item.Attributes["Slot"].Value)
                            {
                            case "主手": slot = 0; break;

                            case "副手": slot = 1; break;

                            case "头盔": slot = 2; break;

                            case "胸甲": slot = 3; break;

                            case "护腿": slot = 4; break;

                            case "靴子": slot = 5; break;
                            }
                            foreach (XmlNode data in item.ChildNodes)
                            {
                                if (data.Name == "Title")
                                {
                                    io.Title = data.Attributes[0].Value; it.Title = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                                else
                                {
                                    io.Pages = data.Attributes[0].Value; it.Pages = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                            }
                            o.Equipments[slot] = io;
                            t.Equipments[slot] = it;
                        }
                        else
                        {
                            Item io = new Item(), it = new Item();
                            int  slot = -1;
                            switch (item.Attributes["Slot"].Value)
                            {
                            case "主手": slot = 0; break;

                            case "副手": slot = 1; break;

                            case "头盔": slot = 2; break;

                            case "胸甲": slot = 3; break;

                            case "护腿": slot = 4; break;

                            case "靴子": slot = 5; break;
                            }
                            if (slot == -1)
                            {
                                continue;
                            }
                            foreach (XmlNode data in item.ChildNodes)
                            {
                                if (data.Name == "Name")
                                {
                                    io.Name = data.Attributes[0].Value; it.Name = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                                else
                                {
                                    io.Lore = data.Attributes[0].Value; it.Lore = data.Attributes.Count == 2 ? data.Attributes[1].Value : "";
                                }
                            }
                            o.Equipments[slot] = io;
                            t.Equipments[slot] = it;
                        }
                    }
                    MobSpawnerList.Add(new LanguageItem {
                        Original = o, Translated = t, Type = LangItemType.MobSpawner, Name = node.Attributes["Id"].Value, Info = node.Attributes["Pos"].Value
                    });
                }
            }

            totalProgress.Maximum         = Directory.GetFiles(path.Text + "\\region").Length;
            totalProgress.IsIndeterminate = false;
            progress.IsIndeterminate      = false;
            string p = path.Text;

            Task.Run(new Action(delegate { Apply(p, ItemList, SignList, ContainerList, EntityList, MobSpawnerList); }));
            //}
            //catch
            //{
            //    (System.Windows.Application.Current.MainWindow as MetroWindow).ShowMessageAsync("无法打开文件", "文件不存在或已经损坏", MessageDialogStyle.Affirmative, new MetroDialogSettings() { AffirmativeButtonText = "确定" });
            //}
        }