Skip to content

Make ACE.Common, ACE.Entity and ACE.DatLoader AOT compatible - #4565

Open
Mag-nus wants to merge 1 commit into
ACEmulator:masterfrom
Mag-nus:aot-compatible-libraries
Open

Mag-nus wants to merge 1 commit into
ACEmulator:masterfrom
Mag-nus:aot-compatible-libraries

Conversation

@Mag-nus

@Mag-nus Mag-nus commented Nov 29, 2026

Copy link
Copy Markdown
Member

The problem

ACE.Common, ACE.Entity and ACE.DatLoader target net10.0 since #4562, but a program that uses them cannot be published trimmed or as native AOT. With the trimming and AOT analyzers turned on, master's build flags 11 places in them:

Where (master) Warning What it flags
ACE.Common/ConfigManager.cs:64 IL2026, IL3050 JsonSerializer.Deserialize<T>(string, JsonSerializerOptions) needs reflection
ACE.Common/ConfigManager.cs:41 IL3000 Assembly.Location is empty in a single-file app
ACE.Common/Extensions/EnumHelper.cs:14 IL3050 Enum.GetValues(Type) creates an array of the enum type at run time
ACE.Entity/Enum/CharacterOption.cs:188 IL3050 the same
ACE.Entity/Enum/AttributeExtensions.cs:8 IL2075 GetType().GetMember() on a type the trimmer cannot see
AssessmentProperties.cs:19, EphemeralProperties.cs:19, SendOnLoginProperties.cs:19 (in ACE.Entity/Enum/Properties) IL2090 typeof(T).GetFields() on a type parameter with no annotation
DatDatabase.cs:15, DatManager.cs:11, Entity/AnimationHook.cs:14 (in ACE.DatLoader) IL2026 MethodBase.GetCurrentMethod()

Three of them fail at run time. A console program that uses the three libraries, published as native AOT against master (see Testing), gets:

  • ConfigManager.Initialize throws InvalidOperationException: Reflection-based serialization has been disabled for this application.
  • With no Config.js in the working directory, the one next to the executable is not found ("missing configuration file"), because Assembly.Location is empty.
  • EnumHelper.GetFlags throws NotSupportedException: 'ACE.Entity.Enum.CharacterOptions1[]' is missing native code or metadata.

The fix

<IsAotCompatible>true</IsAotCompatible> in the three project files. It turns on the trimming, single-file and AOT analyzers for them, so code that breaks this later shows up as a build warning, and it marks the assemblies as trimmable. Then the 11 places:

  • Config.js is read through source-generated JSON metadata. ConfigJsonContext (new, internal) declares MasterConfiguration. ConfigManager.Initialize builds the context from a copy of ConfigManager.SerializerOptions, so the settings (comments, trailing commas, numbers as strings) still come from that one place. SerializerOptions itself is unchanged: ACE.Server uses it with reflection for the starter gear, the mod metadata and the first-run setup.
  • The fallback folder for Config.js is AppContext.BaseDirectory, instead of the folder of Assembly.GetExecutingAssembly().Location.
  • EnumHelper.GetFlags uses Enum.GetValuesAsUnderlyingType and Enum.ToObject instead of Enum.GetValues(Type). It returns the same values in the same order.
  • CharacterOptionExtensions uses Enum.GetValues<CharacterOption>().
  • AssessmentProperties, EphemeralProperties and SendOnLoginProperties: the type parameter of their private GetValues gets [DynamicallyAccessedMembers(PublicFields)]. The code is unchanged.
  • AttributeExtensions.GetAttributeOfType gets [UnconditionalSuppressMessage] for IL2075. It takes any System.Enum, so the analyzer cannot know the type. The lookup is safe because trimming never removes the literal fields of an enum. The runtime suppresses the same kind of warning for the same reason in Type.GetEnumData ("Literal fields on enums can never be trimmed"). The native AOT run below checks it.
  • The three DatLoader loggers use typeof(DatDatabase), typeof(DatManager) and typeof(AnimationHook), the type that MethodBase.GetCurrentMethod().DeclaringType returned.

