Пример #1
0
        public void TestWzFileWithImageAndPropertySerializes()
        {
            var intProp = new WzIntProperty("int1", 100);
            var img     = new WzImage("test.img");
            var file    = new WzFile(1, WzMapleVersionType.Classic);

            img.AddProperty(intProp);
            file.WzDirectory.WzImages.Add(img);

            using var ms = new MemoryStream();
            file.Serialize(ms);
            var contents        = Encoding.ASCII.GetString(ms.ToArray());
            var deserialized    = WzObject.DeserializeFile(contents);
            var deserializedImg = deserialized.WzDirectory.GetImageByName(img.Name);

            Assert.IsNotNull(deserializedImg);
            Assert.AreEqual(intProp.Value, deserializedImg[intProp.Name].GetInt());
        }
Пример #2
0
        private void UpdateNodes()
        {
            void ApplyUpdates(TabControl.TabPageCollection pages)
            {
                foreach (TabPage tab in pages)
                {
                    var view         = (CItemGroup)tab.Controls[0];
                    var table        = view.GridView;
                    var replacements = view.TextBox.Lines;

                    for (var i = 0; i < table.RowCount; i++)
                    {
                        var row = table.Rows[i];
                        var img = (WzImageProperty)row.Tag;

                        // replace price value
                        var priceObject = row.Cells[2]?.Value;
                        if (priceObject != null)
                        {
                            var priceImg = img.GetFromPath("Price");

                            // convert to string then parse as int due to edited values being a string while original, un-values are still ints
                            var price = int.Parse(row.Cells[2].Value.ToString());
                            if (priceImg == null && price > 0)
                            {
                                img.ParentImage.Changed = true;
                                img.WzProperties.Add(new WzIntProperty("Price", price));
                            }
                            else if (priceImg != null && priceImg.GetInt() != price)
                            {
                                img.ParentImage.Changed         = true;
                                ((WzIntProperty)priceImg).Value = price;
                            }
                        }

                        // replace donor value
                        var donorImg = img.GetFromPath("isDonor");
                        var donor    = int.Parse(row.Cells[3].Value.ToString()) == 1;
                        if (donorImg == null && donor)
                        {
                            img.ParentImage.Changed = true;
                            img.WzProperties.Add(new WzIntProperty("isDonor", 1));
                        }
                        else if (donorImg != null && (donorImg.GetInt() == 1) != donor)
                        {
                            img.ParentImage.Changed         = true;
                            ((WzIntProperty)donorImg).Value = donor ? 1 : 0;
                        }

                        // replace period value
                        var periodImg = img.GetFromPath("Period");
                        var period    = int.Parse(row.Cells[4].Value.ToString());
                        if (periodImg == null && period > 0)
                        {
                            img.ParentImage.Changed = true;
                            img.WzProperties.Add(new WzIntProperty("Period", 1));
                        }
                        else if (periodImg != null && periodImg.GetInt() != period)
                        {
                            img.ParentImage.Changed          = true;
                            ((WzIntProperty)periodImg).Value = period;
                        }
                    }

                    for (var i = 0; i < replacements.Length; i++)
                    {
                        if (!int.TryParse(replacements[i], out var itemId))
                        {
                            MessageBox.Show($"Invalid ID at line {i}: {replacements[i]}");
                            return;
                        }

                        // replace existing item
                        if (i < table.RowCount)
                        {
                            var row = table.Rows[i];
                            var img = (WzImageProperty)row.Tag;

                            var itemIdImg = img.GetFromPath("ItemId");
                            img.ParentImage.Changed          = true;
                            ((WzIntProperty)itemIdImg).Value = itemId;
                        }
                        else
                        {
                            // insert new item
                            var isDonor = MainPage.SelectedIndex == 1;
                            var sn      = ItemCategory.GenerateSn(isDonor, itemId);

                            var sub = new WzSubProperty((++largestNodeValue).ToString());
                            sub.AddProperty(new WzIntProperty("Count", 1));
                            sub.AddProperty(new WzIntProperty("Gender", 2));
                            sub.AddProperty(new WzIntProperty("ItemId", itemId));
                            sub.AddProperty(new WzIntProperty("OnSale", 1));
                            sub.AddProperty(new WzIntProperty("Period", 90));
                            sub.AddProperty(new WzIntProperty("Price", 1));
                            sub.AddProperty(new WzIntProperty("Priority", 99));
                            sub.AddProperty(new WzIntProperty("isDonor", isDonor ? 1 : 0));
                            sub.AddProperty(new WzIntProperty("SN", sn));
                            Debug.WriteLine($"Generated SN for item {itemId}: {sn}. Node {sub.Name}");
                            commodityImg.AddProperty(sub);
                            sub.ParentImage.Changed = true;
                        }
                    }
                }
            }

            ApplyUpdates(RegularViewer.tabControl.TabPages);
            ApplyUpdates(DonorViewer.tabControl.TabPages);
        }