private MemoryStream makeVPXYfile(MadScience.Wrappers.ResourceKey headerKey)
        {
            MemoryStream mem = new MemoryStream();
            MadScience.Wrappers.VPXYFile vpxyFile = new MadScience.Wrappers.VPXYFile();
            vpxyFile.rcolHeader.internalChunks.Add(headerKey);

            vpxyFile.Save(mem);

            return mem;
        }
Ejemplo n.º 2
0
        private MemoryStream makeVPXYfile(MadScience.Wrappers.ResourceKey headerKey)
        {
            MemoryStream mem = new MemoryStream();

            MadScience.Wrappers.VPXYFile vpxyFile = new MadScience.Wrappers.VPXYFile();
            vpxyFile.rcolHeader.internalChunks.Add(headerKey);

            vpxyFile.Save(mem);

            return(mem);
        }
        private void showMeshDetails()
        {
            string meshName = "";

            // Translate selection back into af am etc
            switch (cmbSimAge.Text)
            {
                case "Baby":
                    meshName += "b";
                    break;
                case "Toddler":
                    meshName += "p";
                    break;
                case "Child":
                    meshName += "c";
                    break;
                case "Teen":
                    meshName += "t";
                    break;
                case "Young Adult":
                    meshName += "y";
                    break;
                case "Adult":
                    meshName += "a";
                    break;
                case "Elder":
                    meshName += "e";
                    break;
                case "All Ages":
                    meshName += "u";
                    break;
            }

            switch (cmbSimGender.Text)
            {
                case "Female":
                    meshName += "f";
                    break;
                case "Male":
                    meshName += "m";
                    break;
                case "Unisex":
                    meshName += "u";
                    break;
            }

            if (cmbMeshName.Text == "* Custom")
            {

                //if (this.isNew == false)
                //{
                //meshName += casPartSrc.meshName;
                //}
                txtCasPartName.Text = casPartSrc.meshName;
                txtMeshName.Text = casPartSrc.meshName;

                txtCasPartInstance.Text = "0x" + MadScience.StringHelpers.HashFNV64(meshName).ToString("X16");
                picMeshPreview.Image = null;
                picMeshPreview.Invalidate();

            }
            else
            {

                if (cmbPartTypes.Text == "(none)")
                {
                    meshName += cmbMeshName.Text;
                }
                else
                {
                    meshName += cmbPartTypes.Text + cmbMeshName.Text;
                }

                txtCasPartName.Text = meshName;
                txtMeshName.Text = meshName;

                scrollPanelToThumb(meshName);

                for (int i = 0; i < this.lookupList.Items.Length; i++)
                {
                    filesFile f = lookupList.Items[i];
                    if (f.fullCasPartname == meshName)
                    {
                        txtCasPartInstance.Text = f.instanceid;
                        break;
                    }
                }

                picMeshPreview.Image = null;
                picMeshPreview.Invalidate();

                // Find thumbnail
                if (File.Exists(Path.Combine(Application.StartupPath, Path.Combine("cache", meshName + ".png"))))
                {
                    Stream picMeshPreviewStream = File.OpenRead(Path.Combine(Application.StartupPath, Path.Combine("cache", meshName + ".png")));
                    picMeshPreview.Image = Image.FromStream(picMeshPreviewStream);
                    picMeshPreviewStream.Close();
                }
                else
                {
                    extractCASThumbnail(meshName);
                }
            }

            if (this.isNew == true)
            {
                // Attempt to load the existing caspart into memory so we can extract data later.
                Stream casPartFile = File.Open(Path.Combine(Application.StartupPath, Path.Combine("casparts", txtCasPartName.Text + ".caspart")), FileMode.Open, FileAccess.Read, FileShare.Read);
                casPartFile cPartFile = new casPartFile();
                this.casPartSrc = cPartFile.Load(casPartFile);
                casPartFile.Close();
            }

            for (int i = 0; i < checkedListAge.Items.Count; i++)
            {
                checkedListAge.SetItemChecked(i, false);
            }
            for (int i = 0; i < checkedListCategory.Items.Count; i++)
            {
                checkedListCategory.SetItemChecked(i, false);
            }
            for (int i = 0; i < checkedListCategoryExtended.Items.Count; i++)
            {
                checkedListCategoryExtended.SetItemChecked(i, false);
            }
            for (int i = 0; i < checkedListGender.Items.Count; i++)
            {
                checkedListGender.SetItemChecked(i, false);
            }
            for (int i = 0; i < checkedListOther.Items.Count; i++)
            {
                checkedListOther.SetItemChecked(i, false);
            }
            for (int i = 0; i < checkedListType.Items.Count; i++)
            {
                checkedListType.SetItemChecked(i, false);
            }

            // Get the Mesh links for the first LOD
            Stream meshStream = Stream.Null;

            // Use the VPXY to get the mesh lod
            Stream vpxyStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY], 0);

            if (StreamHelpers.isValidStream(vpxyStream))
            {
                VPXYFile vpxyFile = new VPXYFile(vpxyStream);
                // Get the first VPXY internal link
                if (vpxyFile.vpxy.linkEntries.Count >= 1 && vpxyFile.vpxy.linkEntries[0].tgiList.Count >= 1)
                {
                    meshStream = KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[0], 0);
                }
                vpxyStream.Close();
            }

            if (StreamHelpers.isValidStream(meshStream))
            {
                lstMeshTGILinks.Items.Clear();
                SimGeomFile simgeomfile = new SimGeomFile(meshStream);

                // Always put the bumpmap in the first position and get it from the MTNF
                //int bumpmapPos = -1;
                //int tgiAddNo = 0;
                for (int i = 0; i < simgeomfile.simgeom.mtnfChunk.entries.Count; i++)
                {

                    if (simgeomfile.simgeom.mtnfChunk.entries[i].fieldTypeHash == (uint)FieldTypes.NormalMap)
                    {
                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(Enum.GetName(typeof(FieldTypes), simgeomfile.simgeom.mtnfChunk.entries[i].fieldTypeHash));
                        item.Text = simgeomfile.simgeom.keytable.keys[(int)simgeomfile.simgeom.mtnfChunk.entries[i].dwords[0]].ToString();
                        if (simgeomfile.simgeom.keytable.keys[(int)simgeomfile.simgeom.mtnfChunk.entries[i].dwords[0]].typeId == 0x00B2D882)
                        {
                            item.Tag = "texture";
                        }
                        else
                        {
                            item.Tag = "";
                        }
                        //bumpmapPos = (int)simgeomfile.simgeom.mtnfChunk.entries[i].dwords[0];
                        lstMeshTGILinks.Items.Add(item);
                        break;
                    }

                }

                for (int i = 0; i < simgeomfile.simgeom.mtnfChunk.entries.Count; i++)
                {
                    if (simgeomfile.simgeom.mtnfChunk.entries[i].fieldTypeHash != (uint)FieldTypes.NormalMap)
                    {
                        foreach (uint fieldHash in Enum.GetValues(typeof(FieldTypes)))
                        {
                            if (fieldHash == simgeomfile.simgeom.mtnfChunk.entries[i].fieldTypeHash)
                            {
                                if (simgeomfile.simgeom.mtnfChunk.entries[i].dwords.Count > 0)
                                {
                                    ListViewItem item = new ListViewItem();
                                    item.SubItems.Add(Enum.GetName(typeof(FieldTypes), fieldHash));
                                    item.Text = simgeomfile.simgeom.keytable.keys[(int)simgeomfile.simgeom.mtnfChunk.entries[i].dwords[0]].ToString();
                                    if (simgeomfile.simgeom.keytable.keys[(int)simgeomfile.simgeom.mtnfChunk.entries[i].dwords[0]].typeId == 0x00B2D882)
                                    {
                                        item.Tag = "texture";
                                    }
                                    else
                                    {
                                        item.Tag = "";
                                    }
                                    lstMeshTGILinks.Items.Add(item);
                                }
                                break;
                            }
                        }
                    }
                }

                /*
                if (bumpmapPos > -1)
                {
                    ListViewItem item = new ListViewItem();
                    item.SubItems.Add("Bump Map");
                    item.Text = simgeomfile.simgeom.keytable.keys[bumpmapPos].ToString();
                    if (simgeomfile.simgeom.keytable.keys[bumpmapPos].typeId == 0x00B2D882)
                    {
                        item.Tag = "texture";
                    }
                    else
                    {
                        item.Tag = "";
                    }
                    lstMeshTGILinks.Items.Add(item);
                    tgiAddNo++;
                }

                for (int i = 0; i < simgeomfile.simgeom.keytable.keys.Count; i++)
                {
                    if (i != bumpmapPos)
                    {
                        ListViewItem item = new ListViewItem();
                        tgiAddNo++;
                        //item.SubItems.Add("TGI #" + tgiAddNo);
                        //item.SubItems.Add("TGI #" + (i + 1));
                        item.Text = simgeomfile.simgeom.keytable.keys[i].ToString();
                        if (simgeomfile.simgeom.keytable.keys[i].typeId == 0x00B2D882)
                        {
                            item.Tag = "texture";
                        }
                        else
                        {
                            item.Tag = "";
                        }
                        lstMeshTGILinks.Items.Add(item);
                    }
                }
                */
            }

            lstCasPartDetails.Items.Clear();
            // Populate the CAS Part Details
            addCasPartItem("Mesh Name", casPartSrc.meshName);
            toolStripStatusLabel2.Text = casPartSrc.meshName;

            addCasPartItem("Clothing Order", casPartSrc.clothingOrder.ToString());
            addCasPartItem("CAS Part Type", casPartSrc.clothingType.ToString() + " (0x" + casPartSrc.clothingType.ToString("X8") + ")");
            addCasPartItem("Type", casPartSrc.typeFlag.ToString() + " (0x" + casPartSrc.typeFlag.ToString("X8") + ")");
            addCasPartItem("Age/Gender", casPartSrc.ageGenderFlag.ToString() + " (0x" + casPartSrc.ageGenderFlag.ToString("X8") + ")");
            addCasPartItem("Clothing Category", casPartSrc.clothingCategory.ToString() + " (0x" + casPartSrc.clothingCategory.ToString("X8") + ")");
            addCasPartItem("Unk String", casPartSrc.unkString);

            addCasPartItem("Unk2", casPartSrc.unk2.ToString());
            addCasPartItem("TGI Index Body Part 1", casPartSrc.tgiIndexBodyPart1.ToString());
            addCasPartItem("TGI Index Body Part 2", casPartSrc.tgiIndexBodyPart2.ToString());
            addCasPartItem("TGI Index Blend Info Fat", casPartSrc.tgiIndexBlendInfoFat.ToString());
            addCasPartItem("TGI Index Blend Info Fit", casPartSrc.tgiIndexBlendInfoFit.ToString());
            addCasPartItem("TGI Index Blend Info Thin", casPartSrc.tgiIndexBlendInfoThin.ToString());
            addCasPartItem("TGI Index Blend Info Special", casPartSrc.tgiIndexBlendInfoSpecial.ToString());
            addCasPartItem("Unk5", casPartSrc.unk5.ToString());
            addCasPartItem("VPXY", casPartSrc.tgiIndexVPXY.ToString());

            //tgi64 tempvpxy = (tgi64)casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY];
            //txtVPXYPrimary.Text = "key:" + tempvpxy.typeid.ToString("X8") + ":" + tempvpxy.groupid.ToString("X8") + ":" + tempvpxy.instanceid.ToString("X16");

            addCasPartItem("Count 2", casPartSrc.count2.ToString());
            for (int i = 0; i < casPartSrc.count2; i++)
            {
                unkRepeat unk = (unkRepeat)casPartSrc.count2repeat[i];
                addCasPartItem("#" + i.ToString() + ": unkNum ", unk.unkNum.ToString());
                addCasPartItem("#" + i.ToString() + ": unk2", unk.unk2.ToString());
                addCasPartItem("#" + i.ToString() + ": unkRepeatInner", unk.unkRepeatInnerCount.ToString());
                for (int j = 0; j < unk.unkRepeatInnerCount; j++)
                {
                    intTriple iT = (intTriple)unk.unkRepeatInnerLoop[j];
                    addCasPartItem("#" + i.ToString() + "." + j.ToString() + ": One", iT.one.ToString());
                    addCasPartItem("#" + i.ToString() + "." + j.ToString() + ": Two", iT.two.ToString());
                    addCasPartItem("#" + i.ToString() + "." + j.ToString() + ": Three", iT.three.ToString());
                }
            }

            addCasPartItem("TGI Index Diffuse", casPartSrc.tgiIndexDiffuse.ToString());
            addCasPartItem("TGI Index Specular", casPartSrc.tgiIndexSpecular.ToString());

            addCasPartItem("Diffuse Links", casPartSrc.count3.ToString());
            for (int i = 0; i < casPartSrc.count3; i++)
            {
                byte cRepeat = (byte)casPartSrc.count3repeat[i];
                addCasPartItem("#" + i.ToString(), cRepeat.ToString());
            }

            addCasPartItem("Specular Links", casPartSrc.count4.ToString());
            for (int i = 0; i < casPartSrc.count4; i++)
            {
                byte cRepeat = (byte)casPartSrc.count4repeat[i];
                addCasPartItem("#" + i.ToString(), cRepeat.ToString());
            }

            addCasPartItem("Count 5", casPartSrc.count5.ToString());
            for (int i = 0; i < casPartSrc.count5; i++)
            {
                byte cRepeat = (byte)casPartSrc.count5repeat[i];
                addCasPartItem("#" + i.ToString(), cRepeat.ToString());
            }

            for (int i = 0; i < chkDesignType.Items.Count; i++)
            {
                chkDesignType.SetItemChecked(i, false);
            }

            for (int i = 0; i < casPartSrc.count6; i++)
            {
                MadScience.Wrappers.ResourceKey tgi = casPartSrc.tgi64list[i];
                string tgiType = MadScience.Helpers.findMetaEntry(tgi.typeId).shortName;
                Console.WriteLine(tgi.typeId.ToString() + " " + tgiType);

                if (tgi.typeId == 0x0333406C)
                {
                    if (tgi.instanceId == 0x52E8BE209C703561)
                    {
                        chkDesignType.SetItemChecked(0, true);
                    }
                    if (tgi.instanceId == 0xE37696463F6B2D6E)
                    {
                        chkDesignType.SetItemChecked(1, true);
                    }
                    if (tgi.instanceId == 0x01625DDC220C08C6)
                    {
                        chkDesignType.SetItemChecked(2, true);
                    }

                }

                addCasPartItem("TGI #" + i.ToString() + " " + tgiType, tgi.ToString());
            }

            // Category flags
            if ((casPartSrc.typeFlag & 0x1) == 0x1) checkedListType.SetItemChecked(0, true); // Hair
            if ((casPartSrc.typeFlag & 0x2) == 0x2) checkedListType.SetItemChecked(1, true); // Scalp
            if ((casPartSrc.typeFlag & 0x4) == 0x4) checkedListType.SetItemChecked(2, true); // Face Overlay
            if ((casPartSrc.typeFlag & 0x8) == 0x8) checkedListType.SetItemChecked(3, true); // Body
            if ((casPartSrc.typeFlag & 0x10) == 0x10) checkedListType.SetItemChecked(4, true); // Accessory

            switch (casPartSrc.clothingType)
            {
                case 1: checkedListClothingType.SetItemChecked(0, true); break;
                case 2: checkedListClothingType.SetItemChecked(1, true); break;
                case 3: checkedListClothingType.SetItemChecked(2, true); break;
                case 4: checkedListClothingType.SetItemChecked(3, true); break;
                case 5: checkedListClothingType.SetItemChecked(4, true); break;
                case 6: checkedListClothingType.SetItemChecked(5, true); break;
                case 7: checkedListClothingType.SetItemChecked(6, true); break;
                case 11: checkedListClothingType.SetItemChecked(7, true); break;
                case 12: checkedListClothingType.SetItemChecked(8, true); break;
                case 13: checkedListClothingType.SetItemChecked(9, true); break;
                case 14: checkedListClothingType.SetItemChecked(10, true); break;
                case 15: checkedListClothingType.SetItemChecked(11, true); break;
                case 16: checkedListClothingType.SetItemChecked(12, true); break;
                case 17: checkedListClothingType.SetItemChecked(13, true); break;
                case 18: checkedListClothingType.SetItemChecked(14, true); break;
                case 19: checkedListClothingType.SetItemChecked(15, true); break;
                case 20: checkedListClothingType.SetItemChecked(16, true); break;
                case 21: checkedListClothingType.SetItemChecked(17, true); break;
                case 22: checkedListClothingType.SetItemChecked(18, true); break;
                case 24: checkedListClothingType.SetItemChecked(19, true); break;
                case 25: checkedListClothingType.SetItemChecked(20, true); break;
                case 26: checkedListClothingType.SetItemChecked(21, true); break;
                case 29: checkedListClothingType.SetItemChecked(22, true); break;
                case 30: checkedListClothingType.SetItemChecked(23, true); break;
                case 31: checkedListClothingType.SetItemChecked(24, true); break;
            }

            if ((casPartSrc.ageGenderFlag & 0x1) == 0x1) checkedListAge.SetItemChecked(0, true); // Baby
            if ((casPartSrc.ageGenderFlag & 0x2) == 0x2) checkedListAge.SetItemChecked(1, true); // Toddler
            if ((casPartSrc.ageGenderFlag & 0x4) == 0x4) checkedListAge.SetItemChecked(2, true); // Child
            if ((casPartSrc.ageGenderFlag & 0x8) == 0x8) checkedListAge.SetItemChecked(3, true); // Teen
            if ((casPartSrc.ageGenderFlag & 0x10) == 0x10) checkedListAge.SetItemChecked(4, true); // YoungAdult
            if ((casPartSrc.ageGenderFlag & 0x20) == 0x20) checkedListAge.SetItemChecked(5, true); // Adult
            if ((casPartSrc.ageGenderFlag & 0x40) == 0x40) checkedListAge.SetItemChecked(6, true); // Elder

            if ((casPartSrc.ageGenderFlag & 0x1000) == 0x1000) checkedListGender.SetItemChecked(0, true); // Male
            if ((casPartSrc.ageGenderFlag & 0x2000) == 0x2000) checkedListGender.SetItemChecked(1, true); // Female

            if ((casPartSrc.ageGenderFlag & 0x100000) == 0x100000) checkedListOther.SetItemChecked(0, true); // LeftHanded
            if ((casPartSrc.ageGenderFlag & 0x200000) == 0x200000) checkedListOther.SetItemChecked(1, true); // RightHanded
            if ((casPartSrc.ageGenderFlag & 0x10000) == 0x10000) checkedListOther.SetItemChecked(2, true); // Human

            if ((casPartSrc.clothingCategory & 0x1) == 0x1) checkedListCategory.SetItemChecked(0, true); // Naked
            if ((casPartSrc.clothingCategory & 0x2) == 0x2) checkedListCategory.SetItemChecked(1, true); // Everyday
            if ((casPartSrc.clothingCategory & 0x4) == 0x4) checkedListCategory.SetItemChecked(2, true); // Formalwear
            if ((casPartSrc.clothingCategory & 0x8) == 0x8) checkedListCategory.SetItemChecked(3, true); // Sleepwear
            if ((casPartSrc.clothingCategory & 0x10) == 0x10) checkedListCategory.SetItemChecked(4, true); // Swimwear
            if ((casPartSrc.clothingCategory & 0x20) == 0x20) checkedListCategory.SetItemChecked(5, true); // Athletic
            if ((casPartSrc.clothingCategory & 0x40) == 0x40) checkedListCategory.SetItemChecked(6, true); // Singed
            if ((casPartSrc.clothingCategory & 0x100) == 0x100) checkedListCategory.SetItemChecked(7, true); // Career
            if ((casPartSrc.clothingCategory & 0xFFFF) == 0xFFFF) checkedListCategory.SetItemChecked(8, true); // All

            if ((casPartSrc.clothingCategory & 0x100000) == 0x100000) checkedListCategoryExtended.SetItemChecked(0, true); // ValidForMaternity
            if ((casPartSrc.clothingCategory & 0x200000) == 0x200000) checkedListCategoryExtended.SetItemChecked(1, true); // ValidForRandom
            if ((casPartSrc.clothingCategory & 0x400000) == 0x400000) checkedListCategoryExtended.SetItemChecked(2, true); // IsHat
            if ((casPartSrc.clothingCategory & 0x800000) == 0x800000) checkedListCategoryExtended.SetItemChecked(3, true); // IsRevealing
            if ((casPartSrc.clothingCategory & 0x1000000) == 0x1000000) checkedListCategoryExtended.SetItemChecked(4, true); // IsHiddenInCas

            saveAsToolStripMenuItem.Enabled = true;
            btnDumpFromFullbuild2.Enabled = true;

            lstStencilPool.Items.Clear();

            // Default all stencil boxes to blank
            for (int i = 1; i <= 15; i++)
            {
                if (stencilPool.Count < i) { stencilPool.Add(new stencilDetails()); }
                //updateStencilBoxes(i, new stencilDetails());
            }

            // Calculate all the stencils so we can build up the stencil pool
            int curStencilNum = 1;
            for (int i = 0; i < casPartSrc.xmlChunk.Count; i++)
            {
                if (debugModeToolStripMenuItem.Checked)
                {
                    Helpers.logMessageToFile(casPartSrc.xmlChunkRaw[i].ToString().Replace("/><", "/>" + Environment.NewLine + "<"));
                }

                xmlChunkDetails chunk = (xmlChunkDetails)casPartSrc.xmlChunk[i];

                if (!inStencilList(chunk.stencil.A))
                {
                    updateStencilBoxes(curStencilNum, chunk.stencil.A);
                    curStencilNum++;

                }
                if (!inStencilList(chunk.stencil.B))
                {
                    updateStencilBoxes(curStencilNum, chunk.stencil.B);
                    curStencilNum++;

                }
                if (!inStencilList(chunk.stencil.C))
                {
                    updateStencilBoxes(curStencilNum, chunk.stencil.C);
                    curStencilNum++;

                }
                if (!inStencilList(chunk.stencil.D))
                {
                    updateStencilBoxes(curStencilNum, chunk.stencil.D);
                    curStencilNum++;

                }
                if (!inStencilList(chunk.stencil.E))
                {
                    updateStencilBoxes(curStencilNum, chunk.stencil.E);
                    curStencilNum++;

                }
                if (!inStencilList(chunk.stencil.F))
                {
                    updateStencilBoxes(curStencilNum, chunk.stencil.F);
                    curStencilNum++;
                }

            }

            for (int i = curStencilNum; i <= 15; i++)
            {
                //if (stencilPool.Count < i) { stencilPool.Add(new stencilDetails()); }
                updateStencilBoxes(i, new stencilDetails());
            }

            if (this.casPartSrc.xmlChunkRaw.Count == 0)
            {
                label8.Visible = true;
                listView1.Enabled = false;
                //btnDumpFromFullbuild2.Enabled = false;
                btnAddNewDesign.Enabled = false;
                btnDeleteDesign.Enabled = false;
            }
            else
            {

                label8.Visible = false;
                listView1.Enabled = true;
                //btnDumpFromFullbuild2.Enabled = true;
                btnAddNewDesign.Enabled = true;
                btnDeleteDesign.Enabled = true;

                this.casPartNew = (casPart)OX.Copyable.ObjectExtensions.Copy(casPartSrc);
                //this.casPartNew = (casPart)casPartSrc.Copy();
                if (this.isNew)
                {
                    this.casPartNew.xmlChunk.Clear();
                    this.casPartNew.xmlChunkRaw.Clear();
                    lastSelected = -1;
                    listView1.Items.Clear();
                    lstTextureDetails.Items.Clear();
                    lstOtherDetails.Items.Clear();

                    //btnAddNewDesign_Click(this, null);
                }

            }
        }
