diff --git a/Dalamud.Test/Compliance/PublicApiTests.cs b/Dalamud.Test/Compliance/PublicApiTests.cs new file mode 100644 index 000000000..1ce8b19fa --- /dev/null +++ b/Dalamud.Test/Compliance/PublicApiTests.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +using Xunit; + + +namespace Dalamud.Test.Compliance; + +public class PublicApiTests +{ + private static List IgnoredTypes { get; } = + [ + typeof(Utility.CStringExtensions), + ]; + + private static List PermittedAssemblies { get; } = + [ + typeof(object).Assembly, + typeof(Dalamud).Assembly, + + // Imgui and friends + typeof(SharpDX.Color).Assembly, + typeof(Bindings.ImGui.ImGui).Assembly, + typeof(Bindings.ImGuizmo.ImGuizmo).Assembly, + typeof(Bindings.ImPlot.ImPlot).Assembly, + + // exposed to plugins via API + typeof(Lumina.GameData).Assembly, + typeof(Lumina.Excel.Sheets.Action).Assembly, + ]; + + private static List PermittedTypes { get; } = [ + // Used for IPluginLog, limited serilog exposure is OK. + typeof(Serilog.ILogger), + typeof(Serilog.Core.LoggingLevelSwitch), + typeof(Serilog.Events.LogEventLevel), + ]; + + [Fact] + public void NoRestrictedTypes() + { + foreach (var type in typeof(Dalamud).Assembly.GetTypes().Where(t => t.IsPublic).Except(IgnoredTypes)) + { + if (type.GetCustomAttribute() != null) continue; + + foreach (var m in type.GetMethods().Where(m => m.IsPublic && !m.IsSpecialName)) + { + if (m.GetCustomAttribute() != null) continue; + + if (!this.IsPermittedType(m.ReturnType)) + { + Assert.Fail($"Method {type.FullName}.{m.Name} returns unapproved type: {m.ReturnType.FullName}"); + } + + foreach (var param in m.GetParameters()) + { + if (!this.IsPermittedType(param.ParameterType)) + { + Assert.Fail($"Method {type.FullName}.{m.Name} uses unapproved type: {param.ParameterType.FullName}"); + } + } + } + + foreach (var p in type.GetProperties()) + { + if (p.GetCustomAttribute() != null) continue; + if (p.GetMethod?.IsPrivate == true && p.SetMethod?.IsPrivate == true) continue; + + if (!this.IsPermittedType(p.PropertyType)) + { + Assert.Fail( + $"Property {type.FullName}.{p.Name} is unapproved type: {p.PropertyType.FullName}"); + } + } + + foreach (var f in type.GetFields().Where(f => f.IsPublic && !f.IsSpecialName)) + { + if (f.GetCustomAttribute() != null) continue; + + if (!this.IsPermittedType(f.FieldType)) + { + Assert.Fail( + $"Field {type.FullName}.{f.Name} is unapproved type: {f.FieldType.FullName}"); + } + } + } + } + + private bool IsPermittedType(Type subject) + { + if (subject.IsGenericType && !subject.GetGenericArguments().All(this.IsPermittedType)) + { + return false; + } + + return subject.Namespace?.StartsWith("System") == true || + PermittedTypes.Any(t => t == subject) || + PermittedAssemblies.Any(a => a == subject.Assembly); + } +} diff --git a/Dalamud.Test/Dalamud.Test.csproj b/Dalamud.Test/Dalamud.Test.csproj index c6c8f8e8a..0839a39d3 100644 --- a/Dalamud.Test/Dalamud.Test.csproj +++ b/Dalamud.Test/Dalamud.Test.csproj @@ -43,14 +43,14 @@ - + - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonRefreshArgs.cs b/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonRefreshArgs.cs index d28631c3c..83a0ba3e3 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonRefreshArgs.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonRefreshArgs.cs @@ -1,3 +1,5 @@ +using Dalamud.Utility; + using FFXIVClientStructs.FFXIV.Component.GUI; namespace Dalamud.Game.Addon.Lifecycle.AddonArgTypes; @@ -31,6 +33,8 @@ public class AddonRefreshArgs : AddonArgs, ICloneable /// /// Gets the AtkValues in the form of a span. /// + [Api14ToDo(Api14ToDoAttribute.Remove)] + [Obsolete($"Exposed ClientStructs type; use {nameof(AtkValues)}/{nameof(AtkValueCount)} instead.", false)] public unsafe Span AtkValueSpan => new(this.AtkValues.ToPointer(), (int)this.AtkValueCount); /// diff --git a/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonSetupArgs.cs b/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonSetupArgs.cs index 0dd9ecee2..b5f50b551 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonSetupArgs.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonArgTypes/AddonSetupArgs.cs @@ -1,3 +1,5 @@ +using Dalamud.Utility; + using FFXIVClientStructs.FFXIV.Component.GUI; namespace Dalamud.Game.Addon.Lifecycle.AddonArgTypes; @@ -31,6 +33,8 @@ public class AddonSetupArgs : AddonArgs, ICloneable /// /// Gets the AtkValues in the form of a span. /// + [Api14ToDo(Api14ToDoAttribute.Remove)] + [Obsolete($"Exposed ClientStructs type; use {nameof(AtkValues)}/{nameof(AtkValueCount)} instead.", false)] public unsafe Span AtkValueSpan => new(this.AtkValues.ToPointer(), (int)this.AtkValueCount); /// diff --git a/Dalamud/Game/Addon/Lifecycle/AddonEvent.cs b/Dalamud/Game/Addon/Lifecycle/AddonEvent.cs index 5fd0ac964..b5a7e4919 100644 --- a/Dalamud/Game/Addon/Lifecycle/AddonEvent.cs +++ b/Dalamud/Game/Addon/Lifecycle/AddonEvent.cs @@ -12,11 +12,11 @@ public enum AddonEvent /// /// An event that is fired prior to an addon being setup with its implementation of /// . This event is useful for modifying the initial data contained within - /// prior to the addon being created. + /// prior to the addon being created. /// /// PreSetup, - + /// /// An event that is fired after an addon has finished its initial setup. This event is particularly useful for /// developers seeking to add custom elements to now-initialized and populated node lists, as well as reading data @@ -64,7 +64,7 @@ public enum AddonEvent /// /// PreFinalize, - + /// /// An event that is fired before a call to is made in response to a /// change in the subscribed or @@ -81,13 +81,13 @@ public enum AddonEvent /// to the Free Company's overview. /// PreRequestedUpdate, - + /// /// An event that is fired after an addon has finished processing an ArrayData update. /// See for more information. /// PostRequestedUpdate, - + /// /// An event that is fired before an addon calls its method. Refreshes are /// generally triggered in response to certain user interactions such as changing tabs, and are primarily used to @@ -96,13 +96,13 @@ public enum AddonEvent /// /// PreRefresh, - + /// /// An event that is fired after an addon has finished its refresh. /// See for more information. /// PostRefresh, - + /// /// An event that is fired before an addon begins processing a user-driven event via /// , such as mousing over an element or clicking a button. This event @@ -112,7 +112,7 @@ public enum AddonEvent /// /// PreReceiveEvent, - + /// /// An event that is fired after an addon finishes calling its method. /// See for more information. diff --git a/Dalamud/Game/ClientState/JobGauge/Types/SMNGauge.cs b/Dalamud/Game/ClientState/JobGauge/Types/SMNGauge.cs index 899ea78eb..55b08cf2e 100644 --- a/Dalamud/Game/ClientState/JobGauge/Types/SMNGauge.cs +++ b/Dalamud/Game/ClientState/JobGauge/Types/SMNGauge.cs @@ -1,4 +1,6 @@ using Dalamud.Game.ClientState.JobGauge.Enums; +using Dalamud.Utility; + using FFXIVClientStructs.FFXIV.Client.Game.Gauge; namespace Dalamud.Game.ClientState.JobGauge.Types; @@ -69,37 +71,39 @@ public unsafe class SMNGauge : JobGaugeBase /// Gets the current aether flags. /// Use the summon accessors instead. /// + [Api14ToDo("Declare our own enum for this to avoid CS type.")] + [Obsolete("Use specific accessors instead until API14.")] public AetherFlags AetherFlags => this.Struct->AetherFlags; /// /// Gets a value indicating whether Bahamut is ready to be summoned. /// /// true or false. - public bool IsBahamutReady => !this.AetherFlags.HasFlag(AetherFlags.PhoenixReady); + public bool IsBahamutReady => !this.Struct->AetherFlags.HasFlag(AetherFlags.PhoenixReady); /// /// Gets a value indicating whether if Phoenix is ready to be summoned. /// /// true or false. - public bool IsPhoenixReady => this.AetherFlags.HasFlag(AetherFlags.PhoenixReady); + public bool IsPhoenixReady => this.Struct->AetherFlags.HasFlag(AetherFlags.PhoenixReady); /// /// Gets a value indicating whether if Ifrit is ready to be summoned. /// /// true or false. - public bool IsIfritReady => this.AetherFlags.HasFlag(AetherFlags.IfritReady); + public bool IsIfritReady => this.Struct->AetherFlags.HasFlag(AetherFlags.IfritReady); /// /// Gets a value indicating whether if Titan is ready to be summoned. /// /// true or false. - public bool IsTitanReady => this.AetherFlags.HasFlag(AetherFlags.TitanReady); + public bool IsTitanReady => this.Struct->AetherFlags.HasFlag(AetherFlags.TitanReady); /// /// Gets a value indicating whether if Garuda is ready to be summoned. /// /// true or false. - public bool IsGarudaReady => this.AetherFlags.HasFlag(AetherFlags.GarudaReady); + public bool IsGarudaReady => this.Struct->AetherFlags.HasFlag(AetherFlags.GarudaReady); /// /// Gets a value indicating whether if Ifrit is currently attuned. @@ -128,5 +132,5 @@ public unsafe class SMNGauge : JobGaugeBase /// /// Gets the amount of Aetherflow available. /// - public byte AetherflowStacks => (byte)(this.AetherFlags & AetherFlags.Aetherflow); + public byte AetherflowStacks => (byte)(this.Struct->AetherFlags & AetherFlags.Aetherflow); } diff --git a/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs b/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs index 6fdc504ca..42268a5ec 100644 --- a/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs +++ b/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs @@ -143,7 +143,7 @@ internal sealed unsafe class DtrBarEntry : IDisposable, IDtrBarEntry } /// - [Api13ToDo("Maybe make this config scoped to internal name?")] + [Api14ToDo("Maybe make this config scoped to internal name?")] public bool UserHidden => this.configuration.DtrIgnore?.Contains(this.Title) ?? false; /// diff --git a/Dalamud/Interface/Animation/Easing.cs b/Dalamud/Interface/Animation/Easing.cs index 0d2057b3b..cc1f48ce7 100644 --- a/Dalamud/Interface/Animation/Easing.cs +++ b/Dalamud/Interface/Animation/Easing.cs @@ -48,7 +48,7 @@ public abstract class Easing /// Gets the current value of the animation, following unclamped logic. /// [Obsolete($"This field has been deprecated. Use either {nameof(ValueClamped)} or {nameof(ValueUnclamped)} instead.", true)] - [Api13ToDo("Map this field to ValueClamped, probably.")] + [Api14ToDo("Map this field to ValueClamped, probably.")] public double Value => this.ValueUnclamped; /// diff --git a/Dalamud/Interface/Components/ImGuiComponents.HelpMarker.cs b/Dalamud/Interface/Components/ImGuiComponents.HelpMarker.cs index 57a4bd150..19e6eced8 100644 --- a/Dalamud/Interface/Components/ImGuiComponents.HelpMarker.cs +++ b/Dalamud/Interface/Components/ImGuiComponents.HelpMarker.cs @@ -1,7 +1,11 @@ using Dalamud.Bindings.ImGui; using Dalamud.Interface.Utility.Raii; +using Dalamud.Utility; + using FFXIVClientStructs.FFXIV.Common.Math; +#pragma warning disable CS0618 // Type or member is obsolete. To be fixed with API14. + namespace Dalamud.Interface.Components; /// @@ -21,14 +25,10 @@ public static partial class ImGuiComponents /// The text to display on hover. /// The icon to use. /// The color of the icon. - public static void HelpMarker(string helpText, FontAwesomeIcon icon, Vector4? color = null) + public static void HelpMarker(string helpText, FontAwesomeIcon icon, System.Numerics.Vector4 color) { using var col = new ImRaii.Color(); - - if (color.HasValue) - { - col.Push(ImGuiCol.TextDisabled, color.Value); - } + col.Push(ImGuiCol.TextDisabled, color); ImGui.SameLine(); @@ -48,4 +48,40 @@ public static partial class ImGuiComponents } } } + + /// + /// HelpMarker component to add a custom icon with text on hover. + /// + /// The text to display on hover. + /// The icon to use. + /// The color of the icon. + [Api14ToDo(Api14ToDoAttribute.Remove)] + [Obsolete("CS type is deprecated. Use System.Numerics.Vector4 instead.")] + public static void HelpMarker(string helpText, FontAwesomeIcon icon, Vector4? color = null) + { + if (color.HasValue) + { + HelpMarker(helpText, icon, color.Value); + return; + } + + // FIXME: Code duplication is easier than splitting up the Nullable in a way that doesn't break the API. + ImGui.SameLine(); + + using (ImRaii.PushFont(UiBuilder.IconFont)) + { + ImGui.TextDisabled(icon.ToIconString()); + } + + if (ImGui.IsItemHovered()) + { + using (ImRaii.Tooltip()) + { + using (ImRaii.TextWrapPos(ImGui.GetFontSize() * 35.0f)) + { + ImGui.Text(helpText); + } + } + } + } } diff --git a/Dalamud/Memory/MemoryHelper.cs b/Dalamud/Memory/MemoryHelper.cs index 2eae1be6d..acdc37a34 100644 --- a/Dalamud/Memory/MemoryHelper.cs +++ b/Dalamud/Memory/MemoryHelper.cs @@ -382,6 +382,8 @@ public static unsafe class MemoryHelper /// The memory address to read from. /// The read in string. [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Obsolete("CS types in Dalamud are deprecated.")] + [Api14ToDo(Api14ToDoAttribute.Remove)] public static SeString ReadSeString(Utf8String* utf8String) => utf8String == null ? string.Empty : SeString.Parse(utf8String->AsSpan()); @@ -613,6 +615,8 @@ public static unsafe class MemoryHelper /// The memory address to read from. /// The read in string. [MethodImpl(MethodImplOptions.AggressiveInlining)] + [Obsolete("CS types in Dalamud are deprecated.")] + [Api14ToDo(Api14ToDoAttribute.Remove)] public static unsafe void ReadSeString(Utf8String* utf8String, out SeString value) => value = ReadSeString(utf8String); diff --git a/Dalamud/Utility/Api13ToDoAttribute.cs b/Dalamud/Utility/Api14ToDoAttribute.cs similarity index 64% rename from Dalamud/Utility/Api13ToDoAttribute.cs rename to Dalamud/Utility/Api14ToDoAttribute.cs index 576401cda..f24fcb539 100644 --- a/Dalamud/Utility/Api13ToDoAttribute.cs +++ b/Dalamud/Utility/Api14ToDoAttribute.cs @@ -4,7 +4,7 @@ namespace Dalamud.Utility; /// Utility class for marking something to be changed for API 13, for ease of lookup. /// [AttributeUsage(AttributeTargets.All, Inherited = false)] -internal sealed class Api13ToDoAttribute : Attribute +internal sealed class Api14ToDoAttribute : Attribute { /// /// Marks that this should be made internal. @@ -12,11 +12,16 @@ internal sealed class Api13ToDoAttribute : Attribute public const string MakeInternal = "Make internal."; /// - /// Initializes a new instance of the class. + /// Marks that this should be removed entirely. + /// + public const string Remove = "Remove."; + + /// + /// Initializes a new instance of the class. /// /// The explanation. /// The explanation 2. - public Api13ToDoAttribute(string what, string what2 = "") + public Api14ToDoAttribute(string what, string what2 = "") { _ = what; _ = what2; diff --git a/Dalamud/Utility/Numerics/VectorExtensions.cs b/Dalamud/Utility/Numerics/VectorExtensions.cs index 910dbdd00..ffb32b266 100644 --- a/Dalamud/Utility/Numerics/VectorExtensions.cs +++ b/Dalamud/Utility/Numerics/VectorExtensions.cs @@ -56,6 +56,8 @@ public static class VectorExtensions return new Vector2(v.X, y); } + [Obsolete("CS type is deprecated. Use ByteColor instead.")] + [Api14ToDo(Api14ToDoAttribute.Remove)] public static ByteColor ToByteColor(this Vector4 v) { return new ByteColor { A = (byte)(v.W * 255), R = (byte)(v.X * 255), G = (byte)(v.Y * 255), B = (byte)(v.Z * 255) }; diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index ff06618ab..060b6fa31 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -75,7 +75,7 @@ public static partial class Util /// /// Gets the Dalamud version. /// - [Api13ToDo("Remove. Make both versions here internal. Add an API somewhere.")] + [Api14ToDo("Remove. Make both versions here internal. Add an API somewhere.")] public static string AssemblyVersion { get; } = Assembly.GetAssembly(typeof(ChatHandlers))!.GetName().Version!.ToString();