[Peek] Fix Monaco assets folder discovery (#35925)

This commit is contained in:
Dave Rayment 2024-11-22 16:24:15 +00:00 committed by GitHub
parent b81478eb97
commit 863f7aa233
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,34 +29,28 @@ namespace Microsoft.PowerToys.FilePreviewCommon
new XmlFormatter(), new XmlFormatter(),
}.AsReadOnly(); }.AsReadOnly();
private static string? _monacoDirectory; private static readonly Lazy<string> _monacoDirectory = new(GetRuntimeMonacoDirectory);
public static string GetRuntimeMonacoDirectory() /// <summary>
/// Gets the path of the Monaco assets folder.
/// </summary>
public static string MonacoDirectory => _monacoDirectory.Value;
private static string GetRuntimeMonacoDirectory()
{ {
string codeBase = Assembly.GetExecutingAssembly().Location; string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
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"));
}
}
public static string MonacoDirectory // If the executable is within "WinUI3Apps", correct the path first.
{ if (Path.GetFileName(exePath) == "WinUI3Apps")
get
{ {
if (string.IsNullOrEmpty(_monacoDirectory)) exePath = Path.Combine(exePath, "..");
{
_monacoDirectory = GetRuntimeMonacoDirectory();
}
return _monacoDirectory;
} }
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() public static JsonDocument GetLanguages()