Ejemplo n.º 4
0
        private void btnDumpFromFullbuild2_Click(object sender, EventArgs e)
        {
            // Go through the list of DDS files and dump them
            string s3root = MadScience.Helpers.findSims3Root();

            if (s3root != "")
            {

                bool hasShownDialog = false;

                toolStripProgressBar1.Visible = true;
                toolStripProgressBar1.Minimum = 0;
                toolStripProgressBar1.Value = 0;
                toolStripStatusLabel1.Text = "Searching for textures... please wait";
                statusStrip1.Refresh();

                Stream fbuild2 = File.Open(Path.Combine(s3root, Helpers.getGameSubPath("\\GameData\\Shared\\Packages\\FullBuild2.package")), FileMode.Open, FileAccess.Read, FileShare.Read);
                MadScience.Wrappers.Database db = new MadScience.Wrappers.Database(fbuild2, true);

                toolStripProgressBar1.Maximum = casPartSrc.tgi64list.Count;

                Dictionary<ulong, string> keyNames = new Dictionary<ulong, string>();
                long nowTicks = DateTime.Now.Ticks;
                Console.WriteLine("Started at: " + nowTicks);
                foreach (MadScience.Wrappers.ResourceKey entry in db._Entries.Keys)
                {
                    //DatabasePackedFile.Entry entry = db.dbpfEntries[i];
                    if (entry.typeId == (int)0x0166038C)
                    {
                        keyNames = Helpers.getKeyNames(db.GetResourceStream(entry));
                        break;
                    }
                }

                int numFound = 0;
                folderBrowserDialog1.SelectedPath = "";

                for (int j = 0; j < casPartSrc.tgi64list.Count; j++)
                {
                    toolStripProgressBar1.Value++;
                    //keyName tgi = new keyName((tgi64)casPartSrc.tgi64list[j]);
                    MadScience.Wrappers.ResourceKey tgi = casPartSrc.tgi64list[j];
                    if (tgi.typeId == (int)0x736884F1) // VPXY
                    {
                        // Find the meshes, then dump any textures the meshes reference too

                        // Use the VPXY to get the mesh lod
                        Stream vpxyStream = KeyUtils.findKey(tgi, 0);

                        //int numFound = 0;
                        //folderBrowserDialog1.SelectedPath = "";

                        if (vpxyStream != null)
                        {
                            VPXYFile vpxyFile = new VPXYFile(vpxyStream);
                            // Get the first VPXY internal link
                            if (vpxyFile.vpxy.linkEntries.Count >= 1 && vpxyFile.vpxy.linkEntries[0].tgiList.Count >= 1)
                            {
                                for (int i = 0; i < vpxyFile.vpxy.linkEntries[0].tgiList.Count; i++)
                                {
                                    //meshStreams.Add(KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[i], 0));
                                    //KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[i], 0);

                                    //ResourceKey entry = vpxyFile.vpxy.linkEntries[0].tgiList[i];

                                    Stream meshFile = KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[i], 0);

                                    SimGeomFile sgf = new SimGeomFile(meshFile);

                                    if (!hasShownDialog)
                                    {
                                        folderBrowserDialog1.Description = "Please select a folder to save the extracted textures to.";
                                        folderBrowserDialog1.ShowDialog();
                                        hasShownDialog = true;
                                    }
                                    if (folderBrowserDialog1.SelectedPath != "")
                                    {
                                        string extension = "";
                                        for (int k = 0; k < sgf.simgeom.keytable.keys.Count; k++)
                                        {
                                            ResourceKey entry = sgf.simgeom.keytable.keys[k];

                                            if (entry.typeId == 0x00B2D882)
                                            {
                                                extension = ".dds";

                                                string fileNameToSave = "";
                                                if (keyNames.ContainsKey(entry.instanceId))
                                                {
                                                    fileNameToSave = keyNames[entry.instanceId];
                                                    if (fileNameToSave.Contains("0x") == false) { fileNameToSave += "_0x" + entry.instanceId.ToString("X16"); }
                                                }
                                                else
                                                {
                                                    fileNameToSave = entry.typeId.ToString("X8") + "_" + entry.groupId.ToString("X8") + "_" + entry.instanceId.ToString("X16");
                                                }

                                                FileStream saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                                                StreamHelpers.CopyStream(KeyUtils.findKey(entry, 2, db), saveFile);
                                                saveFile.Close();
                                            }
                                        }
                                        //numFound++;
                                    }
                                    meshFile.Close();

                                    //output.Close();
                                }

                            }

                        }
                    }
                    if (tgi.typeId == (int)0x00B2D882) // DDS
                    {
                        //Stream textureStream = KeyUtils.searchForKey(tgi.ToString(), 2);
                        Stream textureStream = KeyUtils.findKey(tgi);
                        if (textureStream != null)
                        {
                            string fileNameToSave = "";
                            if (keyNames.ContainsKey(tgi.instanceId))
                            {
                                fileNameToSave = keyNames[tgi.instanceId];
                            }
                            else
                            {
                                fileNameToSave = tgi.typeId.ToString("X8") + "_" + tgi.groupId.ToString("X8") + "_" + tgi.instanceId.ToString("X16");
                            }

                            if (!hasShownDialog)
                            {
                                folderBrowserDialog1.Description = "Please select a folder to save the extracted textures to.";
                                folderBrowserDialog1.ShowDialog();
                                hasShownDialog = true;
                            }
                            if (folderBrowserDialog1.SelectedPath != "")
                            {

                                Stream output = db.GetResourceStream(tgi);
                                FileStream saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + ".dds"), FileMode.Create, FileAccess.Write);
                                StreamHelpers.CopyStream(output, saveFile);
                                saveFile.Close();
                                output.Close();
                                numFound++;
                            }

                        }
                    }
                }

                toolStripProgressBar1.Value = 0;
                toolStripStatusLabel1.Text = numFound + " textures found.";
                toolStripProgressBar1.Visible = false;
                statusStrip1.Refresh();

                fbuild2.Close();

            }
        }
