Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Attempt to fix a crash sometimes happening after deploying a script a…
Browse files Browse the repository at this point in the history
…nd pressing "copy link".
  • Loading branch information
malware-dev committed May 3, 2023
1 parent d6e0d27 commit cefcda8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
52 changes: 27 additions & 25 deletions Source/MDK/Views/DeploymentBar/DeploymentBar.xaml.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Interaction logic for DeploymentBar.xaml
/// Interaction logic for DeploymentBar.xaml
/// </summary>
public partial class DeploymentBar : NotificationBar
public partial class DeploymentBar: NotificationBar
{
MDKProjectProperties[] _deployedScripts;

/// <summary>
/// Creates a new instance of <see cref="DeploymentBar"/>
/// Creates a new instance of <see cref="DeploymentBar" />
/// </summary>
public DeploymentBar() { InitializeComponent(); }

/// <summary>
/// A list of deployed scripts
/// A list of deployed scripts
/// </summary>
public MDKProjectProperties[] DeployedScripts
{
Expand All @@ -37,7 +37,7 @@ public MDKProjectProperties[] DeployedScripts
}

/// <summary>
/// Whether the Show Me hyperlink should be available
/// Whether the Show Me hyperlink should be available
/// </summary>
public bool CanShowMe { get => showMeLink.IsVisible; set => showMeLink.IsVisible = value; }

Expand All @@ -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);
}
}
}
}
13 changes: 8 additions & 5 deletions Source/MDK/VisualStudio/NotificationBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ public async Task<string> 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)
Expand All @@ -113,13 +115,14 @@ public async Task<string> ShowAsync([NotNull] IServiceProvider serviceProvider)
}
}
var response = await _tcs.Task;
return response;
}
finally
{
_tcs = null;
_element = null;
ServiceProvider = null;
return response;
}

return null;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/MDK/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="MDK.Morten Aune Lyrstad.e02b602e-3099-44a5-88c6-cb30cab978f6" Version="1.5.13" Language="en-US" Publisher="Morten Aune Lyrstad" />
<Identity Id="MDK.Morten Aune Lyrstad.e02b602e-3099-44a5-88c6-cb30cab978f6" Version="1.5.14" Language="en-US" Publisher="Morten Aune Lyrstad" />
<DisplayName>MDK/SE</DisplayName>
<Description xml:space="preserve">A toolkit to help with ingame script (programmable block) development for Keen Software House's space sandbox Space Engineers.

Expand Down

0 comments on commit cefcda8

Please sign in to comment.