private Stream saveGeom(string filename, ResourceKey bumpMapKey)
        {
            int bumpmapPos = -1;

            SimGeomFile simgeomfile = new SimGeomFile();

            Stream blah = File.Open(filename, FileMode.Open);
            simgeomfile.Load(blah);
            blah.Close();

            if (bumpMapKey.ToString() != "key:00000000:00000000:0000000000000000")
            {
                // Figure out bumpmap location
                // To do this we loop through the MTNF
                for (int i = 0; i < simgeomfile.simgeom.mtnfChunk.entries.Count; i++)
                {
                    if (simgeomfile.simgeom.mtnfChunk.entries[i].fieldTypeHash == (uint)FieldTypes.NormalMap)
                    {
                        bumpmapPos = (int)simgeomfile.simgeom.mtnfChunk.entries[i].dwords[0];
                        break;
                    }
                }
                if (bumpmapPos > -1)
                {
                    simgeomfile.simgeom.keytable.keys[bumpmapPos] = bumpMapKey;
                }
            }
            return simgeomfile.Save();
        }
Exemplo 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);
        }
Exemplo n.º 3
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (String.IsNullOrEmpty(txtNewTGI.Text) == false && MadScienceSmall.Helpers.validateKey(txtNewTGI.Text))
     {
         MadScience.Wrappers.ResourceKey rKey = new MadScience.Wrappers.ResourceKey(txtNewTGI.Text);
         faceblend.keytable.keys.Add(rKey);
         ListViewItem item = new ListViewItem();
         item.Text = "TGI #" + listView1.Items.Count.ToString();
         item.SubItems.Add(rKey.ToString());
         listView1.Items.Add(item);
         //rKey = null;
     }
 }
Exemplo n.º 4
0
 private void button5_Click(object sender, EventArgs e)
 {
     if (String.IsNullOrEmpty(txtNewTGI.Text) == false && MadScienceSmall.Helpers.validateKey(txtNewTGI.Text))
     {
         MadScience.Wrappers.ResourceKey rKey = new MadScience.Wrappers.ResourceKey(txtNewTGI.Text);
         simgeomfile.simgeom.keytable.keys.Add(rKey);
         ListViewItem item = new ListViewItem();
         item.Text = "TGI #" + listView1.Items.Count.ToString();
         item.SubItems.Add(rKey.ToString());
         listView1.Items.Add(item);
         //rKey = null;
     }
 }
        private Color skintone = Color.White; //Color.FromArgb(247, 224, 200);

        #endregion Fields

        #region Methods

        public static ResourceKey findDefaultMeshes(uint ageGenderFlag, uint typeFlag)
        {
            ResourceKey ret = new ResourceKey();

            // Load in XML
            if (defaultMeshes.Count == 0)
            {
                // Load in XML
                TextReader r = new StreamReader(Path.Combine(Application.StartupPath, Path.Combine("xml", "defaultMeshes.xml")));
                XmlSerializer s = new XmlSerializer(typeof(meshesFile));
                DeserializeMeshes(r, defaultMeshes);
                r.Close();
            }

            string flags = "";
            string highestAge = "";

            if ((ageGenderFlag & (uint)AgeGenderFlags.Baby) == (uint)AgeGenderFlags.Baby) highestAge = "b";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Toddler) == (uint)AgeGenderFlags.Toddler) highestAge = "p";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Child) == (uint)AgeGenderFlags.Child) highestAge = "c";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Teen) == (uint)AgeGenderFlags.Teen) highestAge = "t";
            if ((ageGenderFlag & (uint)AgeGenderFlags.YoungAdult) == (uint)AgeGenderFlags.YoungAdult) highestAge = "y";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Adult) == (uint)AgeGenderFlags.Adult) highestAge = "a";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Elder) == (uint)AgeGenderFlags.Elder) highestAge = "e";

            flags = highestAge;

            //just default to male for now
            if ((ageGenderFlag & (uint)AgeGenderFlags.Male) == (uint)AgeGenderFlags.Male) flags += "m";
            else if ((ageGenderFlag & (uint)AgeGenderFlags.Female) == (uint)AgeGenderFlags.Female) flags += "f";

            // Face Overlay (ie Makeup)
            if ((typeFlag & 0x4) == 0x4) flags += "Face";
            if ((typeFlag & 0x1) == 0x1) flags += "Face";

            // Body
            if ((typeFlag & 0x8) == 0x8) flags += "Body";

            // Accessory
            if ((typeFlag & 0x10) == 0x10) flags += "Accessory";

            if (defaultMeshes.ContainsKey(flags))
            {
                ret = new ResourceKey(defaultMeshes[flags]);
            }

            return ret;
        }