Ejemplo n.º 5
0
        private void button9_Click(object sender, EventArgs e)
        {
            // Go through the list of DDS files and dump them
            string s3root = MadScience.Helpers.findSims3Root();

            if (s3root != "")
            {

                bool hasShownDialog = false;

                toolStripProgressBar1.Visible = true;
                toolStripProgressBar1.Minimum = 0;
                toolStripProgressBar1.Value = 0;
                toolStripStatusLabel1.Text = "Searching for meshes... please wait";
                statusStrip1.Refresh();

                Stream fbuild0 = File.Open(Path.Combine(s3root, Helpers.getGameSubPath("\\GameData\\Shared\\Packages\\FullBuild0.package")), FileMode.Open, FileAccess.Read, FileShare.Read);
                MadScience.Wrappers.Database db = new MadScience.Wrappers.Database(fbuild0, true);

                toolStripProgressBar1.Maximum = db._Entries.Count;

                Dictionary<ulong, string> keyNames = new Dictionary<ulong, string>();
                long nowTicks = DateTime.Now.Ticks;
                Console.WriteLine("Started at: " + nowTicks);
                foreach (MadScience.Wrappers.ResourceKey entry in db._Entries.Keys)
                {
                    //MadScience.Wrappers.ResourceKey entry = new MadScience.Wrappers.ResourceKey(keyString);
                    //DatabasePackedFile.Entry entry = db.dbpfEntries[i];
                    if (entry.typeId == (int)0x0166038C)
                    {
                        keyNames = Helpers.getKeyNames(db.GetResourceStream(entry));
                        break;
                    }
                }

                // Get the Mesh links for the first LOD
                //List<Stream> meshStreams = new List<Stream>();

                // Use the VPXY to get the mesh lod
                Stream vpxyStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY], 0);

                int numFound = 0;
                folderBrowserDialog1.SelectedPath = "";

                if (vpxyStream != null)
                {
                    VPXYFile vpxyFile = new VPXYFile(vpxyStream);
                    // Get the first VPXY internal link
                    if (vpxyFile.vpxy.linkEntries.Count >= 1) // && vpxyFile.vpxy.linkEntries[0].tgiList.Count >= 1)
                    {
                        for (int p = 0; p < vpxyFile.vpxy.linkEntries.Count; p++)
                        {
                            for (int i = 0; i < vpxyFile.vpxy.linkEntries[p].tgiList.Count; i++)
                            {
                                //meshStreams.Add(KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[i], 0));
                                //KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[i], 0);

                                ResourceKey entry = vpxyFile.vpxy.linkEntries[p].tgiList[i];

                                string fileNameToSave = "";
                                if (keyNames.ContainsKey(entry.instanceId))
                                {
                                    fileNameToSave = keyNames[entry.instanceId];
                                    if (fileNameToSave.Contains("0x") == false) { fileNameToSave += "_0x" + entry.instanceId.ToString("X16"); }
                                }
                                else
                                {
                                    fileNameToSave = entry.typeId.ToString("X8") + "_" + entry.groupId.ToString("X8") + "_" + entry.instanceId.ToString("X16");
                                }

                                Stream output = KeyUtils.findKey(entry, 0);

                                if (!hasShownDialog)
                                {
                                    folderBrowserDialog1.Description = "Please select a folder to save the extracted meshes to.";
                                    folderBrowserDialog1.ShowDialog();
                                    hasShownDialog = true;
                                }
                                if (folderBrowserDialog1.SelectedPath != "")
                                {
                                    string extension = "";
                                    if (entry.typeId == 0x015A1849)
                                    {
                                        extension = ".simgeom";
                                    }

                                    FileStream saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                                    StreamHelpers.CopyStream(output, saveFile);
                                    saveFile.Close();
                                    numFound++;
                                }
                                output.Close();
                            }
                        }

                    }

                    if (numFound > 0)
                    {
                        string fileNameToSave2 = "";
                        ResourceKey entry = casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY];
                        if (keyNames.ContainsKey(entry.instanceId))
                        {
                            fileNameToSave2 = keyNames[entry.instanceId];
                            if (fileNameToSave2.Contains("0x") == false) { fileNameToSave2 += "_0x" + entry.instanceId.ToString("X16"); }
                        }
                        else
                        {
                            fileNameToSave2 = entry.typeId.ToString("X8") + "_" + entry.groupId.ToString("X8") + "_" + entry.instanceId.ToString("X16");
                        }

                        FileStream saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave2) + ".vpxy", FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(vpxyStream, saveFile, true);
                        saveFile.Close();

                    }

                    vpxyStream.Close();

                }

                if (debugModeToolStripMenuItem.Checked)
                {
                    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);

                    if (!hasShownDialog)
                    {
                        folderBrowserDialog1.Description = "Please select a folder to save the extracted meshes to.";
                        folderBrowserDialog1.ShowDialog();
                        hasShownDialog = true;
                    }
                    if (folderBrowserDialog1.SelectedPath != "")
                    {
                        string extension = ".bodyblend";

                        string fileNameToSave = "";
                        fileNameToSave = txtMeshName.Text + "_fit";
                        FileStream saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bodyBlendFitStream, saveFile);
                        saveFile.Close();

                        fileNameToSave = txtMeshName.Text + "_fat";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bodyBlendFatStream, saveFile);
                        saveFile.Close();

                        fileNameToSave = txtMeshName.Text + "_thin";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bodyBlendThinStream, saveFile);
                        saveFile.Close();

                        fileNameToSave = txtMeshName.Text + "_special";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bodyBlendSpecialStream, saveFile);
                        saveFile.Close();

                        // Just grab the one BGEO for now
                        bodyBlendFatStream.Seek(0, SeekOrigin.Begin);
                        FacialBlend fblend = new FacialBlend(bodyBlendFatStream);
                        Stream bgeoStream = KeyUtils.findKey(fblend.blendTgi, 0);
                        bgeoStream.Seek(0, SeekOrigin.Begin);
                        extension = ".blendgeom";
                        fileNameToSave = txtMeshName.Text + "_fat";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bgeoStream, saveFile);
                        saveFile.Close();

                        bodyBlendFitStream.Seek(0, SeekOrigin.Begin);
                        fblend = new FacialBlend(bodyBlendFitStream);
                        bgeoStream = KeyUtils.findKey(fblend.blendTgi, 0);
                        bgeoStream.Seek(0, SeekOrigin.Begin);
                        fileNameToSave = txtMeshName.Text + "_fit";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bgeoStream, saveFile);
                        saveFile.Close();

                        bodyBlendThinStream.Seek(0, SeekOrigin.Begin);
                        fblend = new FacialBlend(bodyBlendThinStream);
                        bgeoStream = KeyUtils.findKey(fblend.blendTgi, 0);
                        bgeoStream.Seek(0, SeekOrigin.Begin);
                        extension = ".blendgeom";
                        fileNameToSave = txtMeshName.Text + "_thin";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bgeoStream, saveFile);
                        saveFile.Close();

                        bodyBlendSpecialStream.Seek(0, SeekOrigin.Begin);
                        fblend = new FacialBlend(bodyBlendSpecialStream);
                        bgeoStream = KeyUtils.findKey(fblend.blendTgi, 0);
                        bgeoStream.Seek(0, SeekOrigin.Begin);
                        extension = ".blendgeom";
                        fileNameToSave = txtMeshName.Text + "_special";
                        saveFile = new FileStream(Path.Combine(folderBrowserDialog1.SelectedPath, fileNameToSave + extension), FileMode.Create, FileAccess.Write);
                        StreamHelpers.CopyStream(bgeoStream, saveFile);
                        saveFile.Close();

                        //numFound++;
                    }
                }
                toolStripProgressBar1.Value = 0;
                toolStripProgressBar1.Visible = false;
                toolStripStatusLabel1.Text = numFound.ToString() + " meshes found";
                statusStrip1.Refresh();

                fbuild0.Close();
            }
        }
