Skip to content

Commit

Permalink
Merge pull request #1952 from Soreepeong/fix/plugin-ctor-isolation
Browse files Browse the repository at this point in the history
Miscellaneous fixes
  • Loading branch information
goaaats committed Jul 21, 2024
2 parents 8ca4738 + 1be5cd4 commit 910b12d
Show file tree
Hide file tree
Showing 27 changed files with 449 additions and 197 deletions.
18 changes: 18 additions & 0 deletions Dalamud.Boot/Dalamud.Boot.rc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)

/////////////////////////////////////////////////////////////////////////////
//
// RT_MANIFEST
//

RT_MANIFEST_THEMES RT_MANIFEST "themes.manifest"

#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// English (United Kingdom) resources

Expand Down
5 changes: 4 additions & 1 deletion Dalamud.Boot/Dalamud.Boot.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@
<ItemGroup>
<None Include="module.def" />
</ItemGroup>
<ItemGroup>
<Manifest Include="themes.manifest" />
</ItemGroup>
<Target Name="RemoveExtraFiles" AfterTargets="PostBuildEvent">
<Delete Files="$(OutDir)$(TargetName).lib" />
<Delete Files="$(OutDir)$(TargetName).exp" />
</Target>
</Project>
</Project>
3 changes: 3 additions & 0 deletions Dalamud.Boot/Dalamud.Boot.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,7 @@
<Filter>Dalamud.Boot DLL</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Manifest Include="themes.manifest" />
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions Dalamud.Boot/themes.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<description>Windows Forms Common Control manifest</description>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" />
</dependentAssembly>
</dependency>
</assembly>
7 changes: 6 additions & 1 deletion Dalamud/Dalamud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ public Dalamud(DalamudStartInfo info, ReliableFileStorage fs, DalamudConfigurati
true, new FileInfo(Path.Combine(cacheDir.FullName, $"{this.StartInfo.GameVersion}.json")));
}

ServiceManager.InitializeProvidedServices(this, fs, configuration, scanner);
ServiceManager.InitializeProvidedServices(
this,
fs,
configuration,
scanner,
Localization.FromAssets(info.AssetDirectory!, configuration.LanguageOverride));

// Set up FFXIVClientStructs
this.SetupClientStructsResolver(cacheDir);
Expand Down
1 change: 0 additions & 1 deletion Dalamud/Dalamud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
</PropertyGroup>