Exemplo n.º 6
0
        private void loadFromStream(Stream input)
        {
            BinaryReader reader = new BinaryReader(input);

            //addListItem("Start RCOL header", "");
            this.version  = reader.ReadUInt32();
            this.datatype = reader.ReadUInt32();
            uint rcolIndex3 = reader.ReadUInt32();
            uint rcolIndex1 = reader.ReadUInt32();
            uint rcolIndex2 = reader.ReadUInt32();

            // Some error checking for invalid VPXYs generated by TSR Workshop
            if (rcolIndex1 == 0 && rcolIndex2 == 0 && rcolIndex3 == 0)
            {
                // Whats the total stream size and is it more than the current position?
                if (input.Length > input.Position)
                {
                    // Try setting the rcolIndex2 to 1 - note VERY dirty hack!
                    rcolIndex2 = 1;
                }
            }


            for (int i = 0; i < rcolIndex2; i++)
            {
                MadScience.Wrappers.ResourceKey rKey = new ResourceKey(input, (int)ResourceKeyOrder.ITG);
                this.internalChunks.Add(rKey);
                //rKey = null;
            }

            for (int i = 0; i < rcolIndex1; i++)
            {
                MadScience.Wrappers.ResourceKey rKey = new ResourceKey(input, (int)ResourceKeyOrder.ITG);
                this.externalChunks.Add(rKey);
                //rKey = null;
            }

            for (int i = 0; i < rcolIndex2; i++)
            {
                MadScience.Wrappers.OffsetSize offSize = new OffsetSize(input);
                this.chunks.Add(offSize);
                offSize = null;
            }
            //addListItem("End RCOL header", "");

            reader = null;
        }
        private void loadFromStream(Stream input)
        {
            BinaryReader reader = new BinaryReader(input);

            //addListItem("Start RCOL header", "");
            this.version = reader.ReadUInt32();
            this.datatype = reader.ReadUInt32();
            uint rcolIndex3 = reader.ReadUInt32();
            uint rcolIndex1 = reader.ReadUInt32();
            uint rcolIndex2 = reader.ReadUInt32();

            // Some error checking for invalid VPXYs generated by TSR Workshop
            if (rcolIndex1 == 0 && rcolIndex2 == 0 && rcolIndex3 == 0)
            {
                // Whats the total stream size and is it more than the current position?
                if (input.Length > input.Position)
                {
                    // Try setting the rcolIndex2 to 1 - note VERY dirty hack!
                    rcolIndex2 = 1;
                }
            }

            for (int i = 0; i < rcolIndex2; i++)
            {
                MadScience.Wrappers.ResourceKey rKey = new ResourceKey(input, (int)ResourceKeyOrder.ITG);
                this.internalChunks.Add(rKey);
                //rKey = null;
            }

            for (int i = 0; i < rcolIndex1; i++)
            {
                MadScience.Wrappers.ResourceKey rKey = new ResourceKey(input, (int)ResourceKeyOrder.ITG);
                this.externalChunks.Add(rKey);
                //rKey = null;
            }

            for (int i = 0; i < rcolIndex2; i++)
            {
                MadScience.Wrappers.OffsetSize offSize = new OffsetSize(input);
                this.chunks.Add(offSize);
                offSize = null;

            }
            //addListItem("End RCOL header", "");

            reader = null;
        }