Ejemplo n.º 6
0
        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();
        }
        public void reload3D(xmlChunkDetails details)
        {
            if (listView1.SelectedItems.Count == 1 && cEnable3DPreview.Checked == true)
            {

                // Get the Mesh links for the first LOD
                List<Stream> meshStreams = new List<Stream>();

                // Use the VPXY to get the mesh lod
                Stream vpxyStream = KeyUtils.findKey(casPartSrc.tgi64list[casPartSrc.tgiIndexVPXY], 0);

                if (StreamHelpers.isValidStream(vpxyStream))
                {
                    VPXYFile vpxyFile = new VPXYFile(vpxyStream);
                    // Get the first VPXY internal link
                    if (vpxyFile.vpxy.linkEntries.Count >= 1 && vpxyFile.vpxy.linkEntries[0].tgiList.Count >= 1)
                    {
                        for (int i = 0; i < vpxyFile.vpxy.linkEntries[0].tgiList.Count; i++)
                        {
                            meshStreams.Add(KeyUtils.findKey(vpxyFile.vpxy.linkEntries[0].tgiList[i], 0));
                        }
                    }
                    vpxyStream.Close();
                }

                if (meshStreams.Count == 0) // || ((casPartSrc.typeFlag & 0x1) == 0x1))
                {
                    ResourceKey findKey = findDefaultMeshes(casPartNew.ageGenderFlag, casPartNew.typeFlag);

                    if (findKey.groupId != 0 && findKey.instanceId != 0)
                    {
                        meshStreams.Add(KeyUtils.findKey(findKey, 0));
                    }
                }

                const uint cBody = 4;
                const uint cTop = 5;
                const uint cBottom = 6;
                const uint cFeet = 7;

                //Load missing meshes. For example if we show a bottom, load a nude top and nude feet
                switch(casPartNew.clothingType)
                {
                    case cBody :
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Feet"));
                    break;
                    case cTop:
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Feet"));
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Bottom"));
                    break;
                    case cBottom:
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Feet"));
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Top"));
                    break;
                    case cFeet:
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Top"));
                        meshStreams.Add(findMesh(casPartNew.ageGenderFlag, "Bottom"));
                    break;
                }

                if (meshStreams.Count > 0)
                {
                    renderWindow1.BackgroundColour = MadScience.Colours.convertColour(MadScience.Helpers.getRegistryValue("renderBackgroundColour"));

                    // For each model, go through, get the model info and send it to the render window
                    for (int i = 0; i < meshStreams.Count; i++)
                    {
                        MadScience.Render.modelInfo newModel = MadScience.Render.Helpers.geomToModel(meshStreams[i]);
                        newModel.name = txtMeshName.Text;
                        //renderWindow1.loadDefaultTextures();
                        renderWindow1.setModel(newModel, i);

                    }

                    reloadTextures(details);
                    renderWindow1.RenderEnabled = true;

                }
                else
                {
                    renderWindow1.statusLabel.Text = "Sorry, we could not find a mesh!";
                }
            }
        }