#pragma warning disable VSTHRD100 // Avoid async void methods
        public async void OnProjectLoaded(Microsoft.Build.Evaluation.Project project, ProjectModel projectModel)
#pragma warning restore VSTHRD100 // Avoid async void methods
        {
            try
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                if (projectModel.IsBSIPAProject)
                {
                    BsipaProjectInSolution = true;
                }
                Microsoft.Build.Evaluation.Project userProj = null;
                try
                {
                    userProj = EnvUtils.GetProject(project.FullPath + ".user");
                    if (userProj == null)
                    {
                        return;
                    }
                }
                catch (InvalidOperationException) { return; }
                var installPath      = BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath;
                var projBeatSaberDir = project.GetPropertyValue("BeatSaberDir");
                var userBeatSaberDir = userProj.GetPropertyValue("BeatSaberDir");
                if (BSMTSettingsManager.Instance.CurrentSettings.GenerateUserFileOnExisting &&
                    !string.IsNullOrEmpty(BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath) &&
                    projectModel.IsBSIPAProject)
                {
                    Utilities.EnvUtils.SetReferencePaths(userProj, projectModel, project, null);
                    if (!string.IsNullOrEmpty(userBeatSaberDir) &&
                        userBeatSaberDir != BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath)
                    {
                        var    prop    = userProj.GetProperty("BeatSaberDir");
                        string message = $"Overriding BeatSaberDir in {projectModel.ProjectName} to \n{prop?.EvaluatedValue}\n(Old path: {userBeatSaberDir})";
                        VsShellUtilities.ShowMessageBox(
                            this.package,
                            message,
                            $"{projectModel.ProjectName}: Auto Set BeatSaberDir",
                            OLEMSGICON.OLEMSGICON_INFO,
                            OLEMSGBUTTON.OLEMSGBUTTON_OK,
                            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                    }
                }
            }
            catch (Exception ex)
            {
                _ = Helpers.ShowErrorAsync("OnProjectLoaded", $"Error in OnProjectLoaded: {ex.Message}\n{ex}");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void Execute(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            string     title = "Error setting BeatSaberDir:";
            OLEMSGICON icon  = OLEMSGICON.OLEMSGICON_CRITICAL;
            string     message;

            try
            {
                if (string.IsNullOrEmpty(BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath))
                {
                    icon    = OLEMSGICON.OLEMSGICON_CRITICAL;
                    message = "You don't appear to have a Beat Saber install path chosen in 'Extensions > Beat Saber Modding Tools > Settings'.";
                }
                else if (TryGetSelectedProject(package, out ProjectModel projectModel, out Project project, out EnvDTE.Project dteProject))
                {
                    if (projectModel.IsBSIPAProject)
                    {
                        if (projectModel.SupportedCapabilities.HasFlag(ProjectCapabilities.BeatSaberDir))
                        {
                            try
                            {
                                var userProj = EnvUtils.GetProject(projectModel.ProjectPath + ".user");
                                message = SetBeatSaberDir(userProj, projectModel, project, dteProject);
                                icon    = OLEMSGICON.OLEMSGICON_INFO;
                                title   = "Set BeatSaberDir:";
                            }
                            catch (Exception ex)
                            {
                                message = $"{ex.Message}\n{ex.StackTrace}";
                            }
                        }
                        else
                        {
                            message = $"Project {projectModel.ProjectName} doesn't support the BeatSaberDir property";
                        }
                    }
                    else
                    {
                        message = $"Project {projectModel.ProjectName} does not appear to be a BSIPA project.";
                    }
                }
                else
                {
                    message = "Unable to get project information.";
                }
            }
#pragma warning disable VSTHRD100 // Avoid async void methods
        private async void SolutionEvents_OnAfterOpenProject(object sender, Microsoft.VisualStudio.Shell.Events.OpenProjectEventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
        {
            try
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                ProjectModel projModel = null;
                e.Hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out object projectObj);
                if (projectObj is EnvDTE.Project project)
                {
                    var newProject = EnvUtils.GetProject(project.FullName);
                    if (newProject == null)
                    {
                        return;                     // This event seems to randomly trigger if the project is closed and the csproj file is opened in the editor.
                    }
                    {
                        projModel = CreateProjectModel(newProject);
                        if (Projects.TryAdd(newProject.FullPath, projModel))
                        {
                            OnProjectLoaded(newProject, projModel);
                        }
                    }
                }
                else
                {
                    var newProjects = ProjectCollection.GlobalProjectCollection.LoadedProjects.Where(p =>
                                                                                                     !Projects.ContainsKey(p.FullPath) &&
                                                                                                     !p.FullPath.EndsWith(".user")).ToList();
                    foreach (var item in newProjects)
                    {
                        var projectModel = CreateProjectModel(item);
                        if (Projects.TryAdd(item.FullPath, projectModel))
                        {
                            OnProjectLoaded(item, projectModel);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _ = Helpers.ShowErrorAsync("Open Project", $"Error in AfterOpenProject event: {ex.Message}\n{ex}");
            }
        }
Ejemplo n.º 4
0
        public void OnProjectLoaded(Microsoft.Build.Evaluation.Project project, ProjectModel projectModel)
        {
            if (projectModel.IsBSIPAProject)
            {
                BsipaProjectInSolution = true;
            }
            Microsoft.Build.Evaluation.Project userProj = null;
            try
            {
                userProj = EnvUtils.GetProject(project.FullPath + ".user");
                if (userProj == null)
                {
                    return;
                }
            }
            catch (InvalidOperationException) { return; }
            var installPath      = BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath;
            var projBeatSaberDir = project.GetPropertyValue("BeatSaberDir");
            var userBeatSaberDir = userProj.GetPropertyValue("BeatSaberDir");

            if (BSMTSettingsManager.Instance.CurrentSettings.GenerateUserFileOnExisting &&
                !string.IsNullOrEmpty(BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath) &&
                projectModel.IsBSIPAProject)
            {
                Utilities.EnvUtils.SetReferencePaths(userProj, projectModel, project, null);
                if (!string.IsNullOrEmpty(userBeatSaberDir) &&
                    userBeatSaberDir != BSMTSettingsManager.Instance.CurrentSettings.ChosenInstallPath)
                {
                    var    prop    = userProj.GetProperty("BeatSaberDir");
                    string message = $"Overriding BeatSaberDir in {projectModel.ProjectName} to \n{prop?.EvaluatedValue}\n(Old path: {userBeatSaberDir})";
                    VsShellUtilities.ShowMessageBox(
                        this.package,
                        message,
                        $"{projectModel.ProjectName}: Auto Set BeatSaberDir",
                        OLEMSGICON.OLEMSGICON_INFO,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                }
            }
        }
        public bool TryGetProject(string projectFilePath, out ProjectModel projectModel, out Microsoft.Build.Evaluation.Project project)
        {
            projectModel = null;
            project      = EnvUtils.GetProject(projectFilePath);
            bool retVal = false;

            if (Projects.TryGetValue(projectFilePath, out projectModel))
            {
                retVal = true;
            }
            else if (project != null)
            {
                projectModel = CreateProjectModel(project);
                Projects.TryAdd(projectFilePath, projectModel);
                if (projectModel.IsBSIPAProject)
                {
                    BsipaProjectInSolution = true;
                }
                retVal = true;
            }
            return(retVal);
        }
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private async void Execute(object sender, EventArgs e)
        {
            await Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            string     message = null;
            string     title   = "Error setting Reference Paths:";
            bool       skipped = false;
            OLEMSGICON icon    = OLEMSGICON.OLEMSGICON_CRITICAL;

            if (TryGetSelectedProject(package, out ProjectModel projectModel, out var project, out EnvDTE.Project dteProject))
            {
                try
                {
                    //OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, No = 7 depending on what button is pressed.
                    int forceReference = 6;
                    if (projectModel.SupportedCapabilities.HasFlag(ProjectCapabilities.BeatSaberDir))
                    {
                        forceReference = VsShellUtilities.ShowMessageBox(
                            this.package, "It looks like this project supports 'BeatSaberDir', are you sure you want to use ReferencePath?",
                            "", OLEMSGICON.OLEMSGICON_QUERY, OLEMSGBUTTON.OLEMSGBUTTON_YESNO, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_SECOND);
                    }
                    if (forceReference == 6 || forceReference == 1)
                    {
                        var userProj = EnvUtils.GetProject(projectModel.ProjectPath + ".user");
                        message = SetReferencePaths(userProj, projectModel, project, dteProject);
                        icon    = OLEMSGICON.OLEMSGICON_INFO;
                        title   = "Set Reference Paths:";
                    }
                    else
                    {
                        skipped = true;
                    }
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
        private async void SolutionEvents_OnAfterLoadProject(object sender, Microsoft.VisualStudio.Shell.Events.LoadProjectEventArgs e)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            ProjectModel projModel = null;

            e.RealHierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out object projectObj);

            if (projectObj is EnvDTE.Project project)
            {
                var interfaces = project.GetType().GetInterfaces();
                var newProject = EnvUtils.GetProject(project.FullName);
                if (newProject != null)
                {
                    projModel = CreateProjectModel(newProject);
                    if (Projects.TryAdd(newProject.FullPath, projModel))
                    {
                        OnProjectLoaded(newProject, projModel);
                    }
                }
            }
            else
            {
                var newProjects = ProjectCollection.GlobalProjectCollection.LoadedProjects.Where(p =>
                                                                                                 !Projects.ContainsKey(p.FullPath) &&
                                                                                                 !p.FullPath.EndsWith(".user")).ToList();
                foreach (var item in newProjects)
                {
                    var projectModel = CreateProjectModel(item);
                    if (Projects.TryAdd(item.FullPath, projectModel))
                    {
                        OnProjectLoaded(item, projectModel);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private void SolutionEvents_OnAfterOpenProject(object sender, Microsoft.VisualStudio.Shell.Events.OpenProjectEventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            ProjectModel projModel = null;

            e.Hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out object projectObj);
            if (projectObj is EnvDTE.Project project)
            {
                var newProject = EnvUtils.GetProject(project.FullName);
                if (newProject == null)
                {
                    return;                     // This event seems to randomly trigger if the project is closed and the csproj file is opened in the editor.
                }
                {
                    projModel = CreateProjectModel(newProject);
                    if (Projects.TryAdd(newProject.FullPath, projModel))
                    {
                        OnProjectLoaded(newProject, projModel);
                    }
                }
            }
            else
            {
                var newProjects = ProjectCollection.GlobalProjectCollection.LoadedProjects.Where(p =>
                                                                                                 !Projects.ContainsKey(p.FullPath) &&
                                                                                                 !p.FullPath.EndsWith(".user")).ToList();
                foreach (var item in newProjects)
                {
                    var projectModel = CreateProjectModel(item);
                    if (Projects.TryAdd(item.FullPath, projectModel))
                    {
                        OnProjectLoaded(item, projectModel);
                    }
                }
            }
        }