From cefcda8934228f3b00ba6c82577ab4eed1ba2952 Mon Sep 17 00:00:00 2001
From: Morten Aune Lyrstad <4653628+malware-dev@users.noreply.github.com>
Date: Wed, 3 May 2023 20:19:44 +0200
Subject: [PATCH] Attempt to fix a crash sometimes happening after deploying a
script and pressing "copy link".
---
.../Views/DeploymentBar/DeploymentBar.xaml.cs | 52 ++++++++++---------
Source/MDK/VisualStudio/NotificationBar.cs | 13 +++--
Source/MDK/source.extension.vsixmanifest | 2 +-
3 files changed, 36 insertions(+), 31 deletions(-)
diff --git a/Source/MDK/Views/DeploymentBar/DeploymentBar.xaml.cs b/Source/MDK/Views/DeploymentBar/DeploymentBar.xaml.cs
index cd7171e..5d641e4 100644
--- a/Source/MDK/Views/DeploymentBar/DeploymentBar.xaml.cs
+++ b/Source/MDK/Views/DeploymentBar/DeploymentBar.xaml.cs
@@ -1,30 +1,30 @@
-using System;
-using System.IO;
-using System.Linq;
-using System.Text;
-using System.Windows;
-using Malware.MDKServices;
+using Malware.MDKServices;
using MDK.Resources;
using MDK.Views.BlueprintManager;
using MDK.VisualStudio;
using Microsoft.VisualStudio.Shell;
+using System;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Windows;
namespace MDK.Views.DeploymentBar
{
///
- /// Interaction logic for DeploymentBar.xaml
+ /// Interaction logic for DeploymentBar.xaml
///
- public partial class DeploymentBar : NotificationBar
+ public partial class DeploymentBar: NotificationBar
{
MDKProjectProperties[] _deployedScripts;
///
- /// Creates a new instance of
+ /// Creates a new instance of
///
public DeploymentBar() { InitializeComponent(); }
///
- /// A list of deployed scripts
+ /// A list of deployed scripts
///
public MDKProjectProperties[] DeployedScripts
{
@@ -37,7 +37,7 @@ public MDKProjectProperties[] DeployedScripts
}
///
- /// Whether the Show Me hyperlink should be available
+ /// Whether the Show Me hyperlink should be available
///
public bool CanShowMe { get => showMeLink.IsVisible; set => showMeLink.IsVisible = value; }
@@ -48,27 +48,29 @@ void ShowMeLink_OnClicked(object sender, EventArgs e)
ThreadHelper.ThrowIfNotOnUIThread();
Close();
var distinctPaths = DeployedScripts.Select(script => FormattedPath(script.Paths.OutputPath)).Distinct().ToArray();
- if (distinctPaths.Length == 1)
- {
- var model = new BlueprintManagerDialogModel(Text.MDKPackage_Deploy_Description,
- distinctPaths[0], DeployedScripts.Select(s => s.Name));
- BlueprintManagerDialog.ShowDialog(model);
- }
+ if (distinctPaths.Length != 1)
+ return;
+
+ var model = new BlueprintManagerDialogModel(Text.MDKPackage_Deploy_Description, distinctPaths[0], DeployedScripts.Select(s => s.Name));
+ BlueprintManagerDialog.ShowDialog(model);
}
async void CopyLink_OnClicked(object sender, EventArgs e)
{
+ var serviceProvider = ServiceProvider;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
Close();
var item = _deployedScripts.FirstOrDefault();
+ if (item == null)
+ return;
var path = Path.Combine(FormattedPath(item.Paths.OutputPath), item.Name, "script.cs");
- if (File.Exists(path))
- {
- var script = File.ReadAllText(path, Encoding.UTF8);
- System.Windows.Clipboard.SetText(script, TextDataFormat.UnicodeText);
- var bar = new CopiedToClipboardBar();
- await bar.ShowAsync(ServiceProvider);
- }
+ if (!File.Exists(path))
+ return;
+
+ var script = File.ReadAllText(path, Encoding.UTF8);
+ Clipboard.SetText(script, TextDataFormat.UnicodeText);
+ var bar = new CopiedToClipboardBar();
+ await bar.ShowAsync(serviceProvider);
}
}
-}
+}
\ No newline at end of file
diff --git a/Source/MDK/VisualStudio/NotificationBar.cs b/Source/MDK/VisualStudio/NotificationBar.cs
index 2296790..8a686b0 100644
--- a/Source/MDK/VisualStudio/NotificationBar.cs
+++ b/Source/MDK/VisualStudio/NotificationBar.cs
@@ -89,10 +89,12 @@ public async Task ShowAsync([NotNull] IServiceProvider serviceProvider)
{
if (IsShown)
throw new InvalidOperationException("Bar is already shown");
- await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
- if (serviceProvider.GetService(typeof(SVsShell)) is IVsShell shell)
+ try
{
+ await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
+ if (serviceProvider.GetService(typeof(SVsShell)) is not IVsShell shell) return null;
+
shell.GetProperty((int)__VSSPROPID7.VSSPROPID_MainWindowInfoBarHost, out var obj);
var host = (IVsInfoBarHost)obj;
if (host == null)
@@ -113,13 +115,14 @@ public async Task ShowAsync([NotNull] IServiceProvider serviceProvider)
}
}
var response = await _tcs.Task;
+ return response;
+ }
+ finally
+ {
_tcs = null;
_element = null;
ServiceProvider = null;
- return response;
}
-
- return null;
}
///
diff --git a/Source/MDK/source.extension.vsixmanifest b/Source/MDK/source.extension.vsixmanifest
index 8a4234c..53ce665 100644
--- a/Source/MDK/source.extension.vsixmanifest
+++ b/Source/MDK/source.extension.vsixmanifest
@@ -1,7 +1,7 @@
-
+
MDK/SE
A toolkit to help with ingame script (programmable block) development for Keen Software House's space sandbox Space Engineers.