Exemplo n.º 8
0
        public void loadFile(string filename)
        {
            //reset
            initNewFile();

            toolStripStatusLabel1.Text = filename;

            if (debugModeToolStripMenuItem.Checked)
            {
                Helpers.logMessageToFile("Opening file " + filename);
            }

            casPartFile cPartFile;
            Stream inputCasPart = new MemoryStream(); ;

            FileInfo f = new FileInfo(filename);
            if (f.Extension.ToLower() == ".caspart")
            {
                inputCasPart = File.Open(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                cPartFile = new casPartFile();
                this.casPartSrc = cPartFile.Load(inputCasPart);
                inputCasPart.Close();
                this.isNew = false;
                this.fromPackage = false;
                this.stencilPool.Clear();
                //this.stencilPool.TrimExcess();

                displayCasPartFile();
            }
            else if (f.Extension.ToLower() == ".package")
            {

                this.loadedCasPart = MadScience.Package.Search.getKey(f.FullName, (int)0x034AEECB, -1, -1);
                //inputCasPart = searchInPackage(f.FullName, (int)0x034AEECB, -1, -1);
                if (this.loadedCasPart.ToString() != "00000000:00000000:0000000000000000") {

                    inputCasPart = MadScience.Package.Search.getStream(f.FullName, "key:" + this.loadedCasPart.ToString());

                    cPartFile = new casPartFile();
                    this.casPartSrc = cPartFile.Load(inputCasPart);
                    inputCasPart.Close();
                    this.isNew = false;
                    this.fromPackage = true;

                    for (int i = 0; i < this.casPartSrc.numParts; i++)
                    {
                        //ResourceKey keyPNG = new ResourceKey(0x626F60CE, thumbGroup, instanceId, (uint)ResourceKeyOrder.ITG);
                        if (MadScience.Package.Search.getKey(f.FullName, 0x626F60CE, 0x0, (long)loadedCasPart.instanceId).ToString() != "00000000:00000000:0000000000000000")
                        {
                            xmlChunkDetails chunk = (xmlChunkDetails)this.casPartSrc.xmlChunk[i];
                            chunk.hasCustomThumbnail = true;
                        }

                    }

                    Helpers.currentPackageFile = f.FullName;
                    this.stencilPool.Clear();
                    //this.stencilPool.TrimExcess();

                    saveToolStripMenuItem.Enabled = true;
                    copyDefaultsToolStripMenuItem.Enabled = false;

                    displayCasPartFile();

                    reloadTextures();
                }
                else
                {
                    MessageBox.Show("No CAS Part file can be found in this package!");
                }
            }
            this.filename = f.Name;
            makeTitleBar();
            saveAsToolStripMenuItem.Enabled = true;
        }
Exemplo n.º 9
0
        private Image extractCASThumbnail(string meshName)
        {
            //Console.WriteLine("Started " + meshName + " at " + DateTime.Now.ToString());

            Helpers.logMessageToFile("Attemping to extract thumbnail for " + meshName);

            Image tempImage = null;

            if (String.IsNullOrEmpty(sims3root))
            {
                sims3root = MadScience.Helpers.findSims3Root();
            }

            string thumbnailPackage = Helpers.getGameSubPath(@"\Thumbnails\CasThumbnails.package");
            Helpers.logMessageToFile(Path.Combine(sims3root, thumbnailPackage));
            if (sims3root != "" && File.Exists(Path.Combine(sims3root, thumbnailPackage)))
            {
                // Open CAS Thumbnails package
                Stream cast = File.Open(Path.Combine(sims3root, thumbnailPackage), FileMode.Open, FileAccess.Read, FileShare.Read);
                Database castdb = new Database(cast, true);

                ulong instanceid = MadScience.StringHelpers.HashFNV64(meshName);

                if (casThumbsKeyList.Count == 0)
                {
                    Helpers.logMessageToFile("Populating casThumbs entry lists from " + castdb._Entries.Count.ToString() + " entries");
                    foreach (MadScience.Wrappers.ResourceKey entry in castdb._Entries.Keys)
                    {
                        //DatabasePackedFile.Entry entry = castdb.dbpfEntries[i];
                        if (entry.groupId == 0x00000000 && entry.typeId == 0x626f60ce)
                        {
                            casThumbsKeyList.Add(entry.instanceId, entry);
                        }
                        if (entry.groupId == 0x00000001 && entry.typeId == 0x626f60ce)
                        {
                            casThumbsKeyList2.Add(entry.instanceId, entry);
                        }
                    }
                    Helpers.logMessageToFile("casThumbs now contains " + casThumbsKeyList.Count.ToString() + " entries");
                    Helpers.logMessageToFile("casThumbs2 now contains " + casThumbsKeyList2.Count.ToString() + " entries");
                }

                bool foundPic = false;
                ResourceKey temp = new ResourceKey();
                if (casThumbsKeyList.ContainsKey(instanceid))
                {
                    temp = casThumbsKeyList[instanceid];
                    foundPic = true;
                }
                else
                {
                    if (casThumbsKeyList2.ContainsKey(instanceid))
                    {
                        temp = casThumbsKeyList2[instanceid];
                        foundPic = true;
                    }
                }

                if (foundPic)
                {
                    tempImage = Image.FromStream(castdb.GetResourceStream(temp));
                    try
                    {
                        tempImage.Save(Path.Combine(Application.StartupPath, Path.Combine("cache", meshName + ".png")), System.Drawing.Imaging.ImageFormat.Png);
                    }
                    catch (Exception ex)
                    {
                    }

                }
                else
                {
                    Helpers.logMessageToFile("Couldn't find a match for " + meshName + " (0x" + instanceid.ToString("X16") + ")");
                }

                cast.Close();
                //dbpf = null;
                castdb = null;
            }
            else
            {
                Helpers.logMessageToFile(@"Can't find sims3root or Thumbnails\CasThumbnails.package");
            }

            //Console.WriteLine("Stopped " + meshName + " at " + DateTime.Now.ToString());

            return tempImage;
        }
Exemplo n.º 10
0
        /*
        private string convertColour(Color color)
        {
            // Converts a colour dialog box colour into a 0 to 1 style colour
            float newR = color.R / 255f;
            float newG = color.G / 255f;
            float newB = color.B / 255f;

            // Alpha is always 1
            int alpha = 1;

            //CultureInfo englishCulture = new CultureInfo("");
            string red = String.Format(CultureInfo.InvariantCulture, "{0:0.0000000}", newR);
            string green = String.Format(CultureInfo.InvariantCulture, "{0:0.0000000}", newG);
            string blue = String.Format(CultureInfo.InvariantCulture, "{0:0.0000000}", newB);

            return red + "," + green + "," + blue + "," + alpha.ToString();

        }
        */
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (validateFields() == false)
            {
                MessageBox.Show("You have a missing or invalid field!  Please double check everything and try again.");
                return;
            }

            // Save folders in registry
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Mad Scientist Productions\\" + Application.ProductName, true);
            key.SetValue("creatorName", txtCreatorName.Text);
            key.SetValue("creatorHomepage", txtCreatorHomepage.Text);
            key.Close();

            //saveFileDialog1.Filter = "Sims 3 Pack|*.Sims3Pack";
            saveFileDialog1.Filter = "Sims 3 Package|*.package|Sims 3 Pack|*.Sims3Pack";
            saveFileDialog1.Title = "Save Pattern As....";
            saveFileDialog1.FileName = "";
            saveFileDialog1.OverwritePrompt = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK && !String.IsNullOrEmpty(saveFileDialog1.FileName))
            {

                // Check for .package on the end - if it exists, strip it
                string savePath = saveFileDialog1.FileName.Substring(0, saveFileDialog1.FileName.LastIndexOf(Path.DirectorySeparatorChar) + 1);

                string saveName = "";

                FileInfo f = new FileInfo(saveFileDialog1.FileName);
                saveName = saveFileDialog1.FileName.Substring(saveFileDialog1.FileName.LastIndexOf(Path.DirectorySeparatorChar) + 1).Replace(f.Extension, "");

                toggleEverything(false);

                label1.Visible = true;
                label1.Refresh();
                this.Refresh();

                // Construct name for XML
                string fName = MadScience.Helpers.sanitiseString(saveName);

                string pName = "DPP_" + MadScience.Helpers.sanitiseString(txtCreatorName.Text.Trim()) + "_" + fName + "_" + MadScience.Helpers.sanitiseString(dateTimePicker1.Value.ToString());

                ulong instanceId = MadScience.StringHelpers.HashFNV64(pName);
                ulong specInstanceId = MadScience.StringHelpers.HashFNV64(pName + "_spec");

                //uint groupId = 33554432;
                uint groupId = 0;

                int numChans = numChannels();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<complate category=\"" + cmbCategory.Text + "\" type=\"fabric\" name=\"" + pName + "\" typeConverter=\"Medator.ComplateConvertor,Medator\" surfaceMaterial=\"" + cmbSurfaceMat.Text + "\" reskey=\"key:0333406c:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "\">");
                sb.AppendLine("  <GUID>" + System.Guid.NewGuid().ToString() + "</GUID>");
                sb.AppendLine("  <variables>");

                // Convert color boxes into 0 to 1 style colours
                if (numChans >= 1) { sb.AppendLine("    <param type=\"color\" name=\"Color 0\" default=\"" + MadScience.Colours.convertColour(lblPalette1.BackColor) + "\" uiEditor=\"Medator.Color4TypeEditor, Medator\" uiCategory=\"Colors\" />"); }
                if (numChans >= 2) { sb.AppendLine("    <param type=\"color\" name=\"Color 1\" default=\"" + MadScience.Colours.convertColour(lblPalette2.BackColor) + "\" uiEditor=\"Medator.Color4TypeEditor, Medator\" uiCategory=\"Colors\" />"); }
                if (numChans >= 3) { sb.AppendLine("    <param type=\"color\" name=\"Color 2\" default=\"" + MadScience.Colours.convertColour(lblPalette3.BackColor) + "\" uiEditor=\"Medator.Color4TypeEditor, Medator\" uiCategory=\"Colors\" />"); }
                if (numChans == 4) { sb.AppendLine("    <param type=\"color\" name=\"Color 3\" default=\"" + MadScience.Colours.convertColour(lblPalette4.BackColor) + "\" uiEditor=\"Medator.Color4TypeEditor, Medator\" uiCategory=\"Colors\" />"); }

                sb.AppendLine("    <param type=\"texture\" name=\"rgbmask\" uiVisible=\"false\" default=\"key:00b2d882:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "\" />");
                if (chkUseDefaultSpecular.Checked)
                {
                    sb.AppendLine("    <param type=\"texture\" name=\"specmap\" uiVisible=\"false\" default=\"($assetRoot)\\InGame\\GlobalTextures\\Objects\\defaultBlackSpecular.tga\" />");
                }
                else
                {
                    sb.AppendLine("    <param type=\"texture\" name=\"specmap\" uiVisible=\"false\" default=\"key:00b2d882:" + groupId.ToString("X8").ToLower() + ":" + specInstanceId.ToString("X16").ToLower() + "\" />");
                }
                sb.AppendLine("  </variables>");

                sb.AppendLine("  <texturePart>");
                sb.AppendLine("    <destination>");
                if (!chkAllowDecal.Checked)
                {
                    sb.AppendLine("      <step type=\"ColorFill\" color=\"" + MadScience.Colours.convertColour(lblBackgroundColour.BackColor) + "\" />");
                }
                // 2 lines per channel
                if (numChans >= 1)
                {
                    sb.AppendLine("      <step type=\"ChannelSelect\" texture=\"($rgbmask)\" select=\"1.0000,0.0000,0.0000,0.0000\" colorWrite=\"Alpha\" />");
                    sb.AppendLine("      <step type=\"ColorFill\" color=\"($Color 0)\" enableBlending=\"" + chkPalette1Blend.Checked.ToString().ToLower() + "\" srcBlend=\"DestAlpha\" dstBlend=\"InvDestAlpha\" sourceRect=\"0,0,1,1\" />");
                }
                if (numChans >= 2)
                {
                    sb.AppendLine("      <step type=\"ChannelSelect\" texture=\"($rgbmask)\" select=\"0.0000,1.0000,0.0000,0.0000\" colorWrite=\"Alpha\" />");
                    sb.AppendLine("      <step type=\"ColorFill\" color=\"($Color 1)\" enableBlending=\"" + chkPalette1Blend.Checked.ToString().ToLower() + "\" srcBlend=\"DestAlpha\" dstBlend=\"InvDestAlpha\" sourceRect=\"0,0,1,1\" />");
                }
                if (numChans >= 3)
                {
                    sb.AppendLine("      <step type=\"ChannelSelect\" texture=\"($rgbmask)\" select=\"0.0000,0.0000,1.0000,0.0000\" colorWrite=\"Alpha\" />");
                    sb.AppendLine("      <step type=\"ColorFill\" color=\"($Color 2)\" enableBlending=\"" + chkPalette1Blend.Checked.ToString().ToLower() + "\" srcBlend=\"DestAlpha\" dstBlend=\"InvDestAlpha\" sourceRect=\"0,0,1,1\" />");
                }
                if (numChans >= 4)
                {
                    sb.AppendLine("      <step type=\"ChannelSelect\" texture=\"($rgbmask)\" select=\"0.0000,0.0000,0.0000,1.0000\" colorWrite=\"Alpha\" />");
                    sb.AppendLine("      <step type=\"ColorFill\" color=\"($Color 3)\" enableBlending=\"" + chkPalette1Blend.Checked.ToString().ToLower() + "\" srcBlend=\"DestAlpha\" dstBlend=\"InvDestAlpha\" sourceRect=\"0,0,1,1\" />");
                }

                sb.AppendLine("      <step type=\"ChannelSelect\" texture=\"($specmap)\" select=\"1.0,0.0,0.0,0.0\" colorWrite=\"Alpha\" />");
                sb.AppendLine("    </destination>");
                sb.AppendLine("  </texturePart>");

                sb.AppendLine("  <localizedName key=\"Name:" + pName + "\" />");
                sb.AppendLine("  <contentType type=\"kCustomContent\" />");
                sb.AppendLine("  <localizedDescription key=\"Name:" + pName + "\" />");
                sb.AppendLine("</complate>");

                string xml033406c = sb.ToString();

                sb = null;
                sb = new StringBuilder();

                // Now for the d4d9fbe5 patternlist file
                sb.AppendLine("<patternlist>");
                sb.AppendLine("  <category name=\"" + cmbCategory.Text + "\">");
                sb.AppendLine("    <pattern reskey=\"key:0333406c:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "\" name=\"" + pName + "\" complexity=\"\" />");
                sb.AppendLine("  </category>");
                sb.AppendLine("</patternlist>");

                string xmld4d9fbe = sb.ToString();

                sb = null;
                sb = new StringBuilder();

                sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                sb.Append("<manifest version=\"3\" packagetype=\"pattern\" packagesubtype=\"0x00000000\" paidcontent=\"false\">");
                sb.Append("<gameversion>0.0.0.11195</gameversion>");
                sb.Append("<packagedate>" + dateTimePicker1.Value.ToString() + "</packagedate>");
                sb.Append("<packageid>" + pName + "</packageid>");
                sb.Append("<packagetitle><![CDATA[" + txtPatternTitle.Text + "]]></packagetitle>");
                sb.Append("<packagedesc><![CDATA[" + txtPatternDesc.Text + "]]></packagedesc>");
                sb.Append("<assetversion>0</assetversion>");
                sb.Append("<mingamever>1.0.0.0</mingamever>");
                sb.Append("<thumbnail>2e75c765:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "</thumbnail>");
                sb.Append("<localizednames>");
                sb.Append("<localizedname language=\"en-US\"><![CDATA[" + txtPatternTitle.Text + "]]></localizedname>");
                sb.Append("</localizednames>");
                sb.Append("<localizeddescriptions>");
                sb.Append("<localizeddescription language=\"en-US\"><![CDATA[" + txtPatternDesc.Text + "]]></localizeddescription>");
                sb.Append("</localizeddescriptions>");
                sb.Append("<handler />");
                sb.Append("<dependencylist>");
                sb.Append("<packageid>0x050cffe800000000050cffe800000000</packageid>");
                sb.Append("<packageid>" + pName + "</packageid>");
                sb.Append("</dependencylist>");
                sb.Append("<keylist>");
                sb.Append("<reskey>1:0333406c:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "</reskey>");
                sb.Append("<reskey>1:00b2d882:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "</reskey>");
                sb.Append("<reskey>1:d4d9fbe5:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "</reskey>");
                sb.Append("<reskey>1:2e75c765:" + groupId.ToString("X8").ToLower() + ":" + instanceId.ToString("X16").ToLower() + "</reskey>");
                if (chkUseDefaultSpecular.Checked == false) { sb.Append("<reskey>1:00b2d882:" + groupId.ToString("X8").ToLower() + ":" + specInstanceId.ToString("X16").ToLower() + "</reskey>"); }
                sb.Append("</keylist>");
                sb.Append("<metatags>");
                sb.Append("<numofthumbs>1</numofthumbs>");
                sb.Append("<matcategory>" + cmbCategory.Text + "</matcategory>");
                sb.Append("</metatags>");
                sb.Append("</manifest>");

                string xml73e93eeb = sb.ToString();
                sb = null;

                sb = new StringBuilder();

                sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                sb.Append("<communityCustomContentManifest>");
                sb.Append("<installTo>{CCROOT}/installTo>");
                sb.Append("<contentType>Pattern</contentType>");
                sb.Append("<creatorName><![CDATA[" + MadScience.Helpers.sanitiseString(txtCreatorName.Text.Trim()) + "]]></creatorName>");
                sb.Append("<homePage><![CDATA[" + txtCreatorHomepage.Text.Trim() + "]]></homePage>");
                sb.Append("<toolName>DelphysPatternCreator</toolName>");
                sb.Append("</communityCustomContentManifest>");

                string xmlCommunityCustomContentPackage = sb.ToString();
                sb = null;

                Stream output;
                if (f.Extension.ToLower() == ".package")
                {
                    output = File.Open(savePath + saveName + ".package", FileMode.Create, FileAccess.ReadWrite);
                }
                else
                {
                    output = new MemoryStream();
                }

                Database db = new Database(output, false);
                ResourceKey rkey;

                rkey = new ResourceKey((uint)0xDEADBEEF, (uint)0xDEADBEEF, (ulong)0xcafebabeb000b135);
                db.SetResource(rkey, Encoding.UTF8.GetBytes(xmlCommunityCustomContentPackage));

                // 0x73e93eeb
                rkey = new ResourceKey(1944665835, 0, (ulong)0);
                db.SetResource(rkey, Encoding.UTF8.GetBytes(xml73e93eeb));

                // 0x0333406
                rkey = new ResourceKey(53690476, groupId, instanceId);
                db.SetResource(rkey, Encoding.UTF8.GetBytes(xml033406c));

                // 0xd4d9fbe5
                rkey = new ResourceKey(3571055589, groupId, instanceId);
                db.SetResource(rkey, Encoding.UTF8.GetBytes(xmld4d9fbe));

                // 0x00B2D882
                Stream ddsImage = File.Open(txtSourceDDS.Text, FileMode.Open);
                rkey = new ResourceKey(11720834, groupId, instanceId);
                db.SetResourceStream(rkey, ddsImage);
                ddsImage.Close();

                if (chkUseDefaultSpecular.Checked == false)
                {
                    // 0x00B2D882
                    Stream ddsImageSpec = File.Open(lblSpecularCustom.Text, FileMode.Open);
                    rkey = new ResourceKey(11720834, groupId, specInstanceId);
                    db.SetResourceStream(rkey, ddsImageSpec);
                    ddsImageSpec.Close();

                }

                // PNG file
                MemoryStream pngFile = new MemoryStream();
                if (radioButton1.Checked)
                {
                    toggleColourPalettes(true, true, false, false, false);
                }
                if (radioButton2.Checked)
                {
                    toggleColourPalettes(true, true, true, false, false);
                }
                if (radioButton3.Checked)
                {
                    toggleColourPalettes(true, true, true, true, false);
                }
                if (radioButton4.Checked)
                {
                    toggleColourPalettes(true, true, true, true, true);
                }

                pictureBox1.Image.Save(pngFile, System.Drawing.Imaging.ImageFormat.Png);

                pngFile.Seek(0, SeekOrigin.Begin);
                rkey = new ResourceKey(0x2E75C765, groupId, instanceId);
                db.SetResourceStream(rkey, pngFile);

                db.Commit(true);

                //output.Close();

                //FileInfo fi = new FileInfo(saveFileDialog1.FileName);

                if (f.Extension.ToLower() == ".sims3pack")
                {

                    sb = new StringBuilder();
                    sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    sb.Append("<Sims3Package Type=\"pattern\" SubType=\"0x00000000\">");
                    sb.Append("<ArchiveVersion>1.4</ArchiveVersion>");
                    sb.Append("<CodeVersion>0.0.0.34</CodeVersion>");
                    sb.Append("<GameVersion>0.0.0.0</GameVersion>");
                    sb.Append("<PackageId>" + pName + "</PackageId>");
                    sb.Append("<Date>" + dateTimePicker1.Value.ToString() + "</Date>");
                    sb.Append("<AssetVersion>0</AssetVersion>");
                    sb.Append("<MinReqVersion>1.0.0.0</MinReqVersion>");
                    sb.Append("<DisplayName><![CDATA[" + txtPatternTitle.Text + "]]></DisplayName>");
                    sb.Append("<Description><![CDATA[" + txtPatternDesc.Text + "]]></Description>");
                    //sb.Append("<DisplayName>" + txtPatternTitle.Text + "</DisplayName>");
                    //sb.Append("<Description>" + txtPatternDesc.Text + "</Description>");
                    sb.Append("<Dependencies>");
                    sb.Append("<Dependency>0x050cffe800000000050cffe800000000</Dependency>");
                    //sb.Append("<Dependency>" + pName + "</Dependency>");
                    sb.Append("</Dependencies>");
                    sb.Append("<LocalizedNames>");
                    sb.Append("<LocalizedName Language=\"en-US\"><![CDATA[" + txtPatternTitle.Text + "]]></LocalizedName>");
                    sb.Append("</LocalizedNames>");
                    sb.Append("<LocalizedDescriptions>");
                    sb.Append("<LocalizedDescription Language=\"en-US\"><![CDATA[" + txtPatternDesc.Text + "]]></LocalizedDescription>");
                    sb.Append("</LocalizedDescriptions>");
                    sb.Append("<PackagedFile>");
                    sb.Append("<Name>" + pName + ".package</Name>");
                    sb.Append("<Length>" + output.Length + "</Length>");
                    sb.Append("<Offset>0</Offset>");
                    //sb.Append("    <Crc>317211BAD0B3E0F5</Crc>");
                    sb.Append("<Guid>" + pName + "</Guid>");
                    sb.Append("<ContentType>pattern</ContentType>");
                    sb.Append("<MetaTags />");
                    sb.Append("</PackagedFile>");
                    /*
                    sb.Append("    <metatags>");
                    sb.Append("      <numOfThumbs>1</numOfThumbs>");
                    sb.Append("      <matCategory>" + cmbCategory.Text + "</matCategory>");
                    sb.Append("    </metatags>");

                    */
                    /*
                    sb.Append("  <PackagedFile>");
                    sb.Append("    <Name>" + pName + ".png</Name>");
                    sb.Append("    <Length>" + pngFile.Length + "</Length>");
                    sb.Append("    <Offset>" + output.Length + "</Offset>");
                    //sb.Append("    <Crc>317211BAD0B3E0F5</Crc>");
                    sb.Append("    <Guid>" + pName + "png</Guid>");
                    sb.Append("    <ContentType>unknown</ContentType>");
                    sb.Append("    <metatags />");
                    sb.Append("  </PackagedFile>");
                    */
                    sb.Append("</Sims3Package>");

                    string s3p_xml = sb.ToString();
                    sb = null;

                    Stream sims3pack = File.Open(savePath + saveName + ".Sims3Pack", FileMode.Create, FileAccess.ReadWrite);

                    MadScience.StreamHelpers.WriteValueU32(sims3pack, 7);
                    MadScience.StreamHelpers.WriteStringASCII(sims3pack, "TS3Pack");
                    MadScience.StreamHelpers.WriteValueU16(sims3pack, 257);
                    MadScience.StreamHelpers.WriteValueU32(sims3pack, (uint)s3p_xml.Length);
                    MadScience.StreamHelpers.WriteStringUTF8(sims3pack, s3p_xml);

                    MadScience.StreamHelpers.CopyStream(output, sims3pack, true);
                    //ReadWriteStream(pngFile, sims3pack, true);

                    sims3pack.Close();

                }
                pngFile.Close();
                output.Close();

                toggleEverything(true);
                label1.Visible = false;

            }
        }
        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();
        }
        private void writeLocalResource(Database db, string keyName)
        {
            if (String.IsNullOrEmpty(keyName)) return;
            //if (!validateKey(keyName)) return;

            if (Helpers.localFiles.ContainsKey(keyName))
            {
                ResourceKey key = new ResourceKey(keyName);
                Stream newDDS = File.Open((string)Helpers.localFiles[keyName], FileMode.Open, FileAccess.Read, FileShare.Read);
                db.SetResourceStream(key, newDDS);
                newDDS.Close();
            }
        }
        public static Stream findMesh(uint ageGenderFlag, string Type)
        {
            ResourceKey ret = new ResourceKey();

            // Load in XML
            if (defaultMeshes.Count == 0)
            {
                // Load in XML
                TextReader r = new StreamReader(Path.Combine(Application.StartupPath, Path.Combine("xml", "defaultMeshes.xml")));
                XmlSerializer s = new XmlSerializer(typeof(meshesFile));
                DeserializeMeshes(r, defaultMeshes);
                r.Close();
            }

            string flags = "";
            string highestAge = "";

            if ((ageGenderFlag & (uint)AgeGenderFlags.Baby) == (uint)AgeGenderFlags.Baby) highestAge = "b";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Toddler) == (uint)AgeGenderFlags.Toddler) highestAge = "p";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Child) == (uint)AgeGenderFlags.Child) highestAge = "c";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Teen) == (uint)AgeGenderFlags.Teen) highestAge = "t";
            if ((ageGenderFlag & (uint)AgeGenderFlags.YoungAdult) == (uint)AgeGenderFlags.YoungAdult) highestAge = "y";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Adult) == (uint)AgeGenderFlags.Adult) highestAge = "a";
            if ((ageGenderFlag & (uint)AgeGenderFlags.Elder) == (uint)AgeGenderFlags.Elder) highestAge = "e";

            flags = highestAge;

            //just default to male for now
            if ((ageGenderFlag & (uint)AgeGenderFlags.Male) == (uint)AgeGenderFlags.Male) flags += "m";
            else if ((ageGenderFlag & (uint)AgeGenderFlags.Female) == (uint)AgeGenderFlags.Female) flags += "f";

            flags += Type;

            if (defaultMeshes.ContainsKey(flags))
            {
                ret = new ResourceKey(0x015A1849,GetMeshGroup(defaultMeshes[flags]),GetMeshInstance(defaultMeshes[flags]));
            }

            return KeyUtils.findKey(ret,0);
        }
        public static ResourceKey getKey(string filename, int typeID, int groupID, long instanceID)
        {
            ResourceKey matchChunk = new ResourceKey();

            if (String.IsNullOrEmpty(filename)) { return matchChunk; }

            // Open the package file and search
            Stream package = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            MadScience.Wrappers.Database db = new MadScience.Wrappers.Database(package, true);

            int searchType = 0;
            if (typeID != -1) { searchType += 1; }
            if (groupID != -1) { searchType += 2; }
            if (instanceID != -1) { searchType += 4; }

            bool foundMatch = false;

            foreach (MadScience.Wrappers.ResourceKey entry in db._Entries.Keys)
            {
                //ResourceKey key = db.Entries.Keys[i];
                //DatabasePackedFile.Entry entry = db.Entries.Keys[i];
                //DatabasePackedFile.Entry entry = db.dbpfEntries[i];
                //MadScience.Wrappers.ResourceKey entry = new MadScience.Wrappers.ResourceKey(keyString);

                switch (searchType)
                {
                    case 7:
                        if (entry.typeId == typeID && entry.groupId == groupID && entry.instanceId == (ulong)instanceID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                    case 6:
                        if (entry.groupId == groupID && entry.instanceId == (ulong)instanceID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                    case 5:
                        if (entry.typeId == typeID && entry.instanceId == (ulong)instanceID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                    case 4:
                        if (entry.instanceId == (ulong)instanceID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                    case 3:
                        if (entry.typeId == typeID && entry.groupId == groupID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                    case 2:
                        if (entry.groupId == groupID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                    case 1:
                        if (entry.typeId == typeID)
                        {
                            //loadedCasPart = entry;
                            //matchChunk = db.GetResourceStream(entry);
                            matchChunk = entry;
                            foundMatch = true;
                        }
                        break;
                }
                if (foundMatch)
                {
                    break;
                }

            }
            package.Close();

            return matchChunk;
        }
Exemplo n.º 15
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");
        }
Exemplo n.º 16
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 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);
                }
            }
        }