private void addToDBPF(string filename, string typeID, string groupID, string instanceID, Database db)
 {
     Stream input = File.Open(filename, FileMode.Open, FileAccess.Read);
     ResourceKey rkey = new ResourceKey((ulong)Gibbed.Helpers.StringHelpers.ParseHex64(instanceID), (uint)Gibbed.Helpers.StringHelpers.ParseHex32(typeID), (uint)Gibbed.Helpers.StringHelpers.ParseHex32(groupID));
     db.SetResourceStream(rkey, input);
     input.Close();
 }
        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 listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {

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

                txtLongName.Text = lookupList.lookup(indexEntry.Key.TypeId).longName;
                txtInstanceID.Text = indexEntry.Key.InstanceId.ToString("X16");
                txtOffset.Text = indexEntry.Offset.ToString();
                txtGroupId.Text = indexEntry.Key.GroupId.ToString("X8");

                textBox25.Text = "key:" + indexEntry.Key.TypeId.ToString("X8") + ":" + txtGroupId.Text + ":" + txtInstanceID.Text;

                Stream input = File.OpenRead(this.currentFile.FullName);

                Database db = new Database(input, true);

                textBox4.Visible = false;
                panel1.Visible = false;
                listView2.Visible = false;

                this.lockImage = true;
                chkShowAlpha.Visible = false;
                chkShowBlue.Visible = false;
                chkShowGreen.Visible = false;
                chkShowRed.Visible = false;
                this.lockImage = false;

                switch (indexEntry.Key.TypeId)
                {
                    case 0x034AEECB: // Cas Part
                        textBox4.Visible = true;
                        textBox4.Text = "";
                        Stream caspStream = db.GetResourceStream(indexEntry.Key);
                        showCaspFile(caspStream);
                        caspStream.Close();
                        break;

                    case 0x01661233: // MODL
                        listView2.Visible = true;
                        listView2.Items.Clear();
                        Stream modlStream = db.GetResourceStream(indexEntry.Key);
                        showModlFile(modlStream);
                        modlStream.Close();
                        break;
                    case 0x067CAA11: // Blend Geometry
                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream bgeoStream = db.GetResourceStream(indexEntry.Key);
                        showBlendGeomFile(bgeoStream);
                        bgeoStream.Close();

                        break;

                    case 0x0358B08A: // Face Part File
                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream fpartStream = db.GetResourceStream(indexEntry.Key);
                        showFacePartFile(fpartStream);
                        fpartStream.Close();

                        break;

                    case 0xb52f5055: // Facial Blend

                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream fblendStream = db.GetResourceStream(indexEntry.Key);
                        showFacialBlendFile(fblendStream);
                        fblendStream.Close();

                        break;

                    case 0x033A1435: // Texture Compositor

                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream txtcStream = db.GetResourceStream(indexEntry.Key);
                        showTextureCompositorFile(txtcStream);
                        txtcStream.Close();
                        break;

                    case 0x736884F1: // VPXY

                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream vpxyStream = db.GetResourceStream(indexEntry.Key);
                        showVpxyFile(vpxyStream);
                        vpxyStream.Close();

                        break;

                    case 0x0166038C: // Key name

                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream keyNameFile = db.GetResourceStream(indexEntry.Key);
                        showKeyNameFile(keyNameFile);
                        keyNameFile.Close();

                        break;

                    case 0x00b2d882: // DDS

                        panel1.Visible = true;

                        ddsFile.Load(db.GetResourceStream(indexEntry.Key));
                        this.lockImage = true;
                        pictureBox1.Image = ddsFile.Image();

                        chkShowAlpha.Visible = true;
                        chkShowBlue.Visible = true;
                        chkShowGreen.Visible = true;
                        chkShowRed.Visible = true;
                        chkShowAlpha.Checked = false;
                        chkShowBlue.Checked = true;
                        chkShowGreen.Checked = true;
                        chkShowRed.Checked = true;
                        this.lockImage = false;

                        break;
                    // XML
                    case 0x73e93eeb:
                    case 0xd4d9fbe5:
                    case 0x0333406c:
                        textBox4.Visible = true;
                        textBox4.Text = Encoding.ASCII.GetString(db.GetResource(indexEntry.Key));
                        break;
                    case 0x015a1849:
                        // GEOM
                        listView2.Visible = true;
                        listView2.Items.Clear();

                        Stream geominput = db.GetResourceStream(indexEntry.Key);
                        showGeomFile(geominput);
                        geominput.Close();

                        break;
                }

                input.Close();

            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 1)
            {

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

                metaEntry mEntry = lookupList.lookup(indexEntry.Key.TypeId);
                if (mEntry.shortName == "") { mEntry.shortName = "unk"; }
                txtLongName.Text = mEntry.longName;
                txtInstanceID.Text = indexEntry.Key.InstanceId.ToString("X16");
                txtOffset.Text = indexEntry.Offset.ToString();

                Stream input = File.OpenRead(this.currentFile.FullName);

                Database db = new Database(input, true);

                Stream output = db.GetResourceStream(indexEntry.Key);
                FileStream saveFile = new FileStream(this.currentFile.DirectoryName + "\\" + txtLongName.Text + "_" + indexEntry.Key.TypeId.ToString("X8") + "_" + indexEntry.Key.GroupId.ToString("X8") + "_" + txtInstanceID.Text + "." + mEntry.shortName, FileMode.Create, FileAccess.Write);

                ReadWriteStream(output, saveFile);

                saveFile.Close();
                output.Close();
                input.Close();

            }
        }
        public void ProcessFiles()
        {
            DateTime start = new DateTime();
            start = DateTime.Now;

            int numFiles = this.fileList.Count;
            string filename = "";

            for (int i = 0; i < numFiles; i++)
            {
                try
                {
                    filename = this.fileList.Pop().ToString();
                    FileInfo f = new FileInfo(filename);
                    Console.WriteLine("Processing " + f.Name);
                    Stream input = File.OpenRead(filename);
                    Database db = new Database(input);

                    foreach (ResourceKey entry in db.Entries.Keys)
                    {
                        if (entry.GroupId == 0x00DCE592 || entry.GroupId == 0x01DCE592)
                        //if (entry.GroupId == 0x00FACADE || entry.GroupId == 0x01FACADE)
                        {
                            ListViewItem item = new ListViewItem();
                            item.Text = "";
                            item.SubItems.Add(entry.TypeId.ToString("X8"));
                            item.SubItems.Add(entry.GroupId.ToString("X8"));
                            item.SubItems.Add(entry.InstanceId.ToString("X16"));
                            item.SubItems.Add(f.Name);
                            item.SubItems.Add("");
                            listView1.Items.Add(item);

                        }
                    }

                    input.Close();

                }
                catch (System.Exception excpt)
                {
                    MessageBox.Show(excpt.Message + " " + excpt.StackTrace);
                }
            }

            DateTime stop = new DateTime();
            stop = DateTime.Now;
            int timeTaken = (stop.Second - start.Second);
            if (timeTaken == 0) timeTaken++;
        }
        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 button3_Click(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;

            string openPath = "";
            if (comboBox1.Text.StartsWith("\\"))
            {
                openPath = MadScience.Helpers.findSims3Root() + comboBox1.Text;
            }
            else
            {
                openPath = comboBox1.Text;
            }

            if (openPath == "") return;

            Stream input = File.OpenRead(openPath);

            Database db = new Database(input, true);

            input.Seek(0, SeekOrigin.Begin);

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

            this.currentFile = new FileInfo(openPath);

            lookupTypes();
            indexEntries.Clear();
            listView1.Items.Clear();

            txtPkgIndexType.Text = dbpf.IndexType.ToString();
            txtPkgNumChunks.Text = dbpf.Entries.Count.ToString();

            uint searchTypeID = 0;
            if (comboBox2.Text != "") { searchTypeID = Gibbed.Helpers.StringHelpers.ParseHex32(comboBox2.Text); }
            ulong instanceID = 0;
            bool searchInstance = false;
            if (textBox8.Text != "")
            {
                instanceID = Gibbed.Helpers.StringHelpers.ParseHex64(textBox8.Text);
                searchInstance = true;
            }

            string tempChunk = "";
            int count = 0;

            progressBar1.Maximum = dbpf.Entries.Count;

            for (int i = 0; i < dbpf.Entries.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                DatabasePackedFile.Entry entry = dbpf.Entries[i];
                progressBar1.Value++;

                bool searchChunk = false;

                if (entry.Key.TypeId == searchTypeID)
                {
                    if (searchInstance == true)
                    {
                        if (entry.Key.InstanceId == instanceID)
                        {
                            searchChunk = true;
                        }
                    }
                    else
                    {
                        searchChunk = true;
                    }
                }
                else
                {
                    if (searchInstance == true)
                    {
                        if (entry.Key.InstanceId == instanceID)
                        {
                            searchChunk = true;
                        }
                    }
                }
                //if (searchTypeID == 0) { searchChunk = true; }

                if (searchChunk)
                {

                    tempChunk = Encoding.UTF8.GetString(db.GetResource(entry.Key));
                    bool addChunk = false;

                    // If everything is blank we just list... everything
                    if (textBox3.Text == "" && textBox6.Text == "" && textBox7.Text == "")
                    {
                        addChunk = true;
                    }
                    else
                    {

                        if (tempChunk.Contains(textBox3.Text))
                        {
                            if (textBox6.Text != "")
                            {
                                if (tempChunk.Contains(textBox6.Text))
                                {
                                    if (textBox7.Text != "")
                                    {
                                        if (tempChunk.Contains(textBox7.Text))
                                        {
                                            addChunk = true;
                                        }
                                    }
                                    else
                                    {
                                        addChunk = true;
                                    }
                                }
                            }
                            else
                            {
                                addChunk = true;
                            }

                        }

                    }

                    if (addChunk == true)
                    {
                        item.Text = this.lookupList.lookup(entry.Key.TypeId).shortName;
                        item.SubItems.Add(entry.Key.TypeId.ToString("X8"));
                        item.SubItems.Add(entry.Key.GroupId.ToString("X8"));
                        item.SubItems.Add(entry.Key.InstanceId.ToString("X16"));
                        item.SubItems.Add(entry.DecompressedSize.ToString());
                        item.SubItems.Add(count.ToString());
                        listView1.Items.Add(item);
                        indexEntries.Add(entry);
                        count++;
                    }
                }
            }

            input.Close();
        }
        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();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;

            textBox1.Text = "";

            string firstPath = textBox2.Text;
            FileInfo f1 = new FileInfo(firstPath);
            Stream firstStream = File.OpenRead(firstPath);
            Database firstDb = new Database(firstStream);

            string secondPath = textBox3.Text;
            FileInfo f2 = new FileInfo(secondPath);
            Stream secondStream = File.OpenRead(secondPath);
            Database secondDb = new Database(secondStream);

            textBox1.Text += firstStream.Length.ToString() + " bytes vs " + secondStream.Length.ToString() + " bytes" + Environment.NewLine;
            textBox1.Text += "Comparing " + f1.Name + " to " + f2.Name + Environment.NewLine;

            textBox1.Text += f1.Name + " has " + firstDb.Entries.Count.ToString() + Environment.NewLine;
            textBox1.Text += f2.Name + " has " + secondDb.Entries.Count.ToString() + Environment.NewLine;

            StringBuilder sb = new StringBuilder();

            progressBar1.Maximum = firstDb.Entries.Count;

            int numChanged = 0;
            int identical = 0;

            foreach (KeyValuePair<ResourceKey, Database.Entry> entry in firstDb.Entries)
            {
                progressBar1.Value++;
                if (secondDb.Entries.ContainsKey(entry.Key) == false)
                {
                    numChanged++;
                    sb.AppendLine("Only in " + f1.Name + ": " + entry.Key + " (Size: " + entry.Value.CompressedSize + ")" );
                }
            }

            progressBar1.Value = 0;
            progressBar1.Maximum = secondDb.Entries.Count;

            foreach (KeyValuePair<ResourceKey, Database.Entry> entry in secondDb.Entries)
            {
                progressBar1.Value++;
                if (firstDb.Entries.ContainsKey(entry.Key) == false)
                {
                    numChanged++;
                    sb.AppendLine("Only in " + f2.Name + ": " + entry.Key + " (Size: " + entry.Value.CompressedSize + ")");
                }
            }

            numChanged = 0;

            progressBar1.Value = 0;
            progressBar1.Maximum = firstDb.Entries.Count;

            System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
            cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();

            foreach (KeyValuePair<ResourceKey, Database.Entry> entry in firstDb.Entries)
            {
                progressBar1.Value++;
                bool different = false;

                if (secondDb.Entries.ContainsKey(entry.Key) == false)
                {
                    // Check the second file to see if it has a chunk with the same type and group, but a different instance,
                    // but also has the same size and hash etc
                    foreach (KeyValuePair<ResourceKey, Database.Entry> entry2 in secondDb.Entries)
                    {
                        if (entry.Key.TypeId == entry2.Key.TypeId && entry.Key.GroupId == entry2.Key.GroupId && entry.Value.CompressedSize == entry2.Value.CompressedSize && entry.Value.DecompressedSize == entry2.Value.DecompressedSize)
                        {
                            identical++;
                            sb.AppendLine("Chunk " + progressBar1.Value.ToString() + ": Identical chunks: " + entry.Key + " and " + entry2.Key);
                            different = true;
                            break;
                        }
                    }
                    if (!different)
                    {
                        sb.AppendLine("Chunk " + progressBar1.Value.ToString() + ": Does not appear in second package " + entry.Key);
                    }
                    continue;
                }

                ResourceKey key = entry.Key;

                string diffReason = "";

                if (firstDb.Entries[key].DecompressedSize != secondDb.Entries[key].DecompressedSize)
                {
                    diffReason = "Size";
                    different = true;
                }
                else
                {

                    //if (firstDb.Entries[key].

                    byte[] firstData = firstDb.GetResource(key);
                    byte[] secondData = secondDb.GetResource(key);

                    if (firstData.Length != secondData.Length)
                    {
                        diffReason = "Size2";
                        different = true;
                    }
                    else
                    {
                        byte[] hash1 = cryptHandler.ComputeHash(firstData);
                        byte[] hash2 = cryptHandler.ComputeHash(secondData);

                        if (hash1.ToString() != hash2.ToString())
                        {
                            diffReason = "Hash of contents";
                            different = true;
                        }
                    }
                }

                if (different == true)
                {
                    numChanged++;
                    sb.AppendLine("Chunk " + progressBar1.Value.ToString() + ": Differs in " + diffReason + ": " + key);
                }
                else
                {
                    identical++;
                    sb.AppendLine("Chunk " + progressBar1.Value.ToString() + ": Identical chunks: " + key);
                }
            }

            textBox1.Text += sb.ToString();
            textBox1.Text += identical.ToString() + " identical entries" + Environment.NewLine;
            textBox1.Text += numChanged.ToString() + " changed entries" + Environment.NewLine;

            progressBar1.Value = 0;
            secondStream.Close();
            firstStream.Close();
        }