Files
godot-csharp-visualstudio/GodotAddinVS/Commands/CommandResetGodot.cs
Pando 94d71d8add Fix build errors due to typos (#39)
Replaced all occurrences of "GoDot" with "Godot"
2022-09-13 23:41:35 +02:00

102 lines
4.0 KiB
C#

using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell.Settings;
using Task = System.Threading.Tasks.Task;
namespace GodotAddinVS.Commands {
/// <summary>
/// Command handler
/// </summary>
internal sealed class CommandResetGodot {
/// <summary>
/// Command ID.
/// </summary>
public const int CommandId = 256;
/// <summary>
/// Command menu group (command set GUID).
/// </summary>
public static readonly Guid CommandSet = new Guid("38009f93-330e-4875-ab88-e127fd85bb88");
/// <summary>
/// VS Package that provides this command, not null.
/// </summary>
private readonly AsyncPackage package;
/// <summary>
/// Initializes a new instance of the <see cref="CommandResetGodot"/> class.
/// Adds our command handlers for menu (commands must exist in the command table file)
/// </summary>
/// <param name="package">Owner package, not null.</param>
/// <param name="commandService">Command service to add command to, not null.</param>
private CommandResetGodot(AsyncPackage package, OleMenuCommandService commandService) {
this.package = package ?? throw new ArgumentNullException(nameof(package));
commandService = commandService ?? throw new ArgumentNullException(nameof(commandService));
var menuCommandID = new CommandID(CommandSet, CommandId);
var menuItem = new MenuCommand(this.Execute, menuCommandID);
commandService.AddCommand(menuItem);
}
/// <summary>
/// Gets the instance of the command.
/// </summary>
public static CommandResetGodot Instance {
get;
private set;
}
/// <summary>
/// Gets the service provider from the owner package.
/// </summary>
private Microsoft.VisualStudio.Shell.IAsyncServiceProvider ServiceProvider {
get {
return this.package;
}
}
/// <summary>
/// Initializes the singleton instance of the command.
/// </summary>
/// <param name="package">Owner package, not null.</param>
public static async Task InitializeAsync(AsyncPackage package) {
// Switch to the main thread - the call to AddCommand in CommandResetGodot's constructor requires
// the UI thread.
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
Instance = new CommandResetGodot(package, commandService);
}
/// <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();
var settingsManager = new ShellSettingsManager(package);
var config = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);
var ofd = new OpenFileDialog {
Filter = @"Godot executable (.exe)|*.exe"
};
var result = ofd.ShowDialog(null);
if (result != DialogResult.OK) return;
config.SetString("External Tools", "GodotExecutable", ofd.FileName);
config.SetString("External Tools", "GodotPath", Path.GetDirectoryName(ofd.FileName));
}
}
}