private void button6_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "Sims 3 Package|*.package";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != "")
            {

                Stream file = File.Open(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite);
                Database db = new Database(file, false);

                if (label9.Text != "") addToDBPF(label9.Text, textBox9.Text, textBox11.Text, textBox5.Text, db);
                if (label13.Text != "") addToDBPF(label13.Text, textBox13.Text, textBox12.Text, textBox14.Text, db);
                if (label14.Text != "") addToDBPF(label14.Text, textBox16.Text, textBox15.Text, textBox17.Text, db);
                if (label15.Text != "") addToDBPF(label15.Text, textBox19.Text, textBox18.Text, textBox20.Text, db);
                if (label16.Text != "") addToDBPF(label16.Text, textBox22.Text, textBox21.Text, textBox23.Text, db);

                db.Commit(true);
                file.Close();

            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Stream file;
            Database db;
            ResourceKey rkey;
            Stream input;

            file = File.Open("P:\\Games\\Working\\Skins\\afface - dark\\afface-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\afface - dark\\afface-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x304910BE2CB17463, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\afface - light\\afface-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\afface - dark\\afface-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x40E5744B0DBFC323, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - dark\\af-tf-efbody fullybarbie-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - dark\\fullybarbie-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x185D7126C73DC404, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x13F78C079D70D7F8, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - dark\\af-tf-efbody nipplesandpubes-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - dark\\nipplesandpubes-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x185D7126C73DC404, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x13F78C079D70D7F8, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - dark\\af-tf-efbody nipplesnopubes-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - dark\\nipplesnopubes-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x185D7126C73DC404, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x13F78C079D70D7F8, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - light\\af-tf-efbody fullybarbie-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - light\\fullybarbie.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x798410349B50700C, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xB4CDC208D8D51BF0, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - light\\af-tf-efbody nipplesandpubes-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - light\\nipplesandpubes.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x798410349B50700C, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xB4CDC208D8D51BF0, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - light\\af-tf-efbody nipplesnopubes-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\af-tf-efbody - light\\nipplesnopubes.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x798410349B50700C, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xB4CDC208D8D51BF0, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\am-embody fullhair-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\ambody-fullhair.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0xB1D30A51A5ED1903, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xD4EE1715BCEA0F77, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\am-embody hairless-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\ambody-hairless.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0xB1D30A51A5ED1903, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xD4EE1715BCEA0F77, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\am-embody lighterchest-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\ambody-lighterchest.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0xB1D30A51A5ED1903, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xD4EE1715BCEA0F77, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\am-embody nopubesnohappytrail-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\ambody-nopubesnohappytraillighthair.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0xB1D30A51A5ED1903, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xD4EE1715BCEA0F77, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\am-embody lighthairwithpubesnohappytrail-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - dark\\am-lighthairwithpubesnohappytrail.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0xB1D30A51A5ED1903, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0xD4EE1715BCEA0F77, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\am-embody fullhair-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\ambody-fullhair.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x4DB46D1662895FC3, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x9121FA3B0B65C88F, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\am-embody hairless-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\ambodyhairless.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x4DB46D1662895FC3, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x9121FA3B0B65C88F, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\am-embody lighterchest-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\ambody-lighterchest.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x4DB46D1662895FC3, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x9121FA3B0B65C88F, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\am-embody nopubesnohappytrail-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\ambody-nopubesnohappytraillighthair.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x4DB46D1662895FC3, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x9121FA3B0B65C88F, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\am-embody lighthairwithpubesnohappytrail-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\am-embody - light\\am-lighthairwithpubesnohappytrail.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x4DB46D1662895FC3, 11720834, 0);
            db.SetResourceStream(rkey, input);
            rkey = new ResourceKey((ulong)0x9121FA3B0B65C88F, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\amface - dark\\amface-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\amface - dark\\amface-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x6CA41C919ECE1BE0, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\amface - light\\amface-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\amface - light\\amface.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x6F853F0E35157E24, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\efface - dark\\efface-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\efface - dark\\efface-dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x629E533C0262E927, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\efface - light\\efface-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\efface - light\\efface.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x503C527E546E8C5F, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\emface - dark\\emface-dark.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\emface - dark\\emface - dark.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x21F6BAC9B048B934, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("P:\\Games\\Working\\Skins\\emface - light\\emface-light.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("P:\\Games\\Working\\Skins\\emface - light\\emface.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x7EDD5D417BC66680, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("J:\\Users\\Stuart\\AppData\\Roaming\\Miranda\\Received Files\\[email protected]\\afBottomBriefs_biki_no.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("J:\\Users\\Stuart\\AppData\\Roaming\\Miranda\\Received Files\\[email protected]\\afBottomBriefs_biki_0x849f383fd886bb35_0x00B2D882-0x00000000-0x849F383FD886BB35.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x849F383FD886BB35, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("J:\\Users\\Stuart\\AppData\\Roaming\\Miranda\\Received Files\\[email protected]\\afTopBra_Strapless_no.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("J:\\Users\\Stuart\\AppData\\Roaming\\Miranda\\Received Files\\[email protected]\\afTopBra_Strapless__0x0baf3c417b3d7bd2_0x00B2D882-0x00000000-0x0BAF3C417B3D7BD2.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0x0BAF3C417B3D7BD2, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            file = File.Open("J:\\Users\\Stuart\\AppData\\Roaming\\Miranda\\Received Files\\[email protected]\\NoZzzs.package", FileMode.Create, FileAccess.ReadWrite);
            input = File.Open("J:\\Users\\Stuart\\AppData\\Roaming\\Miranda\\Received Files\\[email protected]\\z_0xaf63bd4c8601b7a5_0x00B2D882-0x00000000-0xAF63BD4C8601B7A5.dds", FileMode.Open, FileAccess.Read);
            db = new Database(file, false);
            rkey = new ResourceKey((ulong)0xAF63BD4C8601B7A5, 11720834, 0);
            db.SetResourceStream(rkey, input);
            db.Commit(true);
            input.Close();
            file.Close();

            MessageBox.Show("Done");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
            if (openFileDialog2.FileName != "")
            {
                Stream inputFile = File.OpenRead(openFileDialog2.FileName);

                ListViewItem item = listView1.SelectedItems[0];
                int destIndexId = Convert.ToInt32(item.SubItems[5].Text);
                DatabasePackedFile.Entry indexEntry = (DatabasePackedFile.Entry)indexEntries[destIndexId];

                // Open DBPF
                Stream dbpf = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite);
                Database db;

                try
                {
                    db = new Database(dbpf, true);
                }
                catch (NotAPackageException)
                {
                    MessageBox.Show("bad file: {0}", openFileDialog2.FileName);
                    dbpf.Close();
                    return;
                }

                db.SetResourceStream(indexEntry.Key, inputFile);

                db.Commit(true);

                dbpf.Close();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {
                ListViewItem item = listView1.SelectedItems[0];
                int destIndexId = Convert.ToInt32(item.SubItems[5].Text);

                DatabasePackedFile.Entry indexEntry = new DatabasePackedFile.Entry();
                DatabasePackedFile.Entry oldEntry = new DatabasePackedFile.Entry();

                indexEntry = (DatabasePackedFile.Entry)indexEntries[destIndexId];
                oldEntry = (DatabasePackedFile.Entry)indexEntries[destIndexId];

                indexEntry.Key.InstanceId = UInt64.Parse(txtInstanceID.Text, System.Globalization.NumberStyles.AllowHexSpecifier);
                indexEntry.Key.GroupId = UInt32.Parse(txtGroupId.Text, System.Globalization.NumberStyles.AllowHexSpecifier);
                indexEntry.Offset = Convert.ToInt64(txtOffset.Text);
                //indexEntry.Key.InstanceId = 0;

                // Open DBPF
                Stream input = File.Open(this.currentFile.FullName, FileMode.Open, FileAccess.ReadWrite);

                /*
                DatabasePackedFile dbpf = new DatabasePackedFile();
                try
                {
                    dbpf.Read(input);
                }
                catch (NotAPackageException)
                {
                    MessageBox.Show("bad file: {0}", openFileDialog1.FileName);
                    input.Close();
                    return;
                }

                input.Seek(0, SeekOrigin.Begin);
                */

                Database db;

                try
                {
                    db = new Database(input, true);
                }
                catch (NotAPackageException)
                {
                    MessageBox.Show("bad file: {0}", this.currentFile.FullName);
                    input.Close();
                    return;
                }

                db.MoveResource(oldEntry.Key, indexEntry.Key);

                indexEntries[destIndexId] = indexEntry;

                item.SubItems[3].Text = indexEntry.Key.InstanceId.ToString("X16");

                db.Commit(true);

                input.Close();
            }
        }