From 82a34f6d6362f5a6be5b03603133727bcafabf83 Mon Sep 17 00:00:00 2001 From: Sour Date: Sun, 23 Jun 2024 15:31:24 +0900 Subject: [PATCH] UI: Fixed crash in AOT build when creating desktop shortcut in Windows --- UI/ViewModels/SetupWizardViewModel.cs | 36 ++++++--------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/UI/ViewModels/SetupWizardViewModel.cs b/UI/ViewModels/SetupWizardViewModel.cs index 75f685b4..1815d9ad 100644 --- a/UI/ViewModels/SetupWizardViewModel.cs +++ b/UI/ViewModels/SetupWizardViewModel.cs @@ -7,8 +7,6 @@ using System; using System.Diagnostics; using System.IO; -using System.Reflection; -using System.Runtime.InteropServices; namespace Mesen.ViewModels { @@ -104,33 +102,13 @@ private void CreateShortcutFile() } if(OperatingSystem.IsWindows()) { - Type? t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); - if(t == null) { - return; - } - -#pragma warning disable IL2072 // Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The return value of the source method does not have matching annotations. - dynamic? shell = Activator.CreateInstance(t); -#pragma warning restore IL2072 // Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The return value of the source method does not have matching annotations. - if(shell == null) { - return; - } - -#pragma warning disable IL2026 // Using dynamic types might cause types or members to be removed by trimmer. - try { - string linkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Mesen.lnk"); - var lnk = shell.CreateShortcut(linkPath); - try { - lnk.TargetPath = Program.ExePath; - lnk.IconLocation = Program.ExePath + ", 0"; - lnk.Save(); - } finally { - Marshal.FinalReleaseComObject(lnk); - } - } finally { - Marshal.FinalReleaseComObject(shell); - } -#pragma warning restore IL2026 // Using dynamic types might cause types or members to be removed by trimmer. + string linkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Mesen.url"); + FileHelper.WriteAllText(linkPath, + "[InternetShortcut]" + Environment.NewLine + + "URL=file:///" + Program.ExePath + Environment.NewLine + + "IconIndex=0" + Environment.NewLine + + "IconFile=" + Program.ExePath.Replace('\\', '/') + Environment.NewLine + ); } else { string shortcutFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "mesen.desktop"); FileAssociationHelper.CreateLinuxShortcutFile(shortcutFile);