Behaviour

  • Nothing public is removed or changes its signature.
  • For ACE.Server nothing should change. The one difference in principle is the fallback folder for Config.js: the application's base directory instead of the folder that holds ACE.Common.dll. They are the same folder whenever ACE.Common.dll sits next to the executable, as it does in the solution's build and publish output.
  • ACE.Server is not published trimmed, so IsAotCompatible on the libraries changes nothing in how it is built or run.
  • This does not make ACE.Server, ACE.Database or ACE.Adapter AOT compatible.

Testing

On a Linux x64 VM (Ubuntu 26.04, .NET SDK 10.0.115) on 2026-11-29, against master 055a66c.

A check program (a console program, not part of this PR) references the three libraries and prints what they return:

  • the config loaded from three files: Config.js.example; a file with comments, trailing commas, numbers as strings, a null section, an unknown property and its own PreloadedLandblocks; and an invalid file, for which it prints the exception;
  • a Config.js found next to the executable, from a working directory without one;
  • GetFlags for 11 values, every CharacterOption with its two attributes, GetDescription for PropertyAttribute and PropertyAttribute2nd, and the 22 [Ephemeral], [SendOnLogin] and [AssessmentProperty] sets;
  • a BCrypt hash and verify;
  • reads from the end-of-retail dat files: all 2,066 animations with their 6,419 hooks counted by type, all 5,935 setups, an eighth of the GfxObjs, and a quarter of the cell dat (1,353 landblock infos and 271,378 EnvCells).
Run Result
Normal runtime, master against this PR The output is identical (360 lines). This run also calls GetFlags for every enum type in the three assemblies (227 types, 49,317 calls, compared by hash), and uses SerializerOptions with reflection before and after Initialize.
Native AOT, master 11 trim and AOT warnings: the places in the table above, except CharacterOption.cs, which only the build analyzer flags. The three failures listed under The problem.
Native AOT, this PR No trim or AOT warning. The output is identical to this PR's normal run (347 lines; the two parts that need reflection are left out of the AOT build).

The native AOT builds use PublishAot with the three assemblies as TrimmerRootAssembly items and TrimmerSingleWarn=false. The compiler then compiles all of their code, not only what the program calls, and lists each warning.

Solution build: 16 warnings on master and with this PR, the same list (13 CA1416 in Texture.cs, 3 CA2022 in DatReader.cs).

Test projects: the same results on master and with this PR, test by test, with the same failure messages.

Project Passed Failed
ACE.Server.Tests 47 4
ACE.DatLoader.Tests 18 6
ACE.Database.Tests 0 7
ACE.Adapter.Tests 23 0

All 17 failures are files that are not on this machine: the dat files under C:\Turbine\Asheron's Call, and the Config.js, Config.js.example and starterGear.json that the test setups look for.

Not verified

  • Nothing was run on Windows.
  • Texture's bitmap code uses System.Drawing.Common, which is Windows-only. It compiled as native AOT without a warning, but it was not run.
  • No test project reaches ConfigManager.Initialize on this machine: they fail in their setup first, on master too. The check program is what covers the config change.
  • No timings were taken. This PR makes no performance claim.

Files

  • Source/ACE.Common/ACE.Common.csproj
  • Source/ACE.Common/ConfigJsonContext.cs (new)
  • Source/ACE.Common/ConfigManager.cs
  • Source/ACE.Common/Extensions/EnumHelper.cs
  • Source/ACE.Entity/ACE.Entity.csproj
  • Source/ACE.Entity/Enum/AttributeExtensions.cs
  • Source/ACE.Entity/Enum/CharacterOption.cs
  • Source/ACE.Entity/Enum/Properties/AssessmentProperties.cs
  • Source/ACE.Entity/Enum/Properties/EphemeralProperties.cs
  • Source/ACE.Entity/Enum/Properties/SendOnLoginProperties.cs
  • Source/ACE.DatLoader/ACE.DatLoader.csproj
  • Source/ACE.DatLoader/DatDatabase.cs
  • Source/ACE.DatLoader/DatManager.cs
  • Source/ACE.DatLoader/Entity/AnimationHook.cs

🤖 Generated with Claude Code

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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant