fix: wait for font rebuild when initially setting up ImGui

This is to work around changed behaviour with ImGui >1.78 wherein a race condition during font rebuild together with plugin loads would cause the remote CLR thread to end
This commit is contained in:
goat 2021-01-22 19:25:22 +01:00
parent 2c66ecc84e
commit d949734fe9
2 changed files with 83 additions and 67 deletions

View file

@ -178,6 +178,7 @@ namespace Dalamud {
} }
public void Start() { public void Start() {
try {
Configuration = DalamudConfiguration.Load(StartInfo.ConfigurationPath); Configuration = DalamudConfiguration.Load(StartInfo.ConfigurationPath);
// Initialize the process information. // Initialize the process information.
@ -216,6 +217,8 @@ namespace Dalamud {
InterfaceManager.Enable(); InterfaceManager.Enable();
isInterfaceLoaded = true; isInterfaceLoaded = true;
InterfaceManager.WaitForFontRebuild();
} catch (Exception e) { } catch (Exception e) {
Log.Information(e, "Could not init interface."); Log.Information(e, "Could not init interface.");
} }
@ -239,18 +242,14 @@ namespace Dalamud {
ChatHandlers = new ChatHandlers(this); ChatHandlers = new ChatHandlers(this);
if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_PLUGINS") ?? "false")) if (!bool.Parse(Environment.GetEnvironmentVariable("DALAMUD_NOT_HAVE_PLUGINS") ?? "false")) {
{ try {
try
{
PluginRepository.CleanupPlugins(); PluginRepository.CleanupPlugins();
PluginManager = PluginManager =
new PluginManager(this, StartInfo.PluginDirectory, StartInfo.DefaultPluginDirectory); new PluginManager(this, StartInfo.PluginDirectory, StartInfo.DefaultPluginDirectory);
PluginManager.LoadPlugins(); PluginManager.LoadPlugins();
} } catch (Exception ex) {
catch (Exception ex)
{
Log.Error(ex, "Plugin load failed."); Log.Error(ex, "Plugin load failed.");
} }
} }
@ -263,6 +262,10 @@ namespace Dalamud {
Troubleshooting.LogTroubleshooting(this, isInterfaceLoaded); Troubleshooting.LogTroubleshooting(this, isInterfaceLoaded);
Log.Information("Dalamud is ready."); Log.Information("Dalamud is ready.");
} catch (Exception ex) {
Log.Error(ex, "Oh no! Dalamud::Start() failed.");
Unload();
}
} }
public void Unload() { public void Unload() {

View file

@ -4,6 +4,7 @@ using System.IO;
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading;
using Dalamud.Game; using Dalamud.Game;
using Dalamud.Game.Internal.DXGI; using Dalamud.Game.Internal.DXGI;
using Dalamud.Hooking; using Dalamud.Hooking;
@ -42,6 +43,8 @@ namespace Dalamud.Interface
[UnmanagedFunctionPointer(CallingConvention.ThisCall)] [UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr SetCursorDelegate(IntPtr hCursor); private delegate IntPtr SetCursorDelegate(IntPtr hCursor);
private ManualResetEvent fontBuildSignal;
private ISwapChainAddressResolver Address { get; } private ISwapChainAddressResolver Address { get; }
private Dalamud dalamud; private Dalamud dalamud;
@ -68,6 +71,8 @@ namespace Dalamud.Interface
{ {
this.dalamud = dalamud; this.dalamud = dalamud;
this.fontBuildSignal = new ManualResetEvent(false);
try { try {
var sigResolver = new SwapChainSigResolver(); var sigResolver = new SwapChainSigResolver();
sigResolver.Setup(scanner); sigResolver.Setup(scanner);
@ -272,6 +277,8 @@ namespace Dalamud.Interface
private unsafe void SetupFonts() private unsafe void SetupFonts()
{ {
this.fontBuildSignal.Reset();
ImGui.GetIO().Fonts.Clear(); ImGui.GetIO().Fonts.Clear();
ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig(); ImFontConfigPtr fontConfig = ImGuiNative.ImFontConfig_ImFontConfig();
@ -317,12 +324,18 @@ namespace Dalamud.Interface
Log.Verbose("[FONT] Fonts built!"); Log.Verbose("[FONT] Fonts built!");
this.fontBuildSignal.Set();
fontConfig.Destroy(); fontConfig.Destroy();
japaneseRangeHandle.Free(); japaneseRangeHandle.Free();
gameRangeHandle.Free(); gameRangeHandle.Free();
iconRangeHandle.Free(); iconRangeHandle.Free();
} }
public void WaitForFontRebuild() {
this.fontBuildSignal.WaitOne();
}
// This is intended to only be called as a handler attached to scene.OnNewRenderFrame // This is intended to only be called as a handler attached to scene.OnNewRenderFrame
private void RebuildFontsInternal() private void RebuildFontsInternal()
{ {