From 832bb57259c03225f88b7d3053507b8c487b544d Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+CptMoore@users.noreply.github.com> Date: Sun, 4 Jun 2023 15:05:00 +0200 Subject: [PATCH 1/4] Added support for *.url files, closes #205 . --- .../Documents/TreeView/DocumentTreeView.cs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs b/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs index beb7bb4d4d..33e683d7d3 100644 --- a/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs +++ b/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs @@ -743,6 +743,7 @@ void OnDropFiles(int index, string[] filenames) { filenames = GetFiles(filenames); ResolveWindowsShortcutFiles(filenames); + ResolveInternetShortcutFiles(filenames); var origFilenames = filenames; var documents = DocumentService.GetDocuments(); @@ -844,6 +845,38 @@ static void ResolveWindowsShortcutFiles(string[] filenames) { } } + static void ResolveInternetShortcutFiles(string[] filenames) { + for (var i = 0; i < filenames.Length; i++) { + var filename = filenames[i]; + if (!filename.EndsWith(".url", StringComparison.OrdinalIgnoreCase)) + continue; + if (!File.Exists(filename)) + continue; + try { + var urlString = GetUrlFromInternetShortcutFile(filename); + if (urlString == null) + continue; + var urlUri = new Uri(urlString); + if (urlUri is { IsAbsoluteUri: true, IsFile: true }) { + filenames[i] = urlUri.AbsolutePath; + } + } + catch { + // ignore any unexpected parsing errors + } + } + } + + // get the URL from a Microsoft [InternetShortcut] .url file + static string? GetUrlFromInternetShortcutFile(string filename) { + foreach (var line in File.ReadAllLines(filename)) { + if (line.StartsWith("URL=")) { + return line.Substring(4); + } + } + return null; + } + static string[] GetFiles(string[] filenames) { var result = new List(filenames.Length); foreach (var filename in filenames) { From d5d5343754bd592260f740957bb031a1e74b74d5 Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+CptMoore@users.noreply.github.com> Date: Sun, 4 Jun 2023 19:38:24 +0200 Subject: [PATCH 2/4] Added PR feedback. --- .../Documents/TreeView/DocumentTreeView.cs | 28 ++++--------------- .../TreeView/InternetShortcutFileFormat.cs | 16 +++++++++++ 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs diff --git a/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs b/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs index 33e683d7d3..cbe5d7fc9e 100644 --- a/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs +++ b/dnSpy/dnSpy/Documents/TreeView/DocumentTreeView.cs @@ -852,29 +852,13 @@ static void ResolveInternetShortcutFiles(string[] filenames) { continue; if (!File.Exists(filename)) continue; - try { - var urlString = GetUrlFromInternetShortcutFile(filename); - if (urlString == null) - continue; - var urlUri = new Uri(urlString); - if (urlUri is { IsAbsoluteUri: true, IsFile: true }) { - filenames[i] = urlUri.AbsolutePath; - } - } - catch { - // ignore any unexpected parsing errors - } - } - } - - // get the URL from a Microsoft [InternetShortcut] .url file - static string? GetUrlFromInternetShortcutFile(string filename) { - foreach (var line in File.ReadAllLines(filename)) { - if (line.StartsWith("URL=")) { - return line.Substring(4); - } + var urlString = InternetShortcutFileFormat.GetUrlFromInternetShortcutFile(filename); + if (!Uri.TryCreate(urlString, UriKind.Absolute, out var urlUri)) + continue; + if (!urlUri.IsFile) + continue; + filenames[i] = urlUri.AbsolutePath; } - return null; } static string[] GetFiles(string[] filenames) { diff --git a/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs b/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs new file mode 100644 index 0000000000..3aad85fca8 --- /dev/null +++ b/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs @@ -0,0 +1,16 @@ +using System.Runtime.InteropServices; +using System.Text; + +namespace dnSpy.Documents.TreeView; + +// Helper class to interact with Microsoft' InternetShortcut file format used in .url files +static class InternetShortcutFileFormat { + internal static string? GetUrlFromInternetShortcutFile(string filename) { + var urlValue = new StringBuilder(255); + int charsCopied = GetPrivateProfileString("InternetShortcut", "URL", "", urlValue, 255, filename); + return charsCopied < 1 ? null : urlValue.ToString(); + } + + [DllImport("kernel32", CharSet = CharSet.Unicode)] + static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); +} From 0c706ee2a3da9e41b531bb233e89bcf2dcea480f Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+CptMoore@users.noreply.github.com> Date: Sun, 4 Jun 2023 20:30:23 +0200 Subject: [PATCH 3/4] Added PR feedback. --- .editorconfig | 1 + .../TreeView/InternetShortcutFileFormat.cs | 38 ++++++++++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/.editorconfig b/.editorconfig index 63fd71df88..5fb94a1414 100644 --- a/.editorconfig +++ b/.editorconfig @@ -100,6 +100,7 @@ csharp_style_expression_bodied_methods = true:suggestion csharp_style_expression_bodied_operators = true:suggestion csharp_style_expression_bodied_properties = true:suggestion csharp_style_inlined_variable_declaration = true:suggestion +csharp_style_namespace_declarations = block_scoped:warning csharp_style_pattern_local_over_anonymous_function = true:suggestion csharp_style_pattern_matching_over_as_with_null_check = true:suggestion csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion diff --git a/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs b/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs index 3aad85fca8..3e5b0d2946 100644 --- a/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs +++ b/dnSpy/dnSpy/Documents/TreeView/InternetShortcutFileFormat.cs @@ -1,16 +1,34 @@ +/* + Copyright (C) 2023 ElektroKill + + This file is part of dnSpy + + dnSpy is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + dnSpy is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with dnSpy. If not, see . +*/ + using System.Runtime.InteropServices; using System.Text; -namespace dnSpy.Documents.TreeView; +namespace dnSpy.Documents.TreeView { + static class InternetShortcutFileFormat { + internal static string? GetUrlFromInternetShortcutFile(string filename) { + var urlValue = new StringBuilder(255); + int charsCopied = GetPrivateProfileString("InternetShortcut", "URL", "", urlValue, 255, filename); + return charsCopied < 1 ? null : urlValue.ToString(); + } -// Helper class to interact with Microsoft' InternetShortcut file format used in .url files -static class InternetShortcutFileFormat { - internal static string? GetUrlFromInternetShortcutFile(string filename) { - var urlValue = new StringBuilder(255); - int charsCopied = GetPrivateProfileString("InternetShortcut", "URL", "", urlValue, 255, filename); - return charsCopied < 1 ? null : urlValue.ToString(); + [DllImport("kernel32", CharSet = CharSet.Unicode)] + static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); } - - [DllImport("kernel32", CharSet = CharSet.Unicode)] - static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); } From 1fd252d95ea618cd7637bb2db799d61e44537ba8 Mon Sep 17 00:00:00 2001 From: CptMoore <39010654+CptMoore@users.noreply.github.com> Date: Sun, 4 Jun 2023 21:59:18 +0200 Subject: [PATCH 4/4] Added PR feedback. --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 5fb94a1414..b8d3a3d281 100644 --- a/.editorconfig +++ b/.editorconfig @@ -100,7 +100,7 @@ csharp_style_expression_bodied_methods = true:suggestion csharp_style_expression_bodied_operators = true:suggestion csharp_style_expression_bodied_properties = true:suggestion csharp_style_inlined_variable_declaration = true:suggestion -csharp_style_namespace_declarations = block_scoped:warning +csharp_style_namespace_declarations = block_scoped:suggestion csharp_style_pattern_local_over_anonymous_function = true:suggestion csharp_style_pattern_matching_over_as_with_null_check = true:suggestion csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion