FlatRedBall.Glue.ProjectManager.UpdateFileMembershipInProject C# (CSharp) Method

UpdateFileMembershipInProject() public static method

Adds the argument fileRelativeToProject to the argument project if it's not already part of the project.
public static UpdateFileMembershipInProject ( ProjectBase project, string fileRelativeToProject, bool useContentPipeline, bool shouldLink, string parentFile = null ) : bool
project ProjectBase
fileRelativeToProject string
useContentPipeline bool
shouldLink bool
parentFile string
return bool
        public static bool UpdateFileMembershipInProject(ProjectBase project, string fileRelativeToProject, bool useContentPipeline, bool shouldLink, string parentFile = null)
        {
            bool wasAdded = false;
            ///////////////////Early Out/////////////////////
            if (project == null) return wasAdded;

            /////////////////End Early Out//////////////////

            string fileToAddAbsolute = MakeAbsolute(fileRelativeToProject);

            fileToAddAbsolute = fileToAddAbsolute.Replace("/", "\\");

            bool isFileAlreadyPartOfProject = false;

            bool needsToBeInContentProject = ShouldFileBeInContentProject(fileToAddAbsolute);

            BuildItemMembershipType bimt = BuildItemMembershipType.CopyIfNewer;
            if (useContentPipeline)
            {
                bimt = BuildItemMembershipType.CompileOrContentPipeline;
            }
            else if (!project.ContentProject.ContentCopiedToOutput)
            {
                bimt = BuildItemMembershipType.Content;
            }

            if (!needsToBeInContentProject)
            {
                isFileAlreadyPartOfProject = project.IsFilePartOfProject(fileRelativeToProject, BuildItemMembershipType.CompileOrContentPipeline);
            }

            if (!isFileAlreadyPartOfProject && needsToBeInContentProject)
            {
                // Here we're going to get the absolute file name.
                // We want to get the file name 
                string fileRelativeToContent = FileManager.MakeRelative(
                    fileToAddAbsolute,
                    FileManager.GetDirectory(project.ContentProject.FullFileName));

                fileRelativeToContent = fileRelativeToContent.Replace("/", "\\");

                isFileAlreadyPartOfProject = project.ContentProject.IsFilePartOfProject(fileRelativeToContent, bimt);

                if (!isFileAlreadyPartOfProject)
                {
                    var buildItem = project.ContentProject.GetItem(fileRelativeToContent);
                    if (buildItem != null)
                    {
                        // The item is here but it's using the wrong build types.  Let's
                        // remove it and readd it so that it gets added with the right options.
                        // Let's remove it and say it's not part of the project so it gets removed and readded
                        project.ContentProject.RemoveItem(fileRelativeToContent);
                    }
                }
            }

            if (!isFileAlreadyPartOfProject)
            {
                wasAdded = true;

                if (needsToBeInContentProject)
                {
                    AddFileToContentProject(project, useContentPipeline, shouldLink, fileToAddAbsolute);
                }
                else
                {
                    CodeProjectHelper.AddFileToCodeProject(project, fileToAddAbsolute);
                }
            }

            var listOfReferencedFiles = new List<string>();

            bool didErrorOccur = false;
            try
            {
                // Glue is going to assume .cs files can't reference content:
                if(!fileToAddAbsolute.EndsWith(".cs"))
                {
                    listOfReferencedFiles = FileReferenceManager.Self.GetFilesReferencedBy(fileToAddAbsolute, TopLevelOrRecursive.TopLevel);
                }
            }
            catch (FileNotFoundException fnfe)
            {
                string errorMessage = "Could not track dependencies because of a missing file:\n" + fnfe.FileName;

                if(parentFile != null)
                {
                    errorMessage += "\n\nwhich is referenced by:\n" + parentFile;
                }

                didErrorOccur = true;
                ErrorReporter.ReportError(fnfe.FileName, errorMessage, false);

            }
            catch (InvalidOperationException ioe)
            {
                string errorMessage = "Problem trying to track dependencies because of a problem parsing the XML file :\n" + fileToAddAbsolute;
                didErrorOccur = true;
                ErrorReporter.ReportError(fileToAddAbsolute, errorMessage, false);
            }

            // We should tell the user that a file is missing here if it can't be found.

            if (!didErrorOccur && System.IO.File.Exists(fileToAddAbsolute) == false)
            {
                string message =
                    "The file\n\n" + fileToAddAbsolute + "\n\nis missing and is needed in this project";

                if (!string.IsNullOrEmpty(parentFile))
                {
                    message += " by\n\n" + parentFile;
                }
                ErrorReporter.ReportError(fileToAddAbsolute, message, false);
            }

            bool shouldAddChildren = true;


            if (fileRelativeToProject.EndsWith(".x") || useContentPipeline)
            {
                shouldAddChildren = false;
            }


            if (shouldAddChildren && listOfReferencedFiles != null)
            {
                for (int i = 0; i < listOfReferencedFiles.Count; i++)
                {
                    string file = listOfReferencedFiles[i];

                    if (file.Contains(@"../"))
                    {
                        string message = "The file\n\n" + fileToAddAbsolute + "\n\nincludes the file\n\n" + file + "\n\n" +
                            "This file should not contain ../ in the path.  This likely happened if you saved the file " +
                            "in a FRBDK tool and didn't select the \"Copy to relative\" option.\n\nYou should probably shut " +
                            "down Glue, fix this problem, then re-open your project.";

                        System.Windows.Forms.MessageBox.Show(message);
                    }
                    else
                    {
                        wasAdded |= UpdateFileMembershipInProject(project, file, useContentPipeline, shouldLink, fileToAddAbsolute);
                    }
                }
            }

            return wasAdded;
        }

Same methods

ProjectManager::UpdateFileMembershipInProject ( ReferencedFileSave referencedFileSave ) : bool