Exemplo n.º 1
0
        private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //ToolTipText is the full filename
            var fileName = ((ToolStripMenuItem)sender).ToolTipText;
            var tag      = ((ToolStripMenuItem)sender).Tag as ExportData;
            var resource = tag.Resource;

            Console.WriteLine($"Export requested for {fileName}");

            string[] extensions = null;
            switch (resource.ResourceType)
            {
            case ResourceType.Sound:
                //WAV or MP3
                extensions = new[] { ((Sound)resource.Blocks[BlockType.DATA]).Type.ToString().ToLower() };
                break;

            case ResourceType.Texture:
                extensions = new[] { "png" };
                break;

            case ResourceType.PanoramaLayout:
                extensions = new[] { "xml", "vxml" };
                break;

            case ResourceType.PanoramaScript:
                extensions = new[] { "js", "vjs" };
                break;

            case ResourceType.PanoramaStyle:
                extensions = new[] { "css", "vcss" };
                break;

            case ResourceType.Mesh:
                extensions = new[] { "obj" };
                break;
            }

            //Did we find a format we like?
            if (extensions != null)
            {
                var dialog = new SaveFileDialog
                {
                    FileName         = Path.GetFileName(Path.ChangeExtension(fileName, extensions[0])),
                    InitialDirectory = Settings.Config.SaveDirectory,
                    DefaultExt       = extensions[0],
                };

                var filter = string.Empty;
                foreach (var extension in extensions)
                {
                    filter += $"{extension} files (*.{extension})|*.{extension}|";
                }

                //Remove the last |
                dialog.Filter = filter.Substring(0, filter.Length - 1);

                var result = dialog.ShowDialog();

                if (result == DialogResult.OK)
                {
                    Settings.Config.SaveDirectory = Path.GetDirectoryName(dialog.FileName);
                    Settings.Save();

                    using (var stream = dialog.OpenFile())
                    {
                        // TODO: move this to FileExtract/VRF too
                        if (resource.ResourceType == ResourceType.Mesh)
                        {
                            using (var objStream = new StreamWriter(stream))
                                using (var mtlStream = new StreamWriter(Path.ChangeExtension(dialog.FileName, "mtl")))
                                {
                                    MeshObject.WriteObject(objStream, mtlStream, Path.GetFileNameWithoutExtension(dialog.FileName), resource);
                                }

                            foreach (var texture in tag.Renderer.MaterialLoader.LoadedTextures)
                            {
                                Console.WriteLine($"Exporting texture for mesh: {texture}");

                                var textureResource = FileExtensions.LoadFileByAnyMeansNecessary(texture + "_c", tag.Renderer.CurrentFileName, tag.Renderer.CurrentPackage);
                                var textureImage    = SKImage.FromBitmap(((Texture)textureResource.Blocks[BlockType.DATA]).GenerateBitmap());

                                using (var texStream = new FileStream(Path.Combine(Path.GetDirectoryName(dialog.FileName), Path.GetFileNameWithoutExtension(texture) + ".png"), FileMode.Create, FileAccess.Write))
                                    using (var data = textureImage.Encode(SKEncodedImageFormat.Png, 100))
                                    {
                                        data.SaveTo(texStream);
                                    }
                            }
                        }
                        else
                        {
                            var data = ValveResourceFormat.IO.FileExtract.Extract(resource).ToArray();
                            stream.Write(data, 0, data.Length);
                        }
                    }
                }
            }

            Console.WriteLine($"Export requested for {fileName} Complete");
        }