/// <summary>
        /// Builds all the content files which have been added to the project,
        /// dynamically creating .xnb files in the OutputDirectory.
        /// </summary>
        public bool Build(XnaContentProject xnaContentProject)
        {
            string tempPath = Path.Combine(Path.GetTempPath(), "XNAContentCompiler", Path.GetRandomFileName());

            Project project      = CreateProject(xnaContentProject, tempPath);
            bool    buildSuccess = BuildProject(project);

            buildSuccess = buildSuccess && CopyOutput(xnaContentProject, tempPath);

            return(buildSuccess);
        }
        // constructors

        public ContentCompilerForm()
        {
            InitializeComponent();

            projectBuilder        = new ProjectBuilder();
            projectBuilder.Error += (sender, e) => LogMessage(e.Message, false);

            xnaContentProject = new XnaContentProject();
            xnaContentProject.ContentItems = new BindingList <XnaContentItem>();

            dataGridView.AutoGenerateColumns = false;
            dataGridView.DataSource          = xnaContentProject.ContentItems;
        }
        private bool CopyOutput(XnaContentProject xnaContentProject, string tempPath)
        {
            OnError("Copying output...");

            Directory.CreateDirectory(xnaContentProject.OutputDirectory);
            foreach (var file in Directory.GetFiles(tempPath, "*.xnb"))
            {
                File.Copy(file, Path.Combine(xnaContentProject.OutputDirectory, Path.GetFileName(file)), true);
            }

            OnError("Copying output complete.");

            return(true);
        }
        public Project CreateProject(XnaContentProject xnaContentProject, string tempPath)
        {
            OnError("Creating project...");

            // Create the build project.
            ProjectRootElement projectRootElement = ProjectRootElement.Create();

            // Include the standard targets file that defines how to build XNA Framework content.
            projectRootElement.AddImport(targetsFile);

            // create the specific project
            Project buildProject = new Project(projectRootElement);

            buildProject.SetProperty("XnaPlatform", "Windows");
            buildProject.SetProperty("XnaProfile", "Reach");
            buildProject.SetProperty("XnaFrameworkVersion", "v4.0");
            buildProject.SetProperty("Configuration", "Release");
            buildProject.SetProperty("OutputPath", tempPath);
            buildProject.SetProperty("XNACompressContent", xnaContentProject.Compressed ? "true" : "false");

            // Register any custom importers or processors.
            foreach (string pipelineAssembly in pipelineAssemblies)
            {
                buildProject.AddItem("Reference", pipelineAssembly);
            }

            // add the content items
            foreach (var contentItem in xnaContentProject.ContentItems)
            {
                // skip the ones without processors or importers
                if (contentItem.Importer == null || contentItem.Processor == null)
                {
                    OnError(string.Format("Skipping content item '{0}' because it does not have both a processor and an importer.", contentItem.FileName));
                    continue;
                }

                OnError(string.Format("Adding content item '{0}'...", contentItem.FileName));

                ProjectItem item = buildProject.AddItem("Compile", contentItem.FilePath, new Dictionary <string, string>
                {
                    { "Link", Path.GetFileName(contentItem.FilePath) },
                    { "Name", contentItem.ContentItem },
                    { "Importer", contentItem.Importer.ToString() },
                    { "Processor", contentItem.Processor.ToString() },
                })[0];

                if (contentItem.GenerateMipmaps.HasValue)
                {
                    item.SetMetadataValue("ProcessorParameters_GenerateMipmaps", contentItem.GenerateMipmaps.Value ? "true" : "false");
                }
                if (contentItem.PremultiplyAlpha.HasValue)
                {
                    item.SetMetadataValue("ProcessorParameters_PremultiplyAlpha", contentItem.PremultiplyAlpha.Value ? "true" : "false");
                }
                if (contentItem.ResizeToPowerOfTwo.HasValue)
                {
                    item.SetMetadataValue("ProcessorParameters_ResizeToPowerOfTwo", contentItem.ResizeToPowerOfTwo.Value ? "true" : "false");
                }
                if (contentItem.TextureFormat.HasValue)
                {
                    item.SetMetadataValue("ProcessorParameters_TextureFormat", contentItem.TextureFormat.ToString());
                }
                if (contentItem.ColorKeyEnabled.HasValue)
                {
                    item.SetMetadataValue("ProcessorParameters_ColorKeyEnabled", contentItem.ColorKeyEnabled.Value ? "true" : "false");
                    if (contentItem.ColorKeyEnabled == true)
                    {
                        item.SetMetadataValue("ProcessorParameters_ColorKeyColor", String.Format("{0}, {1}, {2}, {3}",
                                                                                                 contentItem.ColorKeyColor.R, contentItem.ColorKeyColor.G, contentItem.ColorKeyColor.B, contentItem.ColorKeyColor.A));
                    }
                }
                if (contentItem.Quality.HasValue)
                {
                    item.SetMetadataValue("ProcessorParameters_Quality", contentItem.Quality.ToString());
                }
            }

            OnError("Project creation complete.");

            return(buildProject);
        }