Пример #1
0
        private void InjectBitmap(string filePath)
        {
            // Set the UI to be disabled while we update the arc files.
            this.EditorOwner.SetUIState(false);

            // Parse the DDS image from file.
            DDSImage ddsImage = DDSImage.FromFile(filePath);

            if (ddsImage == null)
            {
                // Failed to parse the image.
                MessageBox.Show("Failed to read " + filePath);
                this.EditorOwner.SetUIState(true);
                return;
            }

            // Convert the dds image to a rtexture.
            rTexture newTexture = rTexture.FromDDSImage(ddsImage, this.Bitmap.FileName, this.Bitmap.Datum, this.Bitmap.FileType, this.Bitmap.IsBigEndian);

            if (newTexture == null)
            {
                // Failed to convert the dds image to rtexture.
                MessageBox.Show("Failed to convert dds image to rtexture!");
                this.EditorOwner.SetUIState(true);
                return;
            }

            // Check if the old texture has a background color, and if so copy it.
            if (this.Bitmap.Flags.HasFlag(TextureFlags.HasD3DClearColor) == true)
            {
                // Copy the background color.
                newTexture.header.Flags   |= TextureFlags.HasD3DClearColor;
                newTexture.BackgroundColor = this.Bitmap.BackgroundColor;
            }

            // Write the texture to a buffer we can use to update all the files for this texture.
            byte[] textureBuffer = newTexture.ToBuffer();

            // Get a list of every datum to be updated for this file and update all of them.
            DatumIndex[] datums = this.EditorOwner.GetDatumsToUpdateForResource(this.GameResource.FileName);
            if (ArchiveCollection.Instance.InjectFile(datums, textureBuffer) == false)
            {
                // Failed to update files.
                this.EditorOwner.SetUIState(true);
                return;
            }

            // Update the game resource instance and reload the UI.
            this.GameResource = newTexture;
            OnGameResourceUpdated();

            // Image successfully injected.
            this.EditorOwner.SetUIState(true);
            MessageBox.Show("Done!");
        }
        public static bool ExportModel(rModel model, string outputFolder)
        {
            // Setup xml writer formatting settings.
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent       = true;
            settings.IndentChars  = "  ";
            settings.NewLineChars = "\r\n";
            settings.Encoding     = Encoding.UTF8;

            // Create a new xml writer for the output collada file.
            string modelName = Path.GetFileName(model.FileName).Replace(".rModel", "");

            using (XmlWriter writer = XmlWriter.Create(string.Format("{0}\\{1}.dae", outputFolder, modelName), settings))
            {
                // Write the xml header.
                writer.WriteStartDocument();

                // Write the collada element start.
                writer.WriteStartElement("COLLADA", "http://www.collada.org/2004/COLLADASchema");
                writer.WriteAttributeString("version", "1.4.1");

                // Write the asset element.
                writer.WriteStartElement("asset");
                writer.WriteStartElement("contributor");
                writer.WriteElementString("authoring_tool", "Dead Rising Arc Tool");
                writer.WriteFullEndElement();
                writer.WriteElementString("created", DateTime.Now.ToString());
                writer.WriteElementString("modified", DateTime.Now.ToString());
                writer.WriteFullEndElement();

                // Write the images library element.
                writer.WriteStartElement("library_images");
                {
                    // Loop and write and image element for every texture the model has.
                    for (int i = 0; i < model.textureFileNames.Length; i++)
                    {
                        // Get name of the texture.
                        string textureName = Path.GetFileName(model.textureFileNames[i]);

                        // Find the arc file the resource is in.
                        string textureFileName = GameResource.GetFullResourceName(model.textureFileNames[i], ResourceType.rTexture);
                        ArchiveCollection.Instance.GetArchiveFileEntryFromFileName(textureFileName, out Archive.Archive arcFile, out ArchiveFileEntry fileEntry);
                        if (arcFile == null || fileEntry == null)
                        {
                            // Failed to find a resource with the specified name.
                            return(false);
                        }

                        // Parse the game resource and cast it to rtexture.
                        rTexture texture = arcFile.GetFileAsResource <rTexture>(textureFileName);

                        // Save the texture to a dds image that can be loaded with the model.
                        DDSImage ddsImage = DDSImage.FromGameTexture(texture);
                        if (ddsImage.WriteToFile(string.Format("{0}\\{1}.dds", outputFolder, textureName)) == false)
                        {
                            // Failed to extract texture to file.
                            return(false);
                        }

                        // Write the image element.
                        writer.WriteStartElement("image");
                        writer.WriteAttributeString("name", textureName);
                        writer.WriteElementString("init_from", "./" + textureName + ".dds");
                        writer.WriteFullEndElement();
                    }
                }
                writer.WriteFullEndElement();

                // Write the materials library element.
                //writer.WriteStartElement("library_materials");
                //{
                //    // Loop through all of the materials and write each one.
                //    for (int i = 0; i < model.materials.Length; i++)
                //    {
                //        // Write the material element.
                //        writer.WriteStartElement("material");
                //        writer.WriteAttributeString("id", "Material " + i.ToString());
                //    }
                //}
                //writer.WriteFullEndElement();

                // TODO: Joints

                // Write the geometries library element.
                writer.WriteStartElement("library_geometries");
                {
                    // Loop through all the primitives and write each one.
                    for (int i = 0; i < model.primitives.Length; i++)
                    {
                        // Write the primitive to file.
                        WritePrimitiveBlock(model, writer, i);
                    }
                }
                writer.WriteFullEndElement();

                // Write the collada and document end.
                writer.WriteEndElement();
                writer.WriteEndDocument();

                // Flush to file.
                writer.Close();
                return(true);
            }
        }