From 863f7aa233628b51394c9c7ed00badd0c63f0c85 Mon Sep 17 00:00:00 2001 From: Dave Rayment Date: Fri, 22 Nov 2024 16:24:15 +0000 Subject: [PATCH] [Peek] Fix Monaco assets folder discovery (#35925) --- src/common/FilePreviewCommon/MonacoHelper.cs | 40 +++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/common/FilePreviewCommon/MonacoHelper.cs b/src/common/FilePreviewCommon/MonacoHelper.cs index 20ea747e2e..e8442c0bff 100644 --- a/src/common/FilePreviewCommon/MonacoHelper.cs +++ b/src/common/FilePreviewCommon/MonacoHelper.cs @@ -29,34 +29,28 @@ namespace Microsoft.PowerToys.FilePreviewCommon new XmlFormatter(), }.AsReadOnly(); - private static string? _monacoDirectory; + private static readonly Lazy _monacoDirectory = new(GetRuntimeMonacoDirectory); - public static string GetRuntimeMonacoDirectory() + /// + /// Gets the path of the Monaco assets folder. + /// + public static string MonacoDirectory => _monacoDirectory.Value; + + private static string GetRuntimeMonacoDirectory() { - string codeBase = Assembly.GetExecutingAssembly().Location; - string path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(codeBase) ?? string.Empty, "Assets", "Monaco")); - if (Path.Exists(path)) - { - return path; - } - else - { - // We're likely in WinUI3Apps directory and need to go back to the base directory. - return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(codeBase) ?? string.Empty, "..", "Assets", "Monaco")); - } - } + string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty; - public static string MonacoDirectory - { - get + // If the executable is within "WinUI3Apps", correct the path first. + if (Path.GetFileName(exePath) == "WinUI3Apps") { - if (string.IsNullOrEmpty(_monacoDirectory)) - { - _monacoDirectory = GetRuntimeMonacoDirectory(); - } - - return _monacoDirectory; + exePath = Path.Combine(exePath, ".."); } + + string monacoPath = Path.Combine(exePath, "Assets", "Monaco"); + + return Directory.Exists(monacoPath) ? + monacoPath : + throw new DirectoryNotFoundException($"Monaco assets directory not found at {monacoPath}"); } public static JsonDocument GetLanguages()