Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed crash that could occur when installing a lot of font files #14024

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Files.App/Actions/Content/Install/InstallFontAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public InstallFontAction()

public Task ExecuteAsync()
{
return Task.WhenAll(context.SelectedItems.Select(x => Win32API.InstallFont(x.ItemPath, false)));
var paths = context.SelectedItems.Select(item => item.ItemPath).ToArray();
return Win32API.InstallFontsAsync(paths, false);
}

public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
30 changes: 30 additions & 0 deletions src/Files.App/Utils/Shell/Win32API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,36 @@ public static Task InstallFont(string fontFilePath, bool forAllUsers)
return RunPowershellCommandAsync($"-command \"Copy-Item '{fontFilePath}' '{fontDirectory}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(fontFilePath)}' -Path '{registryKey}' -PropertyType string -Value '{destinationPath}'\"", forAllUsers);
}

public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUsers)
{
string fontDirectory = forAllUsers
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts")
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");

string registryKey = forAllUsers
? "HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
: "HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";

var psCommand = new StringBuilder("-command \"");

foreach (string fontFilePath in fontFilePaths)
{
var destinationPath = Path.Combine(fontDirectory, Path.GetFileName(fontFilePath));
var appendCommand = $"Copy-Item '{fontFilePath}' '{fontDirectory}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(fontFilePath)}' -Path '{registryKey}' -PropertyType string -Value '{destinationPath}';";

if (psCommand.Length + appendCommand.Length > 32766)
{
// The command is too long to run at once, so run the command once up to this point.
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), forAllUsers);
psCommand.Clear().Append("-command \"");
}

psCommand.Append(appendCommand);
}

await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), forAllUsers);
}

private static Process CreatePowershellProcess(string command, bool runAsAdmin)
{
Process process = new();
Expand Down