From 32551e3dd75d600f73f87a80e8ba4cbfa3b46ac5 Mon Sep 17 00:00:00 2001 From: Mag-nus Date: Sun, 29 Nov 2026 10:15:48 -0600 Subject: [PATCH] Make ACE.Common, ACE.Entity and ACE.DatLoader AOT compatible Sets IsAotCompatible on the three libraries, which turns on the trimming, single-file and AOT analyzers for them, and fixes the 11 places they flag: - ConfigManager reads Config.js through source-generated JSON metadata (ConfigJsonContext) built from SerializerOptions, and falls back to AppContext.BaseDirectory instead of Assembly.Location, which is empty in a single-file or native AOT app. - EnumHelper.GetFlags and CharacterOptionExtensions no longer call Enum.GetValues(Type), which has to create an enum array at runtime. - The [Ephemeral], [SendOnLogin] and [AssessmentProperty] lookups declare that they read the enum's public fields. - AttributeExtensions.GetAttributeOfType suppresses IL2075: trimming never removes an enum's literal fields. - The DatLoader loggers use typeof instead of MethodBase.GetCurrentMethod(). No behaviour change for ACE.Server. ConfigManager.SerializerOptions is unchanged and still serves the callers that use reflection. Co-Authored-By: Claude Fable 5.2 --- Source/ACE.Common/ACE.Common.csproj | 1 + Source/ACE.Common/ConfigJsonContext.cs | 12 ++++++++++++ Source/ACE.Common/ConfigManager.cs | 14 +++++++------- Source/ACE.Common/Extensions/EnumHelper.cs | 5 ++++- Source/ACE.DatLoader/ACE.DatLoader.csproj | 1 + Source/ACE.DatLoader/DatDatabase.cs | 2 +- Source/ACE.DatLoader/DatManager.cs | 2 +- Source/ACE.DatLoader/Entity/AnimationHook.cs | 2 +- Source/ACE.Entity/ACE.Entity.csproj | 1 + Source/ACE.Entity/Enum/AttributeExtensions.cs | 3 +++ Source/ACE.Entity/Enum/CharacterOption.cs | 2 +- .../Enum/Properties/AssessmentProperties.cs | 3 ++- .../Enum/Properties/EphemeralProperties.cs | 3 ++- .../Enum/Properties/SendOnLoginProperties.cs | 3 ++- 14 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 Source/ACE.Common/ConfigJsonContext.cs diff --git a/Source/ACE.Common/ACE.Common.csproj b/Source/ACE.Common/ACE.Common.csproj index c812dad41ca..c3e2442b42e 100644 --- a/Source/ACE.Common/ACE.Common.csproj +++ b/Source/ACE.Common/ACE.Common.csproj @@ -3,6 +3,7 @@ net10.0 14 + true AnyCPU ACEmulator Contributors git diff --git a/Source/ACE.Common/ConfigJsonContext.cs b/Source/ACE.Common/ConfigJsonContext.cs new file mode 100644 index 00000000000..443acc03ab9 --- /dev/null +++ b/Source/ACE.Common/ConfigJsonContext.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace ACE.Common +{ + /// + /// Source-generated JSON metadata for Config.js, so that ConfigManager can read it in a trimmed or native AOT app. + /// + [JsonSerializable(typeof(MasterConfiguration))] + internal partial class ConfigJsonContext : JsonSerializerContext + { + } +} diff --git a/Source/ACE.Common/ConfigManager.cs b/Source/ACE.Common/ConfigManager.cs index 8e9f2e8ddd9..6caf1891412 100644 --- a/Source/ACE.Common/ConfigManager.cs +++ b/Source/ACE.Common/ConfigManager.cs @@ -37,13 +37,10 @@ public static void Initialize(string path = @"Config.js") if (!File.Exists(pathToUse)) { // File not found in Environment.CurrentDirectory - // Lets try the ExecutingAssembly Location - var executingAssemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location; + // Lets try the application's directory. (Assembly.Location is empty in a single-file or native AOT app.) + directoryName = AppContext.BaseDirectory; - directoryName = Path.GetDirectoryName(executingAssemblyLocation); - - if (directoryName != null) - pathToUse = Path.Combine(directoryName, fileName); + pathToUse = Path.Combine(directoryName, fileName); } } else @@ -61,7 +58,10 @@ public static void Initialize(string path = @"Config.js") var fileText = File.ReadAllText(pathToUse); - Config = JsonSerializer.Deserialize(fileText, SerializerOptions); + // The source-generated metadata reads the file without reflection, with the settings of SerializerOptions + var context = new ConfigJsonContext(new JsonSerializerOptions(SerializerOptions)); + + Config = JsonSerializer.Deserialize(fileText, context.MasterConfiguration); } catch (Exception exception) { diff --git a/Source/ACE.Common/Extensions/EnumHelper.cs b/Source/ACE.Common/Extensions/EnumHelper.cs index 2c6f5d8d09e..330e8f0d444 100644 --- a/Source/ACE.Common/Extensions/EnumHelper.cs +++ b/Source/ACE.Common/Extensions/EnumHelper.cs @@ -11,7 +11,10 @@ public static class EnumHelper /// public static List GetFlags(this Enum e) { - return Enum.GetValues(e.GetType()).Cast().Where(e.HasFlag).ToList(); + var type = e.GetType(); + + // Enum.GetValues(Type) has to create an array of the enum type at runtime, which native AOT cannot always do + return Enum.GetValuesAsUnderlyingType(type).Cast().Select(v => (Enum)Enum.ToObject(type, v)).Where(e.HasFlag).ToList(); } /// diff --git a/Source/ACE.DatLoader/ACE.DatLoader.csproj b/Source/ACE.DatLoader/ACE.DatLoader.csproj index 50498f29676..d8c1c8f3b3d 100644 --- a/Source/ACE.DatLoader/ACE.DatLoader.csproj +++ b/Source/ACE.DatLoader/ACE.DatLoader.csproj @@ -3,6 +3,7 @@ net10.0 14 + true AnyCPU ACEmulator Contributors git diff --git a/Source/ACE.DatLoader/DatDatabase.cs b/Source/ACE.DatLoader/DatDatabase.cs index e72c1101512..3df5db6d865 100644 --- a/Source/ACE.DatLoader/DatDatabase.cs +++ b/Source/ACE.DatLoader/DatDatabase.cs @@ -12,7 +12,7 @@ namespace ACE.DatLoader { public class DatDatabase { - private static readonly ILogger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILogger log = LogManager.GetLogger(typeof(DatDatabase)); private const uint DAT_HEADER_OFFSET = 0x140; diff --git a/Source/ACE.DatLoader/DatManager.cs b/Source/ACE.DatLoader/DatManager.cs index 933d113a9ce..6ddbffa7c32 100644 --- a/Source/ACE.DatLoader/DatManager.cs +++ b/Source/ACE.DatLoader/DatManager.cs @@ -8,7 +8,7 @@ namespace ACE.DatLoader { public static class DatManager { - private static readonly ILogger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILogger log = LogManager.GetLogger(typeof(DatManager)); private static string datFile; diff --git a/Source/ACE.DatLoader/Entity/AnimationHook.cs b/Source/ACE.DatLoader/Entity/AnimationHook.cs index 620a5318436..e213db49851 100644 --- a/Source/ACE.DatLoader/Entity/AnimationHook.cs +++ b/Source/ACE.DatLoader/Entity/AnimationHook.cs @@ -11,7 +11,7 @@ namespace ACE.DatLoader.Entity { public class AnimationHook : IUnpackable { - private static readonly ILogger log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILogger log = LogManager.GetLogger(typeof(AnimationHook)); public AnimationHookType HookType { get; private set; } public AnimationHookDir Direction { get; private set; } diff --git a/Source/ACE.Entity/ACE.Entity.csproj b/Source/ACE.Entity/ACE.Entity.csproj index d8b61b5f551..c93557d2dce 100644 --- a/Source/ACE.Entity/ACE.Entity.csproj +++ b/Source/ACE.Entity/ACE.Entity.csproj @@ -3,6 +3,7 @@ net10.0 14 + true AnyCPU ACEmulator Contributors git diff --git a/Source/ACE.Entity/Enum/AttributeExtensions.cs b/Source/ACE.Entity/Enum/AttributeExtensions.cs index e50f24042b2..6660b547b32 100644 --- a/Source/ACE.Entity/Enum/AttributeExtensions.cs +++ b/Source/ACE.Entity/Enum/AttributeExtensions.cs @@ -1,7 +1,10 @@ +using System.Diagnostics.CodeAnalysis; + namespace ACE.Entity.Enum { public static class AttributeExtensions { + [UnconditionalSuppressMessage("Trimming", "IL2075", Justification = "Trimming never removes the literal fields of an enum.")] public static T GetAttributeOfType(this System.Enum enumVal) where T : System.Attribute { var type = enumVal.GetType(); diff --git a/Source/ACE.Entity/Enum/CharacterOption.cs b/Source/ACE.Entity/Enum/CharacterOption.cs index db5c8bc0577..8fd70a6dc2c 100644 --- a/Source/ACE.Entity/Enum/CharacterOption.cs +++ b/Source/ACE.Entity/Enum/CharacterOption.cs @@ -185,7 +185,7 @@ public static class CharacterOptionExtensions static CharacterOptionExtensions() { - var vals = System.Enum.GetValues(typeof(CharacterOption)).Cast(); + var vals = System.Enum.GetValues(); CharacterOptions1Attributes = vals.ToDictionary(o => o, o => o.GetAttributeOfType()); CharacterOptions2Attributes = vals.ToDictionary(o => o, o => o.GetAttributeOfType()); } diff --git a/Source/ACE.Entity/Enum/Properties/AssessmentProperties.cs b/Source/ACE.Entity/Enum/Properties/AssessmentProperties.cs index 89623016a0e..058c36c56aa 100644 --- a/Source/ACE.Entity/Enum/Properties/AssessmentProperties.cs +++ b/Source/ACE.Entity/Enum/Properties/AssessmentProperties.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace ACE.Entity.Enum.Properties @@ -14,7 +15,7 @@ public static class AssessmentProperties /// /// Enum to list by [AssessmentProperty] /// Type of the results - private static HashSet GetValues() + private static HashSet GetValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T, TResult>() { var list = typeof(T).GetFields().Select(x => new { diff --git a/Source/ACE.Entity/Enum/Properties/EphemeralProperties.cs b/Source/ACE.Entity/Enum/Properties/EphemeralProperties.cs index f1246055cb7..c4a4a2937d3 100644 --- a/Source/ACE.Entity/Enum/Properties/EphemeralProperties.cs +++ b/Source/ACE.Entity/Enum/Properties/EphemeralProperties.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace ACE.Entity.Enum.Properties @@ -14,7 +15,7 @@ public static class EphemeralProperties /// /// Enum to list by [Ephemeral] /// Type of the results - private static HashSet GetValues() + private static HashSet GetValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>() { var list = typeof(T).GetFields().Select(x => new { diff --git a/Source/ACE.Entity/Enum/Properties/SendOnLoginProperties.cs b/Source/ACE.Entity/Enum/Properties/SendOnLoginProperties.cs index 79e19d3d0f7..f9fa2920138 100644 --- a/Source/ACE.Entity/Enum/Properties/SendOnLoginProperties.cs +++ b/Source/ACE.Entity/Enum/Properties/SendOnLoginProperties.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace ACE.Entity.Enum.Properties @@ -14,7 +15,7 @@ public static class SendOnLoginProperties /// /// Enum to list by [SendOnLogin] /// Type of the results - private static HashSet GetValues() + private static HashSet GetValues<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T, TResult>() { var list = typeof(T).GetFields().Select(x => new {