diff --git a/Dalamud/Interface/UiBuilder.cs b/Dalamud/Interface/UiBuilder.cs index 7a3eb6fb6..d260868a0 100644 --- a/Dalamud/Interface/UiBuilder.cs +++ b/Dalamud/Interface/UiBuilder.cs @@ -212,7 +212,7 @@ public sealed class UiBuilder : IDisposable /// /// fontAtlas.NewDelegateFontHandle( /// e => e.OnPreBuild( - /// tk => tk.AddDalamudDefaultFont(UiBuilder.DefaultFontSizePt))); + /// tk => tk.AddDalamudDefaultFont(UiBuilder.DefaultFontSizePx))); /// /// public IFontHandle DefaultFontHandle => @@ -231,6 +231,8 @@ public sealed class UiBuilder : IDisposable /// fontAtlas.NewDelegateFontHandle( /// e => e.OnPreBuild( /// tk => tk.AddFontAwesomeIconFont(new() { SizePt = UiBuilder.DefaultFontSizePt }))); + /// // or use + /// tk => tk.AddFontAwesomeIconFont(new() { SizePx = UiBuilder.DefaultFontSizePx }))); /// /// public IFontHandle IconFontHandle => @@ -251,6 +253,8 @@ public sealed class UiBuilder : IDisposable /// tk => tk.AddDalamudAssetFont( /// DalamudAsset.InconsolataRegular, /// new() { SizePt = UiBuilder.DefaultFontSizePt }))); + /// // or use + /// new() { SizePx = UiBuilder.DefaultFontSizePx }))); /// /// public IFontHandle MonoFontHandle => diff --git a/Dalamud/Utility/DisposeSafety.cs b/Dalamud/Utility/DisposeSafety.cs index 909c4e932..8ac891e0a 100644 --- a/Dalamud/Utility/DisposeSafety.cs +++ b/Dalamud/Utility/DisposeSafety.cs @@ -39,21 +39,23 @@ public static class DisposeSafety public static IDisposable ToDisposableIgnoreExceptions(this Task task) where T : IDisposable { - return Disposable.Create(() => task.ContinueWith(r => - { - _ = r.Exception; - if (r.IsCompleted) - { - try + return Disposable.Create( + () => task.ContinueWith( + r => { - r.Dispose(); - } - catch - { - // ignore - } - } - })); + _ = r.Exception; + if (r.IsCompleted) + { + try + { + r.Dispose(); + } + catch + { + // ignore + } + } + })); } /// @@ -102,25 +104,26 @@ public static class DisposeSafety if (disposables is not T[] array) array = disposables?.ToArray() ?? Array.Empty(); - return Disposable.Create(() => - { - List exceptions = null; - foreach (var d in array) + return Disposable.Create( + () => { - try + List exceptions = null; + foreach (var d in array) { - d?.Dispose(); + try + { + d?.Dispose(); + } + catch (Exception de) + { + exceptions ??= new(); + exceptions.Add(de); + } } - catch (Exception de) - { - exceptions ??= new(); - exceptions.Add(de); - } - } - if (exceptions is not null) - throw new AggregateException(exceptions); - }); + if (exceptions is not null) + throw new AggregateException(exceptions); + }); } /// @@ -137,7 +140,11 @@ public static class DisposeSafety public event Action? AfterDispose; /// - public void EnsureCapacity(int capacity) => this.objects.EnsureCapacity(capacity); + public void EnsureCapacity(int capacity) + { + lock (this.objects) + this.objects.EnsureCapacity(capacity); + } /// /// The parameter. @@ -145,7 +152,10 @@ public static class DisposeSafety public T? Add(T? d) where T : IDisposable { if (d is not null) - this.objects.Add(this.CheckAdd(d)); + { + lock (this.objects) + this.objects.Add(this.CheckAdd(d)); + } return d; } @@ -155,7 +165,10 @@ public static class DisposeSafety public Action? Add(Action? d) { if (d is not null) - this.objects.Add(this.CheckAdd(d)); + { + lock (this.objects) + this.objects.Add(this.CheckAdd(d)); + } return d; } @@ -165,7 +178,10 @@ public static class DisposeSafety public Func? Add(Func? d) { if (d is not null) - this.objects.Add(this.CheckAdd(d)); + { + lock (this.objects) + this.objects.Add(this.CheckAdd(d)); + } return d; } @@ -174,7 +190,10 @@ public static class DisposeSafety public GCHandle Add(GCHandle d) { if (d != default) - this.objects.Add(this.CheckAdd(d)); + { + lock (this.objects) + this.objects.Add(this.CheckAdd(d)); + } return d; } @@ -183,29 +202,41 @@ public static class DisposeSafety /// Queue all the given to be disposed later. /// /// Disposables. - public void AddRange(IEnumerable ds) => - this.objects.AddRange(ds.Where(d => d is not null).Select(d => (object)this.CheckAdd(d))); + public void AddRange(IEnumerable ds) + { + lock (this.objects) + this.objects.AddRange(ds.Where(d => d is not null).Select(d => (object)this.CheckAdd(d))); + } /// /// Queue all the given to be run later. /// /// Actions. - public void AddRange(IEnumerable ds) => - this.objects.AddRange(ds.Where(d => d is not null).Select(d => (object)this.CheckAdd(d))); + public void AddRange(IEnumerable ds) + { + lock (this.objects) + this.objects.AddRange(ds.Where(d => d is not null).Select(d => (object)this.CheckAdd(d))); + } /// /// Queue all the given returning to be run later. /// /// Func{Task}s. - public void AddRange(IEnumerable?> ds) => - this.objects.AddRange(ds.Where(d => d is not null).Select(d => (object)this.CheckAdd(d))); + public void AddRange(IEnumerable?> ds) + { + lock (this.objects) + this.objects.AddRange(ds.Where(d => d is not null).Select(d => (object)this.CheckAdd(d))); + } /// /// Queue all the given to be disposed later. /// /// GCHandles. - public void AddRange(IEnumerable ds) => - this.objects.AddRange(ds.Select(d => (object)this.CheckAdd(d))); + public void AddRange(IEnumerable ds) + { + lock (this.objects) + this.objects.AddRange(ds.Select(d => (object)this.CheckAdd(d))); + } /// /// Cancel all pending disposals. @@ -213,9 +244,12 @@ public static class DisposeSafety /// Use this after successful initialization of multiple disposables. public void Cancel() { - foreach (var o in this.objects) - this.CheckRemove(o); - this.objects.Clear(); + lock (this.objects) + { + foreach (var o in this.objects) + this.CheckRemove(o); + this.objects.Clear(); + } } /// @@ -264,11 +298,17 @@ public static class DisposeSafety this.BeforeDispose?.InvokeSafely(this); List? exceptions = null; - while (this.objects.Any()) + while (true) { - var obj = this.objects[^1]; - this.objects.RemoveAt(this.objects.Count - 1); - + object obj; + lock (this.objects) + { + if (this.objects.Count == 0) + break; + obj = this.objects[^1]; + this.objects.RemoveAt(this.objects.Count - 1); + } + try { switch (obj) @@ -294,7 +334,8 @@ public static class DisposeSafety } } - this.objects.TrimExcess(); + lock (this.objects) + this.objects.TrimExcess(); if (exceptions is not null) { @@ -318,10 +359,16 @@ public static class DisposeSafety this.BeforeDispose?.InvokeSafely(this); List? exceptions = null; - while (this.objects.Any()) + while (true) { - var obj = this.objects[^1]; - this.objects.RemoveAt(this.objects.Count - 1); + object obj; + lock (this.objects) + { + if (this.objects.Count == 0) + break; + obj = this.objects[^1]; + this.objects.RemoveAt(this.objects.Count - 1); + } try { @@ -351,7 +398,8 @@ public static class DisposeSafety } } - this.objects.TrimExcess(); + lock (this.objects) + this.objects.TrimExcess(); if (exceptions is not null) { @@ -386,7 +434,8 @@ public static class DisposeSafety private void OnItemDisposed(IDisposeCallback obj) { obj.BeforeDispose -= this.OnItemDisposed; - this.objects.Remove(obj); + lock (this.objects) + this.objects.Remove(obj); } } }