<PropertyGroup Label="Build">
<UseWindowsForms>true</UseWindowsForms>
<EnableDynamicLoading>true</EnableDynamicLoading>
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
Expand Down
5 changes: 5 additions & 0 deletions Dalamud/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Dalamud.Common;
using Dalamud.Configuration.Internal;
using Dalamud.Interface.Internal.Windows;
using Dalamud.Logging.Internal;
using Dalamud.Logging.Retention;
using Dalamud.Plugin.Internal;
Expand Down Expand Up @@ -232,6 +233,10 @@ private static void RunThread(DalamudStartInfo info, IntPtr mainThreadContinueEv

private static void SerilogOnLogLine(object? sender, (string Line, LogEvent LogEvent) ev)
{
if (!LoadingDialog.IsGloballyHidden)
LoadingDialog.NewLogEntries.Enqueue(ev);
ConsoleWindow.NewLogEntries.Enqueue(ev);

if (ev.LogEvent.Exception == null)
return;

Expand Down
15 changes: 11 additions & 4 deletions Dalamud/Game/Config/GameConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ namespace Dalamud.Game.Config;
[ServiceManager.EarlyLoadedService]
internal sealed class GameConfig : IInternalDisposableService, IGameConfig
{
private readonly TaskCompletionSource tcsInitialization = new();
private readonly TaskCompletionSource<GameConfigSection> tcsSystem = new();
private readonly TaskCompletionSource<GameConfigSection> tcsUiConfig = new();
private readonly TaskCompletionSource<GameConfigSection> tcsUiControl = new();
private readonly TaskCompletionSource tcsInitialization =
new(TaskCreationOptions.RunContinuationsAsynchronously);

private readonly TaskCompletionSource<GameConfigSection> tcsSystem =
new(TaskCreationOptions.RunContinuationsAsynchronously);

private readonly TaskCompletionSource<GameConfigSection> tcsUiConfig =
new(TaskCreationOptions.RunContinuationsAsynchronously);

private readonly TaskCompletionSource<GameConfigSection> tcsUiControl =
new(TaskCreationOptions.RunContinuationsAsynchronously);

private readonly GameConfigAddressResolver address = new();
private Hook<ConfigChangeDelegate>? configChangeHook;
Expand Down
2 changes: 1 addition & 1 deletion Dalamud/Game/Framework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public Task DelayTicks(long numTicks, CancellationToken cancellationToken = defa
if (numTicks <= 0)
return Task.CompletedTask;

var tcs = new TaskCompletionSource();
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
this.tickDelayedTaskCompletionSources[tcs] = (this.tickCounter + (ulong)numTicks, cancellationToken);
return tcs.Task;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static readonly (string Name, float Value)[] FontSizeList =

private readonly int counter;
private readonly byte[] fontPreviewText = new byte[2048];
private readonly TaskCompletionSource<SingleFontSpec> tcs = new();
private readonly TaskCompletionSource<SingleFontSpec> tcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly IFontAtlas atlas;

private string popupImGuiName;
Expand Down
15 changes: 8 additions & 7 deletions Dalamud/Interface/Internal/InterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void EnqueueDeferredDispose(IDisposable locked)
/// <returns>A <see cref="Task"/> that resolves once <paramref name="action"/> is run.</returns>
public Task RunBeforeImGuiRender(Action action)
{
var tcs = new TaskCompletionSource();
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
this.runBeforeImGuiRender.Enqueue(
() =>
{
Expand All @@ -359,7 +359,7 @@ public Task RunBeforeImGuiRender(Action action)
/// <returns>A <see cref="Task"/> that resolves once <paramref name="func"/> is run.</returns>
public Task<T> RunBeforeImGuiRender<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
this.runBeforeImGuiRender.Enqueue(
() =>
{
Expand All @@ -380,7 +380,7 @@ public Task<T> RunBeforeImGuiRender<T>(Func<T> func)
/// <returns>A <see cref="Task"/> that resolves once <paramref name="action"/> is run.</returns>
public Task RunAfterImGuiRender(Action action)
{
var tcs = new TaskCompletionSource();
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
this.runAfterImGuiRender.Enqueue(
() =>
{
Expand All @@ -403,7 +403,7 @@ public Task RunAfterImGuiRender(Action action)
/// <returns>A <see cref="Task"/> that resolves once <paramref name="func"/> is run.</returns>
public Task<T> RunAfterImGuiRender<T>(Func<T> func)
{
var tcs = new TaskCompletionSource<T>();
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
this.runAfterImGuiRender.Enqueue(
() =>
{
Expand Down Expand Up @@ -817,8 +817,12 @@ private unsafe void ContinueConstruction(
// This will wait for scene on its own. We just wait for this.dalamudAtlas.BuildTask in this.InitScene.
_ = this.dalamudAtlas.BuildFontsAsync();

SwapChainHelper.BusyWaitForGameDeviceSwapChain();
SwapChainHelper.DetectReShade();

try
{
// Requires that game window to be there, which will be the case once game swap chain is initialized.
if (Service<DalamudConfiguration>.Get().WindowIsImmersive)
this.SetImmersiveMode(true);
}
Expand All @@ -834,9 +838,6 @@ private unsafe void ContinueConstruction(
0,
this.SetCursorDetour);

SwapChainHelper.BusyWaitForGameDeviceSwapChain();
SwapChainHelper.DetectReShade();

Log.Verbose("===== S W A P C H A I N =====");
this.resizeBuffersHook = Hook<ResizeBuffersDelegate>.FromAddress(
(nint)SwapChainHelper.GameDeviceSwapChainVtbl->ResizeBuffers,
Expand Down
21 changes: 6 additions & 15 deletions Dalamud/Interface/Internal/Windows/ConsoleWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.Windowing;
using Dalamud.Logging.Internal;
using Dalamud.Plugin.Internal;
using Dalamud.Plugin.Services;
using Dalamud.Utility;
Expand All @@ -39,9 +38,6 @@ internal class ConsoleWindow : Window, IDisposable
private const int LogLinesMaximum = 1000000;
private const int HistorySize = 50;

// Only this field may be touched from any thread.
private readonly ConcurrentQueue<(string Line, LogEvent LogEvent)> newLogEntries;

// Fields below should be touched only from the main thread.
private readonly RollingList<LogEntry> logText;
private readonly RollingList<LogEntry> filteredLogEntries;
Expand Down Expand Up @@ -94,7 +90,6 @@ public ConsoleWindow(DalamudConfiguration configuration)

this.autoScroll = configuration.LogAutoScroll;
this.autoOpen = configuration.LogOpenAtStartup;
SerilogEventSink.Instance.LogLine += this.OnLogLine;

Service<Framework>.GetAsync().ContinueWith(r => r.Result.Update += this.FrameworkOnUpdate);

Expand All @@ -114,7 +109,6 @@ public ConsoleWindow(DalamudConfiguration configuration)
this.logLinesLimit = configuration.LogLinesLimit;

var limit = Math.Max(LogLinesMinimum, this.logLinesLimit);
this.newLogEntries = new();
this.logText = new(limit);
this.filteredLogEntries = new(limit);

Expand All @@ -126,6 +120,9 @@ public ConsoleWindow(DalamudConfiguration configuration)
}
}

/// <summary>Gets the queue where log entries that are not processed yet are stored.</summary>
public static ConcurrentQueue<(string Line, LogEvent LogEvent)> NewLogEntries { get; } = new();

/// <inheritdoc/>
public override void OnOpen()
{
Expand All @@ -136,7 +133,6 @@ public override void OnOpen()
/// <inheritdoc/>
public void Dispose()
{
SerilogEventSink.Instance.LogLine -= this.OnLogLine;
this.configuration.DalamudConfigurationSaved -= this.OnDalamudConfigurationSaved;
if (Service<Framework>.GetNullable() is { } framework)
framework.Update -= this.FrameworkOnUpdate;
Expand Down Expand Up @@ -324,7 +320,7 @@ public override void Draw()
ImGuiInputTextFlags.CallbackHistory | ImGuiInputTextFlags.CallbackEdit,
this.CommandInputCallback))
{
this.newLogEntries.Enqueue((this.commandText, new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate(string.Empty, []), [])));
NewLogEntries.Enqueue((this.commandText, new LogEvent(DateTimeOffset.Now, LogEventLevel.Information, null, new MessageTemplate(string.Empty, []), [])));
this.ProcessCommand();
getFocus = true;
}
Expand Down Expand Up @@ -372,7 +368,7 @@ private void FrameworkOnUpdate(IFramework framework)
this.pendingClearLog = false;
this.logText.Clear();
this.filteredLogEntries.Clear();
this.newLogEntries.Clear();
NewLogEntries.Clear();
}

if (this.pendingRefilter)
Expand All @@ -388,7 +384,7 @@ private void FrameworkOnUpdate(IFramework framework)

var numPrevFilteredLogEntries = this.filteredLogEntries.Count;
var addedLines = 0;
while (this.newLogEntries.TryDequeue(out var logLine))
while (NewLogEntries.TryDequeue(out var logLine))
addedLines += this.HandleLogLine(logLine.Line, logLine.LogEvent);
this.newRolledLines = addedLines - (this.filteredLogEntries.Count - numPrevFilteredLogEntries);
}
Expand Down Expand Up @@ -1062,11 +1058,6 @@ private bool IsFilterApplicable(LogEntry entry)
/// <summary>Queues filtering the log entries again, before next call to <see cref="Draw"/>.</summary>
private void QueueRefilter() => this.pendingRefilter = true;

/// <summary>Enqueues the new log line to the log-to-be-processed queue.</summary>
/// <remarks>See <see cref="FrameworkOnUpdate"/> for the handler for the queued log entries.</remarks>
private void OnLogLine(object sender, (string Line, LogEvent LogEvent) logEvent) =>
this.newLogEntries.Enqueue(logEvent);

private bool DrawToggleButtonWithTooltip(
string buttonId, string tooltip, FontAwesomeIcon icon, ref bool enabledState)
{
Expand Down
4 changes: 2 additions & 2 deletions Dalamud/Interface/Internal/Windows/PluginImageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public bool TryGetImages(LocalPlugin? plugin, IPluginManifest manifest, bool isT

private Task<T> RunInDownloadQueue<T>(Func<Task<T>> func, ulong requestedFrame)
{
var tcs = new TaskCompletionSource<T>();
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
this.downloadQueue.Add(Tuple.Create(requestedFrame, async () =>
{
try
Expand All @@ -332,7 +332,7 @@ private Task<T> RunInDownloadQueue<T>(Func<Task<T>> func, ulong requestedFrame)

private Task<T> RunInLoadQueue<T>(Func<Task<T>> func)
{
var tcs = new TaskCompletionSource<T>();
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
this.loadQueue.Add(async () =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3774,15 +3774,15 @@ private Task ShowErrorModal(string message)
this.errorModalMessage = message;
this.errorModalDrawing = true;
this.errorModalOnNextFrame = true;
this.errorModalTaskCompletionSource = new TaskCompletionSource();
this.errorModalTaskCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
return this.errorModalTaskCompletionSource.Task;
}

private Task<bool> ShowUpdateModal(LocalPlugin plugin)
{
this.updateModalOnNextFrame = true;
this.updateModalPlugin = plugin;
this.updateModalTaskCompletionSource = new TaskCompletionSource<bool>();
this.updateModalTaskCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
return this.updateModalTaskCompletionSource.Task;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public void BuildFontsImmediately()
$"{nameof(FontAtlasAutoRebuildMode.Async)}.");
}

var tcs = new TaskCompletionSource<FontAtlasBuiltData>();
var tcs = new TaskCompletionSource<FontAtlasBuiltData>(TaskCreationOptions.RunContinuationsAsynchronously);
try
{
var rebuildIndex = Interlocked.Increment(ref this.buildIndex);
Expand Down
2 changes: 1 addition & 1 deletion Dalamud/Interface/ManagedFontAtlas/Internals/FontHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public Task<IFontHandle> WaitAsync()
if (this.Available)
return Task.FromResult<IFontHandle>(this);

var tcs = new TaskCompletionSource<IFontHandle>();
var tcs = new TaskCompletionSource<IFontHandle>(TaskCreationOptions.RunContinuationsAsynchronously);
this.ImFontChanged += OnImFontChanged;
this.Disposed += OnDisposed;
if (this.Available)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ internal sealed class ViewportTextureWrap : IDalamudTextureWrap, IDeferredDispos
private readonly string? debugName;
private readonly LocalPlugin? ownerPlugin;
private readonly CancellationToken cancellationToken;
private readonly TaskCompletionSource<IDalamudTextureWrap> firstUpdateTaskCompletionSource = new();
private readonly TaskCompletionSource<IDalamudTextureWrap> firstUpdateTaskCompletionSource =
new(TaskCreationOptions.RunContinuationsAsynchronously);

private ImGuiViewportTextureArgs args;
private D3D11_TEXTURE2D_DESC desc;
Expand Down
4 changes: 2 additions & 2 deletions Dalamud/Interface/Utility/Internal/DevTextureSaveMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task ShowTextureSaveMenuAsync(
{
var first = true;
var encoders = textureManager.Wic.GetSupportedEncoderInfos().ToList();
var tcs = new TaskCompletionSource<BitmapCodecInfo>();
var tcs = new TaskCompletionSource<BitmapCodecInfo>(TaskCreationOptions.RunContinuationsAsynchronously);
Service<InterfaceManager>.Get().Draw += DrawChoices;

encoder = await tcs.Task;
Expand Down Expand Up @@ -108,7 +108,7 @@ void DrawChoices()

string path;
{
var tcs = new TaskCompletionSource<string>();
var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
this.fileDialogManager.SaveFileDialog(
"Save texture...",
$"{encoder.Name.Replace(',', '.')}{{{string.Join(',', encoder.Extensions)}}}",
Expand Down
Loading

0 comments on commit 910b12d

Please sign in to comment.