DisplayMagician/HeliosPlus.ShellExtension/HeliosSteamUrlMenuExtension.cs

118 lines
3.9 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HeliosPlus.Shared;
using HeliosPlus.ShellExtension.Resources;
2017-02-26 19:23:31 +00:00
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
namespace HeliosPlus.ShellExtension
2017-02-26 19:23:31 +00:00
{
[ComVisible(true)]
[COMServerAssociation(AssociationType.ClassOfExtension, @".url")]
[Guid("E41ECFB2-3E7D-4A47-8A51-8627F1B21AE5")]
2017-02-26 19:23:31 +00:00
internal class HeliosSteamUrlMenuExtension : SharpContextMenu
{
protected override bool CanShowMenu()
{
2017-08-10 14:21:45 +00:00
return Helios.IsInstalled &&
2018-10-20 00:27:25 +00:00
SelectedItemPaths.Count() == 1 &&
ProfileRepository.AllProfiles.Any() &&
2018-10-20 00:27:25 +00:00
ParseSteamAppId() > 0;
2017-02-26 19:23:31 +00:00
}
protected override ContextMenuStrip CreateMenu()
{
var explorerMenu = new ContextMenuStrip();
var extensionMenu = new ToolStripMenuItem(Language.Open_under_Display_Profile,
Properties.Resources.HeliosPlus.ToBitmap());
2018-10-20 00:27:25 +00:00
if (ProfileRepository.AllProfiles.Any())
{
foreach (var profile in ProfileRepository.AllProfiles)
2018-10-20 00:27:25 +00:00
{
extensionMenu.DropDownItems.Add(CreateProfileMenu(profile));
2018-10-20 00:27:25 +00:00
}
extensionMenu.DropDownItems.Add(new ToolStripSeparator());
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
extensionMenu.DropDownItems.Add(new ToolStripMenuItem(Language.Manage_Profiles,
Properties.Resources.HeliosPlus.ToBitmap(),
2018-10-20 00:27:25 +00:00
(sender, args) =>
{
HeliosPlus.Open();
2018-10-20 00:27:25 +00:00
}));
2017-02-26 19:23:31 +00:00
explorerMenu.Items.Add(extensionMenu);
explorerMenu.Items.Add(new ToolStripSeparator());
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return explorerMenu;
}
private ToolStripMenuItem CreateProfileMenu(ProfileItem profile)
2017-02-26 19:23:31 +00:00
{
var appId = ParseSteamAppId();
var profileMenu = new ToolStripMenuItem(profile.Name, new ProfileIcon(profile).ToBitmap(16, 16));
profileMenu.DropDownItems.Add(new ToolStripMenuItem(Language.Run, null,
2017-02-26 19:23:31 +00:00
(sender, args) =>
HeliosPlus.OpenSteamGame(HeliosStartupAction.ChangeProfile, profile,
2017-02-26 19:23:31 +00:00
appId)));
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return profileMenu;
}
private uint ParseSteamAppId()
{
try
{
var fileAddress = SelectedItemPaths.FirstOrDefault();
2018-10-20 00:27:25 +00:00
if (!string.IsNullOrWhiteSpace(fileAddress) &&
File.Exists(fileAddress) &&
new FileInfo(fileAddress).Length < 1024)
2017-02-26 19:23:31 +00:00
{
var fileContent = File.ReadAllText(fileAddress);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (!fileContent.Contains(@"[InternetShortcut]"))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return 0;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var steamUrlPattern = @"steam://rungameid/";
var urlIndex = fileContent.IndexOf(steamUrlPattern, StringComparison.InvariantCultureIgnoreCase);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (urlIndex < 0)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return 0;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var nextLine = fileContent.IndexOf(@"\r", urlIndex + steamUrlPattern.Length,
StringComparison.InvariantCultureIgnoreCase);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (nextLine < 0)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
nextLine = fileContent.Length - 1;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var appIdString = fileContent.Substring(urlIndex + steamUrlPattern.Length,
nextLine - urlIndex - steamUrlPattern.Length);
uint appId;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (uint.TryParse(appIdString, out appId))
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
return appId;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
}
catch
{
// ignored
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return 0;
}
}
}