private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "Please locate the directory where you want to save this package";
            folderBrowserDialog1.ShowDialog();

            //txtSliderName.Text = txtSliderName.Text;

            // Make the instance id
            ulong instanceid = MadScience.StringHelpers.HashFNV64(txtSliderName.Text);
            ulong instance32 = (ulong)MadScience.StringHelpers.HashFNV32(txtSliderName.Text);

            ulong instanceLeft = MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "Left");
            ulong instanceRight = MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "Right");

            // Keys
            ResourceKey nameMapKey = new ResourceKey(0x0166038C, 0, instanceid);
            ResourceKey stblKey = new ResourceKey(0x220557DA, 0, instance32);
            ResourceKey blendunitKey = new ResourceKey(0xB52F5055, 0, instanceid);

            ResourceKey facialblendLeft = new MadScience.Wrappers.ResourceKey(0x0358B08A, 0x0, instanceLeft);
            ResourceKey facialblendRight = new MadScience.Wrappers.ResourceKey(0x0358B08A, 0x0, instanceRight);

            ResourceKey blendGeomLeft = new ResourceKey(0x067CAA11, 0, instanceLeft);
            ResourceKey blendGeomRight = new ResourceKey(0x067CAA11, 0, instanceRight);

            ResourceKey vpxyRightMale = new ResourceKey(0x736884F1, 0x0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "RightMale"));
            ResourceKey vpxyLeftMale = new ResourceKey(0x736884F1, 0x0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "LeftMale"));
            ResourceKey boneDeltaRightMale = new ResourceKey(0x0355E0A6, 0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "RightMale"));
            ResourceKey boneDeltaLeftMale = new ResourceKey(0x0355E0A6, 0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "LeftMale"));
            ResourceKey vpxyRightFemale = new ResourceKey(0x736884F1, 0x0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "RightFemale"));
            ResourceKey vpxyLeftFemale = new ResourceKey(0x736884F1, 0x0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "LeftFemale"));
            ResourceKey boneDeltaRightFemale = new ResourceKey(0x0355E0A6, 0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "RightFemale"));
            ResourceKey boneDeltaLeftFemale = new ResourceKey(0x0355E0A6, 0, MadScience.StringHelpers.HashFNV64(txtSliderName.Text + "LeftFemale"));

            #region Name Map
            NameMap namemap = new NameMap();
            namemap.entries.Add(instanceid, txtSliderName.Text);
            namemap.entries.Add(instanceLeft, txtSliderName.Text + "Left");
            namemap.entries.Add(instanceRight, txtSliderName.Text + "Right");
            if (chkMFLink.Checked)
            {
                namemap.entries.Add(vpxyRightFemale.instanceId, txtSliderName.Text + "RightFemale");
                namemap.entries.Add(vpxyLeftFemale.instanceId, txtSliderName.Text + "LeftFemale");
            }
            else
            {
                if (chkMale.Checked)
                {
                    namemap.entries.Add(vpxyRightMale.instanceId, txtSliderName.Text + "RightMale");
                    namemap.entries.Add(vpxyLeftMale.instanceId, txtSliderName.Text + "LeftMale");
                }
                if (chkFemale.Checked)
                {
                    namemap.entries.Add(vpxyRightFemale.instanceId, txtSliderName.Text + "RightFemale");
                    namemap.entries.Add(vpxyLeftFemale.instanceId, txtSliderName.Text + "LeftFemale");
                }
            }
            Stream nameMapFile = namemap.Save();
            namemap = null;
            #endregion

            #region String Table
            // Start with the STBL
            STBL stbl = new STBL();
            stbl.Items.Add(new STBLEntry(instanceid, txtSliderString.Text));
            Stream stblFile = stbl.Save();
            stbl = null;
            #endregion

            #region BlendUnit
            // Now the BlendUnit (CAS Slider)
            BlendUnit blendunit = new BlendUnit();
            blendunit.localeHash = instanceid;
            blendunit.bidirectional = 1;
            blendunit.casPanelSubGroup = Convert.ToUInt32(txtSubgroup.Text);
            if (chkListCasPanelGroup.GetItemChecked(0) == true) blendunit.casPanelGroup = (uint)casPanelGroup.HeadAndEars;
            if (chkListCasPanelGroup.GetItemChecked(1) == true) blendunit.casPanelGroup = (uint)casPanelGroup.Mouth;
            if (chkListCasPanelGroup.GetItemChecked(2) == true) blendunit.casPanelGroup = (uint)casPanelGroup.Nose;
            if (chkListCasPanelGroup.GetItemChecked(3) == true) blendunit.casPanelGroup = (uint)casPanelGroup.Eyelash;
            if (chkListCasPanelGroup.GetItemChecked(4) == true) blendunit.casPanelGroup = (uint)casPanelGroup.Eyes;
            blendunit.blendLinks.Add(facialblendRight);
            blendunit.blendLinks.Add(facialblendLeft);

            Stream blendunitFile = blendunit.Save();
            blendunit = null;
            #endregion

            #region Facial Blends
            // Now the Facial Blends - need 2 of these, one per slider
            FacialBlend faceblend = new FacialBlend();
            faceblend.partName = txtSliderName.Text + "Left";
            faceblend.blendTgi = blendGeomLeft;
            faceblend.blendType = 2;
            faceblend.keytable.keys.Add(new ResourceKey());
            if (chkMFLink.Checked)
            {
                faceblend.keytable.keys.Add(vpxyLeftFemale);
                if (chkMale.Checked && chkFemale.Checked)
                {
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1));
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 1));
                }
                else
                {
                    if (chkMale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1)); }
                    if (chkFemale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 1)); }
                }
            }
            else
            {
                if (chkMale.Checked) { faceblend.keytable.keys.Add(vpxyLeftMale); }
                if (chkFemale.Checked) { faceblend.keytable.keys.Add(vpxyLeftFemale); }
                if (chkMale.Checked && chkFemale.Checked)
                {
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1));
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 2));
                }
                else
                {
                    if (chkMale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1)); }
                    if (chkFemale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 1)); }
                }
            }
            Stream faceBlendFileLeft = faceblend.Save();
            faceblend = null;

            faceblend = new FacialBlend();
            faceblend.partName = txtSliderName.Text + "Right";
            faceblend.blendTgi = blendGeomRight;
            faceblend.blendType = 2;
            faceblend.keytable.keys.Add(new ResourceKey());
            if (chkMFLink.Checked)
            {
                faceblend.keytable.keys.Add(vpxyRightFemale);
                if (chkMale.Checked && chkFemale.Checked)
                {
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1));
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 1));
                }
                else
                {
                    if (chkMale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1)); }
                    if (chkFemale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 1)); }
                }

            }
            else
            {
                if (chkMale.Checked) { faceblend.keytable.keys.Add(vpxyRightMale); }
                if (chkFemale.Checked) { faceblend.keytable.keys.Add(vpxyRightFemale); }
                if (chkMale.Checked && chkFemale.Checked)
                {
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1));
                    faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 2));
                }
                else
                {
                    if (chkMale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Male, 1)); }
                    if (chkFemale.Checked) { faceblend.geomBoneEntries.Add(makeFBEntry((uint)AgeGenderFlags.Female, 1)); }
                }
            }
            Stream faceBlendFileRight = faceblend.Save();
            faceblend = null;
            #endregion

            #region Blend Geometry
            BlendGeom blendGeom = new BlendGeom();
            BlendGeomSection1 blendGeomS1 = new BlendGeomSection1();
            if (chkMale.Checked)
            {
                blendGeomS1.ageGenderFlags = (uint)AgeGenderFlags.Male + allFlags;
                blendGeomS1.regionFlags = getRegionFlag();
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeom.section1.Add(blendGeomS1);
            }
            if (chkFemale.Checked)
            {
                blendGeomS1 = new BlendGeomSection1();
                blendGeomS1.ageGenderFlags = (uint)AgeGenderFlags.Female + allFlags;
                blendGeomS1.regionFlags = getRegionFlag();
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeom.section1.Add(blendGeomS1);
            }
            Stream blendGeomFileLeft = blendGeom.Save();

            blendGeom = new BlendGeom();
            blendGeomS1 = new BlendGeomSection1();
            if (chkMale.Checked)
            {
                blendGeomS1.ageGenderFlags = (uint)AgeGenderFlags.Male + allFlags;
                blendGeomS1.regionFlags = getRegionFlag();
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeom.section1.Add(blendGeomS1);
            }
            if (chkFemale.Checked)
            {
                blendGeomS1 = new BlendGeomSection1();
                blendGeomS1.ageGenderFlags = (uint)AgeGenderFlags.Female + allFlags;
                blendGeomS1.regionFlags = getRegionFlag();
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeomS1.subentries.Add(new BlendGeomSection1SubEntry());
                blendGeom.section1.Add(blendGeomS1);
            }
            Stream blendGeomFileRight = blendGeom.Save();

            blendGeom = null;

            #endregion

            #region Proxys
            VPXYFile vpxy = new VPXYFile();
            VPXYEntry vpxyEntry = new VPXYEntry();
            Stream vpxyFileLeftMale = Stream.Null;
            Stream vpxyFileRightMale = Stream.Null;
            Stream vpxyFileLeftFemale = Stream.Null;
            Stream vpxyFileRightFemale = Stream.Null;
            if (chkMFLink.Checked)
            {
                if (chkFemale.Checked)
                {
                    // VPXY Female left
                    vpxy = new VPXYFile();
                    vpxy.rcolHeader.internalChunks.Add(vpxyLeftFemale);
                    vpxyEntry = new VPXYEntry();
                    vpxyEntry.tgiList.Add(boneDeltaLeftFemale);
                    vpxy.vpxy.linkEntries.Add(vpxyEntry);
                }
                vpxyFileLeftFemale = vpxy.Save();
                if (chkFemale.Checked)
                {
                    // VPXY Female Right
                    vpxy = new VPXYFile();
                    vpxy.rcolHeader.internalChunks.Add(vpxyRightFemale);
                    vpxyEntry = new VPXYEntry();
                    vpxyEntry.tgiList.Add(boneDeltaRightFemale);
                    vpxy.vpxy.linkEntries.Add(vpxyEntry);
                }
                vpxyFileRightFemale = vpxy.Save();

            }
            else
            {
                if (chkMale.Checked)
                {
                    //VPXY Male Left
                    vpxy.rcolHeader.internalChunks.Add(vpxyLeftMale);
                    vpxyEntry.tgiList.Add(boneDeltaLeftMale);
                    vpxy.vpxy.linkEntries.Add(vpxyEntry);
                }
                vpxyFileLeftMale = vpxy.Save();
                if (chkMale.Checked)
                {
                    //VPXY Male Right
                    vpxy = new VPXYFile();
                    vpxy.rcolHeader.internalChunks.Add(vpxyRightMale);
                    vpxyEntry = new VPXYEntry();
                    vpxyEntry.tgiList.Add(boneDeltaRightMale);
                    vpxy.vpxy.linkEntries.Add(vpxyEntry);
                }
                vpxyFileRightMale = vpxy.Save();
                if (chkFemale.Checked)
                {
                    // VPXY Female left
                    vpxy = new VPXYFile();
                    vpxy.rcolHeader.internalChunks.Add(vpxyLeftFemale);
                    vpxyEntry = new VPXYEntry();
                    vpxyEntry.tgiList.Add(boneDeltaLeftFemale);
                    vpxy.vpxy.linkEntries.Add(vpxyEntry);
                }
                vpxyFileLeftFemale = vpxy.Save();
                if (chkFemale.Checked)
                {
                    // VPXY Female Right
                    vpxy = new VPXYFile();
                    vpxy.rcolHeader.internalChunks.Add(vpxyRightFemale);
                    vpxyEntry = new VPXYEntry();
                    vpxyEntry.tgiList.Add(boneDeltaRightFemale);
                    vpxy.vpxy.linkEntries.Add(vpxyEntry);
                }
                vpxyFileRightFemale = vpxy.Save();

            }
            #endregion

            #region BoneDeltas
            BoneDeltaFile bonedeltaFile = new BoneDeltaFile();
            BoneDeltaEntry bdEntry = new BoneDeltaEntry();
            Stream bonedeltaLeftMaleFile;
            Stream bonedeltaRightMaleFile;
            Stream bonedeltaLeftFemaleFile ;
            Stream bonedeltaRightFemaleFile ;
            if (chkMFLink.Checked)
            {
                if (chkMale.Checked)
                {
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaLeftFemale);
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = -2f;
                    bdEntry.scale.y = -2f;
                    bdEntry.scale.z = -2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaLeftMaleFile = bonedeltaFile.Save();
                if (chkMale.Checked)
                {
                    bonedeltaFile = new BoneDeltaFile();
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaRightFemale);
                    bdEntry = new BoneDeltaEntry();
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = 2f;
                    bdEntry.scale.y = 2f;
                    bdEntry.scale.z = 2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaRightMaleFile = bonedeltaFile.Save();

                if (chkFemale.Checked)
                {
                    bonedeltaFile = new BoneDeltaFile();
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaLeftFemale);
                    bdEntry = new BoneDeltaEntry();
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = -2f;
                    bdEntry.scale.y = -2f;
                    bdEntry.scale.z = -2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaLeftFemaleFile = bonedeltaFile.Save();

                if (chkFemale.Checked)
                {
                    bonedeltaFile = new BoneDeltaFile();
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaRightFemale);
                    bdEntry = new BoneDeltaEntry();
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = 2f;
                    bdEntry.scale.y = 2f;
                    bdEntry.scale.z = 2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaRightFemaleFile = bonedeltaFile.Save();

            }
            else
            {
                if (chkMale.Checked)
                {
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaLeftMale);
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = -2f;
                    bdEntry.scale.y = -2f;
                    bdEntry.scale.z = -2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaLeftMaleFile = bonedeltaFile.Save();
                if (chkMale.Checked)
                {
                    bonedeltaFile = new BoneDeltaFile();
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaRightMale);
                    bdEntry = new BoneDeltaEntry();
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = 2f;
                    bdEntry.scale.y = 2f;
                    bdEntry.scale.z = 2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaRightMaleFile = bonedeltaFile.Save();

                if (chkFemale.Checked)
                {
                    bonedeltaFile = new BoneDeltaFile();
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaLeftFemale);
                    bdEntry = new BoneDeltaEntry();
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = -2f;
                    bdEntry.scale.y = -2f;
                    bdEntry.scale.z = -2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaLeftFemaleFile = bonedeltaFile.Save();

                if (chkFemale.Checked)
                {
                    bonedeltaFile = new BoneDeltaFile();
                    bonedeltaFile.rcolHeader.internalChunks.Add(boneDeltaRightFemale);
                    bdEntry = new BoneDeltaEntry();
                    bdEntry.boneHash = 0x0F97B21B;
                    bdEntry.scale.x = 2f;
                    bdEntry.scale.y = 2f;
                    bdEntry.scale.z = 2f;
                    bonedeltaFile.bonedelta.entries.Add(bdEntry);
                }
                bonedeltaRightFemaleFile = bonedeltaFile.Save();
            }
            #endregion

            Stream packageFile = File.Create(folderBrowserDialog1.SelectedPath + "\\" + txtSliderName.Text + ".package");
            Database db = new Database(packageFile, false);

            for (int i = 0; i < 23; i++)
            {
                ulong actualKey = (ulong)(i * 72057594037927936) + instance32;
                db.SetResourceStream(new ResourceKey(0x220557DA, 0, actualKey), stblFile);
            }

            //db.SetResourceStream(stblKey, stblFile);
            db.SetResourceStream(nameMapKey, nameMapFile);
            db.SetResourceStream(blendunitKey, blendunitFile);
            db.SetResourceStream(blendGeomLeft, blendGeomFileLeft);
            db.SetResourceStream(blendGeomRight, blendGeomFileRight);
            db.SetResourceStream(facialblendLeft, faceBlendFileLeft);
            db.SetResourceStream(facialblendRight, faceBlendFileRight);

            if (chkMFLink.Checked)
            {
                db.SetResourceStream(vpxyLeftFemale, vpxyFileLeftFemale);
                db.SetResourceStream(vpxyRightFemale, vpxyFileRightFemale);
                db.SetResourceStream(boneDeltaLeftFemale, bonedeltaLeftFemaleFile);
                db.SetResourceStream(boneDeltaRightFemale, bonedeltaRightFemaleFile);
            }
            else
            {
                if (chkMale.Checked)
                {
                    db.SetResourceStream(vpxyLeftMale, vpxyFileLeftMale);
                    db.SetResourceStream(vpxyRightMale, vpxyFileRightMale);
                    db.SetResourceStream(boneDeltaLeftMale, bonedeltaLeftMaleFile);
                    db.SetResourceStream(boneDeltaRightMale, bonedeltaRightMaleFile);
                }
                if (chkFemale.Checked)
                {
                    db.SetResourceStream(vpxyLeftFemale, vpxyFileLeftFemale);
                    db.SetResourceStream(vpxyRightFemale, vpxyFileRightFemale);
                    db.SetResourceStream(boneDeltaLeftFemale, bonedeltaLeftFemaleFile);
                    db.SetResourceStream(boneDeltaRightFemale, bonedeltaRightFemaleFile);
                }
            }
            db.Commit(true);

            packageFile.Close();

            MessageBox.Show("Package saved");
        }
        private void saveToDBPF(Database db, ulong instanceId, bool newInstance)
        {
            ResourceKey rkey;

            MemoryStream mem = new MemoryStream();
            casPartFile casPF = new casPartFile();

            // Do we have new meshes?  If so, we need to do some pretty heft modifications. :)

            string meshName = txtMeshName.Text;

            NameMap namemap = new NameMap();
            ResourceKey namemapKey = new ResourceKey(0x0166038C, 0x00000000, instanceId);

            if (!String.IsNullOrEmpty(txtMeshLod1.Text) || !String.IsNullOrEmpty(txtMeshLod0.Text))
            {
                keyName bodyBlendFat = new keyName(0x062C8204, 0x0, meshName + "_fat");
                keyName bodyBlendFit = new keyName(0x062C8204, 0x0, meshName + "_fit");
                keyName bodyBlendThin = new keyName(0x062C8204, 0x0, meshName + "_thin");
                keyName bodyBlendSpecial = new keyName(0x062C8204, 0x0, meshName + "_special");

                namemap.entries.Add(bodyBlendFat.instanceId, bodyBlendFat.name);
                namemap.entries.Add(bodyBlendFit.instanceId, bodyBlendFit.name);
                namemap.entries.Add(bodyBlendThin.instanceId, bodyBlendThin.name);
                namemap.entries.Add(bodyBlendSpecial.instanceId, bodyBlendSpecial.name);

                Stream bodyBlendFatStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoFat].ToString(), 0);
                Stream bodyBlendFitStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoFit].ToString(), 0);
                Stream bodyBlendThinStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoThin].ToString(), 0);
                Stream bodyBlendSpecialStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoSpecial].ToString(), 0);

                // Load in the blend information
                FacialBlend bodyBlendFatFile = new FacialBlend(bodyBlendFatStream);
                bodyBlendFatFile.partName = meshName + "_fat";
                FacialBlend bodyBlendFitFile = new FacialBlend(bodyBlendFitStream);
                bodyBlendFitFile.partName = meshName + "_fit";
                FacialBlend bodyBlendThinFile = new FacialBlend(bodyBlendThinStream);
                bodyBlendThinFile.partName = meshName + "_thin";
                FacialBlend bodyBlendSpecialFile = new FacialBlend(bodyBlendSpecialStream);
                bodyBlendSpecialFile.partName = meshName + "_special";

                if (debugModeToolStripMenuItem.Checked)
                {
                    Stream bgeoStream = KeyUtils.findKey(bodyBlendFatFile.blendTgi, 0);
                    bgeoStream.Seek(0, SeekOrigin.Begin);
                    bodyBlendFatFile.blendTgi = new keyName(0x067CAA11, 0x0, meshName + "_fat").ToResourceKey();
                    db.SetResourceStream(bodyBlendFatFile.blendTgi, bgeoStream);

                    bgeoStream = KeyUtils.findKey(bodyBlendFitFile.blendTgi, 0);
                    bgeoStream.Seek(0, SeekOrigin.Begin);
                    bodyBlendFitFile.blendTgi = new keyName(0x067CAA11, 0x0, meshName + "_fit").ToResourceKey();
                    db.SetResourceStream(bodyBlendFitFile.blendTgi, bgeoStream);

                    bgeoStream = KeyUtils.findKey(bodyBlendThinFile.blendTgi, 0);
                    bgeoStream.Seek(0, SeekOrigin.Begin);
                    bodyBlendThinFile.blendTgi = new keyName(0x067CAA11, 0x0, meshName + "_thin").ToResourceKey();
                    db.SetResourceStream(bodyBlendThinFile.blendTgi, bgeoStream);

                    bgeoStream = KeyUtils.findKey(bodyBlendSpecialFile.blendTgi, 0);
                    bgeoStream.Seek(0, SeekOrigin.Begin);
                    bodyBlendSpecialFile.blendTgi = new keyName(0x067CAA11, 0x0, meshName + "_special").ToResourceKey();
                    db.SetResourceStream(bodyBlendSpecialFile.blendTgi, bgeoStream);

                }

                db.SetResourceStream(bodyBlendFit.ToResourceKey(), bodyBlendFitFile.Save());
                db.SetResourceStream(bodyBlendFat.ToResourceKey(), bodyBlendFatFile.Save());
                db.SetResourceStream(bodyBlendThin.ToResourceKey(), bodyBlendThinFile.Save());
                db.SetResourceStream(bodyBlendSpecial.ToResourceKey(), bodyBlendSpecialFile.Save());

                // Update the CAS part TGI links with the new VPXY
                casPartNew.tgi64list[casPartNew.tgiIndexBlendInfoFat] = bodyBlendFat.ToResourceKey();
                casPartNew.tgi64list[casPartNew.tgiIndexBlendInfoFit] = bodyBlendFit.ToResourceKey();
                casPartNew.tgi64list[casPartNew.tgiIndexBlendInfoThin] = bodyBlendThin.ToResourceKey();
                casPartNew.tgi64list[casPartNew.tgiIndexBlendInfoSpecial] = bodyBlendSpecial.ToResourceKey();

                keyName proxyFitKey = new keyName(0x736884F1, 0x00000001, meshName + "_fit");
                keyName proxyFatKey = new keyName(0x736884F1, 0x00000001, meshName + "_fat");
                keyName proxyThinKey = new keyName(0x736884F1, 0x00000001, meshName + "_thin");
                keyName proxySpecialKey = new keyName(0x736884F1, 0x00000001, meshName + "_special");

                Stream proxyFatStream = KeyUtils.findKey(new ResourceKey(0x736884F1, 0x00000001, casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoFat].instanceId), 0);
                Stream proxyFitStream = KeyUtils.findKey(new ResourceKey(0x736884F1, 0x00000001, casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoFit].instanceId), 0);
                Stream proxyThinStream = KeyUtils.findKey(new ResourceKey(0x736884F1, 0x00000001, casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoThin].instanceId), 0);
                Stream proxySpecialStream = KeyUtils.findKey(new ResourceKey(0x736884F1, 0x00000001, casPartSrc.tgi64list[casPartSrc.tgiIndexBlendInfoSpecial].instanceId), 0);

                VPXYFile proxyFat = new VPXYFile(proxyFatStream);
                proxyFat.rcolHeader.internalChunks.Clear();
                proxyFat.rcolHeader.internalChunks.Add(proxyFatKey.ToResourceKey());
                VPXYFile proxyFit = new VPXYFile(proxyFitStream);
                proxyFit.rcolHeader.internalChunks.Clear();
                proxyFit.rcolHeader.internalChunks.Add(proxyFitKey.ToResourceKey());
                VPXYFile proxyThin = new VPXYFile(proxyThinStream);
                proxyThin.rcolHeader.internalChunks.Clear();
                proxyThin.rcolHeader.internalChunks.Add(proxyThinKey.ToResourceKey());
                VPXYFile proxySpecial = new VPXYFile(proxySpecialStream);
                proxySpecial.rcolHeader.internalChunks.Clear();
                proxySpecial.rcolHeader.internalChunks.Add(proxySpecialKey.ToResourceKey());

                db.SetResourceStream(proxyFatKey.ToResourceKey(), proxyFat.Save());
                db.SetResourceStream(proxyFitKey.ToResourceKey(), proxyFit.Save());
                db.SetResourceStream(proxyThinKey.ToResourceKey(), proxyThin.Save());
                db.SetResourceStream(proxySpecialKey.ToResourceKey(), proxySpecial.Save());

                uint customGroup = MadScience.StringHelpers.HashFNV24(meshName);
                keyName meshLod0 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod0"), meshName + "_lod0");
                keyName meshLod0_1 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod0_1"), meshName + "_lod0_1");
                keyName meshLod0_2 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod0_2"), meshName + "_lod0_2");
                keyName meshLod0_3 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod0_3"), meshName + "_lod0_3");

                keyName meshLod1 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod1"), meshName + "_lod1");
                keyName meshLod1_1 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod1_1"), meshName + "_lod1_1");
                keyName meshLod1_2 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod1_2"), meshName + "_lod1_2");
                keyName meshLod1_3 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod1_3"), meshName + "_lod1_3");
                keyName meshLod2 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod2"), meshName + "_lod2");
                keyName meshLod2_1 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod2_1"), meshName + "_lod2_1");
                keyName meshLod2_2 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod2_2"), meshName + "_lod2_2");

                keyName meshLod3 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod3"), meshName + "_lod3");
                keyName meshLod3_1 = new keyName(0x015A1849, customGroup, (ulong)MadScience.StringHelpers.HashFNV32(meshName + "_lod3_1"), meshName + "_lod3_1");

                keyName vpxyKey = new keyName(0x736884F1, 0x00000001, (ulong)customGroup);

                // Load in the VPXY - we need to modify it.
                //keyName oldVpxyKey = new keyName((tgi64)casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY]);
                Stream vpxyStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY].ToString(), 0);
                if (StreamHelpers.isValidStream(vpxyStream))
                {

                    namemap.entries.Add(meshLod0.instanceId, meshName + "_lod0");
                    namemap.entries.Add(meshLod0_1.instanceId, meshName + "_lod0_1");
                    namemap.entries.Add(meshLod0_2.instanceId, meshName + "_lod0_2");
                    namemap.entries.Add(meshLod0_3.instanceId, meshName + "_lod0_3");
                    namemap.entries.Add(meshLod1.instanceId, meshName + "_lod1");
                    namemap.entries.Add(meshLod1_1.instanceId, meshName + "_lod1_1");
                    namemap.entries.Add(meshLod1_2.instanceId, meshName + "_lod1_2");
                    namemap.entries.Add(meshLod1_3.instanceId, meshName + "_lod1_3");
                    namemap.entries.Add(meshLod2.instanceId, meshName + "_lod2");
                    namemap.entries.Add(meshLod2_1.instanceId, meshName + "_lod2_1");
                    namemap.entries.Add(meshLod2_2.instanceId, meshName + "_lod2_2");
                    namemap.entries.Add(meshLod3.instanceId, meshName + "_lod3");
                    namemap.entries.Add(meshLod3_1.instanceId, meshName + "_lod3_1");
                    namemap.entries.Add(vpxyKey.instanceId, meshName);

                    //keyName proxyFit = new keyName(0x736884F1, 0x00000001, meshName + "_fit");
                    //keyName proxyFat = new keyName(0x736884F1, 0x00000001, meshName + "_fat");
                    //keyName proxyThin = new keyName(0x736884F1, 0x00000001, meshName + "_thin");
                    //keyName proxySpecial = new keyName(0x736884F1, 0x00000001, meshName + "_special");

                    VPXYFile vpxyfile = new VPXYFile(vpxyStream);
                    vpxyfile.rcolHeader.internalChunks.Clear();
                    vpxyfile.rcolHeader.internalChunks.Add(vpxyKey.ToResourceKey());

                    vpxyfile.vpxy.linkEntries.Clear();
                    if (!String.IsNullOrEmpty(txtMeshLod0.Text))
                    {
                        // LOD 0
                        VPXYEntry vpxyE = new VPXYEntry();
                        if (!String.IsNullOrEmpty(txtMeshLod0_1.Text)) vpxyE.tgiList.Add(meshLod0_1.ToResourceKey());
                        if (!String.IsNullOrEmpty(txtMeshLod0_2.Text)) vpxyE.tgiList.Add(meshLod0_2.ToResourceKey());
                        if (!String.IsNullOrEmpty(txtMeshLod0_3.Text)) vpxyE.tgiList.Add(meshLod0_3.ToResourceKey());
                        vpxyE.tgiList.Add(meshLod0.ToResourceKey());
                        vpxyfile.vpxy.linkEntries.Add(vpxyE);
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod1.Text))
                    {
                        // LOD 1
                        VPXYEntry vpxyE = new VPXYEntry();
                        if (!String.IsNullOrEmpty(txtMeshLod1_1.Text)) vpxyE.tgiList.Add(meshLod1_1.ToResourceKey());
                        if (!String.IsNullOrEmpty(txtMeshLod1_2.Text)) vpxyE.tgiList.Add(meshLod1_2.ToResourceKey());
                        if (!String.IsNullOrEmpty(txtMeshLod1_3.Text)) vpxyE.tgiList.Add(meshLod1_3.ToResourceKey());
                        vpxyE.tgiList.Add(meshLod1.ToResourceKey());
                        vpxyfile.vpxy.linkEntries.Add(vpxyE);
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod2.Text))
                    {
                        // LOD 2
                        VPXYEntry vpxyE = new VPXYEntry();
                        if (!String.IsNullOrEmpty(txtMeshLod2_1.Text)) vpxyE.tgiList.Add(meshLod2_1.ToResourceKey());
                        if (!String.IsNullOrEmpty(txtMeshLod2_2.Text)) vpxyE.tgiList.Add(meshLod2_2.ToResourceKey());
                        vpxyE.tgiList.Add(meshLod2.ToResourceKey());
                        vpxyfile.vpxy.linkEntries.Add(vpxyE);
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod3.Text))
                    {
                        // LOD 2
                        VPXYEntry vpxyE = new VPXYEntry();
                        if (!String.IsNullOrEmpty(txtMeshLod3_1.Text)) vpxyE.tgiList.Add(meshLod3_1.ToResourceKey());
                        vpxyE.tgiList.Add(meshLod3.ToResourceKey());
                        vpxyfile.vpxy.linkEntries.Add(vpxyE);
                    }

                    vpxyfile.vpxy.keytable.keys.Clear();

                    // If a Hair or an Accessory then set the vpxy start to 0 else starts at 1
                    if (checkedListType.GetItemChecked(0) == true || checkedListType.GetItemChecked(4) == true)
                    {
                        vpxyfile.vpxy.numTypeZero = 0;
                    }
                    else
                    {
                        vpxyfile.vpxy.numTypeZero = 1;
                    }
                    vpxyStream = vpxyfile.Save();

                    //vpxyfile.rcolHeader.internalChunks[0] = proxyFit.ToResourceKey();
                    //Stream proxyFitFile = vpxyfile.Save();

                    //vpxyfile.rcolHeader.internalChunks[0] = proxyFat.ToResourceKey();
                    //Stream proxyFatFile = vpxyfile.Save();

                    //vpxyfile.rcolHeader.internalChunks[0] = proxyThin.ToResourceKey();
                    //Stream proxyThinFile = vpxyfile.Save();

                    //vpxyfile.rcolHeader.internalChunks[0] = proxySpecial.ToResourceKey();
                    //Stream proxySpecialFile = vpxyfile.Save();

                    db.SetResourceStream(vpxyKey.ToResourceKey(), vpxyStream);
                    //db.SetResourceStream(proxyFit.ToResourceKey(), proxyFitFile);
                    //db.SetResourceStream(proxyFat.ToResourceKey(), proxyFatFile);
                    //db.SetResourceStream(proxyThin.ToResourceKey(), proxyThinFile);
                    //db.SetResourceStream(proxySpecial.ToResourceKey(), proxySpecialFile);

                    // Update the CAS part TGI links with the new VPXY
                    casPartNew.tgi64list[casPartNew.tgiIndexVPXY] = vpxyKey.ToResourceKey();

                    keyName bumpMapKey = new keyName();

                    if (String.IsNullOrEmpty(txtOtherBumpMap.Text) == false)
                    {
                        bumpMapKey = new keyName(txtOtherBumpMap.Text, meshName + "_n");
                        //kNames.Add(bumpMapKey);
                        Stream bumpMapStream = File.OpenRead(txtOtherBumpMap.Text);
                        if (txtOtherBumpMap.Text != "" && !txtOtherBumpMap.Text.StartsWith("key:")) db.SetResourceStream(bumpMapKey.ToResourceKey(), bumpMapStream);
                        bumpMapStream.Close();
                    }

                    #region Import Mesh LODs
                    if (!String.IsNullOrEmpty(txtMeshLod0.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod0.ToResourceKey(), saveGeom(txtMeshLod0.Text, bumpMapKey.ToResourceKey()));
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod0_1.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod0_1.ToResourceKey(), saveGeom(txtMeshLod0_1.Text, bumpMapKey.ToResourceKey()));
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod0_2.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod0_2.ToResourceKey(), saveGeom(txtMeshLod0_2.Text, bumpMapKey.ToResourceKey()));
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod0_3.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod0_3.ToResourceKey(), saveGeom(txtMeshLod0_3.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod1.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod1.ToResourceKey(), saveGeom(txtMeshLod1.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod1_1.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod1_1.ToResourceKey(), saveGeom(txtMeshLod1_1.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod1_2.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod1_2.ToResourceKey(), saveGeom(txtMeshLod1_2.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod1_3.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod1_3.ToResourceKey(), saveGeom(txtMeshLod1_3.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod2.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod2.ToResourceKey(), saveGeom(txtMeshLod2.Text, bumpMapKey.ToResourceKey()));
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod2_1.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod2_1.ToResourceKey(), saveGeom(txtMeshLod2_1.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod2_2.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod2_2.ToResourceKey(), saveGeom(txtMeshLod2_2.Text, bumpMapKey.ToResourceKey()));
                    }

                    if (!String.IsNullOrEmpty(txtMeshLod3.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod3.ToResourceKey(), saveGeom(txtMeshLod3.Text, bumpMapKey.ToResourceKey()));
                    }
                    if (!String.IsNullOrEmpty(txtMeshLod3_1.Text.Trim()))
                    {
                        db.SetResourceStream(meshLod3_1.ToResourceKey(), saveGeom(txtMeshLod3_1.Text, bumpMapKey.ToResourceKey()));
                    }

                    #endregion
                }

            }

            db.SetResourceStream(namemapKey, namemap.Save());

            if (casPartNew != null)
            {
                casPF.Save(mem, casPartNew);
            }
            else
            {
                casPF.Save(mem, casPartSrc);
            }
            casPF = null;

            if (this.loadedCasPart.ToString() == "00000000:00000000:0000000000000000")
            {
                rkey = new ResourceKey((uint)0x034AEECB, (uint)0, instanceId, (uint)ResourceKeyOrder.ITG);
            }
            else
            {
                if (!newInstance)
                {
                    rkey = this.loadedCasPart;
                }
                else
                {
                    rkey = new ResourceKey((uint)0x034AEECB, (uint)0, instanceId, (uint)ResourceKeyOrder.ITG);
                }
            }
            db.SetResourceStream(rkey, mem);

            if (casPartNew != null)
            {
                // Go through a list of all the keys and see if they are "local"
                for (int i = 0; i < casPartNew.xmlChunk.Count; i++)
                {
                    xmlChunkDetails chunk = (xmlChunkDetails)casPartNew.xmlChunk[i];

                    for (int j = 0; j < 10; j++)
                    {
                        writeLocalResource(db, stencilPool[j].key);
                    }
                    writeLocalResource(db, chunk.Multiplier);
                    writeLocalResource(db, chunk.Overlay);
                    writeLocalResource(db, chunk.hair.RootColor);
                    writeLocalResource(db, chunk.hair.DiffuseColor);
                    writeLocalResource(db, chunk.hair.HighlightColor);
                    writeLocalResource(db, chunk.hair.TipColor);
                    writeLocalResource(db, chunk.hair.ScalpDiffuseMap);
                    writeLocalResource(db, chunk.hair.ScalpControlMap);
                    writeLocalResource(db, chunk.hair.ScalpSpecularMap);
                    writeLocalResource(db, chunk.hair.ScalpAO);
                    writeLocalResource(db, chunk.hair.FaceDiffuseMap);
                    writeLocalResource(db, chunk.hair.FaceControlMap);
                    writeLocalResource(db, chunk.hair.FaceSpecularMap);
                    writeLocalResource(db, chunk.hair.FaceAO);
                    writeLocalResource(db, chunk.Mask);
                    writeLocalResource(db, chunk.SkinSpecular);
                    writeLocalResource(db, chunk.SkinAmbient);
                    writeLocalResource(db, chunk.ClothingSpecular);
                    writeLocalResource(db, chunk.ClothingAmbient);
                    writeLocalResource(db, chunk.PartMask);
                    writeLocalResource(db, chunk.pattern[0].BackgroundImage);
                    writeLocalResource(db, chunk.pattern[1].BackgroundImage);
                    writeLocalResource(db, chunk.pattern[2].BackgroundImage);
                    writeLocalResource(db, chunk.pattern[3].BackgroundImage);
                    writeLocalResource(db, chunk.pattern[0].rgbmask);
                    writeLocalResource(db, chunk.pattern[1].rgbmask);
                    writeLocalResource(db, chunk.pattern[2].rgbmask);
                    writeLocalResource(db, chunk.pattern[3].rgbmask);
                    writeLocalResource(db, chunk.pattern[0].specmap);
                    writeLocalResource(db, chunk.pattern[1].specmap);
                    writeLocalResource(db, chunk.pattern[2].specmap);
                    writeLocalResource(db, chunk.pattern[3].specmap);
                    writeLocalResource(db, chunk.pattern[0].filename);
                    writeLocalResource(db, chunk.pattern[1].filename);
                    writeLocalResource(db, chunk.pattern[2].filename);
                    writeLocalResource(db, chunk.pattern[3].filename);
                    writeLocalResource(db, chunk.faceOverlay);
                    writeLocalResource(db, chunk.faceSpecular);
                    writeLocalResource(db, chunk.ControlMap);
                    writeLocalResource(db, chunk.DiffuseMap);

                    if (newPNGfiles.ContainsKey(i))
                    {
                        Stream newPNG = File.Open(newPNGfiles[i], FileMode.Open, FileAccess.Read, FileShare.Read);
                        uint thumbGroup = (uint)i;
                        if (useAlternativeToolStripMenuItem.Checked == true) thumbGroup++;

                        ResourceKey keyPNG = new ResourceKey(0x626F60CE, thumbGroup, instanceId, (uint)ResourceKeyOrder.ITG);
                        db.SetResourceStream(keyPNG, newPNG);
                        newPNG.Close();
                    }
                }
            }

            mem.Close();
        }