Skip to content

Commit e0639a2

Browse files
authored
Force disable parallel font enumeration to reduce startup crashes (#34)
1 parent 9f64f5d commit e0639a2

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using HarmonyLib;
5+
using AffinityPluginLoader.Core;
6+
7+
namespace WineFix.Patches
8+
{
9+
/// <summary>
10+
/// Disables parallel font enumeration on Wine to avoid an intermittent
11+
/// access violation (0xC0000005) in libkernel.dll during startup font loading.
12+
/// </summary>
13+
public static class FontEnumerationPatch
14+
{
15+
public static void ApplyPatches(Harmony harmony)
16+
{
17+
Logger.Info("Applying FontEnumeration patch (Wine fix)...");
18+
19+
var serifAssembly = AppDomain.CurrentDomain.GetAssemblies()
20+
.FirstOrDefault(a => a.GetName().Name == "Serif.Affinity");
21+
22+
if (serifAssembly == null)
23+
{
24+
Logger.Error("Serif.Affinity assembly not found");
25+
return;
26+
}
27+
28+
var appType = serifAssembly.GetType("Serif.Affinity.Application");
29+
if (appType == null)
30+
{
31+
Logger.Error("Serif.Affinity.Application type not found");
32+
return;
33+
}
34+
35+
var prop = appType.GetProperty("ParallelFontEnumerationDisabled",
36+
BindingFlags.Public | BindingFlags.Instance);
37+
38+
if (prop?.GetGetMethod() != null)
39+
{
40+
harmony.Patch(prop.GetGetMethod(),
41+
postfix: new HarmonyMethod(typeof(FontEnumerationPatch), nameof(ForceDisabled)));
42+
Logger.Info("Patched ParallelFontEnumerationDisabled to return true");
43+
}
44+
else
45+
{
46+
Logger.Error("ParallelFontEnumerationDisabled property not found");
47+
}
48+
}
49+
50+
public static void ForceDisabled(ref bool __result)
51+
{
52+
__result = true;
53+
}
54+
}
55+
}

WineFix/WineFixPlugin.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using HarmonyLib;
22
using AffinityPluginLoader;
3+
using AffinityPluginLoader.Settings;
34

45
namespace WineFix
56
{
@@ -8,10 +9,28 @@ namespace WineFix
89
/// </summary>
910
public class WineFixPlugin : AffinityPlugin
1011
{
12+
public const string SettingForceSyncFontEnum = "force_sync_font_enum";
13+
14+
public override PluginSettingsDefinition DefineSettings()
15+
{
16+
return new PluginSettingsDefinition("winefix")
17+
.AddSection("Crash Fixes")
18+
.AddBool(SettingForceSyncFontEnum, "Force synchronous font enumeration",
19+
defaultValue: true,
20+
restartRequired: true,
21+
description: "Disable parallel font enumeration to significantly reduce frequency of startup crashes. May increase application startup time on systems with lots of fonts.");
22+
}
23+
1124
public override void OnPatch(Harmony harmony, IPluginContext context)
1225
{
1326
context.Patch("MainWindowLoaded fix",
1427
h => Patches.MainWindowLoadedPatch.ApplyPatches(h));
28+
29+
if (context.Settings.GetEffectiveValue<bool>(SettingForceSyncFontEnum))
30+
{
31+
context.Patch("FontEnumeration fix",
32+
h => Patches.FontEnumerationPatch.ApplyPatches(h));
33+
}
1534
}
1635
}
1736
}

0 commit comments

Comments
 (0)