private static string GetFileToDownload(NewProjectViewModel viewModel)
        {
            PlatformProjectInfo foundInstance = viewModel.ProjectType;

            if (foundInstance == null)
            {
                throw new NotImplementedException("You must first select a template");
            }
            else
            {
                return(foundInstance.Url);
            }
        }
        public static void GetDefaultZipLocationAndStringToReplace(PlatformProjectInfo project, string templateLocation, out string zipToUnpack, out string stringToReplace)
        {
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }
            zipToUnpack = "";

            stringToReplace = "";

            if (!Directory.Exists(templateLocation))
            {
                Directory.CreateDirectory(templateLocation);
            }


            zipToUnpack     = templateLocation + project.ZipName;
            stringToReplace = project.Namespace;
        }
 /// <summary>
 /// Returns with default directory for templates (AppData)
 /// </summary>
 /// <param name="zipToUnpack"></param>
 /// <param name="stringToReplace"></param>
 public static void GetDefaultZipLocationAndStringToReplace(PlatformProjectInfo project, out string zipToUnpack, out string stringToReplace)
 {
     GetDefaultZipLocationAndStringToReplace(project, FileManager.UserApplicationDataForThisApplication, out zipToUnpack, out stringToReplace);
 }
        public static bool MakeNewProject(NewProjectViewModel viewModel)
        {
            IMessageBox messageBox = Container.Get <IMessageBox>();

            string stringToReplace;
            string zipToUnpack;

            GetDefaultZipLocationAndStringToReplace(viewModel.ProjectType, out zipToUnpack, out stringToReplace);
            string fileToDownload = GetFileToDownload(viewModel);

            bool succeeded = true;

            if (NewProjectCreator.Managers.CommandLineManager.Self.OpenedBy != null && NewProjectCreator.Managers.CommandLineManager.Self.OpenedBy.ToLower() == "glue")
            {
                PlatformProjectInfo ppi = viewModel.ProjectType;

                if (!ppi.SupportedInGlue)
                {
                    succeeded = false;
                    messageBox.Show("This project type is not supported in Glue.  You must launch the New Project Creator manually");
                }
            }

            string unpackDirectory = viewModel.ProjectLocation;

            if (succeeded)
            {
                bool isFileNameValid = GetIfFileNameIsValid(viewModel, messageBox, ref unpackDirectory);

                if (!isFileNameValid)
                {
                    succeeded = false;
                }
            }

            if (succeeded)
            {
                bool hasUserCancelled = false;

                bool shouldTryDownloading;
                zipToUnpack = GetZipToUnpack(viewModel, fileToDownload, ref hasUserCancelled, out shouldTryDownloading);

                if (shouldTryDownloading)
                {
                    // Checks for a newer version and downloads it if necessary
                    bool downloadSucceeded = DownloadFileSync(viewModel, zipToUnpack, fileToDownload);

                    if (!downloadSucceeded)
                    {
                        ShowErrorMessageBox(ref hasUserCancelled, ref zipToUnpack, "Error downloading the file.  What would you like to do?");
                    }
                }


                if (!hasUserCancelled)
                {
                    try
                    {
                        Directory.CreateDirectory(unpackDirectory);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        messageBox.Show("The program does not have permission to create a directory at\n\n" + unpackDirectory + "\n\nPlease run as administrator mode");
                        succeeded = false;
                    }

                    if (succeeded)
                    {
                        if (!File.Exists(zipToUnpack))
                        {
                            messageBox.Show("Could not find the template file:\n" + zipToUnpack);
                        }


                        succeeded = UnzipManager.UnzipFile(zipToUnpack, unpackDirectory);
                    }

                    if (succeeded)
                    {
                        RenameEverything(viewModel, stringToReplace, unpackDirectory);

                        GuidLogic.ReplaceGuids(unpackDirectory);

                        if (viewModel.OpenSlnFolderAfterCreation)
                        {
                            Process.Start(unpackDirectory);
                        }

                        System.Console.Out.WriteLine(unpackDirectory);
                    }
                }
            }

            return(succeeded);
        }
        private static void AddEngineProjectsToProject(PlatformProjectInfo projectType, ref string contents)
        {
            #region Switch based off of the HighlightedTemplate



            string projectGUID = "";
            string projectPath = "";
            bool   XNA4        = false;

            switch (projectType.FriendlyName)
            {
            case "FlatRedBall XNA 3.1 (PC)":
                projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBall.csproj";
                projectGUID = GetGUID(projectPath);
                break;

            case "FlatRedBall XNA 4.0 (PC)":
                projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallXna4.csproj";
                projectGUID = GetGUID(projectPath);
                XNA4        = true;
                break;

            case "FlatRedBall XNA 3.1 (Xbox 360)":
                projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallXbox360.csproj";
                projectGUID = GetGUID(projectPath);
                break;

            case "FlatRedBall XNA 4.0 (Xbox 360)":
                projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallXna4_360.csproj";
                projectGUID = GetGUID(projectPath);
                XNA4        = true;
                break;

            case "FlatSilverBall (Browser)":
                projectPath = @"N:\FlatSilverBall\FlatSilverBall\FlatRedBall.csproj";
                projectGUID = GetGUID(projectPath);
                break;

            case "FlatRedBall MDX (PC)":
                projectPath = @"N:\FlatRedBallMDX\FRB.csproj";
                projectGUID = GetGUID(projectPath);
                break;

            case "FlatRedBall WindowsPhone (Phone)":
                projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallWindowsPhone.csproj";
                projectGUID = GetGUID(projectPath);
                XNA4        = true;
                break;

            case "FlatRedBall Android (Phone)":
                // do nothing for now!
                break;
            }

            /*
             * <ItemGroup>
             *  <ProjectReference Include="N:\FlatRedBallXNA\FlatRedBall\FlatRedBall.csproj">
             *    <Project>{E1CB7D7B-E2EC-4DEB-92E2-6EF0B76F40F0}</Project>
             *    <Name>FlatRedBall</Name>
             *  </ProjectReference>N
             * </ItemGroup>
             */
            int referenceStartIndex = contents.IndexOf("<Reference Include=\"FlatRedBall,");

            // This could have something like this: <Reference Include="FlatRedBall">
            if (referenceStartIndex == -1)
            {
                referenceStartIndex = contents.IndexOf("<Reference Include=\"FlatRedBall");
            }

            int referenceEndIndex;


            int nextBackSlashReference = contents.IndexOf("</Reference>", referenceStartIndex);
            int nextBackSlashClose     = referenceEndIndex = contents.IndexOf("/>", referenceStartIndex);

            if (nextBackSlashReference != -1 && nextBackSlashReference < nextBackSlashClose)
            {
                referenceEndIndex = contents.IndexOf("</Reference>", referenceStartIndex) + "</Reference>".Length;
            }
            else
            {
                referenceEndIndex = contents.IndexOf("/>", referenceStartIndex) + "/>".Length;
            }

            contents = contents.Remove(referenceStartIndex, referenceEndIndex - referenceStartIndex);

            string projectReference = "<ItemGroup>\n<ProjectReference Include=\"" + projectPath + "\">\n" +
                                      "<Project>{" + projectGUID + "</Project>\n<Name>" + FileManager.RemoveExtension(FileManager.RemovePath(projectPath)) + "</Name>\n</ProjectReference>\n</ItemGroup>\n";


            contents = contents.Insert(contents.IndexOf("<Import Project=\"$") - 1, projectReference);

            #endregion
        }
        public static void GetDefaultZipLocationAndStringToReplace(PlatformProjectInfo project, string templateLocation, out string zipToUnpack, out string stringToReplace)
        {
            if(project == null)
            {
                throw new ArgumentNullException("project");
            }
            zipToUnpack = "";

            stringToReplace = "";
                        
            if (!Directory.Exists(templateLocation))
            {
                Directory.CreateDirectory(templateLocation);
            }


            zipToUnpack = templateLocation + project.ZipName;
            stringToReplace = project.Namespace;
        }
 /// <summary>
 /// Returns with default directory for templates (AppData) 
 /// </summary>
 /// <param name="zipToUnpack"></param>
 /// <param name="stringToReplace"></param>
 public static void GetDefaultZipLocationAndStringToReplace(PlatformProjectInfo project, out string zipToUnpack, out string stringToReplace)
 {
       GetDefaultZipLocationAndStringToReplace(project,FileManager.UserApplicationDataForThisApplication, out zipToUnpack, out stringToReplace);
 }
        private static void AddEngineProjectsToProject(PlatformProjectInfo projectType, ref string contents)
        {
            
            #region Switch based off of the HighlightedTemplate

            

            string projectGUID = "";
            string projectPath = "";
            bool XNA4 = false;

            switch (projectType.FriendlyName)
            {
                case "FlatRedBall XNA 3.1 (PC)":
                    projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBall.csproj";
                    projectGUID = GetGUID(projectPath);
                    break;
                case "FlatRedBall XNA 4.0 (PC)":
                    projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallXna4.csproj";
                    projectGUID = GetGUID(projectPath);
                    XNA4 = true;
                    break;
                case "FlatRedBall XNA 3.1 (Xbox 360)":
                    projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallXbox360.csproj";
                    projectGUID = GetGUID(projectPath);
                    break;
                case "FlatRedBall XNA 4.0 (Xbox 360)":
                    projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallXna4_360.csproj";
                    projectGUID = GetGUID(projectPath);
                    XNA4 = true;
                    break;
                case "FlatSilverBall (Browser)":
                    projectPath = @"N:\FlatSilverBall\FlatSilverBall\FlatRedBall.csproj";
                    projectGUID = GetGUID(projectPath);
                    break;
                case "FlatRedBall MDX (PC)":
                    projectPath = @"N:\FlatRedBallMDX\FRB.csproj";
                    projectGUID = GetGUID(projectPath);
                    break;
                case "FlatRedBall WindowsPhone (Phone)":
                    projectPath = @"N:\FlatRedBallXNA\FlatRedBall\FlatRedBallWindowsPhone.csproj";
                    projectGUID = GetGUID(projectPath);
                    XNA4 = true;
                    break;

                case "FlatRedBall Android (Phone)":
                    // do nothing for now!
                    break;
            }
            /*
            <ItemGroup>
                <ProjectReference Include="N:\FlatRedBallXNA\FlatRedBall\FlatRedBall.csproj">
                  <Project>{E1CB7D7B-E2EC-4DEB-92E2-6EF0B76F40F0}</Project>
                  <Name>FlatRedBall</Name>
                </ProjectReference>N
            </ItemGroup>
             */
            int referenceStartIndex = contents.IndexOf("<Reference Include=\"FlatRedBall,");

            // This could have something like this: <Reference Include="FlatRedBall">
            if (referenceStartIndex == -1)
            {
                referenceStartIndex = contents.IndexOf("<Reference Include=\"FlatRedBall");
            }

            int referenceEndIndex;


            int nextBackSlashReference = contents.IndexOf("</Reference>", referenceStartIndex);
            int nextBackSlashClose = referenceEndIndex = contents.IndexOf("/>", referenceStartIndex);

            if (nextBackSlashReference != -1 && nextBackSlashReference < nextBackSlashClose)
            {
                referenceEndIndex = contents.IndexOf("</Reference>", referenceStartIndex) + "</Reference>".Length;
            }
            else
            {
                referenceEndIndex = contents.IndexOf("/>", referenceStartIndex) + "/>".Length;
            }
            
            contents = contents.Remove(referenceStartIndex, referenceEndIndex - referenceStartIndex);

            string projectReference = "<ItemGroup>\n<ProjectReference Include=\""+projectPath+"\">\n"+
                "<Project>{" + projectGUID + "</Project>\n<Name>"+FileManager.RemoveExtension(FileManager.RemovePath(projectPath))+"</Name>\n</ProjectReference>\n</ItemGroup>\n"; 


            contents = contents.Insert(contents.IndexOf("<Import Project=\"$") - 1, projectReference);

            #endregion


        }
        public static void GetDefaultZipLocationAndStringToReplace(PlatformProjectInfo project, string templateLocation, out string zipToUnpack, out string stringToReplace)
        {
            zipToUnpack = "";

            stringToReplace = "";
                        
            if (!Directory.Exists(templateLocation))
            {
                Directory.CreateDirectory(templateLocation);
            }


            zipToUnpack = templateLocation + project.ZipName;
            stringToReplace = project.Namespace